install-or-update.sh 8.7 KB

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