kickstart-static64.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC1117,SC2039,SC2059,SC2086
  4. #
  5. # Options to run
  6. # --dont-wait do not wait for input
  7. # --non-interactive do not wait for input
  8. # --dont-start-it do not start netdata after install
  9. # --stable-channel Use the stable release channel, rather than the nightly to fetch sources
  10. # --disable-telemetry Opt-out of anonymous telemetry program (DO_NOT_TRACK=1)
  11. # --local-files Use a manually provided tarball for the installation
  12. # --allow-duplicate-install do not bail if we detect a duplicate install
  13. # --reinstall if an existing install would be updated, reinstall instead
  14. #
  15. # Environment options:
  16. #
  17. # NETDATA_TARBALL_BASEURL set the base url for downloading the dist tarball
  18. #
  19. # ----------------------------------------------------------------------------
  20. # library functions copied from packaging/installer/functions.sh
  21. setup_terminal() {
  22. TPUT_RESET=""
  23. TPUT_YELLOW=""
  24. TPUT_WHITE=""
  25. TPUT_BGRED=""
  26. TPUT_BGGREEN=""
  27. TPUT_BOLD=""
  28. TPUT_DIM=""
  29. # Is stderr on the terminal? If not, then fail
  30. test -t 2 || return 1
  31. if command -v tput > /dev/null 2>&1; then
  32. if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
  33. # Enable colors
  34. TPUT_RESET="$(tput sgr 0)"
  35. TPUT_YELLOW="$(tput setaf 3)"
  36. TPUT_WHITE="$(tput setaf 7)"
  37. TPUT_BGRED="$(tput setab 1)"
  38. TPUT_BGGREEN="$(tput setab 2)"
  39. TPUT_BOLD="$(tput bold)"
  40. TPUT_DIM="$(tput dim)"
  41. fi
  42. fi
  43. return 0
  44. }
  45. setup_terminal || echo > /dev/null
  46. # ----------------------------------------------------------------------------
  47. fatal() {
  48. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  49. exit 1
  50. }
  51. run_ok() {
  52. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} \n\n"
  53. }
  54. run_failed() {
  55. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} \n\n"
  56. }
  57. ESCAPED_PRINT_METHOD=
  58. if printf "%q " test > /dev/null 2>&1; then
  59. ESCAPED_PRINT_METHOD="printfq"
  60. fi
  61. escaped_print() {
  62. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  63. printf "%q " "${@}"
  64. else
  65. printf "%s" "${*}"
  66. fi
  67. return 0
  68. }
  69. progress() {
  70. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  71. }
  72. run_logfile="/dev/null"
  73. run() {
  74. local user="${USER--}" dir="${PWD}" info info_console
  75. if [ "${UID}" = "0" ]; then
  76. info="[root ${dir}]# "
  77. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  78. else
  79. info="[${user} ${dir}]$ "
  80. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  81. fi
  82. {
  83. printf "${info}"
  84. escaped_print "${@}"
  85. printf " ... "
  86. } >> "${run_logfile}"
  87. printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  88. escaped_print >&2 "${@}"
  89. printf >&2 "${TPUT_RESET}\n"
  90. "${@}"
  91. local ret=$?
  92. if [ ${ret} -ne 0 ]; then
  93. run_failed
  94. printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
  95. else
  96. run_ok
  97. printf >> "${run_logfile}" "OK\n"
  98. fi
  99. return ${ret}
  100. }
  101. fatal() {
  102. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  103. exit 1
  104. }
  105. _cannot_use_tmpdir() {
  106. local testfile ret
  107. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  108. ret=0
  109. if [ -z "${testfile}" ] ; then
  110. return "${ret}"
  111. fi
  112. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}" ; then
  113. if chmod +x "${testfile}" ; then
  114. if [ "$("${testfile}")" = "SUCCESS" ] ; then
  115. ret=1
  116. fi
  117. fi
  118. fi
  119. rm -f "${testfile}"
  120. return "${ret}"
  121. }
  122. create_tmp_directory() {
  123. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}" ; then
  124. if _cannot_use_tmpdir /tmp ; then
  125. if _cannot_use_tmpdir "${PWD}" ; then
  126. echo >&2
  127. echo >&2 "Unable to find a usable temprorary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again."
  128. exit 1
  129. else
  130. TMPDIR="${PWD}"
  131. fi
  132. else
  133. TMPDIR="/tmp"
  134. fi
  135. fi
  136. mktemp -d -t netdata-kickstart-XXXXXXXXXX
  137. }
  138. download() {
  139. url="${1}"
  140. dest="${2}"
  141. if command -v curl > /dev/null 2>&1; then
  142. run curl -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"
  143. elif command -v wget > /dev/null 2>&1; then
  144. run wget -T 15 -O "${dest}" "${url}" || fatal "Cannot download ${url}"
  145. else
  146. fatal "I need curl or wget to proceed, but neither is available on this system."
  147. fi
  148. }
  149. set_tarball_urls() {
  150. if [ -n "${NETDATA_LOCAL_TARBALL_OVERRIDE}" ]; then
  151. progress "Not fetching remote tarballs, local override was given"
  152. return
  153. fi
  154. if [ "$1" = "stable" ]; then
  155. local latest
  156. # Simple version
  157. latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
  158. export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.gz.run"
  159. export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
  160. else
  161. export NETDATA_TARBALL_URL="$NETDATA_TARBALL_BASEURL/netdata-latest.gz.run"
  162. export NETDATA_TARBALL_CHECKSUM_URL="$NETDATA_TARBALL_BASEURL/sha256sums.txt"
  163. fi
  164. }
  165. safe_sha256sum() {
  166. # Within the context of the installer, we only use -c option that is common between the two commands
  167. # We will have to reconsider if we start using non-common options
  168. if command -v sha256sum > /dev/null 2>&1; then
  169. sha256sum "$@"
  170. elif command -v shasum > /dev/null 2>&1; then
  171. shasum -a 256 "$@"
  172. else
  173. fatal "I could not find a suitable checksum binary to use"
  174. fi
  175. }
  176. # ----------------------------------------------------------------------------
  177. umask 022
  178. sudo=""
  179. [ -z "${UID}" ] && UID="$(id -u)"
  180. [ "${UID}" -ne "0" ] && sudo="sudo"
  181. # ----------------------------------------------------------------------------
  182. if [ "$(uname -m)" != "x86_64" ]; then
  183. fatal "Static binary versions of netdata are available only for 64bit Intel/AMD CPUs (x86_64), but yours is: $(uname -m)."
  184. fi
  185. if [ "$(uname -s)" != "Linux" ]; then
  186. fatal "Static binary versions of netdata are available only for Linux, but this system is $(uname -s)"
  187. fi
  188. # ----------------------------------------------------------------------------
  189. opts=
  190. NETDATA_INSTALLER_OPTIONS=""
  191. NETDATA_UPDATES="--auto-update"
  192. RELEASE_CHANNEL="nightly"
  193. while [ -n "${1}" ]; do
  194. if [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ] || [ "${1}" = "--accept" ]; then
  195. opts="${opts} --accept"
  196. shift 1
  197. elif [ "${1}" = "--dont-start-it" ]; then
  198. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS:+${NETDATA_INSTALLER_OPTIONS} }${1}"
  199. shift 1
  200. elif [ "${1}" = "--no-updates" ]; then
  201. NETDATA_UPDATES=""
  202. shift 1
  203. elif [ "${1}" = "--auto-update" ]; then
  204. true # This is the default behaviour, so ignore it.
  205. shift 1
  206. elif [ "${1}" = "--stable-channel" ]; then
  207. RELEASE_CHANNEL="stable"
  208. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS:+${NETDATA_INSTALLER_OPTIONS} }${1}"
  209. shift 1
  210. elif [ "${1}" = "--disable-telemetry" ]; then
  211. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS:+${NETDATA_INSTALLER_OPTIONS} }${1}"
  212. shift 1
  213. elif [ "${1}" = "--local-files" ]; then
  214. NETDATA_UPDATES="" # Disable autoupdates if using pre-downloaded files.
  215. shift 1
  216. if [ -z "${1}" ]; then
  217. fatal "Option --local-files requires extra information. The desired tarball full filename is needed"
  218. fi
  219. NETDATA_LOCAL_TARBALL_OVERRIDE="${1}"
  220. shift 1
  221. if [ -z "${1}" ]; then
  222. fatal "Option --local-files requires a pair of the tarball source and the checksum file"
  223. fi
  224. NETDATA_LOCAL_TARBALL_OVERRIDE_CHECKSUM="${1}"
  225. shift 1
  226. elif [ "${1}" = "--allow-duplicate-install" ]; then
  227. NETDATA_ALLOW_DUPLICATE_INSTALL=1
  228. shift 1
  229. elif [ "${1}" = "--reinstall" ]; then
  230. NETDATA_REINSTALL=1
  231. shift 1
  232. else
  233. echo >&2 "Unknown option '${1}' or invalid number of arguments. Please check the README for the available arguments of ${0} and try again"
  234. exit 1
  235. fi
  236. done
  237. if [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] || [ -n "$DO_NOT_TRACK" ]; then
  238. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS:+${NETDATA_INSTALLER_OPTIONS} }--disable-telemtry"
  239. fi
  240. # Netdata Tarball Base URL (defaults to our Google Storage Bucket)
  241. [ -z "$NETDATA_TARBALL_BASEURL" ] && NETDATA_TARBALL_BASEURL=https://storage.googleapis.com/netdata-nightlies
  242. # ---------------------------------------------------------------------------------------------------------------------
  243. # look for an existing install and try to update that instead if it exists
  244. ndpath="$(command -v netdata 2>/dev/null)"
  245. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ] ; then
  246. ndpath="/opt/netdata/bin/netdata"
  247. fi
  248. if [ -n "$ndpath" ] ; then
  249. ndprefix="$(dirname "$(dirname "${ndpath}")")"
  250. if [ "${ndprefix}" = /usr ] ; then
  251. ndprefix="/"
  252. fi
  253. progress "Found existing install of Netdata under: ${ndprefix}"
  254. if [ -r "${ndprefix}/etc/netdata/.environment" ] ; then
  255. ndstatic="$(grep IS_NETDATA_STATIC_BINARY "${ndprefix}/etc/netdata/.environment" | cut -d "=" -f 2 | tr -d \")"
  256. if [ -z "${NETDATA_REINSTALL}" ] && [ -z "${NETDATA_LOCAL_TARBALL_OVERRIDE}" ] ; then
  257. if [ -x "${ndprefix}/usr/libexec/netdata/netdata-updater.sh" ] ; then
  258. progress "Attempting to update existing install instead of creating a new one"
  259. if run ${sudo} "${ndprefix}/usr/libexec/netdata/netdata-updater.sh" --not-running-from-cron ; then
  260. progress "Updated existing install at ${ndpath}"
  261. exit 0
  262. else
  263. fatal "Failed to update existing Netdata install"
  264. exit 1
  265. fi
  266. else
  267. if [ -z "${NETDATA_ALLOW_DUPLICATE_INSTALL}" ] || [ "${ndstatic}" = "no" ] ; then
  268. fatal "Existing installation detected which cannot be safely updated by this script, refusing to continue."
  269. exit 1
  270. else
  271. progress "User explicitly requested duplicate install, proceeding."
  272. fi
  273. fi
  274. else
  275. if [ "${ndstatic}" = "yes" ] ; then
  276. progress "User requested reinstall instead of update, proceeding."
  277. else
  278. fatal "Existing install is not a static install, please use kickstart.sh instead."
  279. exit 1
  280. fi
  281. fi
  282. else
  283. progress "Existing install appears to be handled manually or through the system package manager."
  284. if [ -z "${NETDATA_ALLOW_DUPLICATE_INSTALL}" ] ; then
  285. fatal "Existing installation detected which cannot be safely updated by this script, refusing to continue."
  286. exit 1
  287. else
  288. progress "User explicitly requested duplicate install, proceeding."
  289. fi
  290. fi
  291. fi
  292. # ----------------------------------------------------------------------------
  293. TMPDIR=$(create_tmp_directory)
  294. cd "${TMPDIR}" || exit 1
  295. if [ -z "${NETDATA_LOCAL_TARBALL_OVERRIDE}" ]; then
  296. set_tarball_urls "${RELEASE_CHANNEL}"
  297. progress "Downloading static netdata binary: ${NETDATA_TARBALL_URL}"
  298. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
  299. download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
  300. else
  301. progress "Installation sources were given as input, running installation using \"${NETDATA_LOCAL_TARBALL_OVERRIDE}\""
  302. run cp "${NETDATA_LOCAL_TARBALL_OVERRIDE}" "${TMPDIR}/netdata-latest.gz.run"
  303. run cp "${NETDATA_LOCAL_TARBALL_OVERRIDE_CHECKSUM}" "${TMPDIR}/sha256sum.txt"
  304. fi
  305. if ! grep netdata-latest.gz.run "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  306. fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${TMPDIR}"
  307. fi
  308. # ----------------------------------------------------------------------------
  309. progress "Installing netdata"
  310. run ${sudo} sh "${TMPDIR}/netdata-latest.gz.run" ${opts} -- ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS}
  311. #shellcheck disable=SC2181
  312. if [ $? -eq 0 ]; then
  313. run ${sudo} rm "${TMPDIR}/netdata-latest.gz.run"
  314. if [ ! "${TMPDIR}" = "/" ] && [ -d "${TMPDIR}" ]; then
  315. run ${sudo} rm -rf "${TMPDIR}"
  316. fi
  317. else
  318. echo >&2 "NOTE: did not remove: ${TMPDIR}/netdata-latest.gz.run"
  319. fi