forked from clearpathrobotics/robot-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.sh
executable file
·437 lines (386 loc) · 12.1 KB
/
upgrade.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
#!/bin/bash
# @author Chris Iverach-Brereton <civerachb@clearpathrobotics.com>
# @author David Niewinski <dniewinski@clearpathrobotics.com>
# @description Restores a backup of a robot's core files and directories, and upgrades relevant # files and directories from the backup ROS distro to the robot's current ROS distro.
############################## FUNCTION DEFINITIONS ############################
function cleanup {
echo "Cleaning Up $(pwd)/$1"
rm -rf $1
echo "Done Cleaning"
}
function promptDefaultNo {
# $1: the prompt
# return: 0 for no, 1 for yes
read -r -p "$1 [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]];
then
echo 1
else
echo 0
fi
}
function promptDefaultYes {
# $1: the prompt
# return: 0 for no, 1 for yes
read -r -p "$1 [Y/n] " response
if [[ "$response" =~ ^([nN][oO]|[nN])$ ]];
then
echo 0
else
echo 1
fi
}
function changeRosDistroPackage {
# change all occurences of the ROS distro codename in an arbitrary string to the new codename
# usage: changeRosDistroPackage $from $to $package
FROM="ros-$1"
TO="ros-$2"
PKG=$3
echo "${PKG//$FROM/$TO}"
}
function changeRosDistroFile {
# change all occurences of the ROS distro codename in a file to the new codename
# this creates a backup of the original file too
# usage: changeRosDistroPackage $from $to $file
FROM="$1"
TO="$2"
FILE=$3
echo "Attempting to migrate ROS distro in file $FILE from $FROM to $TO"
if [ -f $FILE ];
then
cp $FILE $FILE.bak.$(date +"%Y%m%d%H%M%S")
REGEX="s/$FROM/$TO/"
CMD="sed -i '$REGEX' $FILE"
bash -c "$CMD"
else
echo "WARNING: $FILE does not exist. Skipping migration"
fi
}
function tryInstallApt {
# usage: tryInstallApt $package
PACKAGE=$1
sudo apt-get install --reinstall --yes $PACKAGE
RESULT=$?
if [ $RESULT -ne 0 ];
then
echo -e "[WARN] ${RED}Failed to install $PACKAGE ${NC}"
if [ ! -f $HOME/could_not_install_apt.sh ];
then
echo "#!/bin/bash" > $HOME/could_not_install_apt.sh
echo "# The following packages could not be reinstalled" >> $HOME/could_not_install_apt.sh
echo "sudo apt-get install --reinstall --yes \\" >> $HOME/could_not_install_apt.sh
fi
echo " $PACKAGE \\" >> $HOME/could_not_install_apt.sh
fi
}
function tryInstallPip {
# usage: tryInstallPip $package $version
PKG=$1
VERSION=$2
pip install -Iv $PKG==$VERSION
RESULT=$?
if [ $RESULT -ne 0 ];
then
echo -e "[WARN] ${RED}Failed to install $PACKAGE v$VERSION ${NC}"
if [ ! -f $HOME/could_not_install_pip.sh ];
then
echo "#!/bin/bash" > $HOME/could_not_install_pip.sh
echo "# The following packages could not be reinstalled" >> $HOME/could_not_install_pip.sh
fi
echo "pip install $PKG==$VERSION" >> $HOME/could_not_install_pip.sh
fi
}
############################## ARGUMENT PARSING ############################
# the username used during the original backup
# by default on robots we ship this should always be "administrator"
USERNAME=administrator
# the version of _this_ script
VERSION=2.0.2
RED='\e[31m'
GREEN='\e[32m'
NC='\e[39m' # No Color
if [ $# -ge 1 ]
then
if [ $# == 2 ];
then
echo "Overriding default username"
USERNAME=$2
echo "Username: $USERNAME"
fi
CUSTOMER=$1
######################## UNPACK ARCHIVE ##########################
echo "Unpacking backup"
tar -xf $CUSTOMER.tar.gz $CUSTOMER
cd $CUSTOMER
############################ METADATA CHECK ###############################
echo "Checking backup version"
if [ ! -f "BKUP_VERSION" ];
then
echo "ERROR: no backup script version specified; cannot restore this backup!"
exit 1
else
BKUP_VERSION=$(cat BKUP_VERSION)
if [ "$VERSION" != "$BKUP_VERSION" ];
then
cd ..
cleanup "$1"
echo "ERROR: backup was made with a different version; please download v$BKUP_VERSION of this script"
exit 1
else
echo "Backup can be restored with this script"
fi
fi
echo "Checking backup ROS distro"
if [ ! -f "ROS_DISTRO" ];
then
echo "ERROR: no ROS distro specified in backup. Aborting."
cd ..
cleanup "$1"
exit 1
fi
ROSDISTRO=$(cat ROS_DISTRO)
echo "ROS distro in backup is $ROSDISTRO"
OLD_ROSDISTRO="$ROSDISTRO"
echo "Checking current ROS distro"
ROSDISTRO=$(ls -rt /opt/ros | tail -1)
echo "ROS distro is $ROSDISTRO"
echo "+++++ Upgrading from ROS $OLD_ROSDISTRO to $ROSDISTRO +++++"
if [ "$USERNAME" != "$(whoami)" ];
then
echo "WARNING: current user ($(whoami)) does not match expected account ($USERNAME)"
CONTINUE=$(promptDefaultNo "Continue?")
if [ $CONTINUE == 1 ]
then
echo "Ignoring username mismatch"
else
cd ..
cleanup "$1"
echo "User aborted"
exit 0
fi
fi
############################ ROS & BRINGUP #############################
if [ -f setup.bash ];
then
echo "Restoring etc/ros/setup.bash"
sudo cp setup.bash /etc/ros/setup.bash
sudo bash -c "$(declare -f changeRosDistroFile); changeRosDistroFile $OLD_ROSDISTRO $ROSDISTRO /etc/ros/setup.bash"
else
echo "Skipping setup.bash; no backup"
fi
if [ -d ros.d ];
then
echo "Restoring Bringup"
sudo mkdir -p /etc/ros/$ROSDISTRO/ros.d
sudo cp -r ros.d/. /etc/ros/$ROSDISTRO/ros.d
else
echo "Skipping bringup; no backup"
fi
if [ -d usr/sbin ];
then
echo "Restoring sbin"
sudo cp -r usr/sbin/. /usr/sbin
else
echo "Skipping sbin; no backup"
fi
if [ -d rosdep ];
then
echo "Restoring RosDep sources"
sudo cp -r rosdep/. /etc/ros/rosdep/
else
echo "Skipping rosdep sources; no backup"
fi
if [ -d system ];
then
echo "Restoring Systemd"
sudo cp -r system/. /etc/systemd/system
else
echo "Skipping systemd; no backup"
fi
############################ HOME FOLDER #############################
echo "Restoring Home Folder"
cp -r $USERNAME/. ~
changeRosDistroFile $OLD_ROSDISTRO $ROSDISTRO ~/.bashrc
############################ UDEV #############################
if [ -d rules.d ];
then
echo "Restoring udev rules"
sudo cp -r rules.d/. /etc/udev/rules.d
else
echo "Skipping udev rules; no backup"
fi
############################ NETWORK #############################
echo "Restoring Network Setup"
if [ -f interfaces ];
then
echo "Restoring interfaces"
sudo cp interfaces /etc/network/interfaces
else
echo "Skipping /etc/network/interfaces; no backup"
fi
if [ -d netplan ];
then
echo "Restoring netplan files"
sudo cp -r netplan/. /etc/netplan
else
echo "Skipping netplan; no backup"
fi
if [ -f hostname ];
then
echo "Restoring hostname"
sudo cp hostname /etc/hostname
else
echo "Skipping /etc/hostname; no backup"
fi
if [ -f hosts ];
then
echo "Restoring hosts"
sudo cp hosts /etc/hosts
else
echo "Skipping /etc/hosts; no backup"
fi
if [ -d iptables ];
then
echo "Restoring iptables"
sudo cp -r iptables/. /etc/iptables
else
echo "Skipping /etc/iptables; no backup"
fi
############################ RC.LOCAL #############################
if [ -f rc.local ];
then
echo "Restoring rclocal"
sudo cp rc.local /etc/rc.local
else
echo "Skipping rc.local; no backup"
fi
############################ USER GROUPS #############################
echo "Restoring user groups"
while read LINE; do
for GROUP in ${LINE}; do
echo "Adding user to group $GROUP"
echo "sudo usermod -a -G $GROUP $(whoami)"
sudo usermod -a -G $GROUP $(whoami)
done
done < groups
############################ APT #############################
if [ -d sources.list.d ];
then
echo "Restoring APT sources"
sudo cp -r sources.list.d/. /etc/apt/sources.list.d
else
echo "Skipping additional APT sources; no backup present"
fi
INSTALL_APT=$(promptDefaultYes "Reinstall APT packages?")
if [ $INSTALL_APT == 1 ];
then
echo "Reinstalling APT packages"
else
echo "Creating a script to let you reinstall APT packages later"
echo "#!/bin/bash" > $HOME/restore-apt.sh
echo "# Automatically generated by $0 v$VERSION" >> $HOME/restore-apt.sh
echo "# $(date)" >> $HOME/restore-apt.sh
echo "sudo apt-get install --reinstall --yes \\" >> $HOME/restore-apt.sh
chmod +x $HOME/restore-apt.sh
fi
while read PKG; do
# only reinstall ROS packages; there are too many other packages whose versions/names/etc... may have changed
# to be able to reliably reinstall them all
if [[ $PKG = ros-$OLD_ROSDISTRO-* ]];
then
NEW_PKG=$(changeRosDistroPackage "$OLD_ROSDISTRO" "$ROSDISTRO" $PKG)
if [ $INSTALL_APT == 1 ];
then
tryInstallApt $NEW_PKG
else
echo " $NEW_PKG \\" >> $HOME/restore-apt.sh
fi
else
# other packages we'll try to install, but they may not be available
if [ $INSTALL_APT == 1 ];
then
tryInstallApt $PKG
else
echo " $PKG \\" >> $HOME/restore-apt.sh
fi
fi
done < installed_pkgs.list
############################ PIP #############################
# we have to do pip last, as some pip packages may be provided via apt
# this should _only_ install pip packages that were manually installed
INSTALL_PIP=$(promptDefaultYes "Restore PIP packages?")
if [ $INSTALL_PIP == 1 ];
then
echo "Restoring pip packages"
echo "If you have not already restored APT packages you may encounter errors"
else
echo "Creating script so you can restore PIP packages later..."
echo "#!/bin/bash" > $HOME/restore-pip.sh
echo "# Automatically generated by $0 v$VERSION" >> $HOME/restore-pip.sh
echo "# $(date)" >> $HOME/restore-pip.sh
chmod +x $HOME/restore-pip.sh
fi
# the default format of pip list is changing, so to keep this script working explicitly use the legacy format if it's not already
echo "Setting PIP to use legacy list format"
echo "To revert this change, edit $HOME/.config/pip/pip.conf"
if [ ! -f $HOME/.config/pip/pip.conf ];
then
mkdir -p $HOME/.config/pip/
touch $HOME/.config/pip/pip.conf
fi
echo "[list]" >> $HOME/.config/pip/pip.conf
echo "format=legacy" >> $HOME/.config/pip/pip.conf
while read line; do
TOKENS=($line)
PKG=${TOKENS[0]}
VERSION=$(echo ${TOKENS[1]} | sed 's/[)(]//g')
PIP_OUT=$(pip list|grep "^$PKG\s")
TOKENS=($PIP_OUT)
INSTALLED_PKG=${TOKENS[0]}
INSTALLED_VERSION=$(echo ${TOKENS[1]} | sed 's/[)(]//g')
if [ $INSTALL_PIP == 1 ];
then
# check if the package is installed, if it's not then install it
if [[ "$INSTALLED_PKG" == "$PKG" && ("$INSTALLED_VERSION" == "$VERSION" || "$INSTALLED_VERSION" > "$VERSION") ]];
then
echo "pip package $PKG $VERSION (or newer) is already installed"
else
echo "Installing pip package $PKG"
tryInstallPip $PKG $VERSION
fi
else
# add this package to the install script
if [[ "$INSTALLED_PKG" == "$PKG" && ("$INSTALLED_VERSION" == "$VERSION" || "$INSTALLED_VERSION" > "$VERSION") ]];
then
echo "pip package $PKG $VERSION (or newer) is already installed"
else
echo "pip install -Iv $PKG==$VERSION" >> $HOME/restore-pip.sh
fi
fi
done < pip.list
############################ CLEANUP #############################
echo "Done Restoring Files"
cd ..
cleanup "$CUSTOMER"
############################ FINAL MESSAGES #############################
if [ $INSTALL_APT == 0 ];
then
echo -e "Run ${GREEN}$HOME/restore-apt.sh${NC} to resintall APT packages"
else
if [ -f $HOME/could_not_install_apt.sh ];
then
echo -e "[WARN] ${RED}Some APT packages could not be installed. See ${GREEN}$HOME/could_not_install_apt.sh${RED} for more details.${NC}"
fi
fi
if [ $INSTALL_PIP == 0 ];
then
echo -e "Run ${GREEN}$HOME/restore-pip.sh${NC} to resintall PIP packages"
else
if [ -f $HOME/could_not_install_pip.sh ];
then
echo -e "[WARN] ${RED}Some PIP packages could not be installed. See ${GREEN}$HOME/could_not_install_pip.sh${RED} for more details.${NC}"
fi
fi
else
echo "USAGE: bash restore.sh customer_name [username]"
fi