kickstart.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. #
  4. # Run me with:
  5. #
  6. # bash <(curl -Ss https://my-netdata.io/kickstart.sh)
  7. #
  8. # or (to install all netdata dependencies):
  9. #
  10. # bash <(curl -Ss https://my-netdata.io/kickstart.sh) all
  11. #
  12. # Other options:
  13. # --src-dir PATH keep netdata.git at PATH/netdata.git
  14. # --dont-wait do not prompt for user input
  15. # --non-interactive do not prompt for user input
  16. # --no-updates do not install script for daily updates
  17. #
  18. # This script will:
  19. #
  20. # 1. install all netdata compilation dependencies
  21. # using the package manager of the system
  22. #
  23. # 2. download netdata source code in /usr/src/netdata.git
  24. #
  25. # 3. install netdata
  26. # shellcheck disable=SC1117,SC2016,SC2034,SC2039,SC2059,SC2086,SC2119,SC2120,SC2129,SC2162,SC2166,SC2181
  27. umask 022
  28. [ -z "${UID}" ] && UID="$(id -u)"
  29. # ---------------------------------------------------------------------------------------------------------------------
  30. # library functions copied from installer/functions.sh
  31. which_cmd() {
  32. # shellcheck disable=SC2230
  33. which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
  34. }
  35. check_cmd() {
  36. which_cmd "${1}" >/dev/null 2>&1 && return 0
  37. return 1
  38. }
  39. setup_terminal() {
  40. TPUT_RESET=""
  41. TPUT_BLACK=""
  42. TPUT_RED=""
  43. TPUT_GREEN=""
  44. TPUT_YELLOW=""
  45. TPUT_BLUE=""
  46. TPUT_PURPLE=""
  47. TPUT_CYAN=""
  48. TPUT_WHITE=""
  49. TPUT_BGBLACK=""
  50. TPUT_BGRED=""
  51. TPUT_BGGREEN=""
  52. TPUT_BGYELLOW=""
  53. TPUT_BGBLUE=""
  54. TPUT_BGPURPLE=""
  55. TPUT_BGCYAN=""
  56. TPUT_BGWHITE=""
  57. TPUT_BOLD=""
  58. TPUT_DIM=""
  59. TPUT_UNDERLINED=""
  60. TPUT_BLINK=""
  61. TPUT_INVERTED=""
  62. TPUT_STANDOUT=""
  63. TPUT_BELL=""
  64. TPUT_CLEAR=""
  65. # Is stderr on the terminal? If not, then fail
  66. test -t 2 || return 1
  67. if check_cmd tput
  68. then
  69. if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
  70. then
  71. # Enable colors
  72. TPUT_RESET="$(tput sgr 0)"
  73. TPUT_BLACK="$(tput setaf 0)"
  74. TPUT_RED="$(tput setaf 1)"
  75. TPUT_GREEN="$(tput setaf 2)"
  76. TPUT_YELLOW="$(tput setaf 3)"
  77. TPUT_BLUE="$(tput setaf 4)"
  78. TPUT_PURPLE="$(tput setaf 5)"
  79. TPUT_CYAN="$(tput setaf 6)"
  80. TPUT_WHITE="$(tput setaf 7)"
  81. TPUT_BGBLACK="$(tput setab 0)"
  82. TPUT_BGRED="$(tput setab 1)"
  83. TPUT_BGGREEN="$(tput setab 2)"
  84. TPUT_BGYELLOW="$(tput setab 3)"
  85. TPUT_BGBLUE="$(tput setab 4)"
  86. TPUT_BGPURPLE="$(tput setab 5)"
  87. TPUT_BGCYAN="$(tput setab 6)"
  88. TPUT_BGWHITE="$(tput setab 7)"
  89. TPUT_BOLD="$(tput bold)"
  90. TPUT_DIM="$(tput dim)"
  91. TPUT_UNDERLINED="$(tput smul)"
  92. TPUT_BLINK="$(tput blink)"
  93. TPUT_INVERTED="$(tput rev)"
  94. TPUT_STANDOUT="$(tput smso)"
  95. TPUT_BELL="$(tput bel)"
  96. TPUT_CLEAR="$(tput clear)"
  97. fi
  98. fi
  99. return 0
  100. }
  101. setup_terminal || echo >/dev/null
  102. progress() {
  103. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  104. }
  105. run_ok() {
  106. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  107. }
  108. run_failed() {
  109. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  110. }
  111. ESCAPED_PRINT_METHOD=
  112. printf "%q " test >/dev/null 2>&1
  113. [ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
  114. escaped_print() {
  115. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]
  116. then
  117. printf "%q " "${@}"
  118. else
  119. printf "%s" "${*}"
  120. fi
  121. return 0
  122. }
  123. run_logfile="/dev/null"
  124. run() {
  125. local user="${USER--}" dir="${PWD}" info info_console
  126. if [ "${UID}" = "0" ]
  127. then
  128. info="[root ${dir}]# "
  129. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  130. else
  131. info="[${user} ${dir}]$ "
  132. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  133. fi
  134. printf >> "${run_logfile}" "${info}"
  135. escaped_print >> "${run_logfile}" "${@}"
  136. printf >> "${run_logfile}" " ... "
  137. printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  138. escaped_print >&2 "${@}"
  139. printf >&2 "${TPUT_RESET}\n"
  140. "${@}"
  141. local ret=$?
  142. if [ ${ret} -ne 0 ]
  143. then
  144. run_failed
  145. printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
  146. else
  147. run_ok
  148. printf >> "${run_logfile}" "OK\n"
  149. fi
  150. return ${ret}
  151. }
  152. # ---------------------------------------------------------------------------------------------------------------------
  153. # collect system information
  154. fatal() {
  155. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  156. exit 1
  157. }
  158. export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  159. curl="$(which_cmd curl)"
  160. wget="$(which_cmd wget)"
  161. bash="$(which_cmd bash)"
  162. if [ -z "${BASH_VERSION}" ]
  163. then
  164. # we don't run under bash
  165. if [ ! -z "${bash}" -a -x "${bash}" ]
  166. then
  167. BASH_MAJOR_VERSION=$(${bash} -c 'echo "${BASH_VERSINFO[0]}"')
  168. fi
  169. else
  170. # we run under bash
  171. BASH_MAJOR_VERSION="${BASH_VERSINFO[0]}"
  172. fi
  173. HAS_BASH4=1
  174. if [ -z "${BASH_MAJOR_VERSION}" ]
  175. then
  176. echo >&2 "No BASH is available on this system"
  177. HAS_BASH4=0
  178. elif [ $((BASH_MAJOR_VERSION)) -lt 4 ]
  179. then
  180. echo >&2 "No BASH v4+ is available on this system (installed bash is v${BASH_MAJOR_VERSION}"
  181. HAS_BASH4=0
  182. fi
  183. SYSTEM="$(uname -s)"
  184. OS="$(uname -o)"
  185. MACHINE="$(uname -m)"
  186. cat <<EOF
  187. System : ${SYSTEM}
  188. Operating System : ${OS}
  189. Machine : ${MACHINE}
  190. BASH major version: ${BASH_MAJOR_VERSION}
  191. EOF
  192. sudo=""
  193. [ "${UID}" -ne "0" ] && sudo="sudo"
  194. # ---------------------------------------------------------------------------------------------------------------------
  195. # install required system packages
  196. INTERACTIVE=1
  197. PACKAGES_INSTALLER_OPTIONS="netdata"
  198. NETDATA_INSTALLER_OPTIONS=""
  199. NETDATA_UPDATES="-u"
  200. SOURCE_DST="/usr/src"
  201. while [ ! -z "${1}" ]
  202. do
  203. if [ "${1}" = "all" ]
  204. then
  205. PACKAGES_INSTALLER_OPTIONS="netdata-all"
  206. shift 1
  207. elif [ "${1}" = "--dont-wait" -o "${1}" = "--non-interactive" ]
  208. then
  209. INTERACTIVE=0
  210. shift 1
  211. elif [ "${1}" = "--src-dir" ]
  212. then
  213. SOURCE_DST="${2}"
  214. # echo >&2 "netdata source will be installed at ${SOURCE_DST}/netdata.git"
  215. shift 2
  216. elif [ "${1}" = "--no-updates" ]
  217. then
  218. # echo >&2 "netdata will not auto-update"
  219. NETDATA_UPDATES=
  220. shift 1
  221. else
  222. break
  223. fi
  224. done
  225. if [ "${INTERACTIVE}" = "0" ]
  226. then
  227. PACKAGES_INSTALLER_OPTIONS="--dont-wait --non-interactive ${PACKAGES_INSTALLER_OPTIONS}"
  228. NETDATA_INSTALLER_OPTIONS="--dont-wait"
  229. fi
  230. # echo "PACKAGES_INSTALLER_OPTIONS=${PACKAGES_INSTALLER_OPTIONS}"
  231. # echo "NETDATA_INSTALLER_OPTIONS=${NETDATA_INSTALLER_OPTIONS} ${*}"
  232. if [ "${OS}" = "GNU/Linux" -o "${SYSTEM}" = "Linux" ]
  233. then
  234. if [ "${HAS_BASH4}" = "1" ]
  235. then
  236. tmp="$(mktemp /tmp/netdata-kickstart-XXXXXX)"
  237. url="https://raw.githubusercontent.com/netdata/netdata-demo-site/master/install-required-packages.sh"
  238. progress "Downloading script to detect required packages..."
  239. if [ ! -z "${curl}" ]
  240. then
  241. run ${curl} "${url}" >"${tmp}" || fatal "Cannot download ${url}"
  242. elif [ ! -z "${wget}" ]
  243. then
  244. run "${wget}" -O - "${url}" >"${tmp}" || fatal "Cannot download ${url}"
  245. else
  246. rm "${tmp}"
  247. fatal "I need curl or wget to proceed, but neither is available on this system."
  248. fi
  249. ask=0
  250. if [ -s "${tmp}" ]
  251. then
  252. progress "Running downloaded script to detect required packages..."
  253. run ${sudo} "${bash}" "${tmp}" ${PACKAGES_INSTALLER_OPTIONS} || ask=1
  254. rm "${tmp}"
  255. else
  256. rm "${tmp}"
  257. fatal "Downloaded script is empty..."
  258. fi
  259. if [ "${ask}" = "1" ]
  260. then
  261. echo >&2 "It failed to install all the required packages, but I can try to install netdata."
  262. read -p "Press ENTER to continue to netdata installation > "
  263. progress "OK, let's give it a try..."
  264. fi
  265. else
  266. echo >&2 "WARNING"
  267. echo >&2 "Cannot detect the packages to be installed in this system, without BASH v4+."
  268. echo >&2 "We can only attempt to install netdata..."
  269. echo >&2
  270. fi
  271. else
  272. echo >&2 "WARNING"
  273. echo >&2 "Cannot detect the packages to be installed on a ${SYSTEM} - ${OS} system."
  274. echo >&2 "We can only attempt to install netdata..."
  275. echo >&2
  276. fi
  277. # ---------------------------------------------------------------------------------------------------------------------
  278. # download netdata source
  279. # this has to checked after we have installed the required packages
  280. git="$(which_cmd git)"
  281. NETDATA_SOURCE_DIR=
  282. if [ ! -z "${git}" -a -x "${git}" ]
  283. then
  284. [ ! -d "${SOURCE_DST}" ] && run ${sudo} mkdir -p "${SOURCE_DST}"
  285. if [ ! -d "${SOURCE_DST}/netdata.git" ]
  286. then
  287. progress "Downloading netdata source code..."
  288. run ${sudo} ${git} clone https://github.com/netdata/netdata.git "${SOURCE_DST}/netdata.git" || fatal "Cannot download netdata source"
  289. cd "${SOURCE_DST}/netdata.git" || fatal "Cannot cd to netdata source tree"
  290. else
  291. progress "Updating netdata source code..."
  292. cd "${SOURCE_DST}/netdata.git" || fatal "Cannot cd to netdata source tree"
  293. run ${sudo} ${git} fetch --all || fatal "Cannot fetch netdata source updates"
  294. run ${sudo} ${git} reset --hard origin/master || fatal "Cannot update netdata source tree"
  295. fi
  296. NETDATA_SOURCE_DIR="${SOURCE_DST}/netdata.git"
  297. else
  298. fatal "Cannot find the command 'git' to download the netdata source code."
  299. fi
  300. # ---------------------------------------------------------------------------------------------------------------------
  301. # install netdata from source
  302. if [ ! -z "${NETDATA_SOURCE_DIR}" -a -d "${NETDATA_SOURCE_DIR}" ]
  303. then
  304. cd "${NETDATA_SOURCE_DIR}" || fatal "Cannot cd to netdata source tree"
  305. install=0
  306. if [ -x netdata-updater.sh ]
  307. then
  308. # attempt to run the updater, to respect any compilation settings already in place
  309. progress "Re-installing netdata..."
  310. run ${sudo} ./netdata-updater.sh -f || install=1
  311. else
  312. install=1
  313. fi
  314. if [ "${install}" = "1" ]
  315. then
  316. if [ -x netdata-installer.sh ]
  317. then
  318. progress "Installing netdata..."
  319. run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || \
  320. fatal "netdata-installer.sh exited with error"
  321. else
  322. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)."
  323. fi
  324. fi
  325. else
  326. fatal "Cannot install netdata from source, on this system (cannot download the source code)."
  327. fi