forked from thin-edge/thin-edge.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-thin-edge_io.sh
executable file
·419 lines (354 loc) · 11.7 KB
/
get-thin-edge_io.sh
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
#!/bin/sh
set -e
TYPE=full
TMPDIR=/tmp/tedge
LOGFILE=/tmp/tedge/install.log
# Packages names were changed to confirm to debian naming conventions
# But we should still care about installing older versions. It will still cause
# issues when downgrading as there will be a package name clash as we can't re-release
# older packages
TEDGE=tedge
TEDGE_MAPPER=tedge-mapper
TEDGE_AGENT=tedge-agent
TEDGE_WATCHDOG=tedge-watchdog
TEDGE_APT_PLUGIN=tedge-apt-plugin
C8Y_CONFIGURATION_PLUGIN=c8y-configuration-plugin
C8Y_LOG_PLUGIN=c8y-log-plugin
C8Y_FIRMWARE_PLUGIN=c8y-firmware-plugin
C8Y_REMOTE_ACCESS_PLUGIN=c8y-remote-access-plugin
PURGE_OLD_PACKAGES=
# Set shell used by the script (can be overwritten during dry run mode)
sh_c='sh -c'
usage() {
cat <<EOF
USAGE:
get-thin-edge_io [<VERSION>] [--minimal]
ARGUMENTS:
<VERSION> Install specific version of thin-edge.io - if not provided installs latest minor release
OPTIONS:
--minimal Install only basic set of components - tedge cli and tedge mappers
--dry-run Don't install anything, just let me know what it does
EOF
}
log() {
echo "$@" | tee -a "$LOGFILE"
}
debug() {
echo "$@" >> "$LOGFILE" 2>&1
}
print_debug() {
echo
echo "--------------- machine details ---------------------"
echo "date: $(date || true)"
echo "tedge: $VERSION"
echo "Machine: $(uname -a || true)"
echo "Architecture: $(dpkg --print-architecture || true)"
if command_exists "lsb_release"; then
DISTRIBUTION=$(lsb_release -a 2>/dev/null | grep "Description" | cut -d: -f2- | xargs)
echo "Distribution: $DISTRIBUTION"
fi
echo
echo "--------------- error details ------------------------"
if [ -f "$LOGFILE" ]; then
cat "$LOGFILE"
fi
echo "------------------------------------------------------"
echo
}
# Enable print of info if something unexpected happens
trap print_debug EXIT
fail() {
exit_code="$1"
shift
log "Failed to install thin-edge.io"
echo
log "Reason: $*"
log "Please create a ticket using the following link and include the console output"
log " https://github.com/thin-edge/thin-edge.io/issues/new?assignees=&labels=bug&template=bug_report.md"
exit "$exit_code"
}
command_exists() {
command -v "$@" > /dev/null 2>&1
}
is_dry_run() {
if [ -z "$DRY_RUN" ]; then
return 1
else
return 0
fi
}
check_prerequisites() {
if ! command_exists dpkg; then
fail 1 "Missing prerequisite: dpkg"
fi
if ! command_exists curl && ! command_exists wget; then
fail 1 "Missing prerequisite: wget or curl"
fi
}
configure_shell() {
# Check if has sudo rights or if it can be requested
user="$(id -un 2>/dev/null || true)"
sh_c='sh -c'
if [ "$user" != 'root' ]; then
if command_exists sudo; then
sh_c='sudo -E sh -c'
elif command_exists su; then
sh_c='su -c'
else
cat >&2 <<-EOF
Error: this installer needs the ability to run commands as root.
We are unable to find either "sudo" or "su" available to make this happen.
EOF
exit 1
fi
fi
if is_dry_run; then
sh_c="echo"
fi
}
install_artifact() {
#
# Download and install a package using either wget or curl (whatever is available)
# Usage
# install_artifact <name>
#
name="$1"
package_type="deb"
echo
printf 'Downloading %s...' "$name"
filename="${name}_${VERSION}_${ARCH}.${package_type}"
url="https://github.com/thin-edge/thin-edge.io/releases/download/${VERSION}/${filename}"
if [ ! -d "$TMPDIR" ]; then
mkdir -p "$TMPDIR"
fi
# Prefer curl over wget as docs instruct the user to download this script using curl
if command_exists curl; then
if ! (cd "$TMPDIR" && $sh_c "curl -fsSLO '$url'" >> "$LOGFILE" 2>&1 ); then
fail 2 "Could not download package from url: $url"
fi
elif command_exists wget; then
if ! $sh_c "wget --quiet '$url' -P '$TMPDIR'" >> "$LOGFILE" 2>&1; then
fail 2 "Could not download package from url: $url"
fi
else
# This should not happen due to the pre-requisite check
echo "FAILED"
fail 1 "Could not download file because neither wget or curl is installed. Please install 'wget' or 'curl' and try again"
fi
if is_dry_run; then
echo "OK (DRY-RUN)"
else
echo "OK"
fi
printf 'Installing %s...' "$name"
if $sh_c "dpkg -i '${TMPDIR}/${filename}'" >> "$LOGFILE" 2>&1; then
if is_dry_run; then
echo "OK (DRY-RUN)"
else
echo "OK"
fi
else
echo "FAILED"
fail 2 "Failed to install package '$name'"
fi
}
remove_package() {
#
# Remove/purge a package. This should only be
# used to remove packages which are no longer needed
#
name="$1"
if dpkg -s >/dev/null 2>&1; then
printf 'Purging renamed package %s...' "$name"
if $sh_c "dpkg --purge $name" >> "$LOGFILE" 2>&1; then
if is_dry_run; then
echo "OK (DRY-RUN)"
else
echo "OK"
fi
else
echo "FAILED"
fail 3 "Failed to purge old renamed package '$name'"
fi
fi
}
install_basic_components() {
install_artifact "$TEDGE"
install_artifact "$TEDGE_MAPPER"
if [ -n "$PURGE_OLD_PACKAGES" ]; then
remove_package "tedge_mapper"
fi
}
install_tedge_agent() {
install_artifact "$TEDGE_AGENT"
if [ -n "$PURGE_OLD_PACKAGES" ]; then
remove_package "tedge_agent"
fi
}
install_tedge_plugins() {
install_artifact "$TEDGE_APT_PLUGIN"
install_artifact "$C8Y_CONFIGURATION_PLUGIN"
install_artifact "$C8Y_LOG_PLUGIN"
install_artifact "$TEDGE_WATCHDOG"
if [ -n "$C8Y_FIRMWARE_PLUGIN" ]; then
install_artifact "$C8Y_FIRMWARE_PLUGIN"
fi
if [ -n "$C8Y_REMOTE_ACCESS_PLUGIN" ]; then
install_artifact "$C8Y_REMOTE_ACCESS_PLUGIN"
fi
if [ -n "$PURGE_OLD_PACKAGES" ]; then
remove_package "tedge_apt_plugin"
remove_package "tedge_apama_plugin"
remove_package "c8y_configuration_plugin"
remove_package "c8y_log_plugin"
remove_package "tedge_watchdog"
fi
}
get_latest_version() {
# Detect latest version from github using api to avoid having a default version in the script
# The request checks the redirected url behind the latest url.
# Example:
# https://github.com/thin-edge/thin-edge.io/releases/latest => https://github.com/thin-edge/thin-edge.io/releases/tag/0.12.0
#
# Note: avoid using the api endpoint (api.github.com) as it is rate limited for unauthenticated requests.
if command_exists curl; then
response=$(curl -Ls -o /dev/null -w "%{url_effective}" 'https://github.com/thin-edge/thin-edge.io/releases/latest')
elif command_exists wget; then
response=$(wget --max-redirect=0 https://github.com/thin-edge/thin-edge.io/releases/latest 2>&1 | awk '/Location: /,// { print }' | awk '{print $2}')
else
fail 1 "Detecting latest version requires either curl or wget to be installed"
fi
version=$(echo "$response" | rev | cut -d/ -f1 | rev)
if [ -z "$version" ]; then
fail 1 "Failed to detect latest version. You can try specifying an explicit version. Check the help for more details"
fi
echo "$version"
}
main() {
if [ -d "$TMPDIR" ]; then
rm -Rf "$TMPDIR"
fi
mkdir -p "$TMPDIR"
check_prerequisites
configure_shell
ARCH=$(dpkg --print-architecture)
if [ -z "$VERSION" ]; then
VERSION="$(get_latest_version)"
log "Version argument has not been provided, installing latest: $VERSION"
log "To install a particular version use this script with the version as an argument."
log "For example: sudo ./get-thin-edge_io.sh $VERSION"
fi
if dpkg --compare-versions "$VERSION" le "0.8.1"; then
# Use older style packages names (with underscore)
TEDGE=tedge
TEDGE_MAPPER=tedge_mapper
TEDGE_AGENT=tedge_agent
TEDGE_WATCHDOG=tedge_watchdog
TEDGE_APT_PLUGIN=tedge_apt_plugin
C8Y_CONFIGURATION_PLUGIN=c8y_configuration_plugin
C8Y_LOG_PLUGIN=c8y_log_plugin
else
# New package names will be installed so activate
# flag to remove the old packages after the installation
# Note: No configuration files will be removed as the legacy postrm
# are removed by the renamed packages
PURGE_OLD_PACKAGES=1
fi
# Ignore plugins for older versions
if dpkg --compare-versions "$VERSION" lt "0.10.0"; then
log "ignore c8y-firmware-plugin and c8y-remote-access-plugin as they are not supported in <= 0.10.0"
C8Y_FIRMWARE_PLUGIN=
C8Y_REMOTE_ACCESS_PLUGIN=
fi
echo "Thank you for trying thin-edge.io!"
echo
if [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ] || [ "$ARCH" = "armhf" ] || [ "$ARCH" = "amd64" ]; then
# Some OSes may read architecture type as `aarch64`, `aarch64` and `arm64` are the same architectures types.
if [ "$ARCH" = "aarch64" ]; then
ARCH='arm64'
fi
# For arm64, only the versions above 0.3.0 are available.
if [ "$ARCH" = "arm64" ] && ! dpkg --compare-versions "$VERSION" ge "0.3.0"; then
log "aarch64/arm64 compatible packages are only available for version 0.3.0 or above."
exit 1
fi
log "Installing for architecture $ARCH"
else
log "$ARCH is currently not supported. Currently supported are aarch64/arm64, armhf and amd64."
exit 0
fi
if ! command_exists mosquitto; then
log "Installing mosquitto as prerequisite for thin-edge.io"
# Lazily check apt-get dependency
if command_exists apt-get; then
export DEBIAN_FRONTEND=noninteractive
$sh_c "apt-get -y install mosquitto" >> "$LOGFILE"
elif command_exists apk; then
$sh_c "apk add mosquitto"
else
fail 1 "Missing prerequisite: 'apt-get' or 'apk'"
fi
fi
case "$TYPE" in
minimal) install_basic_components ;;
full)
install_basic_components
install_tedge_agent
install_tedge_plugins
;;
*)
log "Unsupported argument type."
exit 1
;;
esac
if is_dry_run; then
echo
echo "Dry run complete"
# Test if tedge command is there and working
elif tedge help >/dev/null; then
# remove error handler
trap - EXIT
# Only delete when everything was ok to help with debugging
rm -Rf "$TMPDIR"
echo
echo "thin-edge.io is now installed on your system!"
echo
echo "You can go to our documentation to find next steps: https://thin-edge.github.io/thin-edge.io/start/getting-started"
else
echo "Something went wrong in the installation process please try the manual installation steps instead:"
echo "https://thin-edge.github.io/thin-edge.io/install/"
fi
}
DRY_RUN=${DRY_RUN:-}
VERSION=
if [ $# -lt 4 ]; then
while :; do
case $1 in
--minimal)
TYPE="minimal"
shift
;;
--dry-run)
DRY_RUN=1
shift
;;
*)
if [ -z "$1" ]; then
break
fi
if [ -n "$VERSION" ]; then
break
fi
VERSION="$1"
shift $(( $# > 0 ? 1 : 0 ))
break
;;
esac
done
else
usage
exit 0
fi
# wrapped up in a function so that we have some protection against only getting
# half the file during "curl | sh"
main