kickstart.sh 10 KB

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