kickstart.sh 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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 provide 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 provide 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. if command -v netdata-claim.sh > /dev/null 2>&1; then
  966. NETDATA_CLAIM_PATH="$(command -v netdata-claim.sh)"
  967. elif [ -z "${INSTALL_PREFIX}" ] || [ "${INSTALL_PREFIX}" = "/" ]; then
  968. NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
  969. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  970. NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
  971. elif [ ! -d "${INSTALL_PREFIX}/netdata" ]; then
  972. if [ -d "${INSTALL_PREFIX}/usr" ]; then
  973. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/usr/sbin/netdata-claim.sh"
  974. else
  975. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/sbin/netdata-claim.sh"
  976. fi
  977. else
  978. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
  979. fi
  980. err_msg=
  981. err_code=
  982. if [ -z "${NETDATA_CLAIM_PATH}" ]; then
  983. err_msg="Unable to claim node: could not find usable claiming script. Reinstalling Netdata may resolve this."
  984. err_code=F050B
  985. elif [ ! -e "${NETDATA_CLAIM_PATH}" ]; then
  986. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} does not exist."
  987. err_code=F0512
  988. elif [ ! -f "${NETDATA_CLAIM_PATH}" ]; then
  989. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} is not a file."
  990. err_code=F0513
  991. elif [ ! -x "${NETDATA_CLAIM_PATH}" ]; then
  992. err_msg="Unable to claim node: claiming script at ${NETDATA_CLAIM_PATH} is not executable. Reinstalling Netdata may resolve this."
  993. err_code=F0514
  994. fi
  995. if [ -n "$err_msg" ]; then
  996. if [ "${ACTION}" = "claim" ]; then
  997. fatal "$err_msg" "$err_code"
  998. else
  999. warning "$err_msg"
  1000. return 1
  1001. fi
  1002. fi
  1003. if ! is_netdata_running; then
  1004. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
  1005. fi
  1006. # shellcheck disable=SC2086
  1007. run_as_root "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
  1008. case $? in
  1009. 0)
  1010. progress "Successfully claimed node"
  1011. return 0
  1012. ;;
  1013. 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}" ;;
  1014. 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." ;;
  1015. 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}." ;;
  1016. 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." ;;
  1017. 5)
  1018. 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."
  1019. return 0
  1020. ;;
  1021. 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." ;;
  1022. 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." ;;
  1023. 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." ;;
  1024. 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." ;;
  1025. 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." ;;
  1026. 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)\"" ;;
  1027. 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}." ;;
  1028. 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}." ;;
  1029. 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." ;;
  1030. *) 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}" ;;
  1031. esac
  1032. if [ "${ACTION}" = "claim" ]; then
  1033. deferred_warnings
  1034. printf >&2 "%s\n" "For community support, you can connect with us on:"
  1035. support_list
  1036. cleanup
  1037. trap - EXIT
  1038. exit 1
  1039. fi
  1040. }
  1041. # ======================================================================
  1042. # Auto-update handling code.
  1043. set_auto_updates() {
  1044. if [ -x "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh" ]; then
  1045. updater="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  1046. elif [ -x "${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh" ]; then
  1047. updater="${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"
  1048. else
  1049. 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."
  1050. return 0
  1051. fi
  1052. if [ "${AUTO_UPDATE}" -eq 1 ]; then
  1053. if [ "${DRY_RUN}" -eq 1 ]; then
  1054. progress "Would have attempted to enable automatic updates."
  1055. # 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.
  1056. elif ! grep -q '\-\-enable-auto-updates' "${updater}"; then
  1057. echo
  1058. elif ! run_as_root "${updater}" --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
  1059. warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
  1060. fi
  1061. else
  1062. if [ "${DRY_RUN}" -eq 1 ]; then
  1063. progress "Would have attempted to disable automatic updates."
  1064. else
  1065. run_as_root "${updater}" --disable-auto-updates
  1066. fi
  1067. fi
  1068. }
  1069. # ======================================================================
  1070. # Native package install code.
  1071. # Check for an already installed package with a given name.
  1072. pkg_installed() {
  1073. case "${SYSTYPE}" in
  1074. Linux)
  1075. case "${DISTRO_COMPAT_NAME}" in
  1076. debian|ubuntu)
  1077. # shellcheck disable=SC2016
  1078. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  1079. return $?
  1080. ;;
  1081. centos|fedora|opensuse|ol|amzn)
  1082. rpm -q "${1}" > /dev/null 2>&1
  1083. return $?
  1084. ;;
  1085. alpine)
  1086. apk -e info "${1}" > /dev/null 2>&1
  1087. return $?
  1088. ;;
  1089. arch)
  1090. pacman -Qi "${1}" > /dev/null 2>&1
  1091. return $?
  1092. ;;
  1093. *) return 1 ;;
  1094. esac
  1095. ;;
  1096. Darwin)
  1097. if command -v brew > /dev/null 2>&1; then
  1098. brew list "${1}" > /dev/null 2>&1
  1099. return $?
  1100. else
  1101. return 1
  1102. fi
  1103. ;;
  1104. FreeBSD)
  1105. if pkg -N > /dev/null 2>&1; then
  1106. pkg info "${1}" > /dev/null 2>&1
  1107. return $?
  1108. else
  1109. return 1
  1110. fi
  1111. ;;
  1112. *) return 1 ;;
  1113. esac
  1114. }
  1115. # Check for the existence of a usable netdata package in the repo.
  1116. netdata_avail_check() {
  1117. case "${DISTRO_COMPAT_NAME}" in
  1118. debian|ubuntu)
  1119. env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q repo.netdata.cloud/repos/;
  1120. return $?
  1121. ;;
  1122. centos|fedora|ol|amzn)
  1123. # shellcheck disable=SC2086
  1124. ${pm_cmd} search --nogpgcheck -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
  1125. return $?
  1126. ;;
  1127. opensuse)
  1128. zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
  1129. return $?
  1130. ;;
  1131. *) return 1 ;;
  1132. esac
  1133. }
  1134. # Check for any distro-specific dependencies we know we need.
  1135. check_special_native_deps() {
  1136. if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
  1137. progress "Checking for libuv availability."
  1138. if ${pm_cmd} search --nogpgcheck -v libuv | grep -q "No matches found"; then
  1139. progress "libuv not found, checking for EPEL availability."
  1140. if ${pm_cmd} search --nogpgcheck -v epel-release | grep -q "No matches found"; then
  1141. warning "Unable to find a suitable source for libuv, cannot install using native packages on this system."
  1142. return 1
  1143. else
  1144. progress "EPEL is available, attempting to install so that required dependencies are available."
  1145. # shellcheck disable=SC2086
  1146. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
  1147. warning "Failed to install EPEL, even though it is required to install native packages on this system."
  1148. return 1
  1149. fi
  1150. fi
  1151. else
  1152. return 0
  1153. fi
  1154. fi
  1155. }
  1156. common_rpm_opts() {
  1157. pkg_type="rpm"
  1158. pkg_suffix=".noarch"
  1159. pkg_vsep="-"
  1160. INSTALL_TYPE="binpkg-rpm"
  1161. NATIVE_VERSION="${INSTALL_VERSION:+"-${INSTALL_VERSION}.${SYSARCH}"}"
  1162. }
  1163. common_dnf_opts() {
  1164. if command -v dnf > /dev/null; then
  1165. pm_cmd="dnf"
  1166. repo_subcmd="makecache"
  1167. else
  1168. pm_cmd="yum"
  1169. fi
  1170. pkg_install_opts="${interactive_opts}"
  1171. repo_update_opts="${interactive_opts}"
  1172. uninstall_subcmd="remove"
  1173. }
  1174. try_package_install() {
  1175. 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."
  1176. if [ -z "${DISTRO_COMPAT_NAME}" ] || [ "${DISTRO_COMPAT_NAME}" = "unknown" ]; then
  1177. warning "Unable to determine Linux distribution for native packages."
  1178. return 2
  1179. elif [ -z "${SYSCODENAME}" ]; then
  1180. case "${DISTRO_COMPAT_NAME}" in
  1181. debian|ubuntu)
  1182. warning "Release codename not set. Unable to check availability of native packages for this system."
  1183. return 2
  1184. ;;
  1185. esac
  1186. fi
  1187. set_tmpdir
  1188. if [ "${DRY_RUN}" -eq 1 ]; then
  1189. progress "Would attempt to install using native packages..."
  1190. else
  1191. progress "Attempting to install using native packages..."
  1192. fi
  1193. if [ "${SELECTED_RELEASE_CHANNEL}" = "nightly" ]; then
  1194. release="-edge"
  1195. else
  1196. release=""
  1197. fi
  1198. if [ "${INTERACTIVE}" = "0" ]; then
  1199. interactive_opts="-y"
  1200. env="DEBIAN_FRONTEND=noninteractive"
  1201. else
  1202. interactive_opts=""
  1203. env=""
  1204. fi
  1205. case "${DISTRO_COMPAT_NAME}" in
  1206. debian|ubuntu)
  1207. needs_early_refresh=1
  1208. pm_cmd="apt-get"
  1209. repo_subcmd="update"
  1210. pkg_type="deb"
  1211. pkg_vsep="_"
  1212. pkg_install_opts="${interactive_opts}"
  1213. repo_update_opts="${interactive_opts}"
  1214. uninstall_subcmd="purge"
  1215. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSCODENAME}"
  1216. pkg_suffix="+${DISTRO_COMPAT_NAME}${SYSVERSION}_all"
  1217. INSTALL_TYPE="binpkg-deb"
  1218. NATIVE_VERSION="${INSTALL_VERSION:+"=${INSTALL_VERSION}"}"
  1219. ;;
  1220. centos)
  1221. common_rpm_opts
  1222. common_dnf_opts
  1223. repo_prefix="el/${SYSVERSION}"
  1224. ;;
  1225. fedora|ol)
  1226. common_rpm_opts
  1227. common_dnf_opts
  1228. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSVERSION}"
  1229. ;;
  1230. opensuse)
  1231. common_rpm_opts
  1232. pm_cmd="zypper"
  1233. repo_subcmd="--gpg-auto-import-keys refresh"
  1234. repo_prefix="opensuse/${SYSVERSION}"
  1235. pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
  1236. repo_update_opts=""
  1237. uninstall_subcmd="remove"
  1238. ;;
  1239. amzn)
  1240. common_rpm_opts
  1241. common_dnf_opts
  1242. repo_prefix="amazonlinux/${SYSVERSION}"
  1243. ;;
  1244. *)
  1245. warning "We do not provide native packages for ${DISTRO}."
  1246. return 2
  1247. ;;
  1248. esac
  1249. if [ -n "${SKIP_DISTRO_DETECTION}" ]; then
  1250. 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."
  1251. fi
  1252. if [ -n "${INSTALL_VERSION}" ]; then
  1253. if echo "${INSTALL_VERSION}" | grep -q "nightly"; then
  1254. new_release="-edge"
  1255. else
  1256. new_release=
  1257. fi
  1258. if { [ -n "${new_release}" ] && [ -z "${release}" ]; } || { [ -z "${new_release}" ] && [ -n "${release}" ]; }; then
  1259. warning "Selected release channel does not match this version and it will be changed automatically."
  1260. fi
  1261. release="${new_release}"
  1262. fi
  1263. repoconfig_name="netdata-repo${release}"
  1264. case "${pkg_type}" in
  1265. deb)
  1266. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_DEB_VERSION}${pkg_suffix}.${pkg_type}"
  1267. repoconfig_url="${REPOCONFIG_DEB_URL_PREFIX}/${repo_prefix}/${repoconfig_file}"
  1268. ;;
  1269. rpm)
  1270. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_RPM_VERSION}${pkg_suffix}.${pkg_type}"
  1271. repoconfig_url="${REPOCONFIG_RPM_URL_PREFIX}/${repo_prefix}/${SYSARCH}/${repoconfig_file}"
  1272. ;;
  1273. esac
  1274. if ! pkg_installed "${repoconfig_name}"; then
  1275. progress "Checking for availability of repository configuration package."
  1276. if ! check_for_remote_file "${repoconfig_url}"; then
  1277. warning "No repository configuration package available for ${DISTRO} ${SYSVERSION}. Cannot install native packages on this system."
  1278. return 2
  1279. fi
  1280. if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
  1281. fatal "Failed to download repository configuration package. ${BADNET_MSG}." F0209
  1282. fi
  1283. if [ -n "${needs_early_refresh}" ]; then
  1284. # shellcheck disable=SC2086
  1285. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1286. warning "${failed_refresh_msg}"
  1287. return 2
  1288. fi
  1289. fi
  1290. # shellcheck disable=SC2086
  1291. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
  1292. warning "Failed to install repository configuration package."
  1293. return 2
  1294. fi
  1295. if [ -n "${repo_subcmd}" ]; then
  1296. # shellcheck disable=SC2086
  1297. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1298. fatal "${failed_refresh_msg}" F0205
  1299. fi
  1300. fi
  1301. else
  1302. progress "Repository configuration is already present, attempting to install netdata."
  1303. fi
  1304. if [ "${ACTION}" = "repositories-only" ]; then
  1305. progress "Successfully installed repository configuraion package."
  1306. deferred_warnings
  1307. cleanup
  1308. trap - EXIT
  1309. exit 1
  1310. fi
  1311. if ! check_special_native_deps; then
  1312. warning "Could not find secondary dependencies for ${DISTRO} on ${SYSARCH}."
  1313. if [ -z "${NO_CLEANUP}" ]; then
  1314. progress "Attempting to uninstall repository configuration package."
  1315. # shellcheck disable=SC2086
  1316. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1317. fi
  1318. return 2
  1319. fi
  1320. if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
  1321. warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
  1322. if [ -z "${NO_CLEANUP}" ]; then
  1323. progress "Attempting to uninstall repository configuration package."
  1324. # shellcheck disable=SC2086
  1325. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1326. fi
  1327. return 2
  1328. fi
  1329. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
  1330. run_as_root mkdir -p "/etc/netdata"
  1331. run_as_root touch "/etc/netdata/.opt-out-from-anonymous-statistics"
  1332. fi
  1333. # shellcheck disable=SC2086
  1334. if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
  1335. warning "Failed to install Netdata package."
  1336. if [ -z "${NO_CLEANUP}" ]; then
  1337. progress "Attempting to uninstall repository configuration package."
  1338. # shellcheck disable=SC2086
  1339. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1340. fi
  1341. return 2
  1342. fi
  1343. }
  1344. # ======================================================================
  1345. # Static build install code
  1346. # shellcheck disable=SC2034,SC2086,SC2126
  1347. set_static_archive_urls() {
  1348. if [ -z "${2}" ]; then
  1349. arch="${SYSARCH}"
  1350. else
  1351. arch="${2}"
  1352. fi
  1353. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1354. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  1355. export NETDATA_STATIC_ARCHIVE_URL="file://${path}/netdata-${arch}-latest.gz.run"
  1356. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1357. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="file://${path}/sha256sums.txt"
  1358. elif [ "${1}" = "stable" ]; then
  1359. if [ -n "${INSTALL_VERSION}" ]; then
  1360. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1361. export NETDATA_STATIC_ARCHIVE_OLD_URL="https://github.com/netdata/netdata/releases/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="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1365. else
  1366. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1367. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${arch}-latest.gz.run"
  1368. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1369. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1370. fi
  1371. else
  1372. if [ -n "${INSTALL_VERSION}" ]; then
  1373. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1374. export NETDATA_STATIC_ARCHIVE_OLD_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1375. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1376. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1377. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1378. else
  1379. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1380. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-${arch}-latest.gz.run"
  1381. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1382. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1383. fi
  1384. fi
  1385. }
  1386. try_static_install() {
  1387. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1388. if [ "${DRY_RUN}" -eq 1 ]; then
  1389. progress "Would attempt to install using static build..."
  1390. else
  1391. progress "Attempting to install using static build..."
  1392. fi
  1393. # Check status code first, so that we can provide nicer fallback for dry runs.
  1394. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1395. netdata_agent="${NETDATA_STATIC_ARCHIVE_NAME}"
  1396. elif [ "${SYSARCH}" = "x86_64" ] && check_for_remote_file "${NETDATA_STATIC_ARCHIVE_OLD_URL}"; then
  1397. netdata_agent="${NETDATA_STATIC_ARCHIVE_OLD_NAME}"
  1398. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1399. else
  1400. warning "There is no static build available for ${SYSARCH} CPUs. This usually means we simply do not currently provide static builds for ${SYSARCH} CPUs."
  1401. return 2
  1402. fi
  1403. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/${netdata_agent}"; then
  1404. fatal "Unable to download static build archive for ${SYSARCH}. ${BADNET_MSG}." F0208
  1405. fi
  1406. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1407. fatal "Unable to fetch checksums to verify static build archive. ${BADNET_MSG}." F0206
  1408. fi
  1409. if [ "${DRY_RUN}" -eq 1 ]; then
  1410. progress "Would validate SHA256 checksum of downloaded static build archive."
  1411. else
  1412. if [ -z "${INSTALL_VERSION}" ]; then
  1413. if ! grep "${netdata_agent}" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1414. fatal "Static binary checksum validation failed. ${BADCACHE_MSG}." F0207
  1415. fi
  1416. fi
  1417. fi
  1418. if [ "${INTERACTIVE}" -eq 0 ]; then
  1419. opts="${opts} --accept"
  1420. fi
  1421. progress "Installing netdata"
  1422. # shellcheck disable=SC2086
  1423. if ! run_as_root sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
  1424. warning "Failed to install static build of Netdata on ${SYSARCH}."
  1425. run rm -rf /opt/netdata
  1426. return 2
  1427. fi
  1428. if [ "${DRY_RUN}" -ne 1 ]; then
  1429. install_type_file="/opt/netdata/etc/netdata/.install-type"
  1430. if [ -f "${install_type_file}" ]; then
  1431. run_as_root sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
  1432. run_as_root chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
  1433. # shellcheck disable=SC1090,SC1091
  1434. . "${tmpdir}/install-type"
  1435. cat > "${tmpdir}/install-type" <<- EOF
  1436. INSTALL_TYPE='kickstart-static'
  1437. PREBUILT_ARCH='${PREBUILT_ARCH}'
  1438. EOF
  1439. run_as_root chown netdata:netdata "${tmpdir}/install-type"
  1440. run_as_root cp "${tmpdir}/install-type" "${install_type_file}"
  1441. fi
  1442. fi
  1443. }
  1444. # ======================================================================
  1445. # Local build install code
  1446. set_source_archive_urls() {
  1447. if [ "$1" = "stable" ]; then
  1448. if [ -n "${INSTALL_VERSION}" ]; then
  1449. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.tar.gz"
  1450. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1451. else
  1452. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1453. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${latest}.tar.gz"
  1454. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1455. fi
  1456. else
  1457. if [ -n "${INSTALL_VERSION}" ]; then
  1458. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-latest.tar.gz"
  1459. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1460. else
  1461. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1462. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-latest.tar.gz"
  1463. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1464. fi
  1465. fi
  1466. }
  1467. install_local_build_dependencies() {
  1468. set_tmpdir
  1469. bash="$(command -v bash 2> /dev/null)"
  1470. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  1471. warning "Unable to find a usable version of \`bash\` (required for local build)."
  1472. return 1
  1473. fi
  1474. if ! download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"; then
  1475. fatal "Failed to download dependency handling script for local build. ${BADNET_MSG}." F000D
  1476. fi
  1477. if [ "${DRY_RUN}" -eq 1 ]; then
  1478. progress "Would run downloaded script to install required build dependencies..."
  1479. else
  1480. progress "Running downloaded script to install required build dependencies..."
  1481. fi
  1482. if [ "${INTERACTIVE}" -eq 0 ]; then
  1483. opts="--dont-wait --non-interactive"
  1484. fi
  1485. # shellcheck disable=SC2086
  1486. if ! run_as_root "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
  1487. warning "Failed to install all required packages, but installation might still be possible."
  1488. fi
  1489. }
  1490. build_and_install() {
  1491. if [ "${DRY_RUN}" -eq 1 ]; then
  1492. progress "Would attempt to build netdata..."
  1493. else
  1494. progress "Building netdata..."
  1495. fi
  1496. echo "INSTALL_TYPE='kickstart-build'" > system/.install-type
  1497. opts="${NETDATA_INSTALLER_OPTIONS}"
  1498. if [ "${INTERACTIVE}" -eq 0 ]; then
  1499. opts="${opts} --dont-wait"
  1500. fi
  1501. if [ "${SELECTED_RELEASE_CHANNEL}" = "stable" ]; then
  1502. opts="${opts} --stable-channel"
  1503. fi
  1504. if [ "${NETDATA_REQUIRE_CLOUD}" -eq 1 ]; then
  1505. opts="${opts} --require-cloud"
  1506. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  1507. opts="${opts} --disable-cloud"
  1508. fi
  1509. # shellcheck disable=SC2086
  1510. run_script ./netdata-installer.sh ${opts}
  1511. case $? in
  1512. 1)
  1513. if [ -n "${EXIT_REASON}" ]; then
  1514. fatal "netdata-installer.sh failed to run: ${EXIT_REASON}" "${EXIT_CODE}"
  1515. else
  1516. fatal "netdata-installer.sh failed to run: Encountered an unhandled error in the installer code." I0000
  1517. fi
  1518. ;;
  1519. 2) fatal "Insufficient RAM to install netdata." F0008 ;;
  1520. esac
  1521. }
  1522. try_build_install() {
  1523. set_tmpdir
  1524. if [ "${DRY_RUN}" -eq 1 ]; then
  1525. progress "Would attempt to install by building locally..."
  1526. else
  1527. progress "Attempting to install by building locally..."
  1528. fi
  1529. if ! install_local_build_dependencies; then
  1530. return 1
  1531. fi
  1532. set_source_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1533. if [ -n "${INSTALL_VERSION}" ]; then
  1534. if ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz"; then
  1535. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1536. fi
  1537. elif ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-latest.tar.gz"; then
  1538. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1539. fi
  1540. if ! download "${NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1541. fatal "Failed to download checksums for source tarball verification. ${BADNET_MSG}." F000C
  1542. fi
  1543. if [ "${DRY_RUN}" -eq 1 ]; then
  1544. progress "Would validate SHA256 checksum of downloaded source archive."
  1545. else
  1546. if [ -z "${INSTALL_VERSION}" ]; then
  1547. # shellcheck disable=SC2086
  1548. if ! grep netdata-latest.tar.gz "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1549. fatal "Tarball checksum validation failed. ${BADCACHE_MSG}." F0005
  1550. fi
  1551. fi
  1552. fi
  1553. if [ -n "${INSTALL_VERSION}" ]; then
  1554. run tar -xf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" -C "${tmpdir}"
  1555. rm -rf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" > /dev/null 2>&1
  1556. else
  1557. run tar -xf "${tmpdir}/netdata-latest.tar.gz" -C "${tmpdir}"
  1558. rm -rf "${tmpdir}/netdata-latest.tar.gz" > /dev/null 2>&1
  1559. fi
  1560. if [ "${DRY_RUN}" -ne 1 ]; then
  1561. cd "$(find "${tmpdir}" -mindepth 1 -maxdepth 1 -type d -name netdata-)" || fatal "Cannot change directory to netdata source tree" F0006
  1562. fi
  1563. if [ -x netdata-installer.sh ] || [ "${DRY_RUN}" -eq 1 ]; then
  1564. build_and_install || return 1
  1565. else
  1566. # This case is needed because some platforms produce an extra directory on the source tarball extraction.
  1567. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  1568. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" && build_and_install || return 1
  1569. else
  1570. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)." F0009
  1571. fi
  1572. fi
  1573. }
  1574. # ======================================================================
  1575. # Offline install support code
  1576. prepare_offline_install_source() {
  1577. if [ -e "${1}" ]; then
  1578. if [ ! -d "${1}" ]; then
  1579. fatal "${1} is not a directory, unable to prepare offline install source." F0503
  1580. fi
  1581. else
  1582. run mkdir -p "${1}" || fatal "Unable to create target directory for offline install preparation." F0504
  1583. fi
  1584. run cd "${1}" || fatal "Failed to switch to target directory for offline install preparation." F0505
  1585. case "${NETDATA_FORCE_METHOD}" in
  1586. static|'')
  1587. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "x86_64"
  1588. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1589. for arch in ${STATIC_INSTALL_ARCHES}; do
  1590. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "${arch}"
  1591. progress "Fetching ${NETDATA_STATIC_ARCHIVE_URL}"
  1592. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "netdata-${arch}-latest.gz.run"; then
  1593. warning "Failed to download static installer archive for ${arch}. ${BADNET_MSG}."
  1594. fi
  1595. done
  1596. legacy=0
  1597. else
  1598. 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."
  1599. progress "Fetching ${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1600. legacy=1
  1601. if ! download "${NETDATA_STATIC_ARCHIVE_OLD_URL}" "netdata-x86_64-latest.gz.run"; then
  1602. warning "Failed to download static installer archive for x86_64. ${BADNET_MSG}."
  1603. fi
  1604. fi
  1605. progress "Fetching ${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}"
  1606. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "sha256sums.txt"; then
  1607. fatal "Failed to download checksum file. ${BADNET_MSG}." F0506
  1608. fi
  1609. ;;
  1610. esac
  1611. if [ "${legacy:-0}" -eq 1 ]; then
  1612. sed -e 's/netdata-latest.gz.run/netdata-x86_64-latest.gz.run' sha256sums.txt > sha256sums.tmp
  1613. mv sha256sums.tmp sha256sums.txt
  1614. fi
  1615. if [ "${DRY_RUN}" -ne 1 ]; then
  1616. progress "Verifying checksums."
  1617. if ! grep -e "$(find . -name '*.gz.run')" sha256sums.txt | safe_sha256sum -c -; then
  1618. fatal "Checksums for offline install files are incorrect. ${BADCACHE_MSG}." F0507
  1619. fi
  1620. else
  1621. progress "Would verify SHA256 checksums of downloaded installation files."
  1622. fi
  1623. if [ "${DRY_RUN}" -ne 1 ]; then
  1624. progress "Preparing install script."
  1625. cat > "install.sh" <<-EOF
  1626. #!/bin/sh
  1627. dir=\$(CDPATH= cd -- "\$(dirname -- "\$0")" && pwd)
  1628. "\${dir}/kickstart.sh" --offline-install-source "\${dir}" \${@}
  1629. EOF
  1630. chmod +x "install.sh"
  1631. else
  1632. progress "Would create install script"
  1633. fi
  1634. if [ "${DRY_RUN}" -ne 1 ]; then
  1635. progress "Copying kickstart script."
  1636. cp "${KICKSTART_SOURCE}" "kickstart.sh"
  1637. chmod +x "kickstart.sh"
  1638. else
  1639. progress "Would copy kickstart.sh to offline install source directory"
  1640. fi
  1641. if [ "${DRY_RUN}" -ne 1 ]; then
  1642. progress "Saving release channel information."
  1643. echo "${SELECTED_RELEASE_CHANNEL}" > "channel"
  1644. else
  1645. progress "Would save release channel information to offline install source directory"
  1646. fi
  1647. 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."
  1648. }
  1649. # ======================================================================
  1650. # Per system-type install logic
  1651. install_on_linux() {
  1652. if [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1653. SELECTED_INSTALL_METHOD="native"
  1654. try_package_install
  1655. case "$?" in
  1656. 0)
  1657. NETDATA_INSTALL_SUCCESSFUL=1
  1658. INSTALL_PREFIX="/"
  1659. ;;
  1660. 1) fatal "Unable to install on this system." F0300 ;;
  1661. 2)
  1662. case "${NETDATA_FORCE_METHOD}" in
  1663. native) fatal "Could not install native binary packages." F0301 ;;
  1664. *) warning "Could not install native binary packages, falling back to alternative installation method." ;;
  1665. esac
  1666. ;;
  1667. esac
  1668. fi
  1669. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1670. SELECTED_INSTALL_METHOD="static"
  1671. INSTALL_TYPE="kickstart-static"
  1672. try_static_install
  1673. case "$?" in
  1674. 0)
  1675. NETDATA_INSTALL_SUCCESSFUL=1
  1676. INSTALL_PREFIX="/opt/netdata"
  1677. ;;
  1678. 1) fatal "Unable to install on this system." F0302 ;;
  1679. 2)
  1680. case "${NETDATA_FORCE_METHOD}" in
  1681. static) fatal "Could not install static build." F0303 ;;
  1682. *) warning "Could not install static build, falling back to alternative installation method." ;;
  1683. esac
  1684. ;;
  1685. esac
  1686. fi
  1687. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1688. SELECTED_INSTALL_METHOD="build"
  1689. INSTALL_TYPE="kickstart-build"
  1690. try_build_install
  1691. case "$?" in
  1692. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1693. *) fatal "Unable to install on this system." F0304 ;;
  1694. esac
  1695. fi
  1696. }
  1697. install_on_macos() {
  1698. case "${NETDATA_FORCE_METHOD}" in
  1699. native) fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option." F0305 ;;
  1700. static) fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option." F0306 ;;
  1701. *)
  1702. SELECTED_INSTALL_METHOD="build"
  1703. INSTALL_TYPE="kickstart-build"
  1704. try_build_install
  1705. case "$?" in
  1706. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1707. *) fatal "Unable to install on this system." F0307 ;;
  1708. esac
  1709. ;;
  1710. esac
  1711. }
  1712. install_on_freebsd() {
  1713. case "${NETDATA_FORCE_METHOD}" in
  1714. native) fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option." F0308 ;;
  1715. static) fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option." F0309 ;;
  1716. *)
  1717. SELECTED_INSTALL_METHOD="build"
  1718. INSTALL_TYPE="kickstart-build"
  1719. try_build_install
  1720. case "$?" in
  1721. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1722. *) fatal "Unable to install on this system." F030A ;;
  1723. esac
  1724. ;;
  1725. esac
  1726. }
  1727. # ======================================================================
  1728. # Argument parsing code
  1729. validate_args() {
  1730. check_claim_opts
  1731. if [ -n "${NETDATA_FORCE_METHOD}" ]; then
  1732. SELECTED_INSTALL_METHOD="${NETDATA_FORCE_METHOD}"
  1733. fi
  1734. if [ "${ACTION}" = "repositories-only" ] && [ "${NETDATA_FORCE_METHOD}" != "native" ]; then
  1735. fatal "Repositories can only be installed for native installs." F050D
  1736. fi
  1737. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1738. case "${NETDATA_FORCE_METHOD}" in
  1739. native|build) fatal "Offline installs are only supported for static builds currently." F0502 ;;
  1740. esac
  1741. fi
  1742. if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
  1743. case "${NETDATA_FORCE_METHOD}" in
  1744. build) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}" ;;
  1745. *) fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401 ;;
  1746. esac
  1747. fi
  1748. if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
  1749. case "${NETDATA_FORCE_METHOD}" in
  1750. static) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}" ;;
  1751. *) fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402 ;;
  1752. esac
  1753. fi
  1754. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
  1755. fatal "Specifying an install version alongside an offline install source is not supported." F050A
  1756. fi
  1757. if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
  1758. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
  1759. AUTO_UPDATE=0
  1760. else
  1761. AUTO_UPDATE=1
  1762. fi
  1763. elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
  1764. AUTO_UPDATE=1
  1765. else
  1766. AUTO_UPDATE=0
  1767. fi
  1768. if [ "${RELEASE_CHANNEL}" = "default" ]; then
  1769. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1770. SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
  1771. if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
  1772. fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
  1773. fi
  1774. else
  1775. SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
  1776. fi
  1777. else
  1778. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
  1779. fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
  1780. fi
  1781. SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
  1782. fi
  1783. }
  1784. set_action() {
  1785. new_action="${1}"
  1786. if [ -n "${ACTION}" ]; then
  1787. warning "Ignoring previously specified '${ACTION}' operation in favor of '${new_action}' specified later on the command line."
  1788. fi
  1789. ACTION="${new_action}"
  1790. NETDATA_COMMAND="${new_action}"
  1791. }
  1792. parse_args() {
  1793. while [ -n "${1}" ]; do
  1794. case "${1}" in
  1795. "--help")
  1796. usage
  1797. cleanup
  1798. trap - EXIT
  1799. exit 0
  1800. ;;
  1801. "--no-cleanup") NO_CLEANUP=1 ;;
  1802. "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
  1803. "--interactive") INTERACTIVE=1 ;;
  1804. "--dry-run") DRY_RUN=1 ;;
  1805. "--release-channel")
  1806. RELEASE_CHANNEL="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1807. case "${RELEASE_CHANNEL}" in
  1808. nightly|stable|default) shift 1 ;;
  1809. *)
  1810. echo "Unrecognized value for --release-channel. Valid release channels are: stable, nightly, default"
  1811. exit 1
  1812. ;;
  1813. esac
  1814. ;;
  1815. "--stable-channel") RELEASE_CHANNEL="stable" ;;
  1816. "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
  1817. "--reinstall") set_action 'reinstall' ;;
  1818. "--reinstall-even-if-unsafe") set_action 'unsafe-reinstall' ;;
  1819. "--reinstall-clean") set_action 'reinstall-clean' ;;
  1820. "--uninstall") set_action 'uninstall' ;;
  1821. "--claim-only") set_action 'claim' ;;
  1822. "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
  1823. "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
  1824. "--auto-update-method")
  1825. NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1826. case "${NETDATA_AUTO_UPDATE_TYPE}" in
  1827. systemd|interval|crontab) shift 1 ;;
  1828. *)
  1829. echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
  1830. exit 1
  1831. ;;
  1832. esac
  1833. ;;
  1834. "--disable-cloud")
  1835. NETDATA_DISABLE_CLOUD=1
  1836. NETDATA_REQUIRE_CLOUD=0
  1837. ;;
  1838. "--require-cloud")
  1839. NETDATA_DISABLE_CLOUD=0
  1840. NETDATA_REQUIRE_CLOUD=1
  1841. ;;
  1842. "--dont-start-it")
  1843. NETDATA_NO_START=1
  1844. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
  1845. ;;
  1846. "--disable-telemetry")
  1847. NETDATA_DISABLE_TELEMETRY="1"
  1848. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
  1849. ;;
  1850. "--install-prefix")
  1851. INSTALL_PREFIX="${2}"
  1852. shift 1
  1853. ;;
  1854. "--old-install-prefix")
  1855. OLD_INSTALL_PREFIX="${2}"
  1856. shift 1
  1857. ;;
  1858. "--install-version")
  1859. INSTALL_VERSION="${2}"
  1860. AUTO_UPDATE=0
  1861. shift 1
  1862. ;;
  1863. "--distro-override")
  1864. if [ -n "${2}" ]; then
  1865. SKIP_DISTRO_DETECTION=1
  1866. DISTRO="$(echo "${2}" | cut -f 1 -d ':' | tr '[:upper:]' '[:lower:]')"
  1867. SYSVERSION="$(echo "${2}" | cut -f 2 -d ':')"
  1868. SYSCODENAME="$(echo "${2}" | cut -f 3 -d ':' | tr '[:upper:]' '[:lower:]')"
  1869. if [ -z "${SYSVERSION}" ]; then
  1870. fatal "You must specify a release as well as a distribution name." F0510
  1871. fi
  1872. shift 1
  1873. else
  1874. fatal "A distribution name and release must be specified for the --distro-override option." F050F
  1875. fi
  1876. ;;
  1877. "--repositories-only")
  1878. set_action 'repositories-only'
  1879. NETDATA_FORCE_METHOD="native"
  1880. ;;
  1881. "--native-only") NETDATA_FORCE_METHOD="native" ;;
  1882. "--static-only") NETDATA_FORCE_METHOD="static" ;;
  1883. "--build-only") NETDATA_FORCE_METHOD="build" ;;
  1884. "--claim-token")
  1885. NETDATA_CLAIM_TOKEN="${2}"
  1886. shift 1
  1887. ;;
  1888. "--claim-rooms")
  1889. NETDATA_CLAIM_ROOMS="${2}"
  1890. shift 1
  1891. ;;
  1892. "--claim-url")
  1893. NETDATA_CLAIM_URL="${2}"
  1894. shift 1
  1895. ;;
  1896. "--claim-"*)
  1897. optname="$(echo "${1}" | cut -d '-' -f 4-)"
  1898. case "${optname}" in
  1899. id|proxy|user|hostname)
  1900. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
  1901. shift 1
  1902. ;;
  1903. verbose|insecure|noproxy|noreload|daemon-not-running) NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}" ;;
  1904. *) warning "Ignoring unrecognized claiming option ${optname}" ;;
  1905. esac
  1906. ;;
  1907. "--local-build-options")
  1908. LOCAL_BUILD_OPTIONS="${LOCAL_BUILD_OPTIONS} ${2}"
  1909. shift 1
  1910. ;;
  1911. "--static-install-options")
  1912. STATIC_INSTALL_OPTIONS="${STATIC_INSTALL_OPTIONS} ${2}"
  1913. shift 1
  1914. ;;
  1915. "--prepare-offline-install-source")
  1916. if [ -n "${2}" ]; then
  1917. set_action 'prepare-offline'
  1918. OFFLINE_TARGET="${2}"
  1919. shift 1
  1920. else
  1921. fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
  1922. fi
  1923. ;;
  1924. "--offline-install-source")
  1925. if [ -d "${2}" ]; then
  1926. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  1927. shift 1
  1928. else
  1929. fatal "A source directory must be specified with the --offline-install-source option." F0501
  1930. fi
  1931. ;;
  1932. *) 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 ;;
  1933. esac
  1934. shift 1
  1935. done
  1936. validate_args
  1937. }
  1938. # ======================================================================
  1939. # Main program
  1940. setup_terminal || echo > /dev/null
  1941. # shellcheck disable=SC2068
  1942. parse_args $@
  1943. confirm_root_support
  1944. get_system_info
  1945. confirm_install_prefix
  1946. if [ -z "${ACTION}" ]; then
  1947. handle_existing_install
  1948. fi
  1949. main