install-or-update.sh 8.8 KB

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