-
Notifications
You must be signed in to change notification settings - Fork 12
/
car_activation.sh
executable file
·439 lines (364 loc) · 14.1 KB
/
car_activation.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
#!/usr/bin/env bash
USAGE()
{
echo -e -n "\nUsage, run as root: $0 -h HOSTNAME -p PASSWORD [ -c SSMCODE -i SSMID -r AWS_REGION ] [ -s SSID -w WIFI_PASSWORD]"
echo -e -n "\nIf no details are provided for SSM the agent is installed but not activated\n\n"
exit 0
}
# Check we have the privileges we need
if [ `whoami` != root ]; then
echo -e -n "\nPlease run this script as root using 'sudo'\n"
exit 0
fi
oldHost=NULL
varHost=NULL
varPass=NULL
ssmCode=NULL
ssmId=NULL
ssmRegion=NULL
ssid=NULL
wifiPass=NULL
optstring=":h:p:c:i:r:s:w:"
while getopts $optstring arg; do
case ${arg} in
h) varHost=${OPTARG};;
p) varPass=${OPTARG};;
c) ssmCode=${OPTARG};;
i) ssmId=${OPTARG};;
r) ssmRegion=${OPTARG};;
s) ssid=${OPTARG};;
w) wifiPass=${OPTARG};;
?) USAGE ;;
esac
done
if [ $OPTIND -eq 1 ]; then
echo -e -n "\nNo options selected.\n"
USAGE
fi
# Disable IPV6 networking
DISABLE_IPV6() {
echo -e -n "\n\nDISABLE_IPV6\n"
# Disable IPV6 on all interfaces
echo -e -n "\n- Disable IPV6"
cp /etc/sysctl.conf ${backupDir}/sysctl.conf.bak
printf "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
}
# Optionally create a WiFi connection service
CREATE_WIFI_SERVICE()
{
echo -e -n "\n\nCREATE_WIFI_SERVICE\n"
# Sort out the Wifi (if we have SSID + Password)
if [ ${ssid} != NULL ] && [ ${wifiPass} != NULL ]; then
echo -e -n "\n- Adding WiFi as a service: ${ssid} \n"
mkdir /etc/deepracer-wifi
cat > /etc/deepracer-wifi/start-wifi.sh << EOF
#!/bin/sh -e
#
# This should add the hidden DeepRacer network, configure it, then connect to it
#
nmcli c add type wifi con-name reinvent ifname mlan0 ssid ${ssid}
nmcli con modify reinvent wifi-sec.key-mgmt wpa-psk
nmcli con modify reinvent wifi-sec.psk ${wifiPass}
nmcli con up reinvent
exit 0
EOF
chmod u+x /etc/deepracer-wifi/start-wifi.sh
cat > /etc/deepracer-wifi/stop-wifi.sh << EOF
#!/bin/sh -e
#
# This should disconnect from the hidden DeepRacer network
#
nmcli con down reinvent
exit 0
EOF
chmod u+x /etc/deepracer-wifi/stop-wifi.sh
cat > /etc/systemd/system/deepracer-wifi.service << EOF
[Unit]
Description=DeepRacer Wifi Service
After=local-fs.target
After=network.target
[Service]
ExecStart=/etc/deepracer-wifi/start-wifi.sh
RemainAfterExit=true
Type=oneshot
[Install]
WantedBy=multi-user.target
EOF
systemctl enable deepracer-wifi
systemctl start deepracer-wifi
echo -e -n "\nDeepRacer Wifi service installed"
echo -e -n "Checking for connection."
while [ "$(hostname -I)" = "" ]; do
echo -e "\e[1A\e[KNo network: $(date)"
sleep 1
done
echo -e -n "\n ..connected\n\n";
fi
}
# Set the device and car console password
SET_PASSWORD()
{
echo -e -n "\n\nSET_PASSWORD\n"
# Update the DeepRacer console password
echo -e -n "\n- Updating password to: ${varPass}"
tempPass=$(echo -n ${varPass} | sha224sum)
IFS=' ' read -ra encryptedPass <<< ${tempPass}
cp /opt/aws/deepracer/password.txt ${backupDir}/password.txt.bak
sudo printf "${encryptedPass[0]}" > /opt/aws/deepracer/password.txt
}
# Update original AWS DeepRacer car software
DR_CAR_UPDATE()
{
echo -e -n "\n\nDR_CAR_UPDATE\n"
echo -e -n "\n- Updating DeepRacer car software...\n"
# Update ROS cert
curl https://repo.ros2.org/repos.key | apt-key add -
# Get latest key from OpenVINO
curl -o GPG-PUB-KEY-INTEL-SW-PRODUCTS https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS
# Update Ubuntu - removed for now as it takes so long from the standard 20.04 build
# echo -e -n "\n- Updating Ubuntu packages"
# apt-get update
# apt-get upgrade -y -o Dpkg::Options::="--force-overwrite"
# Update DeepRacer packages
echo -e -n "\n- Update DeepRacer packages"
apt-get update
apt-get install -y aws-deepracer-* -o Dpkg::Options::="--force-overwrite"
# Remove redundant packages
echo -e -n "\n- Remove redundant packages"
apt -y autoremove
}
# Set the device hostname
SET_HOSTNAME()
{
echo -e -n "\n\nSET_HOSTNAME\n"
# If changing hostname need to change the flag in network_config.py
# ${systemPath}/site-packages/deepracer_systems_pkg/network_monitor_module/network_config.py
# SET_HOSTNAME_TO_CHASSIS_SERIAL_NUMBER = False
echo -e -n "\n- Set hostname: ${varHost}"
oldHost=$HOSTNAME
hostnamectl set-hostname ${varHost}
cp /etc/hosts ${backupDir}/hosts.bak
rm /etc/hosts
cat ${backupDir}/hosts.bak | sed -e "s/${oldHost}/${varHost}/" > /etc/hosts
cp ${systemPath}/network_monitor_module/network_config.py ${backupDir}/network_config.py.bak
rm ${systemPath}/network_monitor_module/network_config.py
cat ${backupDir}/network_config.py.bak | sed -e "s/SET_HOSTNAME_TO_CHASSIS_SERIAL_NUMBER = True/SET_HOSTNAME_TO_CHASSIS_SERIAL_NUMBER = False/" > ${systemPath}/network_monitor_module/network_config.py
}
# Disable software_update
DISABLE_DR_UPDATE()
{
echo -e -n "\n\nDISABLE_DR_UPDATE\n"
echo -e -n "\n- Disable software update"
cp ${systemPath}/software_update_module/software_update_config.py ${backupDir}/software_update_config.py.bak
rm ${systemPath}/software_update_module/software_update_config.py
cat ${backupDir}/software_update_config.py.bak | sed -e "s/ENABLE_PERIODIC_SOFTWARE_UPDATE = True/ENABLE_PERIODIC_SOFTWARE_UPDATE = False/" > ${systemPath}/software_update_module/software_update_config.py
}
# Install and optionally activate AWS SSM agent
SSM_ACTIVATION()
{
echo -e -n "\n\nSSM_ACTIVATION\n"
# Install SSM Agent - https://github.com/aws/amazon-ssm-agent
# DeepRacer -> https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
# RPi 64 -> https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_arm64/amazon-ssm-agent.deb
# RPi 32 -> https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_arm/amazon-ssm-agent.deb
echo -e -n "\n- Install SSM\n"
mkdir /tmp/ssm
curl https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_${ARCH}/amazon-ssm-agent.deb -o /tmp/ssm/amazon-ssm-agent.deb
dpkg -i /tmp/ssm/amazon-ssm-agent.deb
rm -rf /tmp/ssm
# Enable, Configure and Start SSM if we have the right info
if [ ${ssmCode} != NULL ]; then
echo -e -n "\n- Activate SSM"
systemctl enable amazon-ssm-agent
service amazon-ssm-agent stop
amazon-ssm-agent -register -code "${ssmCode}" -id "${ssmId}" -region "${ssmRegion}"
service amazon-ssm-agent start
echo -e -n "\n- Car ${varHost} should be visible in DREM in ~5 minutes"
fi
}
# Tweaks that only apply to the AWS DeepRacer car
DR_CAR_TWEAKS()
{
echo -e -n "\n\nDR_CAR_TWEAKS\n"
# Disable system suspend
echo -e -n "\n- Disable system suspend"
systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# Increase time before the console locks (helpful when troubleshooting) - 30 minutes
echo -e -n "\n- Increase console time out"
gsettings set org.gnome.desktop.session idle-delay 1800
# Disable network power saving
echo -e -n "\n- Disable network power saving"
echo -e '#!/bin/sh\n/usr/sbin/iw dev mlan0 set power_save off\n' > /etc/network/if-up.d/disable_power_saving
chmod 755 /etc/network/if-up.d/disable_power_saving
# Enable SSH
echo -e -n "\n- Enable SSH"
service ssh start
ufw allow ssh
# Disable Gnome and other services
# - to enable gnome - systemctl set-default graphical
# - to start gnome - systemctl start gdm3
echo -e -n "\n- Disable unused services"
systemctl set-default multi-user
systemctl stop bluetooth
systemctl stop cups-browsed
# Default running service list
# service --status-all | grep '\[ + \]'
# [ + ] acpid
# [ + ] alsa-utils
# [ + ] apparmor
# [ + ] apport
# [ + ] avahi-daemon
# [ + ] binfmt-support
# [ + ] bluetooth
# [ + ] console-setup
# [ + ] cron
# [ + ] cups-browsed
# [ + ] dbus
# [ + ] dnsmasq
# [ + ] fail2ban
# [ + ] grub-common
# [ + ] irqbalance
# [ + ] isc-dhcp-server
# [ + ] keyboard-setup
# [ + ] kmod
# [ + ] lightdm
# [ + ] network-manager
# [ + ] networking
# [ + ] nginx
# [ + ] ondemand
# [ + ] procps
# [ + ] rc.local
# [ + ] resolvconf
# [ + ] rsyslog
# [ + ] speech-dispatcher
# [ + ] ssh
# [ + ] thermald
# [ + ] udev
# [ + ] ufw
# [ + ] urandom
# [ + ] uuidd
# [ + ] watchdog
# [ + ] whoopsie
}
CAR_TWEAKS()
{
echo -e -n "\n\nCAR_TWEAKS\n"
# Disable video stream by default
echo -e -n "\n- Disable video stream"
cp ${bundlePath}/bundle.js ${backupDir}/bundle.js.bak
rm ${bundlePath}/bundle.js
cat ${backupDir}/bundle.js.bak | sed -e "s/isVideoPlaying\: true/isVideoPlaying\: false/" > ${bundlePath}/bundle.js
# Allow multiple logins on the console
echo -e -n "\n- Enable multiple logins to the console"
cp /etc/nginx/sites-enabled/default ${backupDir}/default.bak
rm /etc/nginx/sites-enabled/default
cat ${backupDir}/default.bak | sed -e "s/auth_request \/auth;/#auth_request \/auth;/" > /etc/nginx/sites-enabled/default
# Change the cookie duration
echo -e -n "\n- Update the cookie duration"
cp ${webserverPath}/login.py ${backupDir}/login.py.bak
rm ${webserverPath}/login.py
cat ${backupDir}/login.py.bak | sed -e "s/datetime.timedelta(hours=1)/datetime.timedelta(hours=12)/" > $webserverPath/login.py
# Replace the login page
echo -e -n "\n- Replace the login.html page"
cp ${templatesPath}/login.html ${backupDir}/login.html.bak
rm ${templatesPath}/login.html
mv login.html ${templatesPath}/login.html
# Enable use of model optimizer cache
echo -e -n "\n- Enable model optimizer cache"
patch -p3 -d ${modelOptimizerPath} << 'EOF'
diff --git a/model_optimizer_pkg/model_optimizer_pkg/model_optimizer_node.py b/model_optimizer_pkg/model_optimizer_pkg/model_optimizer_node.py
index 1b6c315..6db49ac 100644
--- a/model_optimizer_pkg/model_optimizer_pkg/model_optimizer_node.py
+++ b/model_optimizer_pkg/model_optimizer_pkg/model_optimizer_node.py
@@ -240,6 +240,15 @@ class ModelOptimizerNode(Node):
"""
if not os.path.isfile(common_params[constants.MOKeys.MODEL_PATH]):
raise Exception(f"Model file {common_params[constants.MOKeys.MODEL_PATH]} not found")
+
+ # Check if model exists
+ if os.path.isfile(os.path.join(common_params[constants.MOKeys.OUT_DIR],
+ f"{common_params[constants.MOKeys.MODEL_NAME]}.bin")):
+ self.get_logger().info(f"Cached model: {common_params[constants.MOKeys.MODEL_NAME]}.xml")
+ return 0, os.path.join(common_params[constants.MOKeys.OUT_DIR],
+ f"{common_params[constants.MOKeys.MODEL_NAME]}.xml")
+
+
cmd = f"{constants.PYTHON_BIN} {constants.INTEL_PATH}{mo_path}"
# Construct the cli command
for flag, value in dict(common_params, **platform_parms).items():
EOF
# Prevent double click on the range buttons to zoom in
echo -e -n "\n- Fix range button zoom issue"
cp ${staticPath}/bundle.css ${backupDir}/bundle.css.bak
sed -i 's/.range-btn-minus button,.range-btn-plus button{background-color:#aab7b8!important;border-radius:4px!important;border:1px solid #879596!important}/.range-btn-minus button,.range-btn-plus button{background-color:#aab7b8!important;border-radius:4px!important;border:1px solid #879596!important;touch-action: manipulation;user-select: none;}/' ${staticPath}/bundle.css
}
# Check the operating system version and architecture
# Possible OS versions
# Ubuntu 16.04 (unsupported), 20.04, 22.04
# Possible hardware
# amd64 (Intel ATOM - AWS DeepRacer)
# arm64 (Raspberry 4)
# Check version
DEVICE=dr # [dr, rpi]
ARCH=amd64 # [amd64, arm64]
. /etc/lsb-release
if [ $DISTRIB_RELEASE = "16.04" ]; then
echo -e -n "\n- Ubuntu 16.04 detected"
echo -e -n "\nPlease update your car to at least 20.04 -> https://docs.aws.amazon.com/deepracer/latest/developerguide/deepracer-ubuntu-update.html\n"
exit 1
elif [ $DISTRIB_RELEASE = "20.04" ] || [ $DISTRIB_RELEASE = "22.04" ]; then
echo -e -n "\n- Ubuntu 20.04 or 22.04 detected"
pythonPath=python3.8
if [ $DISTRIB_RELEASE = "22.04" ]; then
pythonPath=python3.10
fi
# Set some paths
bundlePath=/opt/aws/deepracer/lib/device_console/static
systemPath=/opt/aws/deepracer/lib/deepracer_systems_pkg/lib/${pythonPath}/site-packages/deepracer_systems_pkg
templatesPath=/opt/aws/deepracer/lib/device_console/templates
staticPath=/opt/aws/deepracer/lib/device_console/static
webserverPath=/opt/aws/deepracer/lib/webserver_pkg/lib/${pythonPath}/site-packages/webserver_pkg
modelOptimizerPath=/opt/aws/deepracer/lib/model_optimizer_pkg/lib/${pythonPath}/site-packages/model_optimizer_pkg
# Create backup directory
homeDir=$(eval echo ~${SUDO_USER})
backupDir=${homeDir}/backup
if [ ! -d ${backupDir} ]; then
echo -e -n "\n- Creating backup directory: ${backupDir}"
mkdir ${backupDir}
fi
# What are we running on?
hw=$(tr -d '\0' </proc/device-tree/model)
if [[ $hw == *"Raspberry Pi 4"* ]]; then
echo -e -n "\n- Raspberry Pi"
DEVICE=rpi
ARCH=arm64
fi
# All cars
if [ $varHost != NULL ]; then
SET_HOSTNAME
fi
SET_PASSWORD
SSM_ACTIVATION
# AWS DeepRacer only
if [ $DEVICE = "dr" ]; then
DISABLE_IPV6
CREATE_WIFI_SERVICE
DR_CAR_UPDATE
DISABLE_DR_UPDATE
DR_CAR_TWEAKS
fi
# Raspberry Pi only
# if [ $DEVICE = "rpi" ]; then
# fi
# All cars
CAR_TWEAKS
else
echo -e -n "\nSorry, not sure what we're running here, terminating.\n"
exit 1
fi
# Restart services
echo -e -n "\n- Restarting services\n"
systemctl restart deepracer-core
service nginx restart
echo -e -n "\nDone!\n\n"