-
Notifications
You must be signed in to change notification settings - Fork 3
/
install-torizon-plugin.sh
executable file
·354 lines (299 loc) · 11.1 KB
/
install-torizon-plugin.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
#!/usr/bin/env bash
set -e
echo "======================================================"
echo " Torizon Plugin Installer for apt-based distributions "
echo "======================================================"
echo ' '
echo ' ******, '
echo ' ************ '
echo ' ***** ****, *** '
echo ' ************, .********** '
echo ' ** ******* ********* '
echo ' .********* (((((((( *. ******* '
echo ' ********** /((((((((((( ************* '
echo ' **. *****. (((( **** ***** '
echo ' ************ ************ '
echo ' %%% ****** * ******** &%%% '
echo ' *%%%%% ********* %%%%% '
echo ' %%%%%% *********** %%%%% . '
echo ' *** %%%%%& *** %%%%%. **** '
echo ' ***** %%%%%. %%%%%% ****** '
echo ' .***** %%%%% %%%%%% ***** '
echo ' ****** .%%%%%%%% ***** '
echo ' ****** ***** '
echo ' ***** ****** '
echo ' ******** '
echo ' '
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
check_if_already_provisioned () {
if [ -f /var/sota/import/info.json ]; then
read -rp "Device already provisioned! Do you want to reprovision it? [y/N]" reprovision
if [ -z "$reprovision" ] || [ "$reprovision" = "N" ] || [ "$reprovision" = "n" ]; then
exit 0
elif [ "$reprovision" = "Y" ] || [ "$reprovision" = "y" ]; then
:
else
check_if_already_provisioned
fi
fi
}
check_if_install () {
read -rp "Do you want to continue? [Y/n]" install
if [ -z "$install" ] || [ "$install" = "Y" ] || [ "$install" = "y" ]; then
:
elif [ "$install" = "N" ] || [ "$install" = "n" ]; then
exit 0
else
check_if_install
fi
}
# Determine package type to install: https://unix.stackexchange.com/a/6348
# OS used by all - for Debs it must be Ubuntu or Debian
# CODENAME only used for Debs
if [ -f /etc/os-release ]; then
# Debian uses Dash which does not support source
# shellcheck source=/dev/null
. /etc/os-release
OS=$( echo "${ID}" | tr '[:upper:]' '[:lower:]')
CODENAME=$( echo "${VERSION_CODENAME}" | tr '[:upper:]' '[:lower:]')
elif lsb_release &>/dev/null; then
OS=$(lsb_release -is | tr '[:upper:]' '[:lower:]')
CODENAME=$(lsb_release -cs)
else
OS=$(uname -s)
fi
ARCH=$(dpkg --print-architecture)
if [ "$(id -u)" != "0" ]; then
echo "This script should execute as root. Use sudo or run from root user."
exit 1
fi
install_torizon_repo () {
SUITE=$1
COMPONENT=$2
echo "Installation has started, it may take a few minutes."
export DEBIAN_FRONTEND=noninteractive
mkdir -p /usr/share/keyrings/
echo "Installing curl and gpg" > /tmp/install-torizon-plugin.log
apt-get -y update -qq >> /tmp/install-torizon-plugin.log 2>&1 && apt-get install -y -qq curl gpg >>/tmp/install-torizon-plugin.log 2>&1
curl -fsSL https://feeds.toradex.com/staging/"${OS}"/toradex-debian-repo-19092023.asc | gpg --dearmor > /usr/share/keyrings/toradex.gpg
curl -fsSL https://packages.fluentbit.io/fluentbit.key | gpg --dearmor > /usr/share/keyrings/fluentbit-keyring.gpg
curl -fsSL "https://download.docker.com/linux/${OS}/gpg" | gpg --dearmor > /usr/share/keyrings/docker.gpg
cat > /etc/apt/sources.list.d/toradex.list <<EOF
deb [signed-by=/usr/share/keyrings/toradex.gpg] https://feeds.toradex.com/staging/${OS}/ ${SUITE} ${COMPONENT}
deb [signed-by=/usr/share/keyrings/fluentbit-keyring.gpg] https://packages.fluentbit.io/${OS}/${CODENAME} ${CODENAME} main
deb [signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/${OS} ${CODENAME} stable
EOF
echo "Adding the following package feeds:" >> /tmp/install-torizon-plugin.log
cat /etc/apt/sources.list.d/toradex.list >> /tmp/install-torizon-plugin.log
echo "Installing dependencies (${PKGS_TO_INSTALL})" >> /tmp/install-torizon-plugin.log
apt-get -y update -qq >> /tmp/install-torizon-plugin.log 2>&1
apt-get -y install -qq ${PKGS_TO_INSTALL} >> /tmp/install-torizon-plugin.log 2>&1
if [ ! -f /usr/bin/docker-compose ]; then
cat > /usr/bin/docker-compose <<EOF
#!/bin/sh
# make docker-compose an "alias" do docker compose
docker compose \$@
EOF
chmod a+x /usr/bin/docker-compose
echo "Adding /usr/bin/docker-compose:" >> /tmp/install-torizon-plugin.log
cat /usr/bin/docker-compose >> /tmp/install-torizon-plugin.log
fi
if [ ! -f /etc/systemd/system/docker-compose.service ]; then
cat > /etc/systemd/system/docker-compose.service <<EOF
[Unit]
Description=Docker Compose service with docker compose
Requires=docker.service
After=docker.service
ConditionPathExists=/var/sota/storage/docker-compose/docker-compose.yml
ConditionPathExists=!/var/sota/storage/docker-compose/docker-compose.yml.tmp
OnFailure=docker-integrity-checker.service
[Service]
Type=simple
WorkingDirectory=/var/sota/storage/docker-compose/
ExecStart=/usr/bin/docker-compose -p torizon up -d --remove-orphans
ExecStartPost=rm -f /tmp/recovery-attempt.txt
ExecStop=/usr/bin/docker-compose -p torizon down
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable docker-compose >> /tmp/install-torizon-plugin.log 2>&1
echo "Adding /etc/systemd/system/docker-compose.service:" >> /tmp/install-torizon-plugin.log
cat /etc/systemd/system/docker-compose.service >> /tmp/install-torizon-plugin.log
fi
if [ -f /etc/fluent-bit/fluent-bit.conf ]; then
rm -f /etc/fluent-bit/fluent-bit.conf
cat > /etc/fluent-bit/fluent-bit.conf <<EOF
[SERVICE]
flush 1
daemon Off
log_level info
parsers_file parsers.conf
plugins_file plugins.conf
[INPUT]
name cpu
tag cpu
interval_sec 300
Mem_Buf_Limit 5MB
[FILTER]
Name nest
Match cpu
Operation nest
Wildcard *
Nest_under cpu
[INPUT]
name mem
tag memory
interval_sec 300
Mem_Buf_Limit 5MB
[FILTER]
Name nest
Match memory
Operation nest
Wildcard *
Nest_under memory
[INPUT]
name thermal
tag temperature
name_regex thermal_zone0
interval_sec 300
Mem_Buf_Limit 5MB
[FILTER]
Name nest
Match temperature
Operation nest
Wildcard *
Nest_under temperature
[INPUT]
name proc
proc_name dockerd
tag proc_docker
fd false
mem false
interval_sec 300
Mem_Buf_Limit 5MB
[FILTER]
Name nest
Match proc_docker
Operation nest
Wildcard *
Nest_under docker
[INPUT]
Name exec
Tag emmc_health
Command /usr/bin/emmc-health
Parser json
Interval_Sec 300
Mem_Buf_Limit 5MB
[FILTER]
Name nest
Match emmc_health
Operation nest
Wildcard *
Nest_under custom
[OUTPUT]
name http
match *
host dgw.torizon.io
port 443
uri monitoring/fluentbit-metrics
format json
tls on
tls.verify off
tls.ca_file /etc/sota/root.crt
tls.key_file /var/sota/import/pkey.pem
tls.crt_file /var/sota/import/client.pem
Retry_Limit 10
EOF
echo "Adding /etc/fluent-bit/fluent-bit.conf:" >> /tmp/install-torizon-plugin.log
cat /etc/fluent-bit/fluent-bit.conf >> /tmp/install-torizon-plugin.log
fi
# gecos option has changed to comment in bookworm or newer
case $CODENAME in
noble|bookworm)
adduser_gecos_opt="--comment"
;;
*)
adduser_gecos_opt="--gecos"
;;
esac
if [ -z "$(id -u torizon 2>> /tmp/install-torizon-plugin.log)" ]; then
echo "Now we have to create the torizon user so remote access works out of the box. Please, fill in the password for torizon user."
adduser ${adduser_gecos_opt} '' torizon
fi
adduser torizon sudo >> /tmp/install-torizon-plugin.log 2>&1
adduser torizon docker >> /tmp/install-torizon-plugin.log 2>&1
}
check_if_already_provisioned
echo "This script will:
- Add Toradex's, Fluent Bit's and Docker's package feed to your system;
- Install fluent-bit, docker, aktualizr and rac (remote access client) applications;
- Create a docker-compose binary at /usr/bin;
- Install a docker-compose systemd service;
- Create torizon user and add it to sudo and docker groups;
- Attempt to provision the device on Torizon Cloud using a pair code;
- Create a log file in /tmp/install-torizon-plugin.log."
check_if_install
case ${ARCH} in
amd64|arm64)
PKGS_TO_INSTALL="aktualizr-torizon containerd.io docker-ce docker-ce-cli docker-compose-plugin fluent-bit rac sudo"
;;
armhf)
PKGS_TO_INSTALL="aktualizr-torizon containerd.io docker-ce docker-ce-cli docker-compose-plugin rac sudo"
;;
*)
echo "${ARCH} not supported."
exit 1
;;
esac
case ${OS} in
ubuntu|debian)
case ${CODENAME} in
noble|jammy|focal)
install_torizon_repo "${CODENAME}" main
;;
bookworm)
install_torizon_repo stable main
;;
bullseye)
install_torizon_repo oldstable main
;;
*)
echo "Unsupported release: ${CODENAME} for ${OS}."
exit 1
;;
esac
;;
*)
echo "${OS} not supported."
exit 1
;;
esac
echo "Installation of dependencies completed!"
# Early exit for CI
if [ -n "$DO_NOT_PROVISION" ]; then
# set -e is set, will exit on error
aktualizr-torizon --version
exit 0
fi
echo "Retrieving one-time pairing token"
echo "Ready to pair..."
response=$(curl -fsSL "https://app.torizon.io/api/provision-code")
code=$(echo "$response" | awk -F'"' '/provisionCode/{print $4}')
uuid=$(echo "$response" | awk -F'"' '/provisionUuid/{print $8}')
echo "👉 Go to https://pair.torizon.io and use code ${YELLOW}$code ${NC}to provision your device"
echo "This script will terminate automatically after the pairing process is finished!"
while true; do
sleep 10
provision_info=$(curl -fsSL "https://app.torizon.io/api/provision-code?provisionUuid=$uuid")
access=$(echo "$provision_info" | awk -F'"' '/access/{print $4}')
if [ "$access" != "" ]; then
break
fi
done
sh <<SCRIPT
curl -fsSL https://app.torizon.io/statics/scripts/provision-device.sh | bash -s -- -u https://app.torizon.io/api/accounts/devices -t "${access}" && systemctl restart aktualizr remote-access
SCRIPT
echo "Your device is provisioned! ⭐"