Repository navigation
Expand file tree
/
Copy pathnetdata-updater.sh
More file actions
executable file
·1728 lines (1479 loc) · 55.4 KB
/
Copy pathnetdata-updater.sh
File metadata and controls
executable file
·1728 lines (1479 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Netdata updater utility
#
# Variables needed by script:
# - PATH
# - CFLAGS
# - LDFLAGS
# - MAKEOPTS
# - IS_NETDATA_STATIC_BINARY
# - NETDATA_CONFIGURE_OPTIONS
# - REINSTALL_OPTIONS
# - NETDATA_TARBALL_CHECKSUM
# - NETDATA_PREFIX
# - NETDATA_LIB_DIR
#
# Optional environment options:
#
# - TMPDIR (set to a usable temporary directory)
# - NETDATA_BASE_URL (legacy release/repository base URL override)
# - NETDATA_ARTIFACT_URL_PREFIX (override the release artifact directory)
# Next unused error code: U002E
set -e
PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
NETDATA_STABLE_BASE_URL="${NETDATA_BASE_URL:-https://github.com/netdata/netdata/releases}"
NETDATA_NIGHTLY_BASE_URL="${NETDATA_BASE_URL:-https://github.com/netdata/netdata-nightlies/releases}"
NETDATA_STABLE_REPO_URL="${NETDATA_BASE_URL:-https://repository.netdata.cloud/repos/stable}"
NETDATA_NIGHTLY_REPO_URL="${NETDATA_BASE_URL:-https://repository.netdata.cloud/repos/edge}"
RELEASE_ARTIFACT_URL_PREFIX="https://artifacts.netdata.cloud"
NETDATA_DEFAULT_ACCEPT_MAJOR_VERSIONS="1 2"
# Following variables are intended to be overridden by the updater config file.
NETDATA_UPDATER_JITTER=3600
NETDATA_NO_SYSTEMD_JOURNAL=0
NETDATA_ACCEPT_MAJOR_VERSIONS=''
script_dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
if [ -x "${script_dir}/netdata-updater" ]; then
script_source="${script_dir}/netdata-updater"
else
script_source="${script_dir}/netdata-updater.sh"
fi
PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
if [ ! -t 1 ]; then
INTERACTIVE=0
else
INTERACTIVE=1
fi
if [ -n "${script_source}" ]; then
script_name="$(basename "${script_source}")"
else
script_name="netdata-updater.sh"
fi
info() {
echo >&3 "$(date) : INFO: ${script_name}: " "${1}"
}
warning() {
echo >&3 "$(date) : WARNING: ${script_name}: " "${@}"
}
error() {
echo >&3 "$(date) : ERROR: ${script_name}: " "${1}"
if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
fi
}
fatal() {
echo >&3 "$(date) : FATAL: ${script_name}: FAILED TO UPDATE NETDATA: " "${1}"
if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
fi
exit_reason "${1}" "${2}"
exit 1
}
exit_reason() {
if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
EXIT_REASON="${1}"
EXIT_CODE="${2}"
if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
if [ -n "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
{
echo "EXIT_REASON=\"${EXIT_REASON}\""
echo "EXIT_CODE=\"${EXIT_CODE}\""
echo "NETDATA_WARNINGS=\"${NETDATA_WARNINGS}\""
} >> "${NETDATA_SCRIPT_STATUS_PATH}"
else
export EXIT_REASON
export EXIT_CODE
export NETDATA_WARNINGS
fi
fi
fi
}
is_integer () {
case "${1#[+-]}" in
*[!0123456789]*) return 1 ;;
'') return 1 ;;
*) return 0 ;;
esac
}
safe_pidof() {
pidof_cmd="$(command -v pidof 2> /dev/null)"
if [ -n "${pidof_cmd}" ]; then
${pidof_cmd} "${@}"
return $?
else
ps -acxo pid,comm |
sed "s/^ *//g" |
grep netdata |
cut -d ' ' -f 1
return $?
fi
}
issystemd() {
# if there is no systemctl command, it is not systemd
systemctl=$(command -v systemctl 2> /dev/null)
if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
return 1
fi
# Check the output of systemctl is-system-running.
# If this reports 'offline', it’s not systemd. If it reports 'unknown'
# or nothing at all (which indicates the command is not supported), it
# may or may not be systemd, so continue to other checks. If it reports
# anything else, it is systemd.
#
# This may return a non-zero exit status in cases when it actually
# succeeded for our purposes (notably, if the state is `degraded`),
# so we need to toggle set -e off here.
set +e
systemd_state="$(systemctl is-system-running)"
set -e
case "${systemd_state}" in
offline) return 1 ;;
unknown) : ;;
"") : ;;
*) return 0 ;;
esac
# if pid 1 is systemd, it is systemd
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
# if systemd is not running, it is not systemd
pids=$(safe_pidof systemd 2> /dev/null)
[ -z "${pids}" ] && return 1
# check if the running systemd processes are not in our namespace
myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
for p in ${pids}; do
ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
# if pid of systemd is in our namespace, it is systemd
[ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
done
# else, it is not systemd
return 1
}
systemd_unit_exists() {
if systemctl list-unit-files "${1}" 2>&1 | tail -n 1 | grep -qv '^0 '; then
return 0
else
return 1
fi
}
# shellcheck disable=SC2009
running_under_anacron() {
pid="${1:-$$}"
iter="${2:-0}"
[ "${iter}" -gt 50 ] && return 1
if [ "$(uname -s)" = "Linux" ] && [ -r "/proc/${pid}/stat" ]; then
ppid="$(cut -f 4 -d ' ' "/proc/${pid}/stat")"
if [ -n "${ppid}" ]; then
# The below case accounts for the hidepid mount option for procfs, as well as setups with LSM
[ ! -r "/proc/${ppid}/comm" ] && return 1
[ "${ppid}" -eq "${pid}" ] && return 1
grep -q anacron "/proc/${ppid}/comm" && return 0
running_under_anacron "${ppid}" "$((iter + 1))"
return "$?"
fi
else
ppid="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *${pid}" | xargs | cut -f 2 -d ' ')"
if [ -n "${ppid}" ]; then
[ "${ppid}" -eq "${pid}" ] && return 1
ps -o pid= -o command= 2>/dev/null | grep -e "^ *${ppid}" | grep -q anacron && return 0
running_under_anacron "${ppid}" "$((iter + 1))"
return "$?"
fi
fi
return 1
}
_get_intervaldir() {
if [ -d /etc/cron.daily ]; then
echo /etc/cron.daily
elif [ -d /etc/periodic/daily ]; then
echo /etc/periodic/daily
else
return 1
fi
return 0
}
_get_scheduler_type() {
if _get_intervaldir > /dev/null ; then
echo 'interval'
elif issystemd ; then
echo 'systemd'
elif [ -d /etc/cron.d ] ; then
echo 'crontab'
else
echo 'none'
fi
}
confirm() {
prompt="${1} [y/n]"
while true; do
echo "${prompt}"
read -r yn
case "$yn" in
[Yy]*) return 0;;
[Nn]*) return 1;;
*) echo "Please answer yes or no.";;
esac
done
}
warn_major_update() {
nmv_suffix="New major versions generally involve breaking changes, and may not work in the same way as older versions."
if [ "${INTERACTIVE}" -eq 0 ]; then
warning "Would update to a new major version of Netdata. ${nmv_suffix}"
warning "To install the new major version anyway, either run the updater interactively, or include the new major version number in the NETDATA_ACCEPT_MAJOR_VERSIONS variable in ${UPDATER_CONFIG_PATH}."
fatal "Aborting update to new major version to avoid breaking things." U001B
else
warning "This update will install a new major version of Netdata. ${nmv_suffix}"
if confirm "Are you sure you want to update to a new major version of Netdata?"; then
notice "User accepted update to new major version of Netdata."
else
fatal "Aborting update to new major version at user request." U001C
fi
fi
}
install_build_dependencies() {
bash="$(command -v bash 2> /dev/null)"
if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
error "Unable to find a usable version of \`bash\` (required for local build)."
return 1
fi
info "Fetching dependency handling script..."
download "${PACKAGES_SCRIPT}" "./install-required-packages.sh" || true
if [ ! -s "./install-required-packages.sh" ]; then
error "Downloaded dependency installation script is empty."
else
info "Running dependency handling script..."
opts="--dont-wait --non-interactive"
# shellcheck disable=SC2086
if ! "${bash}" "./install-required-packages.sh" ${opts} netdata >&3 2>&3; then
error "Installing build dependencies failed. The update should still work, but you might be missing some features."
fi
fi
}
# Certain versions of this script had a bug that could cause /dev/null
# to be removed mistakenly under some circumstances.
#
# This function attempts to detect and fix the resulting situation.
#
# Fix limited to Linux for the moment because apparently trying to
# proactively fix systems that we aren’t certain have been affected is
# too invasive of a change for some people...
dev_null_fix() {
if [ -f /dev/null ] || [ ! -e /dev/null ]; then
case "$(uname -s)" in
Linux)
rm -f /dev/.null
mknod -m 666 /dev/.null c 1 3
# Some distros use ownership other than root:root for /dev/null,
# but it almost always matches the ownership of /dev/full,
# so if possible copy the onwership from there.
if [ -c /dev/full ]; then
chown --reference=/dev/full /dev/.null
fi
mv -f /dev/.null /dev/null
# If the system seems to be using SELinux, apply the correct
# security context to the new /dev/null.
#
# This check doesn’t use /dev/null as trying to access it
# without the right security context being set may fail.
dummy_null="$(mktemp)"
if command -v restorecon >"${dummy_null}" 2>&1; then
restorecon /dev/null
fi
rm -f "${dummy_null}" || true # Cleanup from the above check
;;
*) ;;
esac
fi
}
enable_netdata_updater() {
updater_type="$(echo "${1}" | tr '[:upper:]' '[:lower:]')"
case "${updater_type}" in
systemd|interval|crontab)
updater_type="${1}"
;;
"")
updater_type="$(_get_scheduler_type)"
;;
*)
fatal "Unrecognized updater type ${updater_type} requested. Supported types are 'systemd', 'interval', and 'crontab'." U0001
;;
esac
case "${updater_type}" in
"systemd")
if issystemd; then
if systemd_unit_exists netdata-updater.timer; then
systemctl enable netdata-updater.timer
systemctl start netdata-updater.timer
info "Auto-updating has been ENABLED using a systemd timer unit.\n"
info "If the update process fails, the failure will be logged to the systemd journal just like a regular service failure."
info "Successful updates should produce empty logs."
else
error "Systemd-based auto-update scheduling requested, but the required timer unit does not exist. Auto-updates have NOT been enabled."
return 1
fi
else
error "Systemd-based auto-update scheduling requested, but this does not appear to be a systemd system. Auto-updates have NOT been enabled."
return 1
fi
;;
"interval")
if _get_intervaldir > /dev/null; then
ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_intervaldir)/netdata-updater"
info "Auto-updating has been ENABLED through cron, updater script linked to $(_get_intervaldir)/netdata-updater\n"
info "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
info "Successful updates will not send an email."
else
error "Interval-based auto-update scheduling requested, but I could not find an interval scheduling directory. Auto-updates have NOT been enabled."
return 1
fi
;;
"crontab")
if [ -d "/etc/cron.d" ]; then
[ -f "/etc/cron.d/netdata-updater" ] && rm -f "/etc/cron.d/netdata-updater"
install -p -m 0644 -o 0 -g 0 "${NETDATA_PREFIX}/usr/lib/netdata/system/cron/netdata-updater-daily" "/etc/cron.d/netdata-updater-daily"
info "Auto-updating has been ENABLED through cron, using a crontab at /etc/cron.d/netdata-updater\n"
info "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
info "Successful updates will not send an email."
else
error "Crontab-based auto-update scheduling requested, but there is no '/etc/cron.d'. Auto-updates have NOT been enabled."
return 1
fi
;;
*)
error "Unable to determine what type of auto-update scheduling to use. Auto-updates have NOT been enabled."
return 1
esac
return 0
}
disable_netdata_updater() {
if issystemd && systemd_unit_exists "netdata-updater.timer" ; then
systemctl disable netdata-updater.timer
systemctl stop netdata-updater.timer
fi
if [ -d /etc/cron.daily ]; then
rm -f /etc/cron.daily/netdata-updater.sh
rm -f /etc/cron.daily/netdata-updater
fi
if [ -d /etc/periodic/daily ]; then
rm -f /etc/periodic/daily/netdata-updater.sh
rm -f /etc/periodic/daily/netdata-updater
fi
if [ -d /etc/cron.d ]; then
rm -f /etc/cron.d/netdata-updater
rm -f /etc/cron.d/netdata-updater-daily
fi
info "Auto-updates have been DISABLED."
return 0
}
auto_update_status() {
case "$(_get_scheduler_type)" in
systemd) info "The default auto-update scheduling method for this system is: systemd timer units" ;;
crontab) info "The default auto-update scheduling method for this system is: drop-in crontab" ;;
interval) info "The default auto-update scheduling method for this system is: drop-in periodic script" ;;
*) info "No recognized auto-update scheduling method found" ; return ;;
esac
duplicate=""
enabled=""
if issystemd; then
if systemd_unit_exists "netdata-updater.timer"; then
if systemctl is-enabled netdata-updater.timer; then
info "Auto-updates using a systemd timer unit are ENABLED"
enabled="systemd"
else
info "Auto-updates using a systemd timer unit are DISABLED"
fi
else
info "Auto-updates using a systemd timer unit are NOT SUPPORTED due to: Required unit files not installed"
fi
else
info "Auto-updates using a systemd timer unit are NOT SUPPORTED due to: Systemd not present"
fi
interval_found=""
if [ -d /etc/cron.daily ]; then
interval_found="1"
if [ -x /etc/cron.daily/netdata-updater.sh ] || [ -x /etc/cron.daily/netdata-updater ]; then
info "Auto-updates using a drop-in periodic script in /etc/cron.daily are ENABLED"
if [ -n "${enabled}" ]; then
duplicate="1"
else
enabled="cron.daily"
fi
else
info "Auto-updates using a drop-in periodic script in /etc/cron.daily are DISABLED"
fi
else
info "Auto-updates using a drop-in periodic script in /etc/cron.daily are NOT SUPPORTED: due to: Directory does not exist"
fi
if [ -d /etc/periodic/daily ]; then
if [ -x /etc/periodic/daily/netdata-updater.sh ] || [ -x /etc/periodic/daily/netdata-updater ]; then
info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are ENABLED"
if [ -n "${enabled}" ]; then
duplicate="1"
else
enabled="periodic/daily"
fi
else
if [ -z "${interval_found}" ]; then
info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are DISABLED"
fi
fi
elif [ -z "${interval_found}" ]; then
info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are NOT SUPPORTED due to: Directory does not exist"
fi
if [ -d /etc/cron.d ]; then
if [ -f /etc/cron.d/netdata-updater ] || [ -f /etc/cron.d/netdata-updater-daily ]; then
info "Auto-updates using a drop-in crontab are ENABLED"
if [ -n "${enabled}" ]; then
duplicate="1"
else
enabled="cron.d"
fi
else
info "Auto-updates using a drop-in crontab are DISABLED"
fi
else
info "Auto-updates using a drop-in crontab are NOT SUPPORTED due to: Directory does not exist"
fi
if [ -n "${duplicate}" ]; then
warning "More than one method of auto-updates is enabled! Please disable and re-enable auto-updates to correct this."
fi
}
str_in_list() {
printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
return $?
}
safe_sha256sum() {
# Within the context of the installer, we only use -c option that is common between the two commands
# We will have to reconsider if we start non-common options
if command -v shasum > /dev/null 2>&1; then
shasum -a 256 "$@"
elif command -v sha256sum > /dev/null 2>&1; then
sha256sum "$@"
else
fatal "I could not find a suitable checksum binary to use" U0002
fi
}
cleanup() {
if [ -n "${logfile}" ]; then
cat >&2 "${logfile}"
rm "${logfile}"
fi
if [ -n "$ndtmpdir" ] && [ -d "$ndtmpdir" ]; then
rm -rf "$ndtmpdir"
fi
}
_cannot_use_tmpdir() {
testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
ret=0
if [ -z "${testfile}" ] ; then
return "${ret}"
fi
if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}" ; then
if chmod +x "${testfile}" ; then
if [ "$("${testfile}" 2>/dev/null)" = "SUCCESS" ] ; then
ret=1
fi
fi
fi
rm -f "${testfile}"
return "${ret}"
}
create_exec_tmp_directory() {
if [ -z "${ndtmpdir}" ]; then
if [ -n "${NETDATA_TMPDIR_PATH}" ]; then
ndtmpdir="${NETDATA_TMPDIR_PATH}"
else
root_dir=""
if [ -n "${NETDATA_TMPDIR}" ] && ! _cannot_use_tmpdir "${NETDATA_TMPDIR}"; then
root_dir="${NETDATA_TMPDIR}"
elif [ -n "${TMPDIR}" ] && ! _cannot_use_tmpdir "${TMPDIR}"; then
root_dir="${TMPDIR}"
elif ! _cannot_use_tmpdir /tmp; then
root_dir="/tmp"
elif ! _cannot_use_tmpdir "${PWD}"; then
root_dir="${PWD}"
else
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." U0003
fi
TMPDIR="${root_dir}"
ndtmpdir="$(mktemp -d -p "${root_dir}" -t netdata-updater-XXXXXXXXXX)"
fi
fi
info "Putting temporary files in ${ndtmpdir}"
}
check_for_curl() {
if [ -z "${curl}" ]; then
curl="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null || true)"
fi
}
check_for_wget() {
if [ -z "${wget}" ]; then
wget="$(command -v wget 2>/dev/null || true)"
fi
if [ -z "${wget}" ]; then
wget="$(command -v wget2 2>/dev/null || true)"
fi
}
_safe_download() {
url="${1}"
dest="${2}"
if echo "${url}" | grep -Eq "^file:///"; then
cp "${url#file://}" "${dest}" || return 1
return 0
fi
check_for_curl
check_for_wget
create_exec_tmp_directory
dl_log="${ndtmpdir}/download.log"
rm -f "${dl_log}"
if [ -n "${curl}" ]; then
set +e
"${curl}" --silent --fail --location --write-out "%{http_code}" --connect-timeout 10 --retry 3 "${url}" --output "${dest}" > "${dl_log}"
result="$?"
set -e
case "${result}" in
0) return 0 ;;
22|56|78)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
case "$(tail -n 1 "${dl_log}")" in
404) return 1 ;;
4*) return 5 ;;
5*) return 6 ;;
*) return 4 ;;
esac
;;
5|6|7)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 2
;;
35|60|83)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 3
;;
*)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 4
;;
esac
elif [ -n "${wget}" ]; then
set +e
"${wget}" -T 15 -S -o "${dl_log}" -O "${dest}" "${url}"
result="$?"
set -e
case "${result}" in
0) return 0 ;;
8)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
case "$(grep "HTTP/" "${dl_log}" | tail -n 1 | awk '{ print $2 }')" in
404) return 1 ;;
4*) return 5 ;;
5*) return 6 ;;
*) return 4 ;;
esac
;;
4)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 2
;;
5)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 3
;;
*)
[ "${dest}" != "/dev/null" ] && rm -f "${dest}"
return 4
;;
esac
fi
return 255
}
download() {
url="${1}"
dest="${2}"
set +e
_safe_download "${url}" "${dest}"
ret=$?
set -e
case "${ret}" in
0) return 0 ;;
1) fatal "File ${url} not found on remote server" U0022 ;;
2) fatal "Unable to connect to remote host to download ${url}" U0023 ;;
3) fatal "TLS error connecting to remote host to download ${url}" U0024 ;;
5) fatal "Client error when trying to download ${url}" U0027 ;;
6) fatal "Internal server error when trying to download ${url}" U0028 ;;
255) fatal "I need curl or wget to proceed, but neither is available on this system." U0004 ;;
*) fatal "Cannot download ${url}" U0005 ;;
esac
}
check_for_remote_file() {
url="${1}"
succeeded=0
checked=0
if echo "${url}" | grep -Eq "^file:///"; then
[ -e "${url#file://}" ] || return 1
return 0
elif [ -n "${NETDATA_ASSUME_REMOTE_FILES_ARE_PRESENT}" ]; then
return 0
fi
check_for_curl
if [ -n "${curl}" ]; then
checked=1
if "${curl}" --output /dev/null --silent --head --fail "${url}"; then
succeeded=1
fi
fi
if [ "${succeeded}" -eq 0 ]; then
if command -v wget > /dev/null 2>&1; then
checked=1
if wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK'; then
succeeded=1
fi
fi
fi
if [ "${succeeded}" -eq 1 ]; then
return 0
elif [ "${checked}" -eq 1 ]; then
return 1
else
return 255
fi
}
get_netdata_latest_tag() {
url="${1}/latest"
check_for_curl
check_for_wget
if [ -z "${curl}" ] && [ -z "${wget}" ]; then
fatal "I need curl or wget to proceed, but neither of them are available on this system." U0006
fi
if [ -n "${curl}" ]; then
tag=$("${curl}" "${url}" -s -L -I -o /dev/null -w '%{url_effective}' || true)
fi
if [ -z "${tag}" ]; then
if [ -n "${wget}" ]; then
tag=$("${wget}" -S -O /dev/null "${url}" 2>&1 | grep Location || true)
fi
fi
if [ -z "${tag}" ]; then
tag='latest'
fi
tag="$(echo "${tag}" | grep -Eom 1 '[^/]*/?$')"
# Fallback case for simpler local testing.
if echo "${tag}" | grep -Eq 'latest/?$'; then
set +e
_safe_download "${url}/latest-version.txt" ./ndupdate-version.txt
result="$?"
set -e
case "${result}" in
0) tag="$(cat ./ndupdate-version.txt)" ;;
*) tag='latest' ;;
esac
rm -f ./ndupdate-version.txt
fi
echo "${tag}"
}
newer_commit_date() {
info "Checking if a newer version of the updater script is available."
create_exec_tmp_directory
commit_check_file="${ndtmpdir}/latest-commit.json"
commit_check_url="https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1"
python_version_check="
from __future__ import print_function
import sys, json
try:
data = json.load(sys.stdin)
except:
print('')
else:
print(data[0]['commit']['committer']['date'] if isinstance(data, list) and data else '')
"
if ! _safe_download "${commit_check_url}" "${commit_check_file}"; then
warning "Failed to check for an updated updater script, skipping self-update check."
rm -f "${commit_check_file}" 2>/dev/null || true
return 1
fi
if command -v jq > /dev/null 2>&1; then
commit_date="$(jq '.[0].commit.committer.date' 2>/dev/null < "${commit_check_file}" | tr -d '"')"
elif command -v python > /dev/null 2>&1;then
commit_date="$(python -c "${python_version_check}" < "${commit_check_file}")"
elif command -v python3 > /dev/null 2>&1;then
commit_date="$(python3 -c "${python_version_check}" < "${commit_check_file}")"
fi
if [ -z "${NETDATA_TMPDIR_PATH}" ]; then
rm -f "${commit_check_file}" 2>/dev/null || true
fi
if [ -z "${commit_date}" ] ; then
return 0
elif [ "$(uname)" = "Linux" ]; then
commit_date="$(date -d "${commit_date}" +%s)"
else # assume BSD-style `date` if we are not on Linux
commit_date="$(/bin/date -j -f "%Y-%m-%dT%H:%M:%SZ" "${commit_date}" +%s 2>/dev/null)"
if [ -z "${commit_date}" ]; then
return 0
fi
fi
if [ -e "${script_source}" ]; then
script_date="$(date -r "${script_source}" +%s)"
else
script_date="$(date +%s)"
fi
[ "${commit_date}" -ge "${script_date}" ]
}
self_update() {
if [ -z "${NETDATA_NO_UPDATER_SELF_UPDATE}" ] && newer_commit_date; then
info "Downloading newest version of updater script."
create_exec_tmp_directory
cd "${ndtmpdir}" || exit 1
if _safe_download "${RELEASE_ARTIFACT_URL_PREFIX}/installer/netdata-updater.sh" ./netdata-updater.sh ||
_safe_download "https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-updater.sh" ./netdata-updater.sh; then
chmod +x ./netdata-updater.sh || exit 1
export ENVIRONMENT_FILE="${ENVIRONMENT_FILE}"
cmd="./netdata-updater.sh --not-running-from-cron --no-updater-self-update"
[ "$NETDATA_FORCE_UPDATE" = "1" ] && cmd="$cmd --force-update"
[ "$INTERACTIVE" = "0" ] && cmd="$cmd --non-interactive"
cmd="$cmd --tmpdir-path $(pwd)"
exec $cmd
else
error "Failed to download newest version of updater script, continuing with current version."
fi
fi
}
# Parse the version into a (large) integer for easy comparison.
#
# The resultant integer consists of three groups of three digits encoding the major, minor, and patch versions,
# followed by a single digit encoding the version type (0 for nightly builds and git builds based on a stable or
# nightly version, 1 for release candidate builds, 2 for git builds based on a release candidate version, 3 for
# stable versions, and 9 for unknown), three digits for the release candidate number (all zeroes if the build
# isn’t a release candidate build), and five digits for the commit number for git builds (all zeroes if the
# build isn’t a git build).
parse_version() {
r="${1}"
vmax="999999999999999999"
if [ "${r}" = "latest" ]; then
# If we get ‘latest’ as a version, return the largest possible
# version value.
printf "%s" "${vmax}"
return 0
elif echo "${r}" | grep -q '^v.*'; then
# shellcheck disable=SC2001
# XXX: Need a regex group substitution here.
r="$(echo "${r}" | sed -e 's/^v\(.*\)/\1/')"
fi
tmpfile="$(mktemp)"
echo "${r}" | tr '-' ' ' > "${tmpfile}"
read -r v b1 b2 _ < "${tmpfile}"
if echo "${b1}" | grep -Eq "^rc[0-9]+$"; then
rc="$(echo "${b1}" | tr -d 'rc')"
if echo "${b2}" | grep -Eq "^[0-9]+$"; then
t=2
b="${b2}"
elif [ -z "${b2}" ]; then
t=1
b=0
else
t=9
b=99999
fi
elif echo "${b1}" | grep -Eq "^[0-9]+$"; then
t=0
rc=0
b="${b1}"
elif [ -z "${b1}" ]; then
t=3
rc=0
b=0
else
t=9
rc=999
b=99999
fi
echo "${v}" | tr '.' ' ' > "${tmpfile}"
read -r maj min patch _ < "${tmpfile}"
rm -f "${tmpfile}"
if [ "${#maj}" -gt 3 ] || [ "${#min}" -gt 3 ] || [ "${#patch}" -gt 3 ] || [ "${#rc}" -gt 3 ] || [ "${#b}" -gt 5 ]; then
warning "Failed to parse version ${r}"
printf "%s" "${vmax}"
return 0
fi
printf "%03d%03d%03d%01d%03d%05d" "${maj}" "${min}" "${patch}" "${t}" "${rc}" "${b}"
}
get_latest_tag() {
if [ -z "${_latest_tag}" ]; then
# A selected Manifest source also supplies Version. Do not silently compare
# against a different release source if its metadata cannot be downloaded.
if [ "${NETDATA_MANIFEST_INSTALL:-0}" -eq 1 ]; then
download "${NETDATA_TARBALL_URL_PREFIX}/Version" "${ndtmpdir}/Version"
_latest_tag="$(cat "${ndtmpdir}/Version")"
[ -n "${_latest_tag}" ] || fatal "Release Version file is empty" U002B
elif [ -z "${NETDATA_BASE_URL}" ] && [ -z "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] &&
{ [ "${1}" = stable ] || [ "${1}" = nightly ]; }; then
create_exec_tmp_directory
if _safe_download "${NETDATA_ARTIFACT_URL_PREFIX:-${RELEASE_ARTIFACT_URL_PREFIX}/${1}/latest}/Version" "${ndtmpdir}/Version"; then
_latest_tag="$(cat "${ndtmpdir}/Version")"
fi
fi
if [ -z "${_latest_tag}" ]; then
case "${1}" in
stable) _latest_tag="$(get_netdata_latest_tag "${NETDATA_STABLE_BASE_URL}")" ;;
nightly) _latest_tag="$(get_netdata_latest_tag "${NETDATA_NIGHTLY_BASE_URL}")" ;;
*) warning "Unknown release channel ${1}, updating may not work correctly" ; _latest_tag="" ;;
esac
fi
fi
echo "${_latest_tag}"
}
validate_environment_file() {
if [ -n "${NETDATA_PREFIX+SET_BUT_NULL}" ] && [ -n "${REINSTALL_OPTIONS+SET_BUT_NULL}" ]; then
return 0
else
fatal "Environment file located at ${ENVIRONMENT_FILE} is not valid, unable to update." U0007
fi
}
get_current_version() {
basepath="$(dirname "$(dirname "$(dirname "${NETDATA_LIB_DIR}")")")"
searchpath="${basepath}/bin:${basepath}/sbin:${basepath}/usr/bin:${basepath}/usr/sbin:${PATH}"
searchpath="${basepath}/netdata/bin:${basepath}/netdata/sbin:${basepath}/netdata/usr/bin:${basepath}/netdata/usr/sbin:${searchpath}"
ndbinary="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
if [ -z "${ndbinary}" ]; then
_current_version=0
else
_current_version="$(parse_version "$(${ndbinary} -V | cut -f 2 -d ' ')")"
fi
echo "${_current_version:-0}"
}
get_latest_version() {
latest_tag="$(get_latest_tag "${RELEASE_CHANNEL}")" || return 1
parse_version "${latest_tag}"
}
update_available() {