blob: 67c0e71289ecc2ac8b99a8176cc617639c5515e8 (
plain)
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
|
#!/bin/sh
# Netdata updater utility
#
# Variables needed by script:
# - PATH
# - CFLAGS
# - LDFLAGS
# - MAKEOPTS
# - IS_NETDATA_STATIC_BINARY
# - NETDATA_CONFIGURE_OPTIONS
# - REINSTALL_OPTIONS
# - NETDATA_TARBALL_URL
# - NETDATA_TARBALL_CHECKSUM_URL
# - NETDATA_TARBALL_CHECKSUM
# - NETDATA_PREFIX
# - NETDATA_LIB_DIR
#
# Optional environment options:
#
# - TMPDIR (set to a usable temporary directory)
# - NETDATA_NIGHTLIES_BASEURL (set the base url for downloading the dist tarball)
#
# Copyright: 2018-2020 Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Author: Paweł Krupa <paulfantom@gmail.com>
# Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud>
# Author: Austin S. Hemmelgarn <austin@netdata.cloud>
set -e
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
info() {
echo >&3 "$(date) : INFO: " "${@}"
}
error() {
echo >&3 "$(date) : ERROR: " "${@}"
}
: "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; then
if [ -r "${script_dir}/../../../etc/netdata/.environment" ]; then
ENVIRONMENT_FILE="${script_dir}/../../../etc/netdata/.environment"
elif [ -r "/etc/netdata/.environment" ]; then
ENVIRONMENT_FILE="/etc/netdata/.environment"
elif [ -r "/opt/netdata/etc/netdata/.environment" ]; then
ENVIRONMENT_FILE="/opt/netdata/etc/netdata/.environment"
else
envpath="$(find / -type d \( -path /sys -o -path /proc -o -path /dev \) -prune -false -o -path '*netdata/.environment' -type f 2> /dev/null | head -n 1)"
if [ -r "${envpath}" ]; then
ENVIRONMENT_FILE="${envpath}"
else
error "Cannot find environment file, unable to update."
exit 1
fi
fi
fi
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 sha256sum > /dev/null 2>&1; then
sha256sum "$@"
elif command -v shasum > /dev/null 2>&1; then
shasum -a 256 "$@"
else
fatal "I could not find a suitable checksum binary to use"
fi
}
# this is what we will do if it fails (head-less only)
fatal() {
error "FAILED TO UPDATE NETDATA : ${1}"
exit 1
}
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}")" = "SUCCESS" ] ; then
ret=1
fi
fi
fi
rm -f "${testfile}"
return "${ret}"
}
create_tmp_directory() {
if [ -n "${NETDATA_TMPDIR_PATH}" ]; then
echo "${NETDATA_TMPDIR_PATH}"
else
if [ -z "${NETDATA_TMPDIR}" ] || _cannot_use_tmpdir "${NETDATA_TMPDIR}" ; then
if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}" ; then
if _cannot_use_tmpdir /tmp ; then
if _cannot_use_tmpdir "${PWD}" ; then
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."
else
TMPDIR="${PWD}"
fi
else
TMPDIR="/tmp"
fi
fi
else
TMPDIR="${NETDATA_TMPDIR}"
fi
mktemp -d -t netdata-updater-XXXXXXXXXX
fi
}
_safe_download() {
url="${1}"
dest="${2}"
if command -v curl > /dev/null 2>&1; then
curl -sSL --connect-timeout 10 --retry 3 "${url}" > "${dest}"
return $?
elif command -v wget > /dev/null 2>&1; then
wget -T 15 -O - "${url}" > "${dest}"
return $?
else
return 255
fi
}
download() {
url="${1}"
dest="${2}"
_safe_download "${url}" "${dest}"
ret=$?
if [ ${ret} -eq 0 ]; then
return 0
elif [ ${ret} -eq 255 ]; then
fatal "I need curl or wget to proceed, but neither is available on this system."
else
fatal "Cannot download ${url}"
fi
}
get_netdata_latest_tag() {
dest="${1}"
url="https://github.com/netdata/netdata/releases/latest"
if command -v curl >/dev/null 2>&1; then
tag=$(curl "${url}" -s -L -I -o /dev/null -w '%{url_effective}' | grep -m 1 -o '[^/]*$')
elif command -v wget >/dev/null 2>&1; then
tag=$(wget --max-redirect=0 "${url}" 2>&1 | grep Location | cut -d ' ' -f2 | grep -m 1 -o '[^/]*$')
else
fatal "I need curl or wget to proceed, but neither of them are available on this system."
fi
echo "${tag}" >"${dest}"
}
newer_commit_date() {
info "Checking if a newer version of the updater script is available."
if command -v jq > /dev/null 2>&1; then
commit_date="$(_safe_download "https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1" /dev/stdout | jq '.[0].commit.committer.date' | tr -d '"')"
elif command -v python > /dev/null 2>&1;then
commit_date="$(_safe_download "https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1" /dev/stdout | python -c 'from __future__ import print_function;import sys,json;print(json.load(sys.stdin)[0]["commit"]["committer"]["date"])')"
elif command -v python3 > /dev/null 2>&1;then
commit_date="$(_safe_download "https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1" /dev/stdout | python3 -c 'from __future__ import print_function;import sys,json;print(json.load(sys.stdin)[0]["commit"]["committer"]["date"])')"
fi
if [ -z "${commit_date}" ] ; then
commit_date="9999-12-31T23:59:59Z"
fi
if [ -e "${script_source}" ]; then
script_date="$(date -r "${script_source}" +%s)"
else
script_date="$(date +%s)"
fi
[ "$(date -d "${commit_date}" +%s)" -ge "${script_date}" ]
}
self_update() {
if [ -z "${NETDATA_NO_UPDATER_SELF_UPDATE}" ] && newer_commit_date; then
info "Downloading newest version of updater script."
ndtmpdir=$(create_tmp_directory)
cd "$ndtmpdir" || exit 1
if _safe_download "https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/
|