kickstart-static64.sh 8.6 KB

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