kickstart-static64.sh 8.2 KB

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