kickstart-static64.sh 14 KB

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