kickstart-static64.sh 11 KB

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