kickstart.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. #
  4. # Run me with:
  5. #
  6. # bash <(curl -Ss https://my-netdata.io/kickstart.sh)
  7. #
  8. # or (to install all netdata dependencies):
  9. #
  10. # bash <(curl -Ss https://my-netdata.io/kickstart.sh) all
  11. #
  12. # Other options:
  13. # --dont-wait do not prompt for user input
  14. # --non-interactive do not prompt for user input
  15. # --no-updates do not install script for daily updates
  16. #
  17. # This script will:
  18. #
  19. # 1. install all netdata compilation dependencies
  20. # using the package manager of the system
  21. #
  22. # 2. download netdata nightly package to temporary directory
  23. #
  24. # 3. install netdata
  25. # shellcheck disable=SC2039,SC2059,SC2086
  26. # External files
  27. PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata-demo-site/master/install-required-packages.sh"
  28. NIGHTLY_PACKAGE_TARBALL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.tar.gz"
  29. NIGHTLY_PACKAGE_CHECKSUM="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
  30. # ---------------------------------------------------------------------------------------------------------------------
  31. # library functions copied from packaging/installer/functions.sh
  32. setup_terminal() {
  33. TPUT_RESET=""
  34. TPUT_YELLOW=""
  35. TPUT_WHITE=""
  36. TPUT_BGRED=""
  37. TPUT_BGGREEN=""
  38. TPUT_BOLD=""
  39. TPUT_DIM=""
  40. # Is stderr on the terminal? If not, then fail
  41. test -t 2 || return 1
  42. if command -v tput >/dev/null 2>&1; then
  43. if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
  44. # Enable colors
  45. TPUT_RESET="$(tput sgr 0)"
  46. TPUT_YELLOW="$(tput setaf 3)"
  47. TPUT_WHITE="$(tput setaf 7)"
  48. TPUT_BGRED="$(tput setab 1)"
  49. TPUT_BGGREEN="$(tput setab 2)"
  50. TPUT_BOLD="$(tput bold)"
  51. TPUT_DIM="$(tput dim)"
  52. fi
  53. fi
  54. return 0
  55. }
  56. progress() {
  57. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  58. }
  59. escaped_print() {
  60. if printf "%q " test >/dev/null 2>&1; then
  61. printf "%q " "${@}"
  62. else
  63. printf "%s" "${*}"
  64. fi
  65. return 0
  66. }
  67. run() {
  68. local dir="${PWD}" info_console
  69. if [ "${UID}" = "0" ]; then
  70. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  71. else
  72. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  73. fi
  74. escaped_print "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" "${@}" "${TPUT_RESET}\n" >&2
  75. "${@}"
  76. local ret=$?
  77. if [ ${ret} -ne 0 ]; then
  78. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  79. else
  80. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  81. fi
  82. return ${ret}
  83. }
  84. warning() {
  85. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*} \n\n"
  86. if [ "${INTERACTIVE}" = "0" ]; then
  87. fatal "Stopping due to non-interactive mode. Fix the issue or retry installation in an interactive mode."
  88. else
  89. read -r -p "Press ENTER to attempt netdata installation > "
  90. progress "OK, let's give it a try..."
  91. fi
  92. }
  93. fatal() {
  94. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  95. exit 1
  96. }
  97. download() {
  98. url="${1}"
  99. dest="${2}"
  100. if command -v curl >/dev/null 2>&1; then
  101. run curl -L --connect-timeout 5 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
  102. elif command -v wget >/dev/null 2>&1; then
  103. run wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
  104. else
  105. fatal "I need curl or wget to proceed, but neither is available on this system."
  106. fi
  107. }
  108. detect_bash4() {
  109. bash="${1}"
  110. if [ -z "${BASH_VERSION}" ]; then
  111. # we don't run under bash
  112. if [ -n "${bash}" ] && [ -x "${bash}" ]; then
  113. # shellcheck disable=SC2016
  114. BASH_MAJOR_VERSION=$(${bash} -c 'echo "${BASH_VERSINFO[0]}"')
  115. fi
  116. else
  117. # we run under bash
  118. BASH_MAJOR_VERSION="${BASH_VERSINFO[0]}"
  119. fi
  120. if [ -z "${BASH_MAJOR_VERSION}" ]; then
  121. echo >&2 "No BASH is available on this system"
  122. return 1
  123. elif [ $((BASH_MAJOR_VERSION)) -lt 4 ]; then
  124. echo >&2 "No BASH v4+ is available on this system (installed bash is v${BASH_MAJOR_VERSION}"
  125. return 1
  126. fi
  127. return 0
  128. }
  129. umask 022
  130. sudo=""
  131. [ -z "${UID}" ] && UID="$(id -u)"
  132. [ "${UID}" -ne "0" ] && sudo="sudo"
  133. export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  134. setup_terminal || echo >/dev/null
  135. # ---------------------------------------------------------------------------------------------------------------------
  136. # try to update using autoupdater in the first place
  137. updater=""
  138. [ -x /etc/periodic/daily/netdata-updater ] && updater=/etc/periodic/daily/netdata-updater
  139. [ -x /etc/cron.daily/netdata-updater ] && updater=/etc/cron.daily/netdata-updater
  140. if [ -L "${updater}" ]; then
  141. # remove old updater (symlink)
  142. run "${sudo}" rm -f "${updater}"
  143. updater=""
  144. fi
  145. if [ -n "${updater}" ]; then
  146. # attempt to run the updater, to respect any compilation settings already in place
  147. progress "Re-installing netdata..."
  148. run "${sudo}" "${updater}" -f || fatal "Failed to forcefully update netdata"
  149. exit 0
  150. fi
  151. # ---------------------------------------------------------------------------------------------------------------------
  152. # install required system packages
  153. INTERACTIVE=1
  154. PACKAGES_INSTALLER_OPTIONS="netdata"
  155. NETDATA_INSTALLER_OPTIONS=""
  156. NETDATA_UPDATES="--auto-update"
  157. while [ -n "${1}" ]; do
  158. if [ "${1}" = "all" ]; then
  159. PACKAGES_INSTALLER_OPTIONS="netdata-all"
  160. shift 1
  161. elif [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ]; then
  162. INTERACTIVE=0
  163. shift 1
  164. elif [ "${1}" = "--no-updates" ]; then
  165. # echo >&2 "netdata will not auto-update"
  166. NETDATA_UPDATES=
  167. shift 1
  168. else
  169. break
  170. fi
  171. done
  172. if [ "${INTERACTIVE}" = "0" ]; then
  173. PACKAGES_INSTALLER_OPTIONS="--dont-wait --non-interactive ${PACKAGES_INSTALLER_OPTIONS}"
  174. NETDATA_INSTALLER_OPTIONS="--dont-wait"
  175. fi
  176. # ---------------------------------------------------------------------------------------------------------------------
  177. # detect system parameters and install dependencies
  178. SYSTEM="$(uname -s)"
  179. OS="$(uname -o)"
  180. MACHINE="$(uname -m)"
  181. cat <<EOF
  182. System : ${SYSTEM}
  183. Operating System : ${OS}
  184. Machine : ${MACHINE}
  185. BASH major version: ${BASH_MAJOR_VERSION}
  186. EOF
  187. # Check if tmp is mounted as noexec
  188. if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
  189. pattern="$(pwd)/netdata-kickstart-XXXXXX"
  190. else
  191. pattern="/tmp/netdata-kickstart-XXXXXX"
  192. fi
  193. tmpdir="$(mktemp -d $pattern)"
  194. cd "${tmpdir}" || :
  195. if [ "${OS}" != "GNU/Linux" ] && [ "${SYSTEM}" != "Linux" ]; then
  196. warning "Cannot detect the packages to be installed on a ${SYSTEM} - ${OS} system."
  197. else
  198. bash="$(command -v bash 2>/dev/null)"
  199. if ! detect_bash4 "${bash}"; then
  200. warning "Cannot detect packages to be installed in this system, without BASH v4+."
  201. else
  202. progress "Downloading script to detect required packages..."
  203. download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"
  204. if [ ! -s "${tmpdir}/install-required-packages.sh" ]; then
  205. warning "Downloaded dependency installation script is empty."
  206. else
  207. progress "Running downloaded script to detect required packages..."
  208. run ${sudo} "${bash}" "${tmpdir}/install-required-packages.sh" ${PACKAGES_INSTALLER_OPTIONS}
  209. if [ $? -ne 0 ] ; then
  210. warning "It failed to install all the required packages, but installation might still be possible."
  211. fi
  212. fi
  213. fi
  214. fi
  215. # ---------------------------------------------------------------------------------------------------------------------
  216. # download netdata nightly package
  217. download "${NIGHTLY_PACKAGE_CHECKSUM}" "${tmpdir}/sha256sum.txt"
  218. download "${NIGHTLY_PACKAGE_TARBALL}" "${tmpdir}/netdata-latest.tar.gz"
  219. if ! grep netdata-latest.tar.gz sha256sum.txt | sha256sum --check - >/dev/null 2>&1; then
  220. failed "Tarball checksum validation failed. Stopping netdata installation and leaving tarball in ${tmpdir}"
  221. fi
  222. run tar -xf netdata-latest.tar.gz
  223. rm -rf netdata-latest.tar.gz >/dev/null 2>&1
  224. cd netdata-* || fatal "Cannot cd to netdata source tree"
  225. # ---------------------------------------------------------------------------------------------------------------------
  226. # install netdata from source
  227. if [ -x netdata-installer.sh ]; then
  228. progress "Installing netdata..."
  229. run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || fatal "netdata-installer.sh exited with error"
  230. rm -rf "${tmpdir}" >/dev/null 2>&1
  231. else
  232. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh). Leaving all files in ${tmpdir}"
  233. fi