1
+#!/bin/sh
2
+#
3
+# SPDX-License-Identifier: GPL-3.0-or-later
4
+
5
+# ======================================================================
6
+# Constants
7
+
8
+REPOCONFIG_URL_PREFIX="https://packagecloud.io/netdata/netdata-repoconfig/packages"
9
+REPOCONFIG_VERSION="1-1"
10
+PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
11
+
12
+# ======================================================================
13
+# Defaults for environment variables
14
+
15
+RELEASE_CHANNEL="nightly"
16
+NETDATA_CLAIM_URL="https://app.netdata.cloud"
17
+NETDATA_CLAIM_ONLY=0
18
+NETDATA_USER_CONFIG_DIR="/etc/netdata"
19
+NETDATA_AUTO_UPDATES="1"
20
+NETDATA_ONLY_NATIVE=0
21
+NETDATA_ONLY_STATIC=0
22
+
23
+NETDATA_DISABLE_TELEMETRY="${DO_NOT_TRACK:-0}"
24
+NETDATA_TARBALL_BASEURL="${NETDATA_TARBALL_BASEURL:-https://storage.googleapis.com/netdata-nightlies}"
25
+
26
+if [ ! -t 1 ]; then
27
+ INTERACTIVE=0
28
+else
29
+ INTERACTIVE=1
30
+fi
31
+
32
+# ======================================================================
33
+# Usage info
34
+
35
+usage() {
36
+ cat << HEREDOC
37
+USAGE: kickstart.sh [options]
38
+ where options include:
39
+
40
+ --non-interactive Do not prompt for user input. (default: prompt if there is a controlling terminal)
41
+ --interactive Prompt for user input even if there is no controlling terminal.
42
+ --dont-start-it Do not start the agent by default (only for static installs or local builds)
43
+ --stable-channel Install a stable version instead of a nightly build (default: install a nightly build)
44
+ --nightly-channel Install a nightly build instead of a stable version
45
+ --no-updates Do not enable automatic updates (default: enable automatic updates)
46
+ --auto-update Enable automatic updates.
47
+ --disable-telemetry Opt-out of anonymous statistics.
48
+ --native-only Only install if native binary packages are available.
49
+ --static-only Only install if a static build is available.
50
+ --reinstall Explicitly reinstall instead of updating any existing install.
51
+ --claim-token Use a specified token for claiming to Netdata Cloud.
52
+ --claim-rooms When claiming, add the node to the specified rooms.
53
+ --claim-only If there is an existing install, only try to claim it, not update it.
54
+ --claim-* Specify other options for the claiming script.
55
+ --no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
56
+
57
+Additionally, this script may use the following environment variables:
58
+
59
+ TMPDIR: Used to specify where to put temporary files. On most systems, the default we select
60
+ automatically should be fine. The user running the script needs to both be able to
61
+ write files to the temporary directory, and run files from that location.
62
+ ROOTCMD: Used to specify a command to use to run another command with root privileges if needed. By
63
+ default we try to use sudo, doas, or pkexec (in that order of preference), but if
64
+ you need special options for one of those to work, or have a different tool to do
65
+ the same thing on your system, you can specify it here.
66
+ DO_NOT_TRACK If set to a value other than 0, behave as if \`--disable-telemetry\` was specified.
67
+
68
+HEREDOC
69
+}
70
+
71
+# ======================================================================
72
+# Utility functions
73
+
74
+setup_terminal() {
75
+ TPUT_RESET=""
76
+ TPUT_WHITE=""
77
+ TPUT_BGRED=""
78
+ TPUT_BGGREEN=""
79
+ TPUT_BOLD=""
80
+ TPUT_DIM=""
81
+
82
+ # Is stderr on the terminal? If not, then fail
83
+ test -t 2 || return 1
84
+
85
+ if command -v tput > /dev/null 2>&1; then
86
+ if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
87
+ # Enable colors
88
+ TPUT_RESET="$(tput sgr 0)"
89
+ TPUT_WHITE="$(tput setaf 7)"
90
+ TPUT_BGRED="$(tput setab 1)"
91
+ TPUT_BGGREEN="$(tput setab 2)"
92
+ TPUT_BOLD="$(tput bold)"
93
+ TPUT_DIM="$(tput dim)"
94
+ fi
95
+ fi
96
+
97
+ echo "${TPUT_RESET}"
98
+
99
+ return 0
100
+}
101
+
102
+fatal() {
103
+ printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*}"
104
+ exit 1
105
+}
106
+
107
+run_ok() {
108
+ printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
109
+}
110
+
111
+run_failed() {
112
+ printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
113
+}
114
+
115
+ESCAPED_PRINT_METHOD=
116
+# shellcheck disable=SC3050
117
+if printf "%q " test > /dev/null 2>&1; then
118
+ ESCAPED_PRINT_METHOD="printfq"
119
+fi
120
+
121
+escaped_print() {
122
+ if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
123
+ # shellcheck disable=SC3050
124
+ printf "%q " "${@}"
125
+ else
126
+ printf "%s" "${*}"
127
+ fi
128
+ return 0
129
+}
130
+
131
+progress() {
132
+ echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
133
+}
134
+
135
+run_logfile="/dev/null"
136
+run() {
137
+ user="${USER--}"
138
+ dir="${PWD}"
139
+
140
+ if [ "$(id -u)" = "0" ]; then
141
+ info="[root ${dir}]# "
142
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
143
+ else
144
+ info="[${user} ${dir}]$ "
145
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
146
+ fi
147
+
148
+ {
149
+ printf "%s" "${info}"
150
+ escaped_print "${@}"
151
+ printf " ... "
152
+ } >> "${run_logfile}"
153
+
154
+ printf >&2 "%s" "${info_console}${TPUT_BOLD}"
155
+ escaped_print >&2 "${@}"
156
+ printf >&2 "%s\n" "${TPUT_RESET}"
157
+
158
+ "${@}"
159
+
160
+ ret=$?
161
+ if [ ${ret} -ne 0 ]; then
162
+ run_failed
163
+ printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
164
+ else
165
+ run_ok
166
+ printf "OK\n" >> "${run_logfile}"
167
+ fi
168
+
169
+ return ${ret}
170
+}
171
+
172
+warning() {
173
+ printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
174
+}
175
+
176
+_cannot_use_tmpdir() {
177
+ testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
178
+ ret=0
179
+
180
+ if [ -z "${testfile}" ]; then
181
+ return "${ret}"
182
+ fi
183
+
184
+ if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
185
+ if chmod +x "${testfile}"; then
186
+ if [ "$("${testfile}")" = "SUCCESS" ]; then
187
+ ret=1
188
+ fi
189
+ fi
190
+ fi
191
+
192
+ rm -f "${testfile}"
193
+ return "${ret}"
194
+}
195
+
196
+create_tmp_directory() {
197
+ if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
198
+ if _cannot_use_tmpdir /tmp; then
199
+ if _cannot_use_tmpdir "${PWD}"; then
200
+ 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."
201
+ else
202
+ TMPDIR="${PWD}"
203
+ fi
204
+ else
205
+ TMPDIR="/tmp"
206
+ fi
207
+ fi
208
+
209
+ mktemp -d -t netdata-kickstart-XXXXXXXXXX
210
+}
211
+
212
+download() {
213
+ url="${1}"
214
+ dest="${2}"
215
+ if command -v curl > /dev/null 2>&1; then
216
+ run curl -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
217
+ elif command -v wget > /dev/null 2>&1; then
218
+ run wget -T 15 -O "${dest}" "${url}" || return 1
219
+ elif command -v fetch > /dev/null 2>&1; then # Native FreeBSD tool
220
+ run fetch -T 15 -a -o "${dest}" "${url}" || return 1
221
+ else
222
+ fatal "I need curl, wget, or fetch to proceed, but none of them are available on this system."
223
+ fi
224
+}
225
+
226
+safe_sha256sum() {
227
+ # Within the context of the installer, we only use -c option that is common between the two commands
228
+ # We will have to reconsider if we start using non-common options
229
+ if command -v sha256sum > /dev/null 2>&1; then
230
+ sha256sum "$@"
231
+ elif command -v shasum > /dev/null 2>&1; then
232
+ shasum -a 256 "$@"
233
+ else
234
+ fatal "I could not find a suitable checksum binary to use"
235
+ fi
236
+}
237
+
238
+get_system_info() {
239
+ case "$(uname -s)" in
240
+ Linux)
241
+ SYSTYPE="Linux"
242
+
243
+ os_release_file=
244
+ if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
245
+ os_release_file="/etc/os-release"
246
+ elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
247
+ os_release_file="/usr/lib/os-release"
248
+ else
249
+ fatal "Cannot find an os-release file ..."
250
+ fi
251
+
252
+ # shellcheck disable=SC1090
253
+ . "${os_release_file}"
254
+
255
+ DISTRO="${ID}"
256
+ SYSVERSION="${VERSION_ID}"
257
+ SYSCODENAME="${VERSION_CODENAME}"
258
+ SYSARCH="$(uname -m)"
259
+
260
+ supported_compat_names="debian ubuntu centos fedora opensuse"
261
+
262
+ if str_in_list "${DISTRO}" "${supported_compat_names}"; then
263
+ DISTRO_COMPAT_NAME="${DISTRO}"
264
+ else
265
+ case "${DISTRO}" in
266
+ opensuse-leap)
267
+ DISTRO_COMPAT_NAME="opensuse"
268
+ ;;
269
+ rhel)
270
+ DISTRO_COMPAT_NAME="centos"
271
+ ;;
272
+ *)
273
+ DISTRO_COMPAT_NAME="unknown"
274
+ ;;
275
+ esac
276
+ fi
277
+ ;;
278
+ Darwin)
279
+ SYSTYPE="Darwin"
280
+ SYSVERSION="$(sw_vers -buildVersion)"
281
+ SYSARCH="$(uname -m)"
282
+ ;;
283
+ FreeBSD)
284
+ SYSTYPE="FreeBSD"
285
+ SYSVERSION="$(uname -K)"
286
+ SYSARCH="$(uname -m)"
287
+ ;;
288
+ *)
289
+ fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script."
290
+ ;;
291
+ esac
292
+}
293
+
294
+str_in_list() {
295
+ printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
296
+ return $?
297
+}
298
+
299
+confirm_root_support() {
300
+ if [ "$(id -u)" -ne "0" ]; then
301
+ if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
302
+ ROOTCMD="sudo"
303
+ fi
304
+
305
+ if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
306
+ ROOTCMD="doas"
307
+ fi
308
+
309
+ if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
310
+ ROOTCMD="pkexec"
311
+ fi
312
+
313
+ if [ -z "${ROOTCMD}" ]; then
314
+ fatal "We need root privileges to continue, but cannot find a way to gain them. Either re-run this script as root, or set \$ROOTCMD to a command that can be used to gain root privileges"
315
+ fi
316
+ fi
317
+}
318
+
319
+# ======================================================================
320
+# Existing install handling code
321
+
322
+update() {
323
+ updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
324
+
325
+ if [ -x "${updater}" ]; then
326
+ if run ${ROOTCMD} "${updater}" --not-running-from-cron; then
327
+ progress "Updated existing install at ${ndprefix}"
328
+ return 0
329
+ else
330
+ fatal "Failed to update existing Netdata install at ${ndprefix}"
331
+ fi
332
+ else
333
+ return 1
334
+ fi
335
+}
336
+
337
+handle_existing_install() {
338
+ if pkg_installed netdata; then
339
+ ndprefix="/"
340
+ else
341
+ ndpath="$(command -v netdata 2>/dev/null)"
342
+ if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
343
+ ndpath="/opt/netdata/bin/netdata"
344
+ fi
345
+
346
+ if [ -n "${ndpath}" ]; then
347
+ ndprefix="$(dirname "$(dirname "${ndpath}")")"
348
+ fi
349
+
350
+ if [ "${ndprefix}" = /usr ]; then
351
+ ndprefix="/"
352
+ fi
353
+ fi
354
+
355
+ if [ -n "${ndprefix}" ]; then
356
+ typefile="${ndprefix}/etc/netdata/.install-type"
357
+ if [ -r "${typefile}" ]; then
358
+ ${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
359
+ # shellcheck disable=SC1091
360
+ . "${tmpdir}/install-type"
361
+ else
362
+ INSTALL_TYPE="unknown"
363
+ fi
364
+ fi
365
+
366
+ if [ -z "${ndprefix}" ]; then
367
+ progress "No existing installations of netdata found, assuming this is a fresh install."
368
+ return 0
369
+ fi
370
+
371
+ case "${INSTALL_TYPE}" in
372
+ kickstart-*|legacy-*|manual-static)
373
+ if [ -n "${NETDATA_REINSTALL}" ]; then
374
+ progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
375
+ return 0
376
+ fi
377
+
378
+ ret=0
379
+
380
+ if [ "${NETDATA_CLAIM_ONLY}" -eq 0 ]; then
381
+ if ! update; then
382
+ warning "Unable to find usable updater script, not updating existing install at ${ndprefix}."
383
+ fi
384
+ fi
385
+
386
+ if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
387
+ progress "Attempting to claim existing install at ${ndprefix}."
388
+ claim
389
+ ret=$?
390
+ elif [ "${NETDATA_CLAIM_ONLY}" -eq 1 ]; then
391
+ fatal "User asked to claim, but did not proide a claiming token."
392
+ fi
393
+
394
+ exit $ret
395
+ ;;
396
+ binpkg-*)
397
+ ret=0
398
+
399
+ if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
400
+ progress "Attempting to claim existing install at ${ndprefix}."
401
+ claim
402
+ ret=$?
403
+ fi
404
+
405
+ exit $ret
406
+ ;;
407
+ oci)
408
+ fatal "This is an OCI container, use the regular image lifecycle management commands in your container instead of this script for managing it."
409
+ ;;
410
+ unknown)
411
+ warning "Found an existing netdata install at ${ndprefix}, but could not determine the install type."
412
+
413
+ if [ -n "${NETDATA_REINSTALL}" ]; then
414
+ progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
415
+ return 0
416
+ fi
417
+
418
+ ret=0
419
+
420
+ if [ "${NETDATA_CLAIM_ONLY}" -eq 0 ]; then
421
+ if ! update; then
422
+ warning "Unable to find usable updater script, not updating existing install at ${ndprefix}."
423
+ fi
424
+ fi
425
+
426
+ if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
427
+ progress "Attempting to claim existing install at ${ndprefix}."
428
+ claim
429
+ ret=$?
430
+ elif [ "${NETDATA_CLAIM_ONLY}" -eq 1 ]; then
431
+ fatal "User asked to claim, but did not proide a claiming token."
432
+ fi
433
+
434
+ exit $ret
435
+ ;;
436
+ *)
437
+ fatal "Found an existing netdata install at ${ndprefix}, but it is not a supported install type, refusing to proceed."
438
+ ;;
439
+ esac
440
+}
441
+
442
+# ======================================================================
443
+# Claiming support code
444
+
445
+check_claim_opts() {
446
+# shellcheck disable=SC2235,SC2030
447
+ if [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_ROOMS}" ]; then
448
+ fatal "Invalid claiming options, claim rooms may only be specified when a token and URL are specified."
449
+ elif [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_EXTRA}" ]; then
450
+ fatal "Invalid claiming options, a claiming token must be specified."
451
+ fi
452
+}
453
+
454
+claim() {
455
+ progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
456
+ if [ -z "${NETDATA_PREFIX}" ]; then
457
+ NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
458
+ elif [ "${NETDATA_PREFIX}" = "/opt/netdata" ]; then
459
+ NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
460
+ else
461
+ NETDATA_CLAIM_PATH="${NETDATA_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
462
+ fi
463
+
464
+ if ! pgrep netdata > /dev/null; then
465
+ NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
466
+ fi
467
+
468
+ # shellcheck disable=SC2086
469
+ if ${ROOTCMD} "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}; then
470
+ progress "Successfully claimed node"
471
+ else
472
+ warning "Unable to claim node, you must do so manually."
473
+ if [ -z "${NETDATA_NEW_INSTALL}" ]; then
474
+ exit 1
475
+ fi
476
+ fi
477
+}
478
+
479
+# ======================================================================
480
+# Native package install code.
481
+
482
+# Check for an already installed package with a given name.
483
+pkg_installed() {
484
+ case "${DISTRO_COMPAT_NAME}" in
485
+ debian|ubuntu)
486
+ dpkg -l "${1}" > /dev/null 2>&1
487
+ return $?
488
+ ;;
489
+ centos|fedora|opensuse)
490
+ rpm -q "${1}" > /dev/null 2>&1
491
+ return $?
492
+ ;;
493
+ *)
494
+ return 1
495
+ ;;
496
+ esac
497
+}
498
+
499
+# Check for the existence of a usable netdata package in the repo.
500
+netdata_avail_check() {
501
+ case "${DISTRO_COMPAT_NAME}" in
502
+ debian|ubuntu)
503
+ env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q packagecloud.io/netdata/netdata;
504
+ return $?
505
+ ;;
506
+ centos|fedora)
507
+ # shellcheck disable=SC2086
508
+ ${pm_cmd} search -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
509
+ return $?
510
+ ;;
511
+ opensuse)
512
+ zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
513
+ return $?
514
+ ;;
515
+ *)
516
+ return 1
517
+ ;;
518
+ esac
519
+}
520
+
521
+# Check for any distro-specific dependencies we know we need.
522
+check_special_native_deps() {
523
+ if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
524
+ progress "Checking for libuv availability."
525
+ # shellcheck disable=SC2086
526
+ if ${pm_cmd} search ${interactive_opts} -v libuv | grep -q "No matches found"; then
527
+ progress "libv not found, checking for EPEL availability."
528
+ # shellcheck disable=SC2086
529
+ if ${pm_cmd} search ${interactive_opts} -v epel-release | grep -q "No matches found"; then
530
+ warning "Unable to find a suitable source for libuv, cannot install on this system."
531
+ return 1
532
+ else
533
+ progress "EPEL is available, attempting to install so that required dependencies are available."
534
+
535
+ # shellcheck disable=SC2086
536
+ if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
537
+ warning "Failed to install EPEL."
538
+ return 1
539
+ fi
540
+ fi
541
+ else
542
+ return 0
543
+ fi
544
+ fi
545
+}
546
+
547
+try_package_install() {
548
+ if [ -z "${DISTRO}" ]; then
549
+ warning "Unable to determine Linux distribution for native packages."
550
+ return 1
551
+ fi
552
+
553
+ progress "Attempting to install using native packages..."
554
+
555
+ if [ "${RELEASE_CHANNEL}" = "nightly" ]; then
556
+ release="-edge"
557
+ else
558
+ release=""
559
+ fi
560
+
561
+ if [ "${INTERACTIVE}" = "0" ]; then
562
+ interactive_opts="-y"
563
+ env="DEBIAN_FRONTEND=noninteractive"
564
+ else
565
+ interactive_opts=""
566
+ env=""
567
+ fi
568
+
569
+ case "${DISTRO_COMPAT_NAME}" in
570
+ debian)
571
+ needs_early_refresh=1
572
+ pm_cmd="apt-get"
573
+ repo_subcmd="update"
574
+ repo_prefix="debian/${SYSCODENAME}"
575
+ pkg_type="deb"
576
+ pkg_suffix="_all"
577
+ pkg_vsep="_"
578
+ pkg_install_opts="${interactive_opts}"
579
+ repo_update_opts="${interactive_opts}"
580
+ uninstall_subcmd="uninstall"
581
+ ;;
582
+ ubuntu)
583
+ needs_early_refresh=1
584
+ pm_cmd="apt-get"
585
+ repo_subcmd="update"
586
+ repo_prefix="ubuntu/${SYSCODENAME}"
587
+ pkg_type="deb"
588
+ pkg_suffix="_all"
589
+ pkg_vsep="_"
590
+ pkg_install_opts="${interactive_opts}"
591
+ repo_update_opts="${interactive_opts}"
592
+ uninstall_subcmd="uninstall"
593
+ ;;
594
+ centos)
595
+ if command -v dnf > /dev/null; then
596
+ pm_cmd="dnf"
597
+ repo_subcmd="makecache"
598
+ else
599
+ pm_cmd="yum"
600
+ fi
601
+ repo_prefix="el/${SYSVERSION}"
602
+ pkg_type="rpm"
603
+ pkg_suffix=".noarch"
604
+ pkg_vsep="-"
605
+ pkg_install_opts="${interactive_opts}"
606
+ repo_update_opts="${interactive_opts}"
607
+ uninstall_subcmd="remove"
608
+ ;;
609
+ fedora)
610
+ if command -v dnf > /dev/null; then
611
+ pm_cmd="dnf"
612
+ repo_subcmd="makecache"
613
+ else
614
+ pm_cmd="yum"
615
+ fi
616
+ repo_prefix="fedora/${SYSVERSION}"
617
+ pkg_type="rpm"
618
+ pkg_suffix=".noarch"
619
+ pkg_vsep="-"
620
+ pkg_install_opts="${interactive_opts}"
621
+ repo_update_opts="${interactive_opts}"
622
+ uninstall_subcmd="remove"
623
+ ;;
624
+ opensuse)
625
+ pm_cmd="zypper"
626
+ repo_subcmd="--gpg-auto-import-keys refresh"
627
+ repo_prefix="opensuse/${SYSVERSION}"
628
+ pkg_type="rpm"
629
+ pkg_suffix=".noarch"
630
+ pkg_vsep="-"
631
+ pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
632
+ repo_update_opts=""
633
+ uninstall_subcmd="remove"
634
+ ;;
635
+ *)
636
+ warning "We do not provide native packages for ${DISTRO}."
637
+ return 2
638
+ ;;
639
+ esac
640
+
641
+ repoconfig_name="netdata-repo${release}"
642
+ repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_VERSION}${pkg_suffix}.${pkg_type}"
643
+ repoconfig_url="${REPOCONFIG_URL_PREFIX}/${repo_prefix}/${repoconfig_file}/download.${pkg_type}"
644
+
645
+ if ! pkg_installed "${repoconfig_name}"; then
646
+ progress "Downloading repository configuration package."
647
+ if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
648
+ warning "Failed to download repository configuration package."
649
+ return 2
650
+ fi
651
+
652
+ if [ -n "${needs_early_refresh}" ]; then
653
+ progress "Updating repository metadata."
654
+ # shellcheck disable=SC2086
655
+ if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
656
+ warning "Failed to refresh repository metadata."
657
+ return 2
658
+ fi
659
+ fi
660
+
661
+ progress "Installing repository configuration package."
662
+ # shellcheck disable=SC2086
663
+ if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
664
+ warning "Failed to install repository configuration package."
665
+ return 2
666
+ fi
667
+
668
+ if [ -n "${repo_subcmd}" ]; then
669
+ progress "Updating repository metadata."
670
+ # shellcheck disable=SC2086
671
+ if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
672
+ fatal "Failed to update repository metadata."
673
+ fi
674
+ fi
675
+ else
676
+ progress "Repository configuration is already present, attempting to install netdata."
677
+ fi
678
+
679
+ if ! check_special_native_deps; then
680
+ warning "Could not find secondary dependencies ${DISTRO} on ${SYSARCH}."
681
+ if [ -z "${NO_CLEANUP}" ]; then
682
+ progress "Attempting to uninstall repository configuration package."
683
+ # shellcheck disable=SC2086
684
+ run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
685
+ fi
686
+ return 2
687
+ fi
688
+
689
+ progress "Checking for usable Netdata package."
690
+ if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
691
+ warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
692
+ if [ -z "${NO_CLEANUP}" ]; then
693
+ progress "Attempting to uninstall repository configuration package."
694
+ # shellcheck disable=SC2086
695
+ run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
696
+ fi
697
+ return 2
698
+ fi
699
+
700
+ progress "Installing Netdata package."
701
+ # shellcheck disable=SC2086
702
+ if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} netdata; then
703
+ warning "Failed to install Netdata package."
704
+ if [ -z "${NO_CLEANUP}" ]; then
705
+ progress "Attempting to uninstall repository configuration package."
706
+ # shellcheck disable=SC2086
707
+ run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
708
+ fi
709
+ return 2
710
+ fi
711
+}
712
+
713
+# ======================================================================
714
+# Static build install code
715
+
716
+set_static_archive_urls() {
717
+ if [ "${RELEASE_CHANNEL}" = "stable" ]; then
718
+ latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
719
+ export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${SYSARCH}-${latest}.gz.run"
720
+ export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
721
+ else
722
+ export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/netdata-latest.gz.run"
723
+ export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/sha256sums.txt"
724
+ fi
725
+}
726
+
727
+try_static_install() {
728
+ set_static_archive_urls "${RELEASE_CHANNEL}"
729
+ progress "Downloading static netdata binary: ${NETDATA_STATIC_ARCHIVE_URL}"
730
+
731
+ if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/netdata-${SYSARCH}-latest.gz.run"; then
732
+ warning "Unable to download static build archive for ${SYSARCH}."
733
+ return 2
734
+ fi
735
+
736
+ if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
737
+ fatal "Unable to fetch checksums to verify static build archive."
738
+ fi
739
+
740
+ if ! grep "netdata-${SYSARCH}-latest.gz.run" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
741
+ fatal "Static binary checksum validation failed. Stopping Netdata Agent installation and leaving binary in ${tmpdir}. 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."
742
+ fi
743
+
744
+ if [ "${INTERACTIVE}" -eq 0 ]; then
745
+ opts="${opts} --accept"
746
+ fi
747
+
748
+ progress "Installing netdata"
749
+ # shellcheck disable=SC2086
750
+ if ! run ${ROOTCMD} sh "${tmpdir}/netdata-${SYSARCH}-latest.gz.run" ${opts} -- ${NETDATA_AUTO_UPDATES:+--auto-update} ${NETDATA_INSTALLER_OPTIONS}; then
751
+ fatal "Failed to install static build of Netdata on ${SYSARCH}."
752
+ fi
753
+
754
+ install_type_file="/opt/netdata/etc/netdata/.install-type"
755
+ if [ -f "${install_type_file}" ]; then
756
+ ${ROOTCMD} sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
757
+ ${ROOTCMD} chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
758
+ # shellcheck disable=SC1091
759
+ . "${tmpdir}/install-type"
760
+ cat > "${tmpdir}/install-type" <<- EOF
761
+ INSTALL_TYPE='kickstart-static'
762
+ PREBUILT_ARCH='${PREBUILT_ARCH}'
763
+ EOF
764
+ ${ROOTCMD} chown netdata:netdata "${tmpdir}/install-type"
765
+ ${ROOTCMD} cp "${tmpdir}/install-type" "${install_type_file}"
766
+ fi
767
+}
768
+
769
+# ======================================================================
770
+# Main program
771
+
772
+setup_terminal || echo > /dev/null
773
+
774
+while [ -n "${1}" ]; do
775
+ case "${1}" in
776
+ "--help")
777
+ usage
778
+ exit 0
779
+ ;;
780
+ "--no-cleanup") NO_CLEANUP=1 ;;
781
+ "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
782
+ "--interactive") INTERACTIVE=1 ;;
783
+ "--stable-channel") RELEASE_CHANNEL="stable" ;;
784
+ "--no-updates") NETDATA_AUTO_UPDATES="" ;;
785
+ "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
786
+ "--disable-telemetry") NETDATA_DISABLE_TELEMETRY="1" ;;
787
+ "--reinstall") NETDATA_REINSTALL=1 ;;
788
+ "--claim-only") NETDATA_CLAIM_ONLY=1 ;;
789
+ "--native-only")
790
+ NETDATA_ONLY_NATIVE=1
791
+ NETDATA_ONLY_STATIC=0
792
+ ;;
793
+ "--static-only")
794
+ NETDATA_ONLY_STATIC=1
795
+ NETDATA_ONLY_NATIVE=0
796
+ ;;
797
+ "--dont-start-it")
798
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
799
+ ;;
800
+ "--claim-token")
801
+ NETDATA_CLAIM_TOKEN="${2}"
802
+ shift 1
803
+ ;;
804
+ "--claim-rooms")
805
+ NETDATA_CLAIM_ROOMS="${2}"
806
+ shift 1
807
+ ;;
808
+ "--claim-url")
809
+ NETDATA_CLAIM_URL="${2}"
810
+ shift 1
811
+ ;;
812
+ "--claim-*")
813
+ optname="$(echo "${1}" | cut -d '-' -f 4-)"
814
+ case "${optname}" in
815
+ id|proxy|user|hostname)
816
+ NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname} ${2}"
817
+ shift 1
818
+ ;;
819
+ verbose|insecure|noproxy|noreload|daemon-not-running)
820
+ NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}"
821
+ ;;
822
+ *)
823
+ warning "Ignoring unrecognized claiming option ${optname}"
824
+ ;;
825
+ esac
826
+ ;;
827
+ esac
828
+ shift 1
829
+done
830
+
831
+check_claim_opts
832
+confirm_root_support
833
+get_system_info
834
+
835
+tmpdir="$(create_tmp_directory)"
836
+progress "Using ${tmpdir} as a temporary directory."
837
+cd "${tmpdir}" || exit 1
838
+
839
+handle_existing_install
840
+
841
+case "${SYSTYPE}" in
842
+ Linux)
843
+ if [ "${NETDATA_ONLY_STATIC}" -ne 1 ]; then
844
+ try_package_install
845
+
846
+ case "$?" in
847
+ 0)
848
+ NETDATA_INSTALL_SUCCESSFUL=1
849
+ ;;
850
+ 1)
851
+ fatal "Unable to install on this system."
852
+ ;;
853
+ 2)
854
+ if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
855
+ fatal "Could not install native binary packages."
856
+ else
857
+ warning "Could not install native binary packages, falling back to alternative installation method."
858
+ fi
859
+ ;;
860
+ esac
861
+ fi
862
+
863
+ if [ "${NETDATA_ONLY_NATIVE}" -ne 1 ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
864
+ try_static_install
865
+
866
+ case "$?" in
867
+ 0)
868
+ NETDATA_INSTALL_SUCCESSFUL=1
869
+ NETDATA_USER_CONFIG_DIR="/opt/netdata/etc/netdata"
870
+ ;;
871
+ 1)
872
+ fatal "Unable to install on this system."
873
+ ;;
874
+ 2)
875
+ if [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
876
+ fatal "Could not install static build."
877
+ else
878
+ warning "Could not install static build, falling back to alternative installation method."
879
+ fi
880
+ ;;
881
+ esac
882
+ fi
883
+ ;;
884
+ Darwin)
885
+ if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
886
+ fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option."
887
+ elif [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
888
+ fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option."
889
+ else
890
+ fatal "This script currently does not support installation on macOS."
891
+ fi
892
+ ;;
893
+ FreeBSD)
894
+ if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
895
+ fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option."
896
+ elif [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
897
+ fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option."
898
+ else
899
+ fatal "This script currently does not support installation on FreeBSD."
900
+ fi
901
+ ;;
902
+esac
903
+
904
+if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
905
+ run ${ROOTCMD} touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
906
+fi
907
+
908
+if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
909
+ claim
910
+fi
911
+
912
+if [ -z "${NO_CLEANUP}" ]; then
913
+ ${ROOTCMD} rm -rf "${tmpdir}"
914
+fi