Add fallback logic in installer for fetching files. (#17045)
Still try to use `curl` first if it exists, but if it fails, fall back to attempting with wget instead.
Austin S. Hemmelgarn committed
Apr 16, 2024 at 07:32 UTC
42595b3f26b1ec2684d09524cb1f397b9a12bbd2
3 files changed
+189
-42
packaging/installer/functions.sh
+46
-6
@@ -103,13 +103,33 @@ check_for_curl() {
103
104
get() {
105
url="${1}"
106
+ checked=0
107
+ succeeded=0
108
109
check_for_curl
110
111
if [ -n "${curl}" ]; then
110
- "${curl}" -q -o - -sSL --connect-timeout 10 --retry 3 "${url}"
111
- elif command -v wget > /dev/null 2>&1; then
112
- wget -T 15 -O - "${url}"
112
+ checked=1
113
+
114
+ if "${curl}" -q -o - -sSL --connect-timeout 10 --retry 3 "${url}"; then
115
+ succeeded=1
116
+ fi
117
+ fi
118
+
119
+ if [ "${succeeded}" -eq 0 ]; then
120
+ if command -v wget > /dev/null 2>&1; then
121
+ checked=1
122
+
123
+ if wget -T 15 -O - "${url}"; then
124
+ succeeded=1
125
+ fi
126
+ fi
127
+ fi
128
+
129
+ if [ "${succeeded}" -eq 1 ]; then
130
+ return 0
131
+ elif [ "${checked}" -eq 1 ]; then
132
+ return 1
133
else
134
fatal "I need curl or wget to proceed, but neither is available on this system." "L0002"
135
fi
@@ -124,9 +144,29 @@ download_file() {
144
check_for_curl
145
146
if [ -n "${curl}" ]; then
127
- run "${curl}" -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"
128
- elif command -v wget > /dev/null 2>&1; then
129
- run wget -T 15 -O "${dest}" "${url}"
147
+ checked=1
148
+
149
+ if run "${curl}" -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"; then
150
+ succeeded=1
151
+ else
152
+ rm -f "${dest}"
153
+ fi
154
+ fi
155
+
156
+ if [ "${succeeded}" -eq 0 ]; then
157
+ if command -v wget > /dev/null 2>&1; then
158
+ checked=1
159
+
160
+ if run wget -T 15 -O "${dest}" "${url}"; then
161
+ succeeded=1
162
+ fi
163
+ fi
164
+ fi
165
+
166
+ if [ "${succeeded}" -eq 1 ]; then
167
+ return 0
168
+ elif [ "${checked}" -eq 1 ]; then
169
+ return 1
170
else
171
echo >&2
172
echo >&2 "Downloading ${name} from '${url}' failed because of missing mandatory packages."
packaging/installer/kickstart.sh
+104
-27
@@ -311,23 +311,31 @@ telemetry_event() {
311
EOF
312
)"
313
314
+ succeeded=0
315
+
316
if [ -n "${CURL}" ]; then
315
- "${CURL}" --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null
316
- elif command -v wget > /dev/null 2>&1; then
317
- if wget --help 2>&1 | grep BusyBox > /dev/null 2>&1; then
318
- # BusyBox-compatible version of wget, there is no --no-check-certificate option
319
- wget -q -O - \
320
- -T 1 \
321
- --header 'Content-Type: application/json' \
322
- --post-data "${REQ_BODY}" \
323
- "${TELEMETRY_URL}" > /dev/null
324
- else
325
- wget -q -O - --no-check-certificate \
326
- --method POST \
327
- --timeout=1 \
328
- --header 'Content-Type: application/json' \
329
- --body-data "${REQ_BODY}" \
330
- "${TELEMETRY_URL}" > /dev/null
317
+ if "${CURL}" --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null; then
318
+ succeeded=1
319
+ fi
320
+ fi
321
+
322
+ if [ "${succeeded}" -eq 0 ]; then
323
+ if command -v wget > /dev/null 2>&1; then
324
+ if wget --help 2>&1 | grep BusyBox > /dev/null 2>&1; then
325
+ # BusyBox-compatible version of wget, there is no --no-check-certificate option
326
+ wget -q -O - \
327
+ -T 1 \
328
+ --header 'Content-Type: application/json' \
329
+ --post-data "${REQ_BODY}" \
330
+ "${TELEMETRY_URL}" > /dev/null
331
+ else
332
+ wget -q -O - --no-check-certificate \
333
+ --method POST \
334
+ --timeout=1 \
335
+ --header 'Content-Type: application/json' \
336
+ --body-data "${REQ_BODY}" \
337
+ "${TELEMETRY_URL}" > /dev/null
338
+ fi
339
fi
340
fi
341
}
@@ -605,15 +613,38 @@ set_tmpdir() {
613
614
check_for_remote_file() {
615
url="${1}"
616
+ succeeded=0
617
+ checked=0
618
619
if echo "${url}" | grep -Eq "^file:///"; then
620
[ -e "${url#file://}" ] || return 1
621
+ return 0
622
elif [ -n "${NETDATA_ASSUME_REMOTE_FILES_ARE_PRESENT}" ]; then
623
return 0
613
- elif [ -n "${CURL}" ]; then
614
- "${CURL}" --output /dev/null --silent --head --fail "${url}" || return 1
615
- elif command -v wget > /dev/null 2>&1; then
616
- wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK' || return 1
624
+ fi
625
+
626
+ if [ -n "${CURL}" ]; then
627
+ checked=1
628
+
629
+ if "${CURL}" --output /dev/null --silent --head --fail "${url}"; then
630
+ succeeded=1
631
+ fi
632
+ fi
633
+
634
+ if [ "${succeeded}" -eq 0 ]; then
635
+ if command -v wget > /dev/null 2>&1; then
636
+ checked=1
637
+
638
+ if wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK'; then
639
+ succeeded=1
640
+ fi
641
+ fi
642
+ fi
643
+
644
+ if [ "${succeeded}" -eq 1 ]; then
645
+ return 0
646
+ elif [ "${checked}" -eq 1 ]; then
647
+ return 1
648
else
649
fatal "${ERROR_F0003}" F0003
650
fi
@@ -622,13 +653,39 @@ check_for_remote_file() {
653
download() {
654
url="${1}"
655
dest="${2}"
656
+ succeeded=0
657
+ checked=0
658
659
if echo "${url}" | grep -Eq "^file:///"; then
660
run cp "${url#file://}" "${dest}" || return 1
628
- elif [ -n "${CURL}" ]; then
629
- run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
630
- elif command -v wget > /dev/null 2>&1; then
631
- run wget -T 15 -O "${dest}" "${url}" || return 1
661
+ return 0
662
+ fi
663
+
664
+
665
+ if [ -n "${CURL}" ]; then
666
+ checked=1
667
+
668
+ if run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"; then
669
+ succeeded=1
670
+ else
671
+ rm -f "${dest}"
672
+ fi
673
+ fi
674
+
675
+ if [ "${succeeded}" -eq 0 ]; then
676
+ if command -v wget > /dev/null 2>&1; then
677
+ checked=1
678
+
679
+ if run wget -T 15 -O "${dest}" "${url}"; then
680
+ succeeded=1
681
+ fi
682
+ fi
683
+ fi
684
+
685
+ if [ "${succeeded}" -eq 1 ]; then
686
+ return 0
687
+ elif [ "${checked}" -eq 1 ]; then
688
+ return 1
689
else
690
fatal "${ERROR_F0003}" F0003
691
fi
@@ -652,11 +709,31 @@ get_actual_version() {
709
710
get_redirect() {
711
url="${1}"
712
+ succeeded=0
713
+ checked=0
714
715
if [ -n "${CURL}" ]; then
657
- run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -Eo '[^/]+$'" || return 1
658
- elif command -v wget > /dev/null 2>&1; then
659
- run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -Eo '[^/]+$'" || return 1
716
+ checked=1
717
+
718
+ if run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -Eo '[^/]+$'"; then
719
+ succeeded=1
720
+ fi
721
+ fi
722
+
723
+ if [ "${succeeded}" -eq 0 ]; then
724
+ if command -v wget > /dev/null 2>&1; then
725
+ checked=1
726
+
727
+ if run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -Eo '[^/]+$'"; then
728
+ succeeded=1
729
+ fi
730
+ fi
731
+ fi
732
+
733
+ if [ "${succeeded}" -eq 1 ]; then
734
+ return 0
735
+ elif [ "${checked}" -eq 1 ]; then
736
+ return 1
737
else
738
fatal "${ERROR_F0003}" F0003
739
fi
packaging/installer/netdata-updater.sh
+39
-9
@@ -396,15 +396,37 @@ check_for_curl() {
396
_safe_download() {
397
url="${1}"
398
dest="${2}"
399
+ succeeded=0
400
+ checked=0
401
402
check_for_curl
403
404
if [ -n "${curl}" ]; then
403
- "${curl}" -fsSL --connect-timeout 10 --retry 3 "${url}" > "${dest}"
404
- return $?
405
- elif command -v wget > /dev/null 2>&1; then
406
- wget -T 15 -O - "${url}" > "${dest}"
407
- return $?
405
+ checked=1
406
+
407
+ if "${curl}" -fsSL --connect-timeout 10 --retry 3 "${url}" > "${dest}"; then
408
+ succeeded=1
409
+ else
410
+ rm -f "${dest}"
411
+ fi
412
+ fi
413
+
414
+ if [ "${succeeded}" -eq 0 ]; then
415
+ if command -v wget > /dev/null 2>&1; then
416
+ checked=1
417
+
418
+ if wget -T 15 -O - "${url}" > "${dest}"; then
419
+ succeeded=1
420
+ else
421
+ rm -f "${dest}"
422
+ fi
423
+ fi
424
+ fi
425
+
426
+ if [ "${succeeded}" -eq 1 ]; then
427
+ return 0
428
+ elif [ "${checked}" -eq 1 ]; then
429
+ return 1
430
else
431
return 255
432
fi
@@ -432,13 +454,21 @@ get_netdata_latest_tag() {
454
check_for_curl
455
456
if [ -n "${curl}" ]; then
435
- tag=$("${curl}" "${url}" -s -L -I -o /dev/null -w '%{url_effective}' | grep -Eom 1 '[^/]*/?$')
436
- elif command -v wget >/dev/null 2>&1; then
437
- tag=$(wget -S -O /dev/null "${url}" 2>&1 | grep -m 1 Location | grep -Eo '[^/]*/?$')
438
- else
457
+ tag=$("${curl}" "${url}" -s -L -I -o /dev/null -w '%{url_effective}')
458
+ fi
459
+
460
+ if [ -z "${tag}" ]; then
461
+ if command -v wget >/dev/null 2>&1; then
462
+ tag=$(wget -S -O /dev/null "${url}" 2>&1 | grep Location)
463
+ fi
464
+ fi
465
+
466
+ if [ -z "${tag}" ]; then
467
fatal "I need curl or wget to proceed, but neither of them are available on this system." U0006
468
fi
469
470
+ tag="$(echo "${tag}" | grep -Eom 1 '[^/]*/?$')"
471
+
472
# Fallback case for simpler local testing.
473
if echo "${tag}" | grep -Eq 'latest/?$'; then
474
if _safe_download "${url}/latest-version.txt" ./ndupdate-version.txt; then