kickstart-static64.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC1117,SC2016,SC2034,SC2039,SC2059,SC2086,SC2119,SC2120,SC2129,SC2162,SC2166,SC2181
  4. umask 022
  5. # make sure UID is set
  6. # shellcheck disable=SC2155
  7. [ -z "${UID}" ] && export UID="$(id -u)"
  8. # ---------------------------------------------------------------------------------------------------------------------
  9. # library functions copied from installer/functions.sh
  10. which_cmd() {
  11. # shellcheck disable=SC2230
  12. which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
  13. }
  14. check_cmd() {
  15. which_cmd "${1}" >/dev/null 2>&1 && return 0
  16. return 1
  17. }
  18. setup_terminal() {
  19. TPUT_RESET=""
  20. TPUT_BLACK=""
  21. TPUT_RED=""
  22. TPUT_GREEN=""
  23. TPUT_YELLOW=""
  24. TPUT_BLUE=""
  25. TPUT_PURPLE=""
  26. TPUT_CYAN=""
  27. TPUT_WHITE=""
  28. TPUT_BGBLACK=""
  29. TPUT_BGRED=""
  30. TPUT_BGGREEN=""
  31. TPUT_BGYELLOW=""
  32. TPUT_BGBLUE=""
  33. TPUT_BGPURPLE=""
  34. TPUT_BGCYAN=""
  35. TPUT_BGWHITE=""
  36. TPUT_BOLD=""
  37. TPUT_DIM=""
  38. TPUT_UNDERLINED=""
  39. TPUT_BLINK=""
  40. TPUT_INVERTED=""
  41. TPUT_STANDOUT=""
  42. TPUT_BELL=""
  43. TPUT_CLEAR=""
  44. # Is stderr on the terminal? If not, then fail
  45. test -t 2 || return 1
  46. if check_cmd tput
  47. then
  48. if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
  49. then
  50. # Enable colors
  51. TPUT_RESET="$(tput sgr 0)"
  52. TPUT_BLACK="$(tput setaf 0)"
  53. TPUT_RED="$(tput setaf 1)"
  54. TPUT_GREEN="$(tput setaf 2)"
  55. TPUT_YELLOW="$(tput setaf 3)"
  56. TPUT_BLUE="$(tput setaf 4)"
  57. TPUT_PURPLE="$(tput setaf 5)"
  58. TPUT_CYAN="$(tput setaf 6)"
  59. TPUT_WHITE="$(tput setaf 7)"
  60. TPUT_BGBLACK="$(tput setab 0)"
  61. TPUT_BGRED="$(tput setab 1)"
  62. TPUT_BGGREEN="$(tput setab 2)"
  63. TPUT_BGYELLOW="$(tput setab 3)"
  64. TPUT_BGBLUE="$(tput setab 4)"
  65. TPUT_BGPURPLE="$(tput setab 5)"
  66. TPUT_BGCYAN="$(tput setab 6)"
  67. TPUT_BGWHITE="$(tput setab 7)"
  68. TPUT_BOLD="$(tput bold)"
  69. TPUT_DIM="$(tput dim)"
  70. TPUT_UNDERLINED="$(tput smul)"
  71. TPUT_BLINK="$(tput blink)"
  72. TPUT_INVERTED="$(tput rev)"
  73. TPUT_STANDOUT="$(tput smso)"
  74. TPUT_BELL="$(tput bel)"
  75. TPUT_CLEAR="$(tput clear)"
  76. fi
  77. fi
  78. return 0
  79. }
  80. setup_terminal || echo >/dev/null
  81. progress() {
  82. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  83. }
  84. run_ok() {
  85. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  86. }
  87. run_failed() {
  88. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  89. }
  90. ESCAPED_PRINT_METHOD=
  91. printf "%q " test >/dev/null 2>&1
  92. [ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
  93. escaped_print() {
  94. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]
  95. then
  96. printf "%q " "${@}"
  97. else
  98. printf "%s" "${*}"
  99. fi
  100. return 0
  101. }
  102. run_logfile="/dev/null"
  103. run() {
  104. local user="${USER--}" dir="${PWD}" info info_console
  105. if [ "${UID}" = "0" ]
  106. then
  107. info="[root ${dir}]# "
  108. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  109. else
  110. info="[${user} ${dir}]$ "
  111. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  112. fi
  113. printf >> "${run_logfile}" "${info}"
  114. escaped_print >> "${run_logfile}" "${@}"
  115. printf >> "${run_logfile}" " ... "
  116. printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  117. escaped_print >&2 "${@}"
  118. printf >&2 "${TPUT_RESET}\n"
  119. "${@}"
  120. local ret=$?
  121. if [ ${ret} -ne 0 ]
  122. then
  123. run_failed
  124. printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
  125. else
  126. run_ok
  127. printf >> "${run_logfile}" "OK\n"
  128. fi
  129. return ${ret}
  130. }
  131. # ---------------------------------------------------------------------------------------------------------------------
  132. fatal() {
  133. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  134. exit 1
  135. }
  136. # ---------------------------------------------------------------------------------------------------------------------
  137. if [ "$(uname -m)" != "x86_64" ]
  138. then
  139. fatal "Static binary versions of netdata are available only for 64bit Intel/AMD CPUs (x86_64), but yours is: $(uname -m)."
  140. fi
  141. if [ "$(uname -s)" != "Linux" ]
  142. then
  143. fatal "Static binary versions of netdata are available only for Linux, but this system is $(uname -s)"
  144. fi
  145. curl="$(which_cmd curl)"
  146. wget="$(which_cmd wget)"
  147. # ---------------------------------------------------------------------------------------------------------------------
  148. progress "Checking the latest version of static build..."
  149. BASE='https://raw.githubusercontent.com/netdata/binary-packages/master'
  150. LATEST=
  151. if [ ! -z "${curl}" -a -x "${curl}" ]
  152. then
  153. LATEST="$(run ${curl} "${BASE}/netdata-latest.gz.run")"
  154. elif [ ! -z "${wget}" -a -x "${wget}" ]
  155. then
  156. LATEST="$(run ${wget} -O - "${BASE}/netdata-latest.gz.run")"
  157. else
  158. fatal "curl or wget are needed for this script to work."
  159. fi
  160. if [ -z "${LATEST}" ]
  161. then
  162. fatal "Cannot find the latest static binary version of netdata."
  163. fi
  164. # ---------------------------------------------------------------------------------------------------------------------
  165. progress "Downloading static netdata binary: ${LATEST}"
  166. ret=1
  167. if [ ! -z "${curl}" -a -x "${curl}" ]
  168. then
  169. run ${curl} "${BASE}/${LATEST}" >"/tmp/${LATEST}"
  170. ret=$?
  171. elif [ ! -z "${wget}" -a -x "${wget}" ]
  172. then
  173. run ${wget} -O "/tmp/${LATEST}" "${BASE}/${LATEST}"
  174. ret=$?
  175. else
  176. fatal "curl or wget are needed for this script to work."
  177. fi
  178. if [ ${ret} -ne 0 -o ! -s "/tmp/${LATEST}" ]
  179. then
  180. fatal "Failed to download the latest static binary version of netdata."
  181. fi
  182. # ---------------------------------------------------------------------------------------------------------------------
  183. opts=
  184. inner_opts=
  185. while [ ! -z "${1}" ]
  186. do
  187. if [ "${1}" = "--dont-wait" -o "${1}" = "--non-interactive" -o "${1}" = "--accept" ]
  188. then
  189. opts="${opts} --accept"
  190. elif [ "${1}" = "--dont-start-it" ]
  191. then
  192. inner_opts="${inner_opts} ${1}"
  193. else
  194. echo >&2 "Unknown option '${1}'"
  195. exit 1
  196. fi
  197. shift
  198. done
  199. [ ! -z "${inner_opts}" ] && inner_opts="-- ${inner_opts}"
  200. # ---------------------------------------------------------------------------------------------------------------------
  201. progress "Installing netdata"
  202. sudo=
  203. [ "${UID}" != "0" ] && sudo="sudo"
  204. run ${sudo} sh "/tmp/${LATEST}" ${opts} ${inner_opts}
  205. if [ $? -eq 0 ]
  206. then
  207. rm "/tmp/${LATEST}"
  208. else
  209. echo >&2 "NOTE: did not remove: /tmp/${LATEST}"
  210. fi