kickstart-static64.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC1117,SC2039,SC2059,SC2086
  4. # ---------------------------------------------------------------------------------------------------------------------
  5. # library functions copied from packaging/installer/functions.sh
  6. setup_terminal() {
  7. TPUT_RESET=""
  8. TPUT_YELLOW=""
  9. TPUT_WHITE=""
  10. TPUT_BGRED=""
  11. TPUT_BGGREEN=""
  12. TPUT_BOLD=""
  13. TPUT_DIM=""
  14. # Is stderr on the terminal? If not, then fail
  15. test -t 2 || return 1
  16. if command -v tput >/dev/null 2>&1; then
  17. if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
  18. # Enable colors
  19. TPUT_RESET="$(tput sgr 0)"
  20. TPUT_YELLOW="$(tput setaf 3)"
  21. TPUT_WHITE="$(tput setaf 7)"
  22. TPUT_BGRED="$(tput setab 1)"
  23. TPUT_BGGREEN="$(tput setab 2)"
  24. TPUT_BOLD="$(tput bold)"
  25. TPUT_DIM="$(tput dim)"
  26. fi
  27. fi
  28. return 0
  29. }
  30. setup_terminal || echo >/dev/null
  31. # -----------------------------------------------------------------------------
  32. fatal() {
  33. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  34. exit 1
  35. }
  36. run_ok() {
  37. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  38. }
  39. run_failed() {
  40. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  41. }
  42. ESCAPED_PRINT_METHOD=
  43. printf "%q " test >/dev/null 2>&1
  44. [ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
  45. escaped_print() {
  46. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  47. printf "%q " "${@}"
  48. else
  49. printf "%s" "${*}"
  50. fi
  51. return 0
  52. }
  53. progress() {
  54. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  55. }
  56. run_logfile="/dev/null"
  57. run() {
  58. local user="${USER--}" dir="${PWD}" info info_console
  59. if [ "${UID}" = "0" ]; then
  60. info="[root ${dir}]# "
  61. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  62. else
  63. info="[${user} ${dir}]$ "
  64. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  65. fi
  66. printf >>"${run_logfile}" "${info}"
  67. escaped_print >>"${run_logfile}" "${@}"
  68. printf >>"${run_logfile}" " ... "
  69. printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  70. escaped_print >&2 "${@}"
  71. printf >&2 "${TPUT_RESET}\n"
  72. "${@}"
  73. local ret=$?
  74. if [ ${ret} -ne 0 ]; then
  75. run_failed
  76. printf >>"${run_logfile}" "FAILED with exit code ${ret}\n"
  77. else
  78. run_ok
  79. printf >>"${run_logfile}" "OK\n"
  80. fi
  81. return ${ret}
  82. }
  83. fatal() {
  84. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  85. exit 1
  86. }
  87. create_tmp_directory() {
  88. # Check if tmp is mounted as noexec
  89. if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
  90. pattern="$(pwd)/netdata-kickstart-XXXXXX"
  91. else
  92. pattern="/tmp/netdata-kickstart-XXXXXX"
  93. fi
  94. mktemp -d $pattern
  95. }
  96. download() {
  97. url="${1}"
  98. dest="${2}"
  99. if command -v curl >/dev/null 2>&1; then
  100. run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
  101. elif command -v wget >/dev/null 2>&1; then
  102. run wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
  103. else
  104. fatal "I need curl or wget to proceed, but neither is available on this system."
  105. fi
  106. }
  107. set_tarball_urls() {
  108. if [ "$1" = "stable" ]; then
  109. local latest
  110. # Simple version
  111. # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
  112. latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
  113. export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.gz.run"
  114. export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
  115. else
  116. export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.gz.run"
  117. export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
  118. fi
  119. }
  120. safe_sha256sum() {
  121. # Within the contexct of the installer, we only use -c option that is common between the two commands
  122. # We will have to reconsider if we start non-common options
  123. if command -v sha256sum >/dev/null 2>&1; then
  124. sha256sum $@
  125. elif command -v shasum >/dev/null 2>&1; then
  126. shasum -a 256 $@
  127. else
  128. fatal "I could not find a suitable checksum binary to use"
  129. fi
  130. }
  131. # ---------------------------------------------------------------------------------------------------------------------
  132. umask 022
  133. sudo=""
  134. [ -z "${UID}" ] && UID="$(id -u)"
  135. [ "${UID}" -ne "0" ] && sudo="sudo"
  136. # ---------------------------------------------------------------------------------------------------------------------
  137. if [ "$(uname -m)" != "x86_64" ]; then
  138. fatal "Static binary versions of netdata are available only for 64bit Intel/AMD CPUs (x86_64), but yours is: $(uname -m)."
  139. fi
  140. if [ "$(uname -s)" != "Linux" ]; then
  141. fatal "Static binary versions of netdata are available only for Linux, but this system is $(uname -s)"
  142. fi
  143. # ---------------------------------------------------------------------------------------------------------------------
  144. opts=
  145. inner_opts=
  146. RELEASE_CHANNEL="nightly"
  147. while [ -n "${1}" ]; do
  148. if [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ] || [ "${1}" = "--accept" ]; then
  149. opts="${opts} --accept"
  150. elif [ "${1}" = "--dont-start-it" ]; then
  151. inner_opts="${inner_opts} ${1}"
  152. elif [ "${1}" = "--stable-channel" ]; then
  153. RELEASE_CHANNEL="stable"
  154. else
  155. echo >&2 "Unknown option '${1}'"
  156. exit 1
  157. fi
  158. shift
  159. done
  160. [ -n "${inner_opts}" ] && inner_opts="-- ${inner_opts}"
  161. # ---------------------------------------------------------------------------------------------------------------------
  162. TMPDIR=$(create_tmp_directory)
  163. cd "${TMPDIR}"
  164. set_tarball_urls "${RELEASE_CHANNEL}"
  165. progress "Downloading static netdata binary: ${NETDATA_TARBALL_URL}"
  166. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
  167. download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
  168. if ! grep netdata-latest.gz.run "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - >/dev/null 2>&1; then
  169. fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${TMPDIR}"
  170. fi
  171. # ---------------------------------------------------------------------------------------------------------------------
  172. progress "Installing netdata"
  173. run ${sudo} sh "${TMPDIR}/netdata-latest.gz.run" ${opts} ${inner_opts}
  174. #shellcheck disable=SC2181
  175. if [ $? -eq 0 ]; then
  176. run ${sudo} rm "${TMPDIR}/netdata-latest.gz.run"
  177. if [ ! "${TMPDIR}" = "/" ] && [ -d "${TMPDIR}" ]; then
  178. run ${sudo} rm -rf "${TMPDIR}"
  179. fi
  180. else
  181. echo >&2 "NOTE: did not remove: ${TMPDIR}/netdata-latest.gz.run"
  182. fi