kickstart.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. # ---------------------------------------------------------------------------------------------------------------------
  29. # library functions copied from packaging/installer/functions.sh
  30. setup_terminal() {
  31. TPUT_RESET=""
  32. TPUT_YELLOW=""
  33. TPUT_WHITE=""
  34. TPUT_BGRED=""
  35. TPUT_BGGREEN=""
  36. TPUT_BOLD=""
  37. TPUT_DIM=""
  38. # Is stderr on the terminal? If not, then fail
  39. test -t 2 || return 1
  40. if command -v tput >/dev/null 2>&1; then
  41. if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
  42. # Enable colors
  43. TPUT_RESET="$(tput sgr 0)"
  44. TPUT_YELLOW="$(tput setaf 3)"
  45. TPUT_WHITE="$(tput setaf 7)"
  46. TPUT_BGRED="$(tput setab 1)"
  47. TPUT_BGGREEN="$(tput setab 2)"
  48. TPUT_BOLD="$(tput bold)"
  49. TPUT_DIM="$(tput dim)"
  50. fi
  51. fi
  52. return 0
  53. }
  54. progress() {
  55. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  56. }
  57. escaped_print() {
  58. if printf "%q " test >/dev/null 2>&1; then
  59. printf "%q " "${@}"
  60. else
  61. printf "%s" "${*}"
  62. fi
  63. return 0
  64. }
  65. run() {
  66. local dir="${PWD}" info_console
  67. if [ "${UID}" = "0" ]; then
  68. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  69. else
  70. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  71. fi
  72. escaped_print "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" "${@}" "${TPUT_RESET}\n" >&2
  73. ${@}
  74. local ret=$?
  75. if [ ${ret} -ne 0 ]; then
  76. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  77. else
  78. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  79. fi
  80. return ${ret}
  81. }
  82. fatal() {
  83. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  84. exit 1
  85. }
  86. warning() {
  87. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*} \n\n"
  88. if [ "${INTERACTIVE}" = "0" ]; then
  89. fatal "Stopping due to non-interactive mode. Fix the issue or retry installation in an interactive mode."
  90. else
  91. read -r -p "Press ENTER to attempt netdata installation > "
  92. progress "OK, let's give it a try..."
  93. fi
  94. }
  95. create_tmp_directory() {
  96. # Check if tmp is mounted as noexec
  97. if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
  98. pattern="$(pwd)/netdata-kickstart-XXXXXX"
  99. else
  100. pattern="/tmp/netdata-kickstart-XXXXXX"
  101. fi
  102. mktemp -d $pattern
  103. }
  104. download() {
  105. url="${1}"
  106. dest="${2}"
  107. if command -v curl >/dev/null 2>&1; then
  108. run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
  109. elif command -v wget >/dev/null 2>&1; then
  110. run wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
  111. else
  112. fatal "I need curl or wget to proceed, but neither is available on this system."
  113. fi
  114. }
  115. set_tarball_urls() {
  116. if [ "$1" == "stable" ]; then
  117. local latest
  118. # Simple version
  119. # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
  120. latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
  121. export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.tar.gz"
  122. export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
  123. else
  124. export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.tar.gz"
  125. export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
  126. fi
  127. }
  128. detect_bash4() {
  129. bash="${1}"
  130. if [ -z "${BASH_VERSION}" ]; then
  131. # we don't run under bash
  132. if [ -n "${bash}" ] && [ -x "${bash}" ]; then
  133. # shellcheck disable=SC2016
  134. BASH_MAJOR_VERSION=$(${bash} -c 'echo "${BASH_VERSINFO[0]}"')
  135. fi
  136. else
  137. # we run under bash
  138. BASH_MAJOR_VERSION="${BASH_VERSINFO[0]}"
  139. fi
  140. if [ -z "${BASH_MAJOR_VERSION}" ]; then
  141. echo >&2 "No BASH is available on this system"
  142. return 1
  143. elif [ $((BASH_MAJOR_VERSION)) -lt 4 ]; then
  144. echo >&2 "No BASH v4+ is available on this system (installed bash is v${BASH_MAJOR_VERSION}"
  145. return 1
  146. fi
  147. return 0
  148. }
  149. dependencies() {
  150. SYSTEM="$(uname -s)"
  151. OS="$(uname -o)"
  152. MACHINE="$(uname -m)"
  153. echo "System : ${SYSTEM}"
  154. echo "Operating System : ${OS}"
  155. echo "Machine : ${MACHINE}"
  156. echo "BASH major version: ${BASH_MAJOR_VERSION}"
  157. if [ "${OS}" != "GNU/Linux" ] && [ "${SYSTEM}" != "Linux" ]; then
  158. warning "Cannot detect the packages to be installed on a ${SYSTEM} - ${OS} system."
  159. else
  160. bash="$(command -v bash 2>/dev/null)"
  161. if ! detect_bash4 "${bash}"; then
  162. warning "Cannot detect packages to be installed in this system, without BASH v4+."
  163. else
  164. progress "Downloading script to detect required packages..."
  165. download "${PACKAGES_SCRIPT}" "${TMPDIR}/install-required-packages.sh"
  166. if [ ! -s "${TMPDIR}/install-required-packages.sh" ]; then
  167. warning "Downloaded dependency installation script is empty."
  168. else
  169. progress "Running downloaded script to detect required packages..."
  170. run ${sudo} "${bash}" "${TMPDIR}/install-required-packages.sh" ${PACKAGES_INSTALLER_OPTIONS}
  171. # shellcheck disable=SC2181
  172. if [ $? -ne 0 ] ; then
  173. warning "It failed to install all the required packages, but installation might still be possible."
  174. fi
  175. fi
  176. fi
  177. fi
  178. }
  179. safe_sha256sum() {
  180. # Within the contexct of the installer, we only use -c option that is common between the two commands
  181. # We will have to reconsider if we start non-common options
  182. if command -v sha256sum >/dev/null 2>&1; then
  183. sha256sum $@
  184. elif command -v shasum >/dev/null 2>&1; then
  185. shasum -a 256 $@
  186. else
  187. fatal "I could not find a suitable checksum binary to use"
  188. fi
  189. }
  190. # ---------------------------------------------------------------------------------------------------------------------
  191. umask 022
  192. sudo=""
  193. [ -z "${UID}" ] && UID="$(id -u)"
  194. [ "${UID}" -ne "0" ] && sudo="sudo"
  195. export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  196. setup_terminal || echo >/dev/null
  197. # ---------------------------------------------------------------------------------------------------------------------
  198. # try to update using autoupdater in the first place
  199. updater=""
  200. [ -x /etc/periodic/daily/netdata-updater ] && updater=/etc/periodic/daily/netdata-updater
  201. [ -x /etc/cron.daily/netdata-updater ] && updater=/etc/cron.daily/netdata-updater
  202. if [ -L "${updater}" ]; then
  203. # remove old updater (symlink)
  204. run "${sudo}" rm -f "${updater}"
  205. updater=""
  206. fi
  207. if [ -n "${updater}" ]; then
  208. # attempt to run the updater, to respect any compilation settings already in place
  209. progress "Re-installing netdata..."
  210. run "${sudo}" "${updater}" -f || fatal "Failed to forcefully update netdata"
  211. exit 0
  212. fi
  213. # ---------------------------------------------------------------------------------------------------------------------
  214. # install required system packages
  215. INTERACTIVE=1
  216. PACKAGES_INSTALLER_OPTIONS="netdata"
  217. NETDATA_INSTALLER_OPTIONS=""
  218. NETDATA_UPDATES="--auto-update"
  219. RELEASE_CHANNEL="nightly"
  220. while [ -n "${1}" ]; do
  221. if [ "${1}" = "all" ]; then
  222. PACKAGES_INSTALLER_OPTIONS="netdata-all"
  223. shift 1
  224. elif [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ]; then
  225. INTERACTIVE=0
  226. shift 1
  227. elif [ "${1}" = "--no-updates" ]; then
  228. # echo >&2 "netdata will not auto-update"
  229. NETDATA_UPDATES=
  230. shift 1
  231. elif [ "${1}" = "--stable-channel" ]; then
  232. RELEASE_CHANNEL="stable"
  233. shift 1
  234. else
  235. break
  236. fi
  237. done
  238. if [ "${INTERACTIVE}" = "0" ]; then
  239. PACKAGES_INSTALLER_OPTIONS="--dont-wait --non-interactive ${PACKAGES_INSTALLER_OPTIONS}"
  240. NETDATA_INSTALLER_OPTIONS="--dont-wait"
  241. fi
  242. TMPDIR=$(create_tmp_directory)
  243. cd ${TMPDIR} || :
  244. dependencies
  245. # ---------------------------------------------------------------------------------------------------------------------
  246. # download netdata package
  247. set_tarball_urls "${RELEASE_CHANNEL}"
  248. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
  249. download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.tar.gz"
  250. if ! grep netdata-latest.tar.gz "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - >/dev/null 2>&1; then
  251. fatal "Tarball checksum validation failed. Stopping netdata installation and leaving tarball in ${TMPDIR}"
  252. fi
  253. run tar -xf netdata-latest.tar.gz
  254. rm -rf netdata-latest.tar.gz >/dev/null 2>&1
  255. cd netdata-* || fatal "Cannot cd to netdata source tree"
  256. # ---------------------------------------------------------------------------------------------------------------------
  257. # install netdata from source
  258. if [ -x netdata-installer.sh ]; then
  259. progress "Installing netdata..."
  260. run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || fatal "netdata-installer.sh exited with error"
  261. rm -rf "${TMPDIR}" >/dev/null 2>&1
  262. else
  263. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh). Leaving all files in ${TMPDIR}"
  264. fi