kickstart.sh 80 KB

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