install-or-update.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. . $(dirname "${0}")/functions.sh
  4. export LC_ALL=C
  5. umask 002
  6. # Be nice on production environments
  7. renice 19 $$ >/dev/null 2>/dev/null
  8. NETDATA_PREFIX="/opt/netdata"
  9. NETDATA_USER_CONFIG_DIR="${NETDATA_PREFIX}/etc/netdata"
  10. # -----------------------------------------------------------------------------
  11. if [ -d /opt/netdata/etc/netdata.old ]; then
  12. progress "Found old etc/netdata directory, reinstating this"
  13. [ -d /opt/netdata/etc/netdata.new ] && rm -rf /opt/netdata/etc/netdata.new
  14. mv -f /opt/netdata/etc/netdata /opt/netdata/etc/netdata.new
  15. mv -f /opt/netdata/etc/netdata.old /opt/netdata/etc/netdata
  16. progress "Trigger stock config clean up"
  17. rm -f /opt/netdata/etc/netdata/.installer-cleanup-of-stock-configs-done
  18. fi
  19. STARTIT=1
  20. AUTOUPDATE=0
  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. AUTOUPDATE=1
  31. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  32. ;;
  33. "--stable-channel")
  34. RELEASE_CHANNEL="stable"
  35. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  36. ;;
  37. "--nightly-channel")
  38. RELEASE_CHANNEL="nightly"
  39. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  40. ;;
  41. "--disable-telemetry")
  42. DISABLE_TELEMETRY=1
  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" ]
  51. then
  52. # -----------------------------------------------------------------------------
  53. progress "Deleting stock configuration files from user configuration directory"
  54. declare -A configs_signatures=()
  55. source "system/configs.signatures"
  56. if [ ! -d etc/netdata ]
  57. then
  58. run mkdir -p etc/netdata
  59. fi
  60. md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
  61. for x in $(find etc -type f)
  62. 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 [ ! -z "${md5sum}" ]
  69. 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}" ]
  75. 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
  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. portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}" || run_failed "Failed to add netdata user to secondary groups"
  97. done
  98. NETDATA_USER="netdata"
  99. NETDATA_GROUP="netdata"
  100. else
  101. run_failed "I could not add user netdata, will be using root"
  102. fi
  103. else
  104. run_failed "I could not add group netdata, so no user netdata will be created as well. Netdata run as root:root"
  105. fi
  106. # -----------------------------------------------------------------------------
  107. progress "Check SSL certificates paths"
  108. if [ ! -f "/etc/ssl/certs/ca-certificates.crt" ]
  109. then
  110. if [ ! -f /opt/netdata/.curlrc ]
  111. then
  112. cacert=
  113. # CentOS
  114. [ -f "/etc/ssl/certs/ca-bundle.crt" ] && cacert="/etc/ssl/certs/ca-bundle.crt"
  115. if [ ! -z "${cacert}" ]
  116. then
  117. echo "Creating /opt/netdata/.curlrc with cacert=${cacert}"
  118. echo >/opt/netdata/.curlrc "cacert=${cacert}"
  119. else
  120. run_failed "Failed to find /etc/ssl/certs/ca-certificates.crt"
  121. fi
  122. fi
  123. fi
  124. # -----------------------------------------------------------------------------
  125. progress "Install logrotate configuration for netdata"
  126. install_netdata_logrotate || run_failed "Cannot install logrotate file for netdata."
  127. # -----------------------------------------------------------------------------
  128. progress "Telemetry configuration"
  129. # Opt-out from telemetry program
  130. if [ -n "${NETDATA_DISABLE_TELEMETRY+x}" ]; then
  131. run touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  132. else
  133. printf "You can opt out from anonymous statistics via the --disable-telemetry option, or by creating an empty file ${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics \n\n"
  134. fi
  135. # -----------------------------------------------------------------------------
  136. progress "Install netdata at system init"
  137. install_netdata_service || run_failed "Cannot install netdata init service."
  138. set_netdata_updater_channel || run_failed "Cannot set netdata updater tool release channel to '${RELEASE_CHANNEL}'"
  139. # -----------------------------------------------------------------------------
  140. progress "Install (but not enable) netdata updater tool"
  141. cleanup_old_netdata_updater || run_failed "Cannot cleanup old netdata updater tool."
  142. install_netdata_updater || run_failed "Cannot install netdata updater tool."
  143. progress "Check if we must enable/disable the netdata updater tool"
  144. if [ "${AUTOUPDATE}" = "1" ]; then
  145. enable_netdata_updater || run_failed "Cannot enable netdata updater tool"
  146. else
  147. disable_netdata_updater || run_failed "Cannot disable netdata updater tool"
  148. fi
  149. # -----------------------------------------------------------------------------
  150. progress "creating quick links"
  151. dir_should_be_link() {
  152. local p="${1}" t="${2}" d="${3}" old
  153. old="${PWD}"
  154. cd "${p}" || return 0
  155. if [ -e "${d}" ]
  156. then
  157. if [ -h "${d}" ]
  158. then
  159. run rm "${d}"
  160. else
  161. run mv -f "${d}" "${d}.old.$$"
  162. fi
  163. fi
  164. run ln -s "${t}" "${d}"
  165. cd "${old}"
  166. }
  167. dir_should_be_link . bin sbin
  168. dir_should_be_link usr ../bin bin
  169. dir_should_be_link usr ../bin sbin
  170. dir_should_be_link usr . local
  171. dir_should_be_link . etc/netdata netdata-configs
  172. dir_should_be_link . usr/share/netdata/web netdata-web-files
  173. dir_should_be_link . usr/libexec/netdata netdata-plugins
  174. dir_should_be_link . var/lib/netdata netdata-dbs
  175. dir_should_be_link . var/cache/netdata netdata-metrics
  176. dir_should_be_link . var/log/netdata netdata-logs
  177. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d orig
  178. if [ ${deleted_stock_configs} -gt 0 ]
  179. then
  180. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d "000.-.USE.THE.orig.LINK.TO.COPY.AND.EDIT.STOCK.CONFIG.FILES"
  181. fi
  182. # -----------------------------------------------------------------------------
  183. progress "fix permissions"
  184. run chmod g+rx,o+rx /opt
  185. run chown -R ${NETDATA_USER}:${NETDATA_GROUP} /opt/netdata
  186. # -----------------------------------------------------------------------------
  187. progress "fix plugin permissions"
  188. for x in apps.plugin freeipmi.plugin ioping cgroup-network
  189. do
  190. f="usr/libexec/netdata/plugins.d/${x}"
  191. if [ -f "${f}" ]
  192. then
  193. run chown root:${NETDATA_GROUP} "${f}"
  194. run chmod 4750 "${f}"
  195. fi
  196. done
  197. # fix the fping binary
  198. if [ -f bin/fping ]
  199. then
  200. run chown root:${NETDATA_GROUP} bin/fping
  201. run chmod 4750 bin/fping
  202. fi
  203. # -----------------------------------------------------------------------------
  204. echo "Save install options"
  205. grep -qv 'IS_NETDATA_STATIC_BINARY="yes"' "${NETDATA_PREFIX}/etc/netdata/.environment" || echo IS_NETDATA_STATIC_BINARY=\"yes\" >> "${NETDATA_PREFIX}/etc/netdata/.environment"
  206. sed -i "s/REINSTALL_OPTIONS=\".*\"/REINSTALL_OPTIONS=\"${REINSTALL_OPTIONS}\"/" "${NETDATA_PREFIX}/etc/netdata/.environment"
  207. # -----------------------------------------------------------------------------
  208. if [ ${STARTIT} -eq 0 ]; then
  209. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  210. netdata_banner "is installed now!"
  211. else
  212. progress "starting netdata"
  213. if ! restart_netdata "${NETDATA_PREFIX}/bin/netdata"; then
  214. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  215. netdata_banner "is installed and running now!"
  216. else
  217. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  218. netdata_banner "is installed now!"
  219. fi
  220. fi
  221. run chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"