kickstart.sh 10 KB

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