install-or-update.sh 8.5 KB

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