install-or-update.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck source=./packaging/makeself/functions.sh
  4. . "$(dirname "${0}")"/functions.sh
  5. export LC_ALL=C
  6. umask 002
  7. # Be nice on production environments
  8. renice 19 $$ > /dev/null 2> /dev/null
  9. NETDATA_PREFIX="/opt/netdata"
  10. NETDATA_USER_CONFIG_DIR="${NETDATA_PREFIX}/etc/netdata"
  11. # -----------------------------------------------------------------------------
  12. if [ -d /opt/netdata/etc/netdata.old ]; then
  13. progress "Found old etc/netdata directory, reinstating this"
  14. [ -d /opt/netdata/etc/netdata.new ] && rm -rf /opt/netdata/etc/netdata.new
  15. mv -f /opt/netdata/etc/netdata /opt/netdata/etc/netdata.new
  16. mv -f /opt/netdata/etc/netdata.old /opt/netdata/etc/netdata
  17. progress "Trigger stock config clean up"
  18. rm -f /opt/netdata/etc/netdata/.installer-cleanup-of-stock-configs-done
  19. fi
  20. STARTIT=1
  21. REINSTALL_OPTIONS=""
  22. RELEASE_CHANNEL="nightly" # check .travis/create_artifacts.sh before modifying
  23. while [ "${1}" ]; do
  24. case "${1}" in
  25. "--dont-start-it")
  26. STARTIT=0
  27. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  28. ;;
  29. "--auto-update" | "-u") ;;
  30. "--stable-channel")
  31. RELEASE_CHANNEL="stable"
  32. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  33. ;;
  34. "--nightly-channel")
  35. RELEASE_CHANNEL="nightly"
  36. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  37. ;;
  38. "--disable-telemetry")
  39. NETDATA_DISABLE_TELEMETRY=1
  40. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  41. ;;
  42. *) echo >&2 "Unknown option '${1}'. Ignoring it." ;;
  43. esac
  44. shift 1
  45. done
  46. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  47. [ -n "$DISABLE_TELEMETRY" ] ||
  48. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  49. [ -n "$DO_NOT_TRACK" ]; then
  50. NETDATA_DISABLE_TELEMETRY=1
  51. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} --disable-telemetry"
  52. fi
  53. deleted_stock_configs=0
  54. if [ ! -f "etc/netdata/.installer-cleanup-of-stock-configs-done" ]; then
  55. # -----------------------------------------------------------------------------
  56. progress "Deleting stock configuration files from user configuration directory"
  57. declare -A configs_signatures=()
  58. source "system/configs.signatures"
  59. if [ ! -d etc/netdata ]; then
  60. run mkdir -p etc/netdata
  61. fi
  62. md5sum="$(command -v md5sum 2> /dev/null || command -v md5 2> /dev/null)"
  63. while IFS= read -r -d '' x; do
  64. # find it relative filename
  65. f="${x/etc\/netdata\//}"
  66. # find the stock filename
  67. t="${f/.conf.old/.conf}"
  68. t="${t/.conf.orig/.conf}"
  69. if [ -n "${md5sum}" ]; then
  70. # find the checksum of the existing file
  71. md5="$(${md5sum} < "${x}" | cut -d ' ' -f 1)"
  72. #echo >&2 "md5: ${md5}"
  73. # check if it matches
  74. if [ "${configs_signatures[${md5}]}" = "${t}" ]; then
  75. # it matches the default
  76. run rm -f "${x}"
  77. deleted_stock_configs=$((deleted_stock_configs + 1))
  78. fi
  79. fi
  80. done < <(find etc -type f)
  81. touch "etc/netdata/.installer-cleanup-of-stock-configs-done"
  82. fi
  83. # -----------------------------------------------------------------------------
  84. progress "Attempt to create user/group netdata/netadata"
  85. NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody I2C"
  86. NETDATA_ADDED_TO_GROUPS=""
  87. # Default user/group
  88. NETDATA_USER="root"
  89. NETDATA_GROUP="root"
  90. if portable_add_group netdata; then
  91. if portable_add_user netdata "/opt/netdata"; then
  92. progress "Add user netdata to required user groups"
  93. for g in ${NETDATA_WANTED_GROUPS}; do
  94. # shellcheck disable=SC2086
  95. if portable_add_user_to_group ${g} netdata; then
  96. NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
  97. else
  98. run_failed "Failed to add netdata user to secondary groups"
  99. fi
  100. done
  101. NETDATA_USER="netdata"
  102. NETDATA_GROUP="netdata"
  103. else
  104. run_failed "I could not add user netdata, will be using root"
  105. fi
  106. else
  107. run_failed "I could not add group netdata, so no user netdata will be created as well. Netdata run as root:root"
  108. fi
  109. # -----------------------------------------------------------------------------
  110. progress "Install logrotate configuration for netdata"
  111. install_netdata_logrotate || run_failed "Cannot install logrotate file for netdata."
  112. # -----------------------------------------------------------------------------
  113. progress "Telemetry configuration"
  114. # Opt-out from telemetry program
  115. if [ -n "${NETDATA_DISABLE_TELEMETRY}" ]; then
  116. run touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  117. else
  118. printf "You can opt out from anonymous statistics via the --disable-telemetry option, or by creating an empty file %s \n\n" "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  119. fi
  120. # -----------------------------------------------------------------------------
  121. progress "Install netdata at system init"
  122. install_netdata_service || run_failed "Cannot install netdata init service."
  123. set_netdata_updater_channel || run_failed "Cannot set netdata updater tool release channel to '${RELEASE_CHANNEL}'"
  124. # -----------------------------------------------------------------------------
  125. progress "Install (but not enable) netdata updater tool"
  126. install_netdata_updater || run_failed "Cannot install netdata updater tool."
  127. # -----------------------------------------------------------------------------
  128. progress "creating quick links"
  129. dir_should_be_link() {
  130. local p="${1}" t="${2}" d="${3}" old
  131. old="${PWD}"
  132. cd "${p}" || return 0
  133. if [ -e "${d}" ]; then
  134. if [ -h "${d}" ]; then
  135. run rm "${d}"
  136. else
  137. run mv -f "${d}" "${d}.old.$$"
  138. fi
  139. fi
  140. run ln -s "${t}" "${d}"
  141. cd "${old}"
  142. }
  143. dir_should_be_link . bin sbin
  144. dir_should_be_link usr ../bin bin
  145. dir_should_be_link usr ../bin sbin
  146. dir_should_be_link usr . local
  147. dir_should_be_link . etc/netdata netdata-configs
  148. dir_should_be_link . usr/share/netdata/web netdata-web-files
  149. dir_should_be_link . usr/libexec/netdata netdata-plugins
  150. dir_should_be_link . var/lib/netdata netdata-dbs
  151. dir_should_be_link . var/cache/netdata netdata-metrics
  152. dir_should_be_link . var/log/netdata netdata-logs
  153. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d orig
  154. if [ ${deleted_stock_configs} -gt 0 ]; then
  155. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d "000.-.USE.THE.orig.LINK.TO.COPY.AND.EDIT.STOCK.CONFIG.FILES"
  156. fi
  157. # -----------------------------------------------------------------------------
  158. progress "fix permissions"
  159. run chmod g+rx,o+rx /opt
  160. run chown -R ${NETDATA_USER}:${NETDATA_GROUP} /opt/netdata
  161. # -----------------------------------------------------------------------------
  162. progress "changing plugins ownership and setting setuid"
  163. for x in apps.plugin freeipmi.plugin ioping cgroup-network ebpf.plugin perf.plugin slabinfo.plugin nfacct.plugin xenstat.plugin; do
  164. f="usr/libexec/netdata/plugins.d/${x}"
  165. if [ -f "${f}" ]; then
  166. run chown root:${NETDATA_GROUP} "${f}"
  167. run chmod 4750 "${f}"
  168. fi
  169. done
  170. # fix the fping binary
  171. if [ -f bin/fping ]; then
  172. run chown root:${NETDATA_GROUP} bin/fping
  173. run chmod 4750 bin/fping
  174. fi
  175. # -----------------------------------------------------------------------------
  176. echo "Configure TLS certificate paths"
  177. if [ ! -L /opt/netdata/etc/ssl ] && [ -d /opt/netdata/etc/ssl ] ; then
  178. echo "Preserving existing user configuration for TLS"
  179. else
  180. if [ -d /etc/pki/tls ] ; then
  181. echo "Using /etc/pki/tls for TLS configuration and certificates"
  182. ln -sf /etc/pki/tls /opt/netdata/etc/ssl
  183. elif [ -d /etc/ssl ] ; then
  184. echo "Using /etc/ssl for TLS configuration and certificates"
  185. ln -sf /etc/ssl /opt/netdata/etc/ssl
  186. else
  187. echo "Using bundled TLS configuration and certificates"
  188. ln -sf /opt/netdata/share/ssl /opt/netdata/etc/ssl
  189. fi
  190. fi
  191. # -----------------------------------------------------------------------------
  192. echo "Save install options"
  193. grep -qv 'IS_NETDATA_STATIC_BINARY="yes"' "${NETDATA_PREFIX}/etc/netdata/.environment" || echo IS_NETDATA_STATIC_BINARY=\"yes\" >> "${NETDATA_PREFIX}/etc/netdata/.environment"
  194. sed -i "s/REINSTALL_OPTIONS=\".*\"/REINSTALL_OPTIONS=\"${REINSTALL_OPTIONS}\"/" "${NETDATA_PREFIX}/etc/netdata/.environment"
  195. # -----------------------------------------------------------------------------
  196. if [ ${STARTIT} -eq 0 ]; then
  197. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  198. netdata_banner "is installed now!"
  199. else
  200. progress "starting netdata"
  201. if ! restart_netdata "${NETDATA_PREFIX}/bin/netdata"; then
  202. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  203. netdata_banner "is installed and running now!"
  204. else
  205. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  206. netdata_banner "is installed now!"
  207. fi
  208. fi
  209. run chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"