kickstart.sh 10 KB

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