-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.sh
executable file
·489 lines (429 loc) · 11.6 KB
/
install.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
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
#!/usr/bin/env bash
SCRIPT_VERSION="1.0.0"
# Exit on errors
set -e
# Colors
NC='\033[0m' # Text Reset
On_Black='\033[40m' # Black background
On_Cyan='\033[46m' # Cyan background
On_Green='\033[42m' # Green background
BIRed='\033[1;91m' # Bold Intense Red
BWhite='\033[1;37m' # Bold White
BIYellow='\033[1;93m' # Bold Intense Yellow
White='\033[0;37m' # White
# Printing utilities
ColErr=${BIRed}${On_Black}
ColPrompt=${BWhite}${On_Cyan}
ColInfo=${White}
ColWarn=${BIYellow}${On_Black}
cout() {
echo -e "${ColInfo}${1}${NC}"
}
cin() {
echo -e "${ColPrompt}${1}${NC}"
}
cerr() {
echo -e "${ColErr}${1}${NC}"
}
cwarn() {
echo -e "${ColWarn}${1}${NC}"
}
ctitle() {
echo -e "${BWhite}${On_Green}${1}${NC}"
}
# Local functions
promptYesNo() {
if [[ $ALWAYS_YES == 1 ]]; then
echo 1
return
fi
local REPLY=${2}
local TXT="[y/n]"
if [[ "$REPLY" == 1 ]]; then
TXT="[Y/n]"
elif [[ "$REPLY" == 0 ]]; then
TXT="[y/N]"
else
REPLY=""
fi
while true; do
local color=$'\033[1m'
local noColor=$'\033[0m'
read -r -e -p "$color${1} $TXT?$noColor " yn
case $yn in
y | Y | Yes | yes | YES)
REPLY=1
break
;;
n | N | No | no | NO)
REPLY=0
break
;;
"")
if [ -n "$REPLY" ]; then
break
fi
;;
*) ;;
esac
done
echo "$REPLY"
}
promptChoiceArch() {
if [[ $ALWAYS_YES == 1 ]]; then
cerr "Always Yes selected but a multichoices question has been raised"
cerr "Use -f -r <ROS_DISTRO> -a <ARCH> -v <UBUNTU_VERSION> to force an install"
exit 1
fi
local REPLY=""
select which in "amd64" "arm64"; do
case $which in
"amd64")
REPLY="amd64"
break
;;
"arm64")
REPLY="arm64"
break
;;
*) ;;
esac
done
echo "$REPLY"
}
promptChoiceUbuntuDistro() {
if [[ $ALWAYS_YES == 1 ]]; then
cerr "Always Yes selected but a multichoices question has been raised"
cerr "Use -f -r <ROS_DISTRO> -a <ARCH> -v <UBUNTU_VERSION> to force an install"
exit 1
fi
local REPLY=""
select which in "22.04" "20.04" "18.04"; do
case $which in
"22.04")
REPLY="22.04"
break
;;
"20.04")
REPLY="20.04"
break
;;
"18.04")
REPLY="18.04"
break
;;
*) ;;
esac
done
echo "$REPLY"
}
JAMMY_ROS=("iron" "humble")
FOCAL_ROS=("foxy" "noetic" "galactic")
BIONIC_ROS=("melodic")
promptChoiceROSDistro() {
if [[ $ALWAYS_YES == 1 ]]; then
cerr "Always Yes selected but a multichoices question has been raised"
cerr "Use -f -r <ROS_DISTRO> -a <ARCH> -v <UBUNTU_VERSION> to force an install"
exit 1
fi
local REPLY=""
if [ -z "$UBUNTU_DISTRO" ]; then
local ros_list=("iron" "humble" "foxy" "galactic" "noetic" "melodic")
fi
if [[ $UBUNTU_DISTRO == "22.04" ]]; then
local ros_list=("${JAMMY_ROS[@]}")
fi
if [[ $UBUNTU_DISTRO == "20.04" ]]; then
local ros_list=("${FOCAL_ROS[@]}")
fi
if [[ $UBUNTU_DISTRO == "18.04" ]]; then
local ros_list=("${BIONIC_ROS[@]}")
fi
select which in "${ros_list[@]}"; do
case $which in
"iron")
REPLY="iron"
break
;;
"humble")
REPLY="humble"
break
;;
"foxy")
REPLY="foxy"
break
;;
"galactic")
REPLY="galactic"
break
;;
"noetic")
REPLY="noetic"
break
;;
"melodic")
REPLY="melodic"
break
;;
*) ;;
esac
done
echo "$REPLY"
}
valid_args() {
if [[ $UBUNTU_DISTRO == "22.04" ]]; then
for value in "${JAMMY_ROS[@]}"; do
[[ $value == "$QUERY_DISTRO" ]] && return 0
done
return 1
fi
if [[ $UBUNTU_DISTRO == "20.04" ]]; then
for value in "${FOCAL_ROS[@]}"; do
[[ $value == "$QUERY_DISTRO" ]] && return 0
done
return 1
fi
if [[ $UBUNTU_DISTRO == "18.04" ]]; then
for value in "${BIONIC_ROS[@]}"; do
[[ $value == "$QUERY_DISTRO" ]] && return 0
done
return 1
fi
}
# Usage info
show_help() {
cat <<EOF
Usage: ${0##*/} [-hyf] [-r <ROS_DISTRO>] [-a <ARCH>] [-v <UBUNTU_VERSION>]
Install 3Laws Supervisor
-h show this help menu
-y answer yes to all yes/no questions
-r Optional: ROS distribution
-f Force install, specify all arguments
-a CPU architecture (arm64v8|amd64)
-v Ubuntu version (22.04|20.04|18.04)
EOF
}
check_values() {
if [[ -z $ARCH ]]; then
cerr "Architecture not found, specify amd64|arm64"
ARCH=$(promptChoiceArch)
fi
if [[ -z $UBUNTU_DISTRO ]]; then
cerr "Ubuntu distro not found, specify 22.04|20.04|18.04"
UBUNTU_DISTRO=$(promptChoiceUbuntuDistro)
fi
if [[ -z $QUERY_DISTRO ]]; then
cerr "ROS distro not found, specify iron|humble|foxy|galactic|noetic|melodic"
QUERY_DISTRO=$(promptChoiceROSDistro)
fi
if ! valid_args; then
if [ "$FORCE" == 1 ]; then
cerr "ROS distro not compatible with your ubuntu distribution"
echo "22.04: iron | humble"
echo "20.04: noetic | galactic | foxy"
echo "18.04: melodic"
cout "Retry with other arguments"
exit 1
fi
cerr "ROS distro not compatible with your ubuntu distribution"
echo "22.04: iron | humble"
echo "20.04: noetic | galactic | foxy"
echo "18.04: melodic"
cout "Specify iron|humble|foxy|galactic|noetic|melodic"
QUERY_DISTRO=$(promptChoiceROSDistro)
fi
cout "The Supervisor package for ubuntu $UBUNTU_DISTRO $ARCH and Ros $QUERY_DISTRO will be downloaded"
}
# Script Options
ALWAYS_YES=0
FORCE=0
WANTED_ROS=""
WANTED_ARCH=""
WANTED_UBUNTU=""
while getopts hyfr:a:v: opt; do
case $opt in
h)
show_help
exit 0
;;
y)
ALWAYS_YES=1
;;
f)
FORCE=1
;;
r)
WANTED_ROS="$OPTARG"
;;
a)
WANTED_ARCH="$OPTARG"
;;
v)
WANTED_UBUNTU="$OPTARG"
;;
*)
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
# Main
ctitle "3Laws Supervisor Installer (v$SCRIPT_VERSION)"
if [ "$FORCE" == 1 ] && { [ -z "$WANTED_ARCH" ] || [ -z "$WANTED_ROS" ] || [ -z "$WANTED_UBUNTU" ]; }; then
cerr "The force arg requires all information to be provided, arch, ros and ubuntu version"
exit 1
fi
HAS_ROS1=0
if command -v roscore &>/dev/null; then
HAS_ROS1=1
ROS1_DISTRO=$(rosversion -d)
QUERY_DISTRO=$ROS1_DISTRO
fi
HAS_ROS2=0
if command -v ros2 &>/dev/null; then
HAS_ROS2=1
QUERY_DISTRO=$ROS_DISTRO
fi
UBUNTU_DISTRO=$(cat /etc/*-release | grep VERSION_ID | grep -oE "[0-9]{2}.[0-9]{2}")
ARCH=amd64
case "$(uname -i)" in
arm* | aarch*)
ARCH=arm64
;;
esac
if [ $FORCE == 0 ]; then
if [ -n "$WANTED_UBUNTU" ]; then
if [ "$UBUNTU_DISTRO" != "$WANTED_UBUNTU" ]; then
cwarn "Specified Ubuntu version does not match the detected one, select your version:"
UBUNTU_DISTRO=$(promptChoiceUbuntuDistro)
fi
fi
if [ -n "$WANTED_ARCH" ]; then
if [[ $ARCH != "$WANTED_ARCH" ]]; then
cwarn "Specified Architecture does not match the detected one, select your version:"
ARCH=$(promptChoiceArch)
fi
fi
if [ -n "$WANTED_ROS" ]; then
if [ "$QUERY_DISTRO" != "$WANTED_ROS" ]; then
cwarn "Specified ROS version does not match the detected one, select your version:"
QUERY_DISTRO=$(promptChoiceROSDistro)
fi
fi
if [ $HAS_ROS1 == "$HAS_ROS2" ]; then
cwarn "Unable to select a ROS distribution, select your version:"
QUERY_DISTRO=$(promptChoiceROSDistro)
fi
else
if [[ $UBUNTU_DISTRO != "$WANTED_UBUNTU" ]]; then
cwarn "Specified Ubuntu version does not match the detected one, continue with $WANTED_UBUNTU"
UBUNTU_DISTRO=$WANTED_UBUNTU
fi
if [[ $ARCH != "$WANTED_ARCH" ]]; then
cwarn "Specified Architecture does not match the detected one, continue with $WANTED_ARCH"
ARCH=$WANTED_ARCH
fi
if [[ $QUERY_DISTRO != "$WANTED_ROS" ]]; then
cwarn "Specified ROS version does not match the detected one, continue with $WANTED_ROS"
QUERY_DISTRO=$WANTED_ROS
fi
fi
# Check Write permission
if [ ! -w . ]; then
cerr "No write permission in current directory, please run this script in a writable directory"
exit 1
fi
# Check args validity
check_values
# Define variables.
GH_API="https://api.github.com"
GH_REPO="$GH_API/repos/3LawsRobotics/3laws"
GH_TAGS="$GH_REPO/releases/latest"
CURL_ARGS="-LJO#"
curl -o /dev/null -s $GH_REPO || {
echo "Error: Invalid repo, token or network issue!"
exit 1
}
PACKAGE_NAME="lll-supervisor-full-${QUERY_DISTRO}"
REGEX_QUERY="${PACKAGE_NAME}_[0-9]\+\.[0-9]\+\.[0-9]\+-[0-9]\+_$ARCH"
# Read asset tags.
RESPONSE=$(curl -s -H "application/vnd.github+json" $GH_TAGS)
ASSET_NAME=$(echo "$RESPONSE" | grep -o "name.:.\+${REGEX_QUERY}.deb" | cut -d ":" -f2- | cut -d "\"" -f2-)
ASSET_ID=$(echo "$RESPONSE" | grep -C3 "name.:.\+$REGEX_QUERY" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | cut -d'=' -f2-)
[ "$ASSET_ID" ] || {
VALID_ASSETS=$(echo "$RESPONSE" | grep -o "name.:.\+lll-supervisor-full-[a-zA-Z]\+_[0-9]\+\.[0-9]\+\.[0-9]\+-[0-9]_[a-zA-Z0-9]\+" | cut -d ":" -f2- | cut -d "\"" -f2-)
if [ -z "$VALID_ASSETS" ]; then
cerr "No valid assets are available for your configuration, please contact support@3lawsrobotics.com"
else
echo -e "Error: Failed to get asset id, valid packages:\n$VALID_ASSETS"
fi
exit 1
}
GH_ASSET="$GH_REPO/releases/assets/$ASSET_ID"
DOWNLOAD=0
if [ -f "$ASSET_NAME" ]; then
cwarn "$ASSET_NAME already in your directory."
OVERWRITE=$(promptYesNo "Do you want to overwrite $ASSET_NAME in the current directory ?" 1)
if [ "$OVERWRITE" -eq 1 ]; then
cwarn "Removing $ASSET_NAME"
rm "$ASSET_NAME"
DOWNLOAD=1
fi
else
DOWNLOAD=$(promptYesNo "Do you want to download $ASSET_NAME in the current directory" 1)
fi
if [[ $DOWNLOAD == 1 ]]; then
echo "Downloading package..." >&2
curl $CURL_ARGS -s -H 'Accept: application/octet-stream' "$GH_ASSET"
cout "Package $ASSET_NAME has been downloaded."
else
cwarn "Package not downloaded."
fi
if [[ -f "$ASSET_NAME" ]]; then
INSTALL=$(promptYesNo "Do you want to install $ASSET_NAME and its dependency (libstd++13) ?" 1)
if [ "$INSTALL" == 0 ]; then
cout "Package downloaded but not installed, if you choose to install manually, be sure to have libstd++13 on your system"
else
SUDO=""
if [[ "$EUID" -ne 0 && $INSTALL == 1 ]]; then
cwarn "To install the supervisor automatically some commands must be run as sudo"
SUDO="sudo "
fi
# Install dependencies
STDLIB=libstdc++-13-dev
STDLIB_INSTALLED=0
dpkg -l $STDLIB &>/dev/null && STDLIB_INSTALLED=1
if [[ $STDLIB_INSTALLED == 0 ]]; then
cout "Installing dependencies..."
$SUDO apt-get update &>/dev/null
fi
if [[ $STDLIB_INSTALLED == 0 ]]; then
{
{
$SUDO apt-get install -y --no-install-recommends $STDLIB &>/dev/null
} || {
$SUDO apt-get install -y --no-install-recommends software-properties-common &>/dev/null
$SUDO add-apt-repository -y "ppa:ubuntu-toolchain-r/test" &>/dev/null
cwarn "Added 'ppa:ubuntu-toolchain-r/test' to apt sources!"
$SUDO apt-get install -y --no-install-recommends $STDLIB &>/dev/null
}
cwarn "Installed '$STDLIB' on system!"
} || {
cerr "Failed to install '$STDLIB' dependency!"
exit 65
}
fi
# Install package
cout "Installing package..."
$SUDO apt install -f ./"$ASSET_NAME" -y --no-install-recommends
# Create 3laws directory
cout "Creating 3laws config directory..."
mkdir -p "$HOME/.3laws/config"
cout "Removing artifacts..."
rm "$ASSET_NAME"
cout "Success installation!"
fi
else
cout "Package not found...If you encounter any issues, please contact: support@3lawsrobotics.com"
fi