kickstart.sh 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  1. #!/bin/sh
  2. #
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. #
  5. # Next unused error code: F0515
  6. # ======================================================================
  7. # Constants
  8. DEFAULT_RELEASE_CHANNEL="nightly"
  9. KICKSTART_OPTIONS="${*}"
  10. KICKSTART_SOURCE="$(
  11. self=${0}
  12. while [ -L "${self}" ]
  13. do
  14. cd "${self%/*}" || exit 1
  15. self=$(readlink "${self}")
  16. done
  17. cd "${self%/*}" || exit 1
  18. echo "$(pwd -P)/${self##*/}"
  19. )"
  20. DEFAULT_PLUGIN_PACKAGES=""
  21. PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  22. REPOCONFIG_DEB_VERSION="2-1"
  23. REPOCONFIG_RPM_VERSION="2-1"
  24. START_TIME="$(date +%s)"
  25. STATIC_INSTALL_ARCHES="x86_64 armv7l aarch64 ppc64le"
  26. # ======================================================================
  27. # URLs used throughout the script
  28. AGENT_BUG_REPORT_URL="https://github.com/netdata/netdata/issues/new/choose"
  29. CLOUD_BUG_REPORT_URL="https://github.com/netdata/netdata-cloud/issues/new/choose"
  30. DISCORD_INVITE="https://discord.gg/5ygS846fR6"
  31. DISCUSSIONS_URL="https://github.com/netdata/netdata/discussions"
  32. DOCS_URL="https://learn.netdata.cloud/docs/"
  33. FORUM_URL="https://community.netdata.cloud/"
  34. INSTALL_DOC_URL="https://learn.netdata.cloud/docs/install-the-netdata-agent/one-line-installer-for-all-linux-systems"
  35. PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
  36. PUBLIC_CLOUD_URL="https://app.netdata.cloud"
  37. REPOCONFIG_DEB_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
  38. REPOCONFIG_RPM_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
  39. TELEMETRY_URL="https://us-east1-netdata-analytics-bi.cloudfunctions.net/ingest_agent_events"
  40. # ======================================================================
  41. # Defaults for environment variables
  42. DRY_RUN=0
  43. SELECTED_INSTALL_METHOD="none"
  44. INSTALL_TYPE="unknown"
  45. INSTALL_PREFIX=""
  46. NETDATA_AUTO_UPDATES="default"
  47. NETDATA_CLAIM_URL="https://api.netdata.cloud"
  48. NETDATA_COMMAND="default"
  49. NETDATA_DISABLE_CLOUD=0
  50. NETDATA_INSTALLER_OPTIONS=""
  51. NETDATA_FORCE_METHOD=""
  52. NETDATA_OFFLINE_INSTALL_SOURCE=""
  53. NETDATA_REQUIRE_CLOUD=1
  54. NETDATA_WARNINGS=""
  55. RELEASE_CHANNEL="default"
  56. if [ -n "$DISABLE_TELEMETRY" ]; then
  57. NETDATA_DISABLE_TELEMETRY="${DISABLE_TELEMETRY}"
  58. elif [ -n "$DO_NOT_TRACK" ]; then
  59. NETDATA_DISABLE_TELEMETRY="${DO_NOT_TRACK}"
  60. else
  61. NETDATA_DISABLE_TELEMETRY=0
  62. fi
  63. NETDATA_TARBALL_BASEURL="${NETDATA_TARBALL_BASEURL:-https://github.com/netdata/netdata-nightlies/releases}"
  64. if echo "${0}" | grep -q 'kickstart-static64'; then
  65. NETDATA_FORCE_METHOD='static'
  66. fi
  67. if [ ! -t 1 ]; then
  68. INTERACTIVE=0
  69. else
  70. INTERACTIVE=1
  71. fi
  72. CURL="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null && true)"
  73. # ======================================================================
  74. # Shared messages used in multiple places throughout the script.
  75. BADCACHE_MSG="Usually this is a result of an older copy of the file being cached somewhere upstream and can be resolved by retrying in an hour"
  76. BADNET_MSG="This is usually a result of a networking issue"
  77. ERROR_F0003="Could not find a usable HTTP client. Either curl or wget is required to proceed with installation."
  78. BADOPT_MSG="If you are following a third-party guide online, please see ${INSTALL_DOC_URL} for current instructions for using this script. If you are using a local copy of this script instead of fetching it from our servers, consider updating it. If you intended to pass this option to the installer code, please use either --local-build-options or --static-install-options to specify it instead."
  79. # ======================================================================
  80. # Core program logic
  81. main() {
  82. case "${ACTION}" in
  83. uninstall)
  84. uninstall
  85. printf >&2 "Finished uninstalling the Netdata Agent."
  86. deferred_warnings
  87. cleanup
  88. trap - EXIT
  89. exit 0
  90. ;;
  91. reinstall-clean)
  92. NEW_INSTALL_PREFIX="${INSTALL_PREFIX}"
  93. uninstall
  94. cleanup
  95. ACTION=''
  96. INSTALL_PREFIX="${NEW_INSTALL_PREFIX}"
  97. # shellcheck disable=SC2086
  98. main
  99. trap - EXIT
  100. exit 0
  101. ;;
  102. prepare-offline)
  103. prepare_offline_install_source "${OFFLINE_TARGET}"
  104. deferred_warnings
  105. trap - EXIT
  106. exit 0
  107. ;;
  108. esac
  109. set_tmpdir
  110. if [ -n "${INSTALL_VERSION}" ]; then
  111. if echo "${INSTALL_VERSION}" | grep -E -o "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$" > /dev/null 2>&1; then
  112. NEW_SELECTED_RELEASE_CHANNEL="stable"
  113. else
  114. NEW_SELECTED_RELEASE_CHANNEL="nightly"
  115. fi
  116. if ! [ "${NEW_SELECTED_RELEASE_CHANNEL}" = "${SELECTED_RELEASE_CHANNEL}" ]; then
  117. warning "Selected release channel does not match this version and it will be changed automatically."
  118. SELECTED_RELEASE_CHANNEL="${NEW_SELECTED_RELEASE_CHANNEL}"
  119. fi
  120. fi
  121. case "${SYSTYPE}" in
  122. Linux) install_on_linux ;;
  123. Darwin) install_on_macos ;;
  124. FreeBSD) install_on_freebsd ;;
  125. esac
  126. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  127. claim
  128. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  129. soft_disable_cloud
  130. fi
  131. set_auto_updates
  132. printf >&2 "%s\n\n" "Successfully installed the Netdata Agent."
  133. deferred_warnings
  134. success_banner
  135. telemetry_event INSTALL_SUCCESS "" ""
  136. cleanup
  137. trap - EXIT
  138. }
  139. # ======================================================================
  140. # Usage info
  141. usage() {
  142. cat << HEREDOC
  143. USAGE: kickstart.sh [options]
  144. where options include:
  145. --non-interactive Do not prompt for user input. (default: prompt if there is a controlling terminal)
  146. --interactive Prompt for user input even if there is no controlling terminal.
  147. --dont-start-it Do not start the agent by default (only for static installs or local builds)
  148. --dry-run Report what we would do with the given options on this system, but don’t actually do anything.
  149. --release-channel Specify the release channel to use for the install (default: ${DEFAULT_RELEASE_CHANNEL})
  150. --stable-channel Equivalent to "--release-channel stable"
  151. --nightly-channel Equivalent to "--release-channel nightly"
  152. --no-updates Do not enable automatic updates (default: enable automatic updates using the best supported scheduling method)
  153. --auto-update Enable automatic updates.
  154. --auto-update-type Specify a particular scheduling type for auto-updates (valid types: systemd, interval, crontab)
  155. --disable-telemetry Opt-out of anonymous statistics.
  156. --native-only Only install if native binary packages are available.
  157. --static-only Only install if a static build is available.
  158. --build-only Only install using a local build.
  159. --disable-cloud Disable support for Netdata Cloud (default: detect)
  160. --require-cloud Only install if Netdata Cloud can be enabled. Overrides --disable-cloud.
  161. --install-prefix <path> Specify an installation prefix for local builds (default: autodetect based on system type).
  162. --old-install-prefix <path> Specify an old local builds installation prefix for uninstall/reinstall (if it's not default).
  163. --install-version <version> Specify the version of Netdata to install.
  164. --claim-token Use a specified token for claiming to Netdata Cloud.
  165. --claim-rooms When claiming, add the node to the specified rooms.
  166. --claim-* Specify other options for the claiming script.
  167. --no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
  168. --local-build-options Specify additional options to pass to the installer code when building locally. Only valid if --build-only is also specified.
  169. --static-install-options Specify additional options to pass to the static installer code. Only valid if --static-only is also specified.
  170. The following options are mutually exclusive and specifiy special operations other than trying to install Netdata normally or update an existing install:
  171. --reinstall If there is an existing install, reinstall it instead of trying to update it. If there is no existing install, install netdata normally.
  172. --reinstall-even-if-unsafe If there is an existing install, reinstall it instead of trying to update it, even if doing so is known to potentially break things. If there is no existing install, install Netdata normally.
  173. --reinstall-clean If there is an existing install, uninstall it before trying to install Netdata. Fails if there is no existing install.
  174. --uninstall Uninstall an existing installation of Netdata. Fails if there is no existing install.
  175. --claim-only If there is an existing install, only try to claim it without attempting to update it. If there is no existing install, install and claim Netdata normally.
  176. --repositories-only Only install repository configuration packages instead of doing a full install of Netdata. Automatically sets --native-only.
  177. --prepare-offline-install-source Instead of installing the agent, prepare a directory that can be used to install on another system without needing to download anything.
  178. Additionally, this script may use the following environment variables:
  179. TMPDIR: Used to specify where to put temporary files. On most systems, the default we select
  180. automatically should be fine. The user running the script needs to both be able to
  181. write files to the temporary directory, and run files from that location.
  182. ROOTCMD: Used to specify a command to use to run another command with root privileges if needed. By
  183. default we try to use sudo, doas, or pkexec (in that order of preference), but if
  184. you need special options for one of those to work, or have a different tool to do
  185. the same thing on your system, you can specify it here.
  186. DISABLE_TELEMETRY If set to a value other than 0, behave as if \`--disable-telemetry\` was specified.
  187. HEREDOC
  188. }
  189. # ======================================================================
  190. # Telemetry functions
  191. telemetry_event() {
  192. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ] || [ "${DRY_RUN}" -eq 1 ]; then
  193. return 0
  194. fi
  195. now="$(date +%s)"
  196. total_duration="$((now - START_TIME))"
  197. if [ -e "/etc/os-release" ]; then
  198. eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /etc/os-release | sed 's/^/HOST_/')"
  199. fi
  200. if [ -z "${HOST_NAME}" ] || [ -z "${HOST_VERSION}" ] || [ -z "${HOST_ID}" ]; then
  201. if [ -f "/etc/lsb-release" ]; then
  202. DISTRIB_ID="unknown"
  203. DISTRIB_RELEASE="unknown"
  204. DISTRIB_CODENAME="unknown"
  205. eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
  206. if [ -z "${HOST_NAME}" ]; then HOST_NAME="${DISTRIB_ID}"; fi
  207. if [ -z "${HOST_VERSION}" ]; then HOST_VERSION="${DISTRIB_RELEASE}"; fi
  208. if [ -z "${HOST_ID}" ]; then HOST_ID="${DISTRIB_CODENAME}"; fi
  209. fi
  210. fi
  211. KERNEL_NAME="$(uname -s)"
  212. if [ "${KERNEL_NAME}" = FreeBSD ]; then
  213. TOTAL_RAM="$(sysctl -n hw.physmem)"
  214. elif [ "${KERNEL_NAME}" = Darwin ]; then
  215. TOTAL_RAM="$(sysctl -n hw.memsize)"
  216. elif [ -r /proc/meminfo ]; then
  217. TOTAL_RAM="$(grep -F MemTotal /proc/meminfo | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -f 1 -d ' ')"
  218. TOTAL_RAM="$((TOTAL_RAM * 1024))"
  219. fi
  220. if [ "${KERNEL_NAME}" = Darwin ] && command -v ioreg >/dev/null 2>&1; then
  221. DISTINCT_ID="macos-$(ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }')"
  222. elif [ -f /etc/machine-id ]; then
  223. DISTINCT_ID="machine-$(cat /etc/machine-id)"
  224. elif [ -f /var/db/dbus/machine-id ]; then
  225. DISTINCT_ID="dbus-$(cat /var/db/dbus/machine-id)"
  226. elif [ -f /var/lib/dbus/machine-id ]; then
  227. DISTINCT_ID="dbus-$(cat /var/lib/dbus/machine-id)"
  228. elif command -v uuidgen > /dev/null 2>&1; then
  229. DISTINCT_ID="uuid-$(uuidgen | tr '[:upper:]' '[:lower:]')"
  230. else
  231. DISTINCT_ID="null"
  232. fi
  233. REQ_BODY="$(cat << EOF
  234. {
  235. "event": "${1}",
  236. "properties": {
  237. "distinct_id": "${DISTINCT_ID}",
  238. "event_source": "agent installer",
  239. "\$current_url": "agent installer",
  240. "\$pathname": "netdata-installer",
  241. "\$host": "installer.netdata.io",
  242. "\$ip": "127.0.0.1",
  243. "script_variant": "kickstart-ng",
  244. "error_code": "${3}",
  245. "error_message": "${2}",
  246. "install_options": "${KICKSTART_OPTIONS}",
  247. "install_interactivity": "${INTERACTIVE}",
  248. "install_auto_updates": "${NETDATA_AUTO_UPDATES}",
  249. "install_command": "${NETDATA_COMMAND}",
  250. "total_runtime": "${total_duration}",
  251. "selected_install_method": "${SELECTED_INSTALL_METHOD}",
  252. "netdata_release_channel": "${RELEASE_CHANNEL:-null}",
  253. "netdata_install_type": "${INSTALL_TYPE}",
  254. "host_os_name": "${HOST_NAME:-unknown}",
  255. "host_os_id": "${HOST_ID:-unknown}",
  256. "host_os_id_like": "${HOST_ID_LIKE:-unknown}",
  257. "host_os_version": "${HOST_VERSION:-unknown}",
  258. "host_os_version_id": "${HOST_VERSION_ID:-unknown}",
  259. "system_kernel_name": "${KERNEL_NAME}",
  260. "system_kernel_version": "$(uname -r)",
  261. "system_architecture": "$(uname -m)",
  262. "system_total_ram": "${TOTAL_RAM:-unknown}"
  263. }
  264. }
  265. EOF
  266. )"
  267. if [ -n "${CURL}" ]; then
  268. "${CURL}" --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null
  269. elif command -v wget > /dev/null 2>&1; then
  270. if wget --help 2>&1 | grep BusyBox > /dev/null 2>&1; then
  271. # BusyBox-compatible version of wget, there is no --no-check-certificate option
  272. wget -q -O - \
  273. -T 1 \
  274. --header 'Content-Type: application/json' \
  275. --post-data "${REQ_BODY}" \
  276. "${TELEMETRY_URL}" > /dev/null
  277. else
  278. wget -q -O - --no-check-certificate \
  279. --method POST \
  280. --timeout=1 \
  281. --header 'Content-Type: application/json' \
  282. --body-data "${REQ_BODY}" \
  283. "${TELEMETRY_URL}" > /dev/null
  284. fi
  285. fi
  286. }
  287. trap_handler() {
  288. code="${1}"
  289. lineno="${2}"
  290. deferred_warnings
  291. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ERROR ${TPUT_RESET} Installer exited unexpectedly (${code}-${lineno})"
  292. case "${code}" in
  293. 0) printf >&2 "%s\n" "This is almost certainly the result of a bug. If you have time, please report it at ${AGENT_BUG_REPORT_URL}." ;;
  294. *)
  295. printf >&2 "%s\n" "This is probably a result of a transient issue on your system. Things should work correctly if you try again."
  296. printf >&2 "%s\n" "If you continue to experience this issue, you can reacn out to us for support on:"
  297. support_list
  298. ;;
  299. esac
  300. telemetry_event INSTALL_CRASH "Installer exited unexpectedly (${code}-${lineno})" "E${code}-${lineno}"
  301. trap - EXIT
  302. cleanup
  303. exit 1
  304. }
  305. trap 'trap_handler 0 ${LINENO}' EXIT
  306. trap 'trap_handler 1 0' HUP
  307. trap 'trap_handler 2 0' INT
  308. trap 'trap_handler 3 0' QUIT
  309. trap 'trap_handler 13 0' PIPE
  310. trap 'trap_handler 15 0' TERM
  311. # ======================================================================
  312. # Utility functions
  313. setup_terminal() {
  314. TPUT_RESET=""
  315. TPUT_WHITE=""
  316. TPUT_BGRED=""
  317. TPUT_BGGREEN=""
  318. TPUT_BOLD=""
  319. TPUT_DIM=""
  320. # Is stderr on the terminal? If not, then fail
  321. test -t 2 || return 1
  322. if command -v tput > /dev/null 2>&1; then
  323. if num_colors=$(tput colors 2> /dev/null) && [ "${num_colors:-0}" -ge 8 ]; then
  324. # Enable colors
  325. TPUT_RESET="$(tput sgr 0)"
  326. TPUT_WHITE="$(tput setaf 7)"
  327. TPUT_BGRED="$(tput setab 1)"
  328. TPUT_BGGREEN="$(tput setab 2)"
  329. TPUT_BOLD="$(tput bold)"
  330. TPUT_DIM="$(tput dim)"
  331. fi
  332. fi
  333. echo "${TPUT_RESET}"
  334. return 0
  335. }
  336. support_list() {
  337. printf >&2 "%s\n" " - GitHub: ${DISCUSSIONS_URL}"
  338. printf >&2 "%s\n" " - Discord: ${DISCORD_INVITE}"
  339. printf >&2 "%s\n" " - Our community forums: ${FORUM_URL}"
  340. }
  341. success_banner() {
  342. printf >&2 "%s\n\n" "Official documentation can be found online at ${DOCS_URL}."
  343. if [ -z "${CLAIM_TOKEN}" ]; then
  344. printf >&2 "%s\n\n" "Looking to monitor all of your infrastructure with Netdata? Check out Netdata Cloud at ${PUBLIC_CLOUD_URL}."
  345. fi
  346. printf >&2 "%s\n" "Join our community and connect with us on:"
  347. support_list
  348. }
  349. cleanup() {
  350. if [ -z "${NO_CLEANUP}" ] && [ -n "${tmpdir}" ]; then
  351. cd || true
  352. run_as_root rm -rf "${tmpdir}"
  353. fi
  354. }
  355. deferred_warnings() {
  356. if [ -n "${NETDATA_WARNINGS}" ]; then
  357. printf >&2 "%s\n" "The following non-fatal warnings or errors were encountered:"
  358. # shellcheck disable=SC2059
  359. printf >&2 "${NETDATA_WARNINGS}"
  360. printf >&2 "\n\n"
  361. fi
  362. }
  363. fatal() {
  364. deferred_warnings
  365. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${1}"
  366. printf >&2 "%s\n" "For community support, you can connect with us on:"
  367. support_list
  368. telemetry_event "INSTALL_FAILED" "${1}" "${2}"
  369. cleanup
  370. trap - EXIT
  371. exit 1
  372. }
  373. ESCAPED_PRINT_METHOD=
  374. # shellcheck disable=SC3050
  375. if printf "%s " test > /dev/null 2>&1; then
  376. ESCAPED_PRINT_METHOD="printfq"
  377. fi
  378. escaped_print() {
  379. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  380. # shellcheck disable=SC3050
  381. printf "%s " "${@}"
  382. else
  383. printf "%s" "${*}"
  384. fi
  385. return 0
  386. }
  387. progress() {
  388. echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
  389. }
  390. run_logfile="/dev/null"
  391. run() {
  392. user="${USER--}"
  393. dir="${PWD}"
  394. if [ "$(id -u)" = "0" ]; then
  395. info="[root ${dir}]# "
  396. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  397. else
  398. info="[${user} ${dir}]$ "
  399. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  400. fi
  401. if [ "${DRY_RUN}" -eq 1 ]; then
  402. printf >&2 "%s" "Would run command:\n"
  403. fi
  404. {
  405. printf "%s" "${info}"
  406. escaped_print "${@}"
  407. printf " ... "
  408. } >> "${run_logfile}"
  409. printf >&2 "%s" "${info_console}${TPUT_BOLD}"
  410. escaped_print >&2 "${@}"
  411. printf >&2 "%s\n" "${TPUT_RESET}"
  412. if [ "${DRY_RUN}" -ne 1 ]; then
  413. "${@}"
  414. ret=$?
  415. else
  416. ret=0
  417. fi
  418. if [ ${ret} -ne 0 ]; then
  419. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
  420. printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
  421. # shellcheck disable=SC2089
  422. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  423. else
  424. printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
  425. printf "OK\n" >> "${run_logfile}"
  426. fi
  427. return ${ret}
  428. }
  429. run_as_root() {
  430. confirm_root_support
  431. if [ "$(id -u)" -ne "0" ]; then
  432. printf >&2 "Root privileges required to run %s\n" "${*}"
  433. fi
  434. run ${ROOTCMD} "${@}"
  435. }
  436. run_script() {
  437. set_tmpdir
  438. export NETDATA_SCRIPT_STATUS_PATH="${tmpdir}/.script-status"
  439. export NETDATA_SAVE_WARNINGS=1
  440. export NETDATA_PROPAGATE_WARNINGS=1
  441. # shellcheck disable=SC2090
  442. export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
  443. # shellcheck disable=SC2086
  444. run ${ROOTCMD} "${@}"
  445. if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  446. # shellcheck disable=SC1090
  447. . "${NETDATA_SCRIPT_STATUS_PATH}"
  448. rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
  449. fi
  450. }
  451. warning() {
  452. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
  453. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${*}"
  454. }
  455. _cannot_use_tmpdir() {
  456. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  457. ret=0
  458. if [ -z "${testfile}" ]; then
  459. return "${ret}"
  460. fi
  461. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
  462. if chmod +x "${testfile}"; then
  463. if [ "$("${testfile}")" = "SUCCESS" ]; then
  464. ret=1
  465. fi
  466. fi
  467. fi
  468. rm -f "${testfile}"
  469. return "${ret}"
  470. }
  471. create_tmp_directory() {
  472. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
  473. if _cannot_use_tmpdir /tmp; then
  474. if _cannot_use_tmpdir "${PWD}"; then
  475. fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." F0400
  476. else
  477. TMPDIR="${PWD}"
  478. fi
  479. else
  480. TMPDIR="/tmp"
  481. fi
  482. fi
  483. mktemp -d -t netdata-kickstart-XXXXXXXXXX
  484. }
  485. set_tmpdir() {
  486. if [ -z "${tmpdir}" ] || [ ! -d "${tmpdir}" ]; then
  487. tmpdir="$(create_tmp_directory)"
  488. progress "Using ${tmpdir} as a temporary directory."
  489. cd "${tmpdir}" || fatal "Failed to change current working directory to ${tmpdir}." F000A
  490. fi
  491. }
  492. check_for_remote_file() {
  493. url="${1}"
  494. if echo "${url}" | grep -Eq "^file:///"; then
  495. [ -e "${url#file://}" ] || return 1
  496. elif [ -n "${CURL}" ]; then
  497. "${CURL}" --output /dev/null --silent --head --fail "${url}" || return 1
  498. elif command -v wget > /dev/null 2>&1; then
  499. wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK' || return 1
  500. else
  501. fatal "${ERROR_F0003}" F0003
  502. fi
  503. }
  504. download() {
  505. url="${1}"
  506. dest="${2}"
  507. if echo "${url}" | grep -Eq "^file:///"; then
  508. run cp "${url#file://}" "${dest}" || return 1
  509. elif [ -n "${CURL}" ]; then
  510. run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
  511. elif command -v wget > /dev/null 2>&1; then
  512. run wget -T 15 -O "${dest}" "${url}" || return 1
  513. else
  514. fatal "${ERROR_F0003}" F0003
  515. fi
  516. }
  517. get_redirect() {
  518. url="${1}"
  519. if [ -n "${CURL}" ]; then
  520. run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -o '[^/]*$'" || return 1
  521. elif command -v wget > /dev/null 2>&1; then
  522. run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -o '[^/]*$'" || return 1
  523. else
  524. fatal "${ERROR_F0003}" F0003
  525. fi
  526. }
  527. safe_sha256sum() {
  528. # Within the context of the installer, we only use -c option that is common between the two commands
  529. # We will have to reconsider if we start using non-common options
  530. if command -v shasum > /dev/null 2>&1; then
  531. shasum -a 256 "$@"
  532. elif command -v sha256sum > /dev/null 2>&1; then
  533. sha256sum "$@"
  534. else
  535. fatal "Could not find a usable checksum tool. Either sha256sum, or a version of shasum supporting SHA256 checksums is required to proceed with installation." F0004
  536. fi
  537. }
  538. get_system_info() {
  539. SYSARCH="$(uname -m)"
  540. case "$(uname -s)" in
  541. Linux)
  542. SYSTYPE="Linux"
  543. if [ -z "${SKIP_DISTRO_DETECTION}" ]; then
  544. os_release_file=
  545. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  546. os_release_file="/etc/os-release"
  547. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  548. os_release_file="/usr/lib/os-release"
  549. else
  550. warning "Cannot find usable OS release information. Native packages will not be available for this install."
  551. fi
  552. if [ -n "${os_release_file}" ]; then
  553. # shellcheck disable=SC1090
  554. . "${os_release_file}"
  555. DISTRO="${ID}"
  556. SYSVERSION="${VERSION_ID}"
  557. SYSCODENAME="${VERSION_CODENAME}"
  558. else
  559. DISTRO="unknown"
  560. DISTRO_COMPAT_NAME="unknown"
  561. SYSVERSION="unknown"
  562. SYSCODENAME="unknown"
  563. fi
  564. else
  565. warning "Distribution auto-detection overridden by user. This is not guaranteed to work, and is not officially supported."
  566. fi
  567. supported_compat_names="debian ubuntu centos fedora opensuse ol amzn arch"
  568. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  569. DISTRO_COMPAT_NAME="${DISTRO}"
  570. else
  571. case "${DISTRO}" in
  572. opensuse-leap)
  573. DISTRO_COMPAT_NAME="opensuse"
  574. ;;
  575. opensuse-tumbleweed)
  576. DISTRO_COMPAT_NAME="opensuse"
  577. SYSVERSION="tumbleweed"
  578. ;;
  579. cloudlinux|almalinux|rocky|rhel)
  580. DISTRO_COMPAT_NAME="centos"
  581. ;;
  582. artix|manjaro|obarun)
  583. DISTRO_COMPAT_NAME="arch"
  584. ;;
  585. *)
  586. DISTRO_COMPAT_NAME="unknown"
  587. ;;
  588. esac
  589. fi
  590. case "${DISTRO_COMPAT_NAME}" in
  591. centos|ol) SYSVERSION=$(echo "$SYSVERSION" | cut -d'.' -f1) ;;
  592. esac
  593. ;;
  594. Darwin)
  595. SYSTYPE="Darwin"
  596. SYSVERSION="$(sw_vers -buildVersion)"
  597. ;;
  598. FreeBSD)
  599. SYSTYPE="FreeBSD"
  600. SYSVERSION="$(uname -K)"
  601. ;;
  602. *) fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script." F0200 ;;
  603. esac
  604. }
  605. str_in_list() {
  606. printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
  607. return $?
  608. }
  609. confirm_root_support() {
  610. if [ "$(id -u)" -ne "0" ]; then
  611. if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
  612. if [ "${INTERACTIVE}" -eq 0 ]; then
  613. ROOTCMD="sudo -n"
  614. else
  615. ROOTCMD="sudo"
  616. fi
  617. fi
  618. if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
  619. if [ "${INTERACTIVE}" -eq 0 ]; then
  620. ROOTCMD="doas -n"
  621. else
  622. ROOTCMD="doas"
  623. fi
  624. fi
  625. if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
  626. ROOTCMD="pkexec"
  627. fi
  628. if [ -z "${ROOTCMD}" ]; then
  629. fatal "This script needs root privileges to install Netdata, but cannot find a way to gain them (we support sudo, doas, and pkexec). Either re-run this script as root, or set \$ROOTCMD to a command that can be used to gain root privileges." F0201
  630. fi
  631. fi
  632. }
  633. confirm() {
  634. prompt="${1} [y/n]"
  635. while true; do
  636. echo "${prompt}"
  637. read -r yn
  638. case "$yn" in
  639. [Yy]*) return 0;;
  640. [Nn]*) return 1;;
  641. *) echo "Please answer yes or no.";;
  642. esac
  643. done
  644. }
  645. # ======================================================================
  646. # Existing install handling code
  647. update() {
  648. updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
  649. if run_as_root test -x "${updater}"; then
  650. if [ "${DRY_RUN}" -eq 1 ]; then
  651. progress "Would attempt to update existing installation by running the updater script located at: ${updater}"
  652. return 0
  653. fi
  654. if [ "${INTERACTIVE}" -eq 0 ]; then
  655. opts="--non-interactive"
  656. else
  657. opts="--interactive"
  658. fi
  659. if run_script "${updater}" ${opts} --not-running-from-cron; then
  660. progress "Updated existing install at ${ndprefix}"
  661. return 0
  662. else
  663. if [ -n "${EXIT_REASON}" ]; then
  664. fatal "Failed to update existing Netdata install at ${ndprefix}: ${EXIT_REASON}" "${EXIT_CODE}"
  665. else
  666. fatal "Failed to update existing Netdata install at ${ndprefix}: Encountered an unhandled error in the updater. Further information about this error may be displayed above." U0000
  667. fi
  668. fi
  669. else
  670. warning "Could not find a usable copy of the updater script. We are unable to update this system in place."
  671. return 1
  672. fi
  673. }
  674. uninstall() {
  675. set_tmpdir
  676. get_system_info
  677. detect_existing_install
  678. if [ -n "${OLD_INSTALL_PREFIX}" ]; then
  679. INSTALL_PREFIX="$(echo "${OLD_INSTALL_PREFIX}/" | sed 's/$/netdata/g')"
  680. else
  681. INSTALL_PREFIX="${ndprefix}"
  682. fi
  683. uninstaller="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh"
  684. uninstaller_url="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh"
  685. if [ $INTERACTIVE = 0 ]; then
  686. FLAGS="--yes --force"
  687. else
  688. FLAGS="--yes"
  689. fi
  690. if [ -x "${uninstaller}" ]; then
  691. if [ "${DRY_RUN}" -eq 1 ]; then
  692. progress "Would attempt to uninstall existing install with uninstaller script found at: ${uninstaller}"
  693. return 0
  694. else
  695. progress "Found existing netdata-uninstaller. Running it.."
  696. # shellcheck disable=SC2086
  697. if ! run_script "${uninstaller}" ${FLAGS}; then
  698. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  699. fi
  700. fi
  701. else
  702. if [ "${DRY_RUN}" -eq 1 ]; then
  703. progress "Would download installer script from: ${uninstaller_url}"
  704. progress "Would attempt to uninstall existing install with downloaded uninstaller script."
  705. return 0
  706. else
  707. progress "Downloading netdata-uninstaller ..."
  708. download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
  709. chmod +x "${tmpdir}/netdata-uninstaller.sh"
  710. # shellcheck disable=SC2086
  711. if ! run_script "${tmpdir}/netdata-uninstaller.sh" ${FLAGS}; then
  712. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  713. fi
  714. fi
  715. fi
  716. }
  717. detect_existing_install() {
  718. set_tmpdir
  719. progress "Checking for existing installations of Netdata..."
  720. if pkg_installed netdata; then
  721. ndprefix="/"
  722. EXISTING_INSTALL_IS_NATIVE="1"
  723. else
  724. EXISTING_INSTALL_IS_NATIVE="0"
  725. if [ -n "${INSTALL_PREFIX}" ]; then
  726. searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
  727. searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
  728. else
  729. searchpath="${PATH}"
  730. fi
  731. ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  732. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
  733. ndpath="/opt/netdata/bin/netdata"
  734. fi
  735. if [ -n "${ndpath}" ]; then
  736. case "${ndpath}" in
  737. */usr/bin/netdata|*/usr/sbin/netdata) ndprefix="$(dirname "$(dirname "$(dirname "${ndpath}")")")" ;;
  738. *) ndprefix="$(dirname "$(dirname "${ndpath}")")" ;;
  739. esac
  740. fi
  741. if echo "${ndprefix}" | grep -Eq '^/usr$'; then
  742. ndprefix="$(dirname "${ndprefix}")"
  743. fi
  744. fi
  745. if [ -n "${ndprefix}" ]; then
  746. typefile="${ndprefix}/etc/netdata/.install-type"
  747. if [ -r "${typefile}" ]; then
  748. run_as_root sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
  749. # shellcheck disable=SC1090,SC1091
  750. . "${tmpdir}/install-type"
  751. else
  752. INSTALL_TYPE="unknown"
  753. fi
  754. envfile="${ndprefix}/etc/netdata/.environment"
  755. if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
  756. if [ -r "${envfile}" ]; then
  757. run_as_root sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
  758. # shellcheck disable=SC1091
  759. . "${tmpdir}/environment"
  760. if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
  761. if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
  762. INSTALL_TYPE="legacy-static"
  763. else
  764. INSTALL_TYPE="legacy-build"
  765. fi
  766. fi
  767. fi
  768. fi
  769. fi
  770. }
  771. handle_existing_install() {
  772. detect_existing_install
  773. if [ -z "${ndprefix}" ] || [ -z "${INSTALL_TYPE}" ]; then
  774. progress "No existing installations of netdata found, assuming this is a fresh install."
  775. return 0
  776. fi
  777. case "${INSTALL_TYPE}" in
  778. kickstart-*|legacy-*|binpkg-*|manual-static|unknown)
  779. if [ "${INSTALL_TYPE}" = "unknown" ]; then
  780. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  781. warning "Found an existing netdata install managed by the system package manager, but could not determine the install type. Usually this means you installed an unsupported third-party netdata package. This script supports claiming most such installs, but attempting to update or reinstall them using this script may be dangerous."
  782. else
  783. warning "Found an existing netdata install at ${ndprefix}, but could not determine the install type. Usually this means you installed Netdata through your distribution’s regular package repositories or some other unsupported method."
  784. fi
  785. else
  786. progress "Found an existing netdata install at ${ndprefix}, with installation type '${INSTALL_TYPE}'."
  787. fi
  788. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  789. progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
  790. case "${INSTALL_TYPE}" in
  791. binpkg-*) NETDATA_FORCE_METHOD='native' ;;
  792. *-build) NETDATA_FORCE_METHOD='build' ;;
  793. *-static) NETDATA_FORCE_METHOD='static' ;;
  794. *)
  795. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  796. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  797. elif [ "${INTERACTIVE}" -eq 0 ]; then
  798. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  799. else
  800. if [ "${EXISTING_INSTALL_IS_NATIVE}" ]; then
  801. reinstall_prompt="Reinstalling over top of an existing install managed by the system package manager is known to cause things to break, are you sure you want to continue?"
  802. else
  803. reinstall_prompt="Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"
  804. fi
  805. if confirm "${reinstall_prompt}"; then
  806. progress "OK, continuing."
  807. else
  808. fatal "Cancelling reinstallation at user request." F0105
  809. fi
  810. fi
  811. ;;
  812. esac
  813. return 0
  814. elif [ "${INSTALL_TYPE}" = "unknown" ]; then
  815. claimonly_notice="If you just want to claim this install, you should re-run this command with the --claim-only option instead."
  816. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  817. failmsg="Attempting to update an installation managed by the system package manager is known to not work in most cases. If you are trying to install the latest version of Netdata, you will need to manually uninstall it through your system package manager. ${claimonly_notice}"
  818. promptmsg="Attempting to update an installation managed by the system package manager is known to not work in most cases. If you are trying to install the latest version of Netdata, you will need to manually uninstall it through your system package manager. ${claimonly_notice} Are you sure you want to continue?"
  819. else
  820. failmsg="We do not support trying to update or claim installations when we cannot determine the install type. You will need to uninstall the existing install using the same method you used to install it to proceed. ${claimonly_notice}"
  821. promptmsg="Attempting to update an existing install with an unknown installation type is not officially supported. It may work, but it also might break your system. ${claimonly_notice} Are you sure you want to continue?"
  822. fi
  823. if [ "${INTERACTIVE}" -eq 0 ] && [ "${ACTION}" != "claim" ]; then
  824. fatal "${failmsg}" F0106
  825. elif [ "${INTERACTIVE}" -eq 1 ] && [ "${ACTION}" != "claim" ]; then
  826. if confirm "${promptmsg}"; then
  827. progress "OK, continuing"
  828. else
  829. fatal "Cancelling update of unknown installation type at user request." F050C
  830. fi
  831. fi
  832. fi
  833. ret=0
  834. if [ "${ACTION}" != "claim" ]; then
  835. if ! update; then
  836. warning "Failed to update existing Netdata install at ${ndprefix}."
  837. else
  838. progress "Successfully updated existing netdata install at ${ndprefix}."
  839. fi
  840. else
  841. warning "Not updating existing install at ${ndprefix}."
  842. fi
  843. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  844. progress "Attempting to claim existing install at ${ndprefix}."
  845. INSTALL_PREFIX="${ndprefix}"
  846. claim
  847. ret=$?
  848. elif [ "${ACTION}" = "claim" ]; then
  849. fatal "User asked to claim, but did not provide a claiming token." F0202
  850. else
  851. progress "Not attempting to claim existing install at ${ndprefix} (no claiming token provided)."
  852. fi
  853. deferred_warnings
  854. success_banner
  855. cleanup
  856. trap - EXIT
  857. exit $ret
  858. ;;
  859. oci)
  860. fatal "This is an OCI container, use the regular container lifecycle management commands for your container tools instead of this script for managing it." F0203
  861. ;;
  862. *)
  863. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  864. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  865. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  866. elif [ "${INTERACTIVE}" -eq 0 ]; then
  867. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  868. else
  869. if confirm "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"; then
  870. progress "OK, continuing."
  871. else
  872. fatal "Cancelling reinstallation at user request." F0105
  873. fi
  874. fi
  875. else
  876. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  877. progress "Attempting to claim existing install at ${ndprefix}."
  878. INSTALL_PREFIX="${ndprefix}"
  879. claim
  880. ret=$?
  881. cleanup
  882. trap - EXIT
  883. exit $ret
  884. elif [ "${ACTION}" = "claim" ]; then
  885. fatal "User asked to claim, but did not provide a claiming token." F0202
  886. else
  887. fatal "Found an existing netdata install at ${ndprefix}, but the install type is '${INSTALL_TYPE}', which is not supported by this script, refusing to proceed." F0103
  888. fi
  889. fi
  890. ;;
  891. esac
  892. }
  893. soft_disable_cloud() {
  894. set_tmpdir
  895. cloud_prefix="${INSTALL_PREFIX}/var/lib/netdata/cloud.d"
  896. run_as_root mkdir -p "${cloud_prefix}"
  897. cat > "${tmpdir}/cloud.conf" << EOF
  898. [global]
  899. enabled = no
  900. EOF
  901. run_as_root cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
  902. if [ -z "${NETDATA_NO_START}" ]; then
  903. case "${SYSTYPE}" in
  904. Darwin) run_as_root launchctl kickstart -k com.github.netdata ;;
  905. FreeBSD) run_as_root service netdata restart ;;
  906. Linux)
  907. initpath="$(run_as_root readlink /proc/1/exe)"
  908. if command -v service > /dev/null 2>&1; then
  909. run_as_root service netdata restart
  910. elif command -v rc-service > /dev/null 2>&1; then
  911. run_as_root rc-service netdata restart
  912. elif [ "$(basename "${initpath}" 2> /dev/null)" = "systemd" ]; then
  913. run_as_root systemctl restart netdata
  914. elif [ -f /etc/init.d/netdata ]; then
  915. run_as_root /etc/init.d/netdata restart
  916. fi
  917. ;;
  918. esac
  919. fi
  920. }
  921. confirm_install_prefix() {
  922. if [ -n "${INSTALL_PREFIX}" ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ]; then
  923. fatal "The --install-prefix option is only supported together with the --build-only option." F0204
  924. fi
  925. if [ -n "${INSTALL_PREFIX}" ]; then
  926. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-prefix ${INSTALL_PREFIX}"
  927. else
  928. case "${SYSTYPE}" in
  929. Darwin)
  930. INSTALL_PREFIX="/usr/local/netdata"
  931. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  932. ;;
  933. FreeBSD)
  934. INSTALL_PREFIX="/usr/local"
  935. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  936. ;;
  937. esac
  938. fi
  939. }
  940. # ======================================================================
  941. # Claiming support code
  942. check_claim_opts() {
  943. # shellcheck disable=SC2235,SC2030
  944. if [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_ROOMS}" ]; then
  945. fatal "Invalid claiming options, claim rooms may only be specified when a token is specified." F0204
  946. elif [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_EXTRA}" ]; then
  947. fatal "Invalid claiming options, a claiming token must be specified." F0204
  948. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ] && [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  949. fatal "Cloud explicitly disabled, but automatic claiming requested. Either enable Netdata Cloud, or remove the --claim-* options." F0204
  950. fi
  951. }
  952. is_netdata_running() {
  953. if command -v pgrep > /dev/null 2>&1; then
  954. if pgrep netdata; then
  955. return 0
  956. else
  957. return 1
  958. fi
  959. else
  960. if [ -z "${INSTALL_PREFIX}" ]; then
  961. NETDATACLI_PATH=/usr/sbin/netdatacli
  962. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  963. NETDATACLI_PATH="/opt/netdata/bin/netdatacli"
  964. else
  965. NETDATACLI_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdatacli"
  966. fi
  967. if "${NETDATACLI_PATH}" ping > /dev/null 2>&1; then
  968. return 0
  969. else
  970. return 1
  971. fi
  972. fi
  973. }
  974. claim() {
  975. if [ "${DRY_RUN}" -eq 1 ]; then
  976. progress "Would attempt to claim agent to ${NETDATA_CLAIM_URL}"
  977. else
  978. progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
  979. fi
  980. if command -v netdata-claim.sh > /dev/null 2>&1; then
  981. NETDATA_CLAIM_PATH="$(command -v netdata-claim.sh)"
  982. elif [ -z "${INSTALL_PREFIX}" ] || [ "${INSTALL_PREFIX}" = "/" ]; then
  983. NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
  984. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  985. NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
  986. elif [ ! -d "${INSTALL_PREFIX}/netdata" ]; then
  987. if [ -d "${INSTALL_PREFIX}/usr" ]; then
  988. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/usr/sbin/netdata-claim.sh"
  989. else
  990. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/sbin/netdata-claim.sh"
  991. fi
  992. else
  993. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
  994. fi
  995. err_msg=
  996. err_code=
  997. if [ -z "${NETDATA_CLAIM_PATH}" ]; then
  998. err_msg="Unable to claim node: could not find usable claiming script. Reinstalling Netdata may resolve this."
  999. err_code=F050B
  1000. elif [ ! -e "${NETDATA_CLAIM_PATH}" ]; then
  1001. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} does not exist."
  1002. err_code=F0512
  1003. elif [ ! -f "${NETDATA_CLAIM_PATH}" ]; then
  1004. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} is not a file."
  1005. err_code=F0513
  1006. elif [ ! -x "${NETDATA_CLAIM_PATH}" ]; then
  1007. err_msg="Unable to claim node: claiming script at ${NETDATA_CLAIM_PATH} is not executable. Reinstalling Netdata may resolve this."
  1008. err_code=F0514
  1009. fi
  1010. if [ -n "$err_msg" ]; then
  1011. if [ "${ACTION}" = "claim" ]; then
  1012. fatal "$err_msg" "$err_code"
  1013. else
  1014. warning "$err_msg"
  1015. return 1
  1016. fi
  1017. fi
  1018. if ! is_netdata_running; then
  1019. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
  1020. fi
  1021. # shellcheck disable=SC2086
  1022. run_as_root "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
  1023. case $? in
  1024. 0)
  1025. progress "Successfully claimed node"
  1026. return 0
  1027. ;;
  1028. 1) warning "Unable to claim node due to invalid claiming options. If you are seeing this message, you’ve probably found a bug and should open a bug report at ${AGENT_BUG_REPORT_URL}" ;;
  1029. 2) warning "Unable to claim node due to issues creating the claiming directory or preparing the local claiming key. Make sure you have a working openssl command and that ${INSTALL_PREFIX}/var/lib/netdata/cloud.d exists, then try again." ;;
  1030. 3) warning "Unable to claim node due to missing dependencies. Usually this means that the Netdata Agent was built without support for Netdata Cloud. If you built the agent from source, please install all needed dependencies for Cloud support. If you used the regular installation script and see this error, please file a bug report at ${AGENT_BUG_REPORT_URL}." ;;
  1031. 4) warning "Failed to claim node due to inability to connect to ${NETDATA_CLAIM_URL}. Usually this either means that the specified claiming URL is wrong, or that you are having networking problems." ;;
  1032. 5)
  1033. progress "Successfully claimed node, but was not able to notify the Netdata Agent. You will need to restart the Netdata service on this node before it will show up in the Cloud."
  1034. return 0
  1035. ;;
  1036. 8) warning "Failed to claim node due to an invalid agent ID. You can usually resolve this by removing ${INSTALL_PREFIX}/var/lib/netdata/registry/netdata.public.unique.id and restarting the agent. Then try to claim it again using the same options." ;;
  1037. 9) warning "Failed to claim node due to an invalid node name. This probably means you tried to specify a custom name for this node (for example, using the --claim-hostname option), but the hostname itself was either empty or consisted solely of whitespace. You can resolve this by specifying a valid host name and trying again." ;;
  1038. 10) warning "Failed to claim node due to an invalid room ID. This issue is most likely caused by a typo. Please check if the room(s) you are trying to add appear on the list of rooms provided to the --claim-rooms option ('${NETDATA_CLAIM_ROOMS}'). Then verify if the rooms are visible in Netdata Cloud and try again." ;;
  1039. 11) warning "Failed to claim node due to an issue with the generated RSA key pair. You can usually resolve this by removing all files in ${INSTALL_PREFIX}/var/lib/netdata/cloud.d and then trying again." ;;
  1040. 12) warning "Failed to claim node due to an invalid or expired claiming token. Please check that the token specified with the --claim-token option ('${NETDATA_CLAIM_TOKEN}') matches what you see in the Cloud and try again." ;;
  1041. 13) warning "Failed to claim node because the Cloud thinks it is already claimed. If this node was created by cloning a VM or as a container from a template, please remove the file ${INSTALL_PREFIX}/var/lib/netdata/registry/netdata.public.unique.id and restart the agent. Then try to claim it again with the same options. Otherwise, if you are certain this node has never been claimed before, you can use the --claim-id option to specify a new node ID to use for claiming, for example by using the uuidgen command like so: --claim-id \"\$(uuidgen)\"" ;;
  1042. 14) warning "Failed to claim node because the node is already in the process of being claimed. You should not need to do anything to resolve this, the node should show up properly in the Cloud soon. If it does not, please report a bug at ${AGENT_BUG_REPORT_URL}." ;;
  1043. 15|16|17) warning "Failed to claim node due to an internal server error in the Cloud. Please retry claiming this node later, and if you still see this message file a bug report at ${CLOUD_BUG_REPORT_URL}." ;;
  1044. 18) warning "Unable to claim node because this Netdata installation does not have a unique ID yet. Make sure the agent is running and started up correctly, and then try again." ;;
  1045. *) warning "Failed to claim node for an unknown reason. This usually means either networking problems or a bug. Please retry claiming later, and if you still see this message file a bug report at ${AGENT_BUG_REPORT_URL}" ;;
  1046. esac
  1047. if [ "${ACTION}" = "claim" ]; then
  1048. deferred_warnings
  1049. printf >&2 "%s\n" "For community support, you can connect with us on:"
  1050. support_list
  1051. cleanup
  1052. trap - EXIT
  1053. exit 1
  1054. fi
  1055. }
  1056. # ======================================================================
  1057. # Auto-update handling code.
  1058. set_auto_updates() {
  1059. if run_as_root test -x "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"; then
  1060. updater="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  1061. elif run_as_root test -x "${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"; then
  1062. updater="${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"
  1063. else
  1064. warning "Could not find netdata-updater.sh. This means that auto-updates cannot (currently) be enabled on this system. See https://learn.netdata.cloud/docs/agent/packaging/installer/update for more information about updating Netdata."
  1065. return 0
  1066. fi
  1067. if [ "${AUTO_UPDATE}" -eq 1 ]; then
  1068. if [ "${DRY_RUN}" -eq 1 ]; then
  1069. progress "Would have attempted to enable automatic updates."
  1070. # This first case is for catching using a new kickstart script with an old build. It can be safely removed after v1.34.0 is released.
  1071. elif ! run_as_root grep -q '\-\-enable-auto-updates' "${updater}"; then
  1072. echo
  1073. elif ! run_as_root "${updater}" --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
  1074. warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
  1075. fi
  1076. else
  1077. if [ "${DRY_RUN}" -eq 1 ]; then
  1078. progress "Would have attempted to disable automatic updates."
  1079. else
  1080. run_as_root "${updater}" --disable-auto-updates
  1081. fi
  1082. fi
  1083. }
  1084. # ======================================================================
  1085. # Native package install code.
  1086. # Check for an already installed package with a given name.
  1087. pkg_installed() {
  1088. case "${SYSTYPE}" in
  1089. Linux)
  1090. case "${DISTRO_COMPAT_NAME}" in
  1091. debian|ubuntu)
  1092. # shellcheck disable=SC2016
  1093. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  1094. return $?
  1095. ;;
  1096. centos|fedora|opensuse|ol|amzn)
  1097. rpm -q "${1}" > /dev/null 2>&1
  1098. return $?
  1099. ;;
  1100. alpine)
  1101. apk -e info "${1}" > /dev/null 2>&1
  1102. return $?
  1103. ;;
  1104. arch)
  1105. pacman -Qi "${1}" > /dev/null 2>&1
  1106. return $?
  1107. ;;
  1108. *) return 1 ;;
  1109. esac
  1110. ;;
  1111. Darwin)
  1112. if command -v brew > /dev/null 2>&1; then
  1113. brew list "${1}" > /dev/null 2>&1
  1114. return $?
  1115. else
  1116. return 1
  1117. fi
  1118. ;;
  1119. FreeBSD)
  1120. if pkg -N > /dev/null 2>&1; then
  1121. pkg info "${1}" > /dev/null 2>&1
  1122. return $?
  1123. else
  1124. return 1
  1125. fi
  1126. ;;
  1127. *) return 1 ;;
  1128. esac
  1129. }
  1130. # Check for the existence of a usable netdata package in the repo.
  1131. netdata_avail_check() {
  1132. case "${DISTRO_COMPAT_NAME}" in
  1133. debian|ubuntu)
  1134. env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q repo.netdata.cloud/repos/;
  1135. return $?
  1136. ;;
  1137. centos|fedora|ol|amzn)
  1138. # shellcheck disable=SC2086
  1139. ${pm_cmd} search --nogpgcheck -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
  1140. return $?
  1141. ;;
  1142. opensuse)
  1143. zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
  1144. return $?
  1145. ;;
  1146. *) return 1 ;;
  1147. esac
  1148. }
  1149. # Check for any distro-specific dependencies we know we need.
  1150. check_special_native_deps() {
  1151. if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
  1152. progress "Checking for libuv availability."
  1153. if ${pm_cmd} search --nogpgcheck -v libuv | grep -q "No matches found"; then
  1154. progress "libuv not found, checking for EPEL availability."
  1155. if ${pm_cmd} search --nogpgcheck -v epel-release | grep -q "No matches found"; then
  1156. warning "Unable to find a suitable source for libuv, cannot install using native packages on this system."
  1157. return 1
  1158. else
  1159. progress "EPEL is available, attempting to install so that required dependencies are available."
  1160. # shellcheck disable=SC2086
  1161. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
  1162. warning "Failed to install EPEL, even though it is required to install native packages on this system."
  1163. return 1
  1164. fi
  1165. fi
  1166. else
  1167. return 0
  1168. fi
  1169. fi
  1170. }
  1171. common_rpm_opts() {
  1172. pkg_type="rpm"
  1173. pkg_suffix=".noarch"
  1174. pkg_vsep="-"
  1175. INSTALL_TYPE="binpkg-rpm"
  1176. NATIVE_VERSION="${INSTALL_VERSION:+"-${INSTALL_VERSION}.${SYSARCH}"}"
  1177. }
  1178. common_dnf_opts() {
  1179. if command -v dnf > /dev/null; then
  1180. pm_cmd="dnf"
  1181. repo_subcmd="makecache"
  1182. else
  1183. pm_cmd="yum"
  1184. fi
  1185. pkg_install_opts="${interactive_opts}"
  1186. repo_update_opts="${interactive_opts}"
  1187. uninstall_subcmd="remove"
  1188. }
  1189. try_package_install() {
  1190. failed_refresh_msg="Failed to refresh repository metadata. ${BADNET_MSG} or incompatibilities with one or more third-party package repositories in the system package manager configuration."
  1191. if [ -z "${DISTRO_COMPAT_NAME}" ] || [ "${DISTRO_COMPAT_NAME}" = "unknown" ]; then
  1192. warning "Unable to determine Linux distribution for native packages."
  1193. return 2
  1194. elif [ -z "${SYSCODENAME}" ]; then
  1195. case "${DISTRO_COMPAT_NAME}" in
  1196. debian|ubuntu)
  1197. warning "Release codename not set. Unable to check availability of native packages for this system."
  1198. return 2
  1199. ;;
  1200. esac
  1201. fi
  1202. set_tmpdir
  1203. if [ "${DRY_RUN}" -eq 1 ]; then
  1204. progress "Would attempt to install using native packages..."
  1205. else
  1206. progress "Attempting to install using native packages..."
  1207. fi
  1208. if [ "${SELECTED_RELEASE_CHANNEL}" = "nightly" ]; then
  1209. release="-edge"
  1210. else
  1211. release=""
  1212. fi
  1213. if [ "${INTERACTIVE}" = "0" ]; then
  1214. interactive_opts="-y"
  1215. env="DEBIAN_FRONTEND=noninteractive"
  1216. else
  1217. interactive_opts=""
  1218. env=""
  1219. fi
  1220. case "${DISTRO_COMPAT_NAME}" in
  1221. debian|ubuntu)
  1222. needs_early_refresh=1
  1223. pm_cmd="apt-get"
  1224. repo_subcmd="update"
  1225. pkg_type="deb"
  1226. pkg_vsep="_"
  1227. pkg_install_opts="${interactive_opts}"
  1228. repo_update_opts="${interactive_opts}"
  1229. uninstall_subcmd="purge"
  1230. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSCODENAME}"
  1231. pkg_suffix="+${DISTRO_COMPAT_NAME}${SYSVERSION}_all"
  1232. INSTALL_TYPE="binpkg-deb"
  1233. NATIVE_VERSION="${INSTALL_VERSION:+"=${INSTALL_VERSION}"}"
  1234. ;;
  1235. centos)
  1236. common_rpm_opts
  1237. common_dnf_opts
  1238. repo_prefix="el/${SYSVERSION}"
  1239. # if [ "${SYSVERSION}" -lt 8 ]; then
  1240. # explicitly_install_native_plugins=1
  1241. # fi
  1242. ;;
  1243. fedora|ol)
  1244. common_rpm_opts
  1245. common_dnf_opts
  1246. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSVERSION}"
  1247. ;;
  1248. opensuse)
  1249. common_rpm_opts
  1250. pm_cmd="zypper"
  1251. repo_subcmd="--gpg-auto-import-keys refresh"
  1252. repo_prefix="opensuse/${SYSVERSION}"
  1253. pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
  1254. repo_update_opts=""
  1255. uninstall_subcmd="remove"
  1256. ;;
  1257. amzn)
  1258. common_rpm_opts
  1259. common_dnf_opts
  1260. repo_prefix="amazonlinux/${SYSVERSION}"
  1261. ;;
  1262. *)
  1263. warning "We do not provide native packages for ${DISTRO}."
  1264. return 2
  1265. ;;
  1266. esac
  1267. if [ -n "${SKIP_DISTRO_DETECTION}" ]; then
  1268. warning "Attempting to use native packages with a distro override. This is not officially supported, but may work in some cases. If your system requires a distro override to use native packages, please open an feature request at ${AGENT_BUG_REPORT_URL} about it so that we can update the installer to auto-detect this."
  1269. fi
  1270. if [ -n "${INSTALL_VERSION}" ]; then
  1271. if echo "${INSTALL_VERSION}" | grep -q "nightly"; then
  1272. new_release="-edge"
  1273. else
  1274. new_release=
  1275. fi
  1276. if { [ -n "${new_release}" ] && [ -z "${release}" ]; } || { [ -z "${new_release}" ] && [ -n "${release}" ]; }; then
  1277. warning "Selected release channel does not match this version and it will be changed automatically."
  1278. fi
  1279. release="${new_release}"
  1280. fi
  1281. repoconfig_name="netdata-repo${release}"
  1282. case "${pkg_type}" in
  1283. deb)
  1284. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_DEB_VERSION}${pkg_suffix}.${pkg_type}"
  1285. repoconfig_url="${REPOCONFIG_DEB_URL_PREFIX}/${repo_prefix}/${repoconfig_file}"
  1286. ;;
  1287. rpm)
  1288. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_RPM_VERSION}${pkg_suffix}.${pkg_type}"
  1289. repoconfig_url="${REPOCONFIG_RPM_URL_PREFIX}/${repo_prefix}/${SYSARCH}/${repoconfig_file}"
  1290. ;;
  1291. esac
  1292. if ! pkg_installed "${repoconfig_name}"; then
  1293. progress "Checking for availability of repository configuration package."
  1294. if ! check_for_remote_file "${repoconfig_url}"; then
  1295. warning "No repository configuration package available for ${DISTRO} ${SYSVERSION}. Cannot install native packages on this system."
  1296. return 2
  1297. fi
  1298. if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
  1299. fatal "Failed to download repository configuration package. ${BADNET_MSG}." F0209
  1300. fi
  1301. if [ -n "${needs_early_refresh}" ]; then
  1302. # shellcheck disable=SC2086
  1303. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1304. warning "${failed_refresh_msg}"
  1305. return 2
  1306. fi
  1307. fi
  1308. # shellcheck disable=SC2086
  1309. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
  1310. warning "Failed to install repository configuration package."
  1311. return 2
  1312. fi
  1313. if [ -n "${repo_subcmd}" ]; then
  1314. # shellcheck disable=SC2086
  1315. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1316. fatal "${failed_refresh_msg} In most cases, disabling any third-party repositories on the system and re-running the installer with the same options should work. If that does not work, consider using a static build with the --static-only option instead of native packages." F0205
  1317. fi
  1318. fi
  1319. else
  1320. progress "Repository configuration is already present, attempting to install netdata."
  1321. fi
  1322. if [ "${ACTION}" = "repositories-only" ]; then
  1323. progress "Successfully installed repository configuraion package."
  1324. deferred_warnings
  1325. cleanup
  1326. trap - EXIT
  1327. exit 1
  1328. fi
  1329. if ! check_special_native_deps; then
  1330. warning "Could not find secondary dependencies for ${DISTRO} on ${SYSARCH}."
  1331. if [ -z "${NO_CLEANUP}" ]; then
  1332. progress "Attempting to uninstall repository configuration package."
  1333. # shellcheck disable=SC2086
  1334. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1335. fi
  1336. return 2
  1337. fi
  1338. if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
  1339. warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
  1340. if [ -z "${NO_CLEANUP}" ]; then
  1341. progress "Attempting to uninstall repository configuration package."
  1342. # shellcheck disable=SC2086
  1343. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1344. fi
  1345. return 2
  1346. fi
  1347. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
  1348. run_as_root mkdir -p "/etc/netdata"
  1349. run_as_root touch "/etc/netdata/.opt-out-from-anonymous-statistics"
  1350. fi
  1351. # shellcheck disable=SC2086
  1352. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
  1353. warning "Failed to install Netdata package."
  1354. if [ -z "${NO_CLEANUP}" ]; then
  1355. progress "Attempting to uninstall repository configuration package."
  1356. # shellcheck disable=SC2086
  1357. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1358. fi
  1359. return 2
  1360. fi
  1361. if [ -n "${explicitly_install_native_plugins}" ]; then
  1362. progress "Installing external plugins."
  1363. # shellcheck disable=SC2086
  1364. if ! run_as_root env ${env} ${pm_cmd} install ${DEFAULT_PLUGIN_PACKAGES}; then
  1365. warning "Failed to install external plugin packages. Some collectors may not be available."
  1366. fi
  1367. fi
  1368. }
  1369. # ======================================================================
  1370. # Static build install code
  1371. # shellcheck disable=SC2034,SC2086,SC2126
  1372. set_static_archive_urls() {
  1373. if [ -z "${2}" ]; then
  1374. arch="${SYSARCH}"
  1375. else
  1376. arch="${2}"
  1377. fi
  1378. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1379. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  1380. export NETDATA_STATIC_ARCHIVE_URL="file://${path}/netdata-${arch}-latest.gz.run"
  1381. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1382. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="file://${path}/sha256sums.txt"
  1383. elif [ "${1}" = "stable" ]; then
  1384. if [ -n "${INSTALL_VERSION}" ]; then
  1385. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1386. export NETDATA_STATIC_ARCHIVE_OLD_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1387. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1388. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1389. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1390. else
  1391. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1392. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${arch}-latest.gz.run"
  1393. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1394. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1395. fi
  1396. else
  1397. if [ -n "${INSTALL_VERSION}" ]; then
  1398. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1399. export NETDATA_STATIC_ARCHIVE_OLD_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1400. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1401. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1402. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1403. else
  1404. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1405. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-${arch}-latest.gz.run"
  1406. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1407. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1408. fi
  1409. fi
  1410. }
  1411. try_static_install() {
  1412. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1413. if [ "${DRY_RUN}" -eq 1 ]; then
  1414. progress "Would attempt to install using static build..."
  1415. else
  1416. progress "Attempting to install using static build..."
  1417. fi
  1418. # Check status code first, so that we can provide nicer fallback for dry runs.
  1419. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1420. netdata_agent="${NETDATA_STATIC_ARCHIVE_NAME}"
  1421. elif [ "${SYSARCH}" = "x86_64" ] && check_for_remote_file "${NETDATA_STATIC_ARCHIVE_OLD_URL}"; then
  1422. netdata_agent="${NETDATA_STATIC_ARCHIVE_OLD_NAME}"
  1423. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1424. else
  1425. warning "There is no static build available for ${SYSARCH} CPUs. This usually means we simply do not currently provide static builds for ${SYSARCH} CPUs."
  1426. return 2
  1427. fi
  1428. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/${netdata_agent}"; then
  1429. fatal "Unable to download static build archive for ${SYSARCH}. ${BADNET_MSG}." F0208
  1430. fi
  1431. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1432. fatal "Unable to fetch checksums to verify static build archive. ${BADNET_MSG}." F0206
  1433. fi
  1434. if [ "${DRY_RUN}" -eq 1 ]; then
  1435. progress "Would validate SHA256 checksum of downloaded static build archive."
  1436. else
  1437. if [ -z "${INSTALL_VERSION}" ]; then
  1438. if ! grep "${netdata_agent}" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1439. fatal "Static binary checksum validation failed. ${BADCACHE_MSG}." F0207
  1440. fi
  1441. fi
  1442. fi
  1443. if [ "${INTERACTIVE}" -eq 0 ]; then
  1444. opts="${opts} --accept"
  1445. fi
  1446. progress "Installing netdata"
  1447. # shellcheck disable=SC2086
  1448. if ! run_as_root sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
  1449. warning "Failed to install static build of Netdata on ${SYSARCH}."
  1450. run rm -rf /opt/netdata
  1451. return 2
  1452. fi
  1453. if [ "${DRY_RUN}" -ne 1 ]; then
  1454. install_type_file="/opt/netdata/etc/netdata/.install-type"
  1455. if [ -f "${install_type_file}" ]; then
  1456. run_as_root sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
  1457. run_as_root chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
  1458. # shellcheck disable=SC1090,SC1091
  1459. . "${tmpdir}/install-type"
  1460. cat > "${tmpdir}/install-type" <<- EOF
  1461. INSTALL_TYPE='kickstart-static'
  1462. PREBUILT_ARCH='${PREBUILT_ARCH}'
  1463. EOF
  1464. run_as_root chown netdata:netdata "${tmpdir}/install-type"
  1465. run_as_root cp "${tmpdir}/install-type" "${install_type_file}"
  1466. fi
  1467. fi
  1468. }
  1469. # ======================================================================
  1470. # Local build install code
  1471. set_source_archive_urls() {
  1472. if [ "$1" = "stable" ]; then
  1473. if [ -n "${INSTALL_VERSION}" ]; then
  1474. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.tar.gz"
  1475. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1476. else
  1477. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1478. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${latest}.tar.gz"
  1479. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1480. fi
  1481. else
  1482. if [ -n "${INSTALL_VERSION}" ]; then
  1483. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-latest.tar.gz"
  1484. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1485. else
  1486. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1487. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-latest.tar.gz"
  1488. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1489. fi
  1490. fi
  1491. }
  1492. install_local_build_dependencies() {
  1493. set_tmpdir
  1494. bash="$(command -v bash 2> /dev/null)"
  1495. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  1496. warning "Unable to find a usable version of \`bash\` (required for local build)."
  1497. return 1
  1498. fi
  1499. if ! download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"; then
  1500. fatal "Failed to download dependency handling script for local build. ${BADNET_MSG}." F000D
  1501. fi
  1502. if [ "${DRY_RUN}" -eq 1 ]; then
  1503. progress "Would run downloaded script to install required build dependencies..."
  1504. else
  1505. progress "Running downloaded script to install required build dependencies..."
  1506. fi
  1507. if [ "${INTERACTIVE}" -eq 0 ]; then
  1508. opts="--dont-wait --non-interactive"
  1509. fi
  1510. # shellcheck disable=SC2086
  1511. if ! run_as_root "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
  1512. warning "Failed to install all required packages, but installation might still be possible."
  1513. fi
  1514. }
  1515. build_and_install() {
  1516. if [ "${DRY_RUN}" -eq 1 ]; then
  1517. progress "Would attempt to build netdata..."
  1518. else
  1519. progress "Building netdata..."
  1520. fi
  1521. echo "INSTALL_TYPE='kickstart-build'" > system/.install-type
  1522. opts="${NETDATA_INSTALLER_OPTIONS}"
  1523. if [ "${INTERACTIVE}" -eq 0 ]; then
  1524. opts="${opts} --dont-wait"
  1525. fi
  1526. if [ "${SELECTED_RELEASE_CHANNEL}" = "stable" ]; then
  1527. opts="${opts} --stable-channel"
  1528. fi
  1529. if [ "${NETDATA_REQUIRE_CLOUD}" -eq 1 ]; then
  1530. opts="${opts} --require-cloud"
  1531. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  1532. opts="${opts} --disable-cloud"
  1533. fi
  1534. # shellcheck disable=SC2086
  1535. run_script ./netdata-installer.sh ${opts}
  1536. case $? in
  1537. 1)
  1538. if [ -n "${EXIT_REASON}" ]; then
  1539. fatal "netdata-installer.sh failed to run: ${EXIT_REASON}" "${EXIT_CODE}"
  1540. else
  1541. fatal "netdata-installer.sh failed to run: Encountered an unhandled error in the installer code." I0000
  1542. fi
  1543. ;;
  1544. 2) fatal "Insufficient RAM to install netdata." F0008 ;;
  1545. esac
  1546. }
  1547. try_build_install() {
  1548. set_tmpdir
  1549. if [ "${DRY_RUN}" -eq 1 ]; then
  1550. progress "Would attempt to install by building locally..."
  1551. else
  1552. progress "Attempting to install by building locally..."
  1553. fi
  1554. if ! install_local_build_dependencies; then
  1555. return 1
  1556. fi
  1557. set_source_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1558. if [ -n "${INSTALL_VERSION}" ]; then
  1559. if ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz"; then
  1560. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1561. fi
  1562. elif ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-latest.tar.gz"; then
  1563. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1564. fi
  1565. if ! download "${NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1566. fatal "Failed to download checksums for source tarball verification. ${BADNET_MSG}." F000C
  1567. fi
  1568. if [ "${DRY_RUN}" -eq 1 ]; then
  1569. progress "Would validate SHA256 checksum of downloaded source archive."
  1570. else
  1571. if [ -z "${INSTALL_VERSION}" ]; then
  1572. # shellcheck disable=SC2086
  1573. if ! grep netdata-latest.tar.gz "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1574. fatal "Tarball checksum validation failed. ${BADCACHE_MSG}." F0005
  1575. fi
  1576. fi
  1577. fi
  1578. if [ -n "${INSTALL_VERSION}" ]; then
  1579. run tar -xf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" -C "${tmpdir}"
  1580. rm -rf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" > /dev/null 2>&1
  1581. else
  1582. run tar -xf "${tmpdir}/netdata-latest.tar.gz" -C "${tmpdir}"
  1583. rm -rf "${tmpdir}/netdata-latest.tar.gz" > /dev/null 2>&1
  1584. fi
  1585. if [ "${DRY_RUN}" -ne 1 ]; then
  1586. cd "$(find "${tmpdir}" -mindepth 1 -maxdepth 1 -type d -name netdata-)" || fatal "Cannot change directory to netdata source tree" F0006
  1587. fi
  1588. if [ -x netdata-installer.sh ] || [ "${DRY_RUN}" -eq 1 ]; then
  1589. build_and_install || return 1
  1590. else
  1591. # This case is needed because some platforms produce an extra directory on the source tarball extraction.
  1592. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  1593. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" && build_and_install || return 1
  1594. else
  1595. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)." F0009
  1596. fi
  1597. fi
  1598. }
  1599. # ======================================================================
  1600. # Offline install support code
  1601. prepare_offline_install_source() {
  1602. if [ -e "${1}" ]; then
  1603. if [ ! -d "${1}" ]; then
  1604. fatal "${1} is not a directory, unable to prepare offline install source." F0503
  1605. fi
  1606. else
  1607. run mkdir -p "${1}" || fatal "Unable to create target directory for offline install preparation." F0504
  1608. fi
  1609. run cd "${1}" || fatal "Failed to switch to target directory for offline install preparation." F0505
  1610. case "${NETDATA_FORCE_METHOD}" in
  1611. static|'')
  1612. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "x86_64"
  1613. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1614. for arch in ${STATIC_INSTALL_ARCHES}; do
  1615. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "${arch}"
  1616. progress "Fetching ${NETDATA_STATIC_ARCHIVE_URL}"
  1617. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "netdata-${arch}-latest.gz.run"; then
  1618. warning "Failed to download static installer archive for ${arch}. ${BADNET_MSG}."
  1619. fi
  1620. done
  1621. legacy=0
  1622. else
  1623. warning "Selected version of Netdata only provides static builds for x86_64. You will only be able to install on x86_64 systems with this offline install source."
  1624. progress "Fetching ${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1625. legacy=1
  1626. if ! download "${NETDATA_STATIC_ARCHIVE_OLD_URL}" "netdata-x86_64-latest.gz.run"; then
  1627. warning "Failed to download static installer archive for x86_64. ${BADNET_MSG}."
  1628. fi
  1629. fi
  1630. progress "Fetching ${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}"
  1631. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "sha256sums.txt"; then
  1632. fatal "Failed to download checksum file. ${BADNET_MSG}." F0506
  1633. fi
  1634. ;;
  1635. esac
  1636. if [ "${legacy:-0}" -eq 1 ]; then
  1637. sed -e 's/netdata-latest.gz.run/netdata-x86_64-latest.gz.run' sha256sums.txt > sha256sums.tmp
  1638. mv sha256sums.tmp sha256sums.txt
  1639. fi
  1640. if [ "${DRY_RUN}" -ne 1 ]; then
  1641. progress "Verifying checksums."
  1642. if ! grep -e "$(find . -name '*.gz.run')" sha256sums.txt | safe_sha256sum -c -; then
  1643. fatal "Checksums for offline install files are incorrect. ${BADCACHE_MSG}." F0507
  1644. fi
  1645. else
  1646. progress "Would verify SHA256 checksums of downloaded installation files."
  1647. fi
  1648. if [ "${DRY_RUN}" -ne 1 ]; then
  1649. progress "Preparing install script."
  1650. cat > "install.sh" <<-EOF
  1651. #!/bin/sh
  1652. dir=\$(CDPATH= cd -- "\$(dirname -- "\$0")" && pwd)
  1653. "\${dir}/kickstart.sh" --offline-install-source "\${dir}" \${@}
  1654. EOF
  1655. chmod +x "install.sh"
  1656. else
  1657. progress "Would create install script"
  1658. fi
  1659. if [ "${DRY_RUN}" -ne 1 ]; then
  1660. progress "Copying kickstart script."
  1661. cp "${KICKSTART_SOURCE}" "kickstart.sh"
  1662. chmod +x "kickstart.sh"
  1663. else
  1664. progress "Would copy kickstart.sh to offline install source directory"
  1665. fi
  1666. if [ "${DRY_RUN}" -ne 1 ]; then
  1667. progress "Saving release channel information."
  1668. echo "${SELECTED_RELEASE_CHANNEL}" > "channel"
  1669. else
  1670. progress "Would save release channel information to offline install source directory"
  1671. fi
  1672. progress "Finished preparing offline install source directory at ${1}. You can now copy this directory to a target system and then run the script ‘install.sh’ from it to install on that system."
  1673. }
  1674. # ======================================================================
  1675. # Per system-type install logic
  1676. install_on_linux() {
  1677. if [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1678. SELECTED_INSTALL_METHOD="native"
  1679. try_package_install
  1680. case "$?" in
  1681. 0)
  1682. NETDATA_INSTALL_SUCCESSFUL=1
  1683. INSTALL_PREFIX="/"
  1684. ;;
  1685. 1) fatal "Unable to install on this system." F0300 ;;
  1686. 2)
  1687. case "${NETDATA_FORCE_METHOD}" in
  1688. native) fatal "Could not install native binary packages." F0301 ;;
  1689. *) warning "Could not install native binary packages, falling back to alternative installation method." ;;
  1690. esac
  1691. ;;
  1692. esac
  1693. fi
  1694. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1695. SELECTED_INSTALL_METHOD="static"
  1696. INSTALL_TYPE="kickstart-static"
  1697. try_static_install
  1698. case "$?" in
  1699. 0)
  1700. NETDATA_INSTALL_SUCCESSFUL=1
  1701. INSTALL_PREFIX="/opt/netdata"
  1702. ;;
  1703. 1) fatal "Unable to install on this system." F0302 ;;
  1704. 2)
  1705. case "${NETDATA_FORCE_METHOD}" in
  1706. static) fatal "Could not install static build." F0303 ;;
  1707. *) warning "Could not install static build, falling back to alternative installation method." ;;
  1708. esac
  1709. ;;
  1710. esac
  1711. fi
  1712. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1713. SELECTED_INSTALL_METHOD="build"
  1714. INSTALL_TYPE="kickstart-build"
  1715. try_build_install
  1716. case "$?" in
  1717. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1718. *) fatal "Unable to install on this system." F0304 ;;
  1719. esac
  1720. fi
  1721. }
  1722. install_on_macos() {
  1723. case "${NETDATA_FORCE_METHOD}" in
  1724. native) fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option." F0305 ;;
  1725. static) fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option." F0306 ;;
  1726. *)
  1727. SELECTED_INSTALL_METHOD="build"
  1728. INSTALL_TYPE="kickstart-build"
  1729. try_build_install
  1730. case "$?" in
  1731. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1732. *) fatal "Unable to install on this system." F0307 ;;
  1733. esac
  1734. ;;
  1735. esac
  1736. }
  1737. install_on_freebsd() {
  1738. case "${NETDATA_FORCE_METHOD}" in
  1739. native) fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option." F0308 ;;
  1740. static) fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option." F0309 ;;
  1741. *)
  1742. SELECTED_INSTALL_METHOD="build"
  1743. INSTALL_TYPE="kickstart-build"
  1744. try_build_install
  1745. case "$?" in
  1746. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1747. *) fatal "Unable to install on this system." F030A ;;
  1748. esac
  1749. ;;
  1750. esac
  1751. }
  1752. # ======================================================================
  1753. # Argument parsing code
  1754. validate_args() {
  1755. check_claim_opts
  1756. if [ -n "${NETDATA_FORCE_METHOD}" ]; then
  1757. SELECTED_INSTALL_METHOD="${NETDATA_FORCE_METHOD}"
  1758. fi
  1759. if [ "${ACTION}" = "repositories-only" ] && [ "${NETDATA_FORCE_METHOD}" != "native" ]; then
  1760. fatal "Repositories can only be installed for native installs." F050D
  1761. fi
  1762. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1763. case "${NETDATA_FORCE_METHOD}" in
  1764. native|build) fatal "Offline installs are only supported for static builds currently." F0502 ;;
  1765. esac
  1766. fi
  1767. if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
  1768. case "${NETDATA_FORCE_METHOD}" in
  1769. build) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}" ;;
  1770. *) fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401 ;;
  1771. esac
  1772. fi
  1773. if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
  1774. case "${NETDATA_FORCE_METHOD}" in
  1775. static) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}" ;;
  1776. *) fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402 ;;
  1777. esac
  1778. fi
  1779. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
  1780. fatal "Specifying an install version alongside an offline install source is not supported." F050A
  1781. fi
  1782. if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
  1783. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
  1784. AUTO_UPDATE=0
  1785. else
  1786. AUTO_UPDATE=1
  1787. fi
  1788. elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
  1789. AUTO_UPDATE=1
  1790. else
  1791. AUTO_UPDATE=0
  1792. fi
  1793. if [ "${RELEASE_CHANNEL}" = "default" ]; then
  1794. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1795. SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
  1796. if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
  1797. fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
  1798. fi
  1799. else
  1800. SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
  1801. fi
  1802. else
  1803. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
  1804. fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
  1805. fi
  1806. SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
  1807. fi
  1808. }
  1809. set_action() {
  1810. new_action="${1}"
  1811. if [ -n "${ACTION}" ]; then
  1812. warning "Ignoring previously specified '${ACTION}' operation in favor of '${new_action}' specified later on the command line."
  1813. fi
  1814. ACTION="${new_action}"
  1815. NETDATA_COMMAND="${new_action}"
  1816. }
  1817. parse_args() {
  1818. while [ -n "${1}" ]; do
  1819. case "${1}" in
  1820. "--help")
  1821. usage
  1822. cleanup
  1823. trap - EXIT
  1824. exit 0
  1825. ;;
  1826. "--no-cleanup") NO_CLEANUP=1 ;;
  1827. "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
  1828. "--interactive") INTERACTIVE=1 ;;
  1829. "--dry-run") DRY_RUN=1 ;;
  1830. "--release-channel")
  1831. RELEASE_CHANNEL="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1832. case "${RELEASE_CHANNEL}" in
  1833. nightly|stable|default) shift 1 ;;
  1834. *)
  1835. echo "Unrecognized value for --release-channel. Valid release channels are: stable, nightly, default"
  1836. exit 1
  1837. ;;
  1838. esac
  1839. ;;
  1840. "--stable-channel") RELEASE_CHANNEL="stable" ;;
  1841. "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
  1842. "--reinstall") set_action 'reinstall' ;;
  1843. "--reinstall-even-if-unsafe") set_action 'unsafe-reinstall' ;;
  1844. "--reinstall-clean") set_action 'reinstall-clean' ;;
  1845. "--uninstall") set_action 'uninstall' ;;
  1846. "--claim-only") set_action 'claim' ;;
  1847. "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
  1848. "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
  1849. "--auto-update-method")
  1850. NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1851. case "${NETDATA_AUTO_UPDATE_TYPE}" in
  1852. systemd|interval|crontab) shift 1 ;;
  1853. *)
  1854. echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
  1855. exit 1
  1856. ;;
  1857. esac
  1858. ;;
  1859. "--disable-cloud")
  1860. NETDATA_DISABLE_CLOUD=1
  1861. NETDATA_REQUIRE_CLOUD=0
  1862. ;;
  1863. "--require-cloud")
  1864. NETDATA_DISABLE_CLOUD=0
  1865. NETDATA_REQUIRE_CLOUD=1
  1866. ;;
  1867. "--dont-start-it")
  1868. NETDATA_NO_START=1
  1869. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
  1870. ;;
  1871. "--disable-telemetry")
  1872. NETDATA_DISABLE_TELEMETRY="1"
  1873. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
  1874. ;;
  1875. "--install-prefix")
  1876. INSTALL_PREFIX="${2}"
  1877. shift 1
  1878. ;;
  1879. "--old-install-prefix")
  1880. OLD_INSTALL_PREFIX="${2}"
  1881. shift 1
  1882. ;;
  1883. "--install-version")
  1884. INSTALL_VERSION="${2}"
  1885. AUTO_UPDATE=0
  1886. shift 1
  1887. ;;
  1888. "--distro-override")
  1889. if [ -n "${2}" ]; then
  1890. SKIP_DISTRO_DETECTION=1
  1891. DISTRO="$(echo "${2}" | cut -f 1 -d ':' | tr '[:upper:]' '[:lower:]')"
  1892. SYSVERSION="$(echo "${2}" | cut -f 2 -d ':')"
  1893. SYSCODENAME="$(echo "${2}" | cut -f 3 -d ':' | tr '[:upper:]' '[:lower:]')"
  1894. if [ -z "${SYSVERSION}" ]; then
  1895. fatal "You must specify a release as well as a distribution name." F0510
  1896. fi
  1897. shift 1
  1898. else
  1899. fatal "A distribution name and release must be specified for the --distro-override option." F050F
  1900. fi
  1901. ;;
  1902. "--repositories-only")
  1903. set_action 'repositories-only'
  1904. NETDATA_FORCE_METHOD="native"
  1905. ;;
  1906. "--native-only") NETDATA_FORCE_METHOD="native" ;;
  1907. "--static-only") NETDATA_FORCE_METHOD="static" ;;
  1908. "--build-only") NETDATA_FORCE_METHOD="build" ;;
  1909. "--claim-token")
  1910. NETDATA_CLAIM_TOKEN="${2}"
  1911. shift 1
  1912. ;;
  1913. "--claim-rooms")
  1914. NETDATA_CLAIM_ROOMS="${2}"
  1915. shift 1
  1916. ;;
  1917. "--claim-url")
  1918. NETDATA_CLAIM_URL="${2}"
  1919. shift 1
  1920. ;;
  1921. "--claim-"*)
  1922. optname="$(echo "${1}" | cut -d '-' -f 4-)"
  1923. case "${optname}" in
  1924. id|proxy|user|hostname)
  1925. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
  1926. shift 1
  1927. ;;
  1928. verbose|insecure|noproxy|noreload|daemon-not-running) NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}" ;;
  1929. *) warning "Ignoring unrecognized claiming option ${optname}" ;;
  1930. esac
  1931. ;;
  1932. "--local-build-options")
  1933. LOCAL_BUILD_OPTIONS="${LOCAL_BUILD_OPTIONS} ${2}"
  1934. shift 1
  1935. ;;
  1936. "--static-install-options")
  1937. STATIC_INSTALL_OPTIONS="${STATIC_INSTALL_OPTIONS} ${2}"
  1938. shift 1
  1939. ;;
  1940. "--prepare-offline-install-source")
  1941. if [ -n "${2}" ]; then
  1942. set_action 'prepare-offline'
  1943. OFFLINE_TARGET="${2}"
  1944. shift 1
  1945. else
  1946. fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
  1947. fi
  1948. ;;
  1949. "--offline-install-source")
  1950. if [ -d "${2}" ]; then
  1951. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  1952. shift 1
  1953. else
  1954. fatal "A source directory must be specified with the --offline-install-source option." F0501
  1955. fi
  1956. ;;
  1957. "--"|"all"|"--yes"|"-y"|"--force"|"--accept") warning "Option '${1}' is not recognized, ignoring it. ${BADOPT_MSG}" ;;
  1958. *) fatal "Unrecognized option '${1}'. ${BADOPT_MSG}" F050E ;;
  1959. esac
  1960. shift 1
  1961. done
  1962. validate_args
  1963. }
  1964. # ======================================================================
  1965. # Main program
  1966. setup_terminal || echo > /dev/null
  1967. # shellcheck disable=SC2068
  1968. parse_args $@
  1969. confirm_root_support
  1970. get_system_info
  1971. confirm_install_prefix
  1972. if [ -z "${ACTION}" ]; then
  1973. handle_existing_install
  1974. fi
  1975. main