-
Notifications
You must be signed in to change notification settings - Fork 11
/
upgrade-ssh-1.x-to-2.x.sh
executable file
·291 lines (252 loc) · 6.9 KB
/
upgrade-ssh-1.x-to-2.x.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
#!/bin/bash
main_script_name="upgrade-1.x-to-2.x.sh"
RESINHUP_ARGS=()
UUIDS=""
SSH_HOST=""
NOCOLORS=no
FAILED=0
NUM=0
QUEUE=""
MAX_THREADS=5
# Help function
function help {
cat << EOF
Wrapper to run host OS updates on fleet of devices over ssh.
$0 <OPTION>
Options:
-h, --help
Display this help and exit.
--staging
Do this update for devices in staging.
By default resinhup assumes the devices are in production.
-u <UUID>, --uuid <UUID>
Update this UUID. Multiple -u can be provided to updated mutiple devices.
-s <SSH_HOST>, --ssh-host <SSH_HOST>
SSH host to be used in ssh connections (e.g. resin or resinstaging).
-m <MAX_THREADS>, --max-threads <MAX_THREADS>
Maximum number of threads to be used when updating devices in parallel. Useful to
not network bash network if devices are in the same one. If value is 0, all
updates will start in parallel.
--hostos-version <HOSTOS_VERSION>
Run ${main_script_name} with --hostos-version <HOSTOS_VERSION>, use e.g. 2.2.0+rev1
See ${main_script_name} help for more details.
This is a mandatory argument.
--supervisor-version <SUPERVISOR_VERSION>
Run ${main_script_name} with --supervisor-version <SUPERVISOR_VERSION>, use e.g. 6.2.5
See ${main_script_name} help for more details.
--nolog
Run ${main_script_name} with --nolog
See ${main_script_name} help for more details. For running over ssh this is likely
recommended, as otherwise the log is just kept on the device, the local log
on the computer running the remote updater script will have only log headers.
--no-reboot
Run ${main_script_name} with --no-reboot . See ${main_script_name} help for more details.
--no-colors
Avoid terminal colors.
EOF
}
# Log function helper
function log {
local COL
local COLEND='\e[0m'
local loglevel=LOG
case $1 in
ERROR)
COL='\e[31m'
loglevel=ERR
shift
;;
WARN)
COL='\e[33m'
loglevel=WRN
shift
;;
SUCCESS)
COL='\e[32m'
loglevel=LOG
shift
;;
*)
COL=$COLEND
loglevel=LOG
;;
esac
if [ "$NOCOLORS" == "yes" ]; then
COLEND=''
COL=''
fi
ENDTIME=$(date +%s)
printf "${COL}[%09d%s%s${COLEND}\n" "$((ENDTIME - STARTTIME))" "][$loglevel]" "$1"
if [ "$loglevel" == "ERR" ]; then
exit 1
fi
}
cleanstop() {
log WARN "Force close requested. Waiting for already started updates... Please wait!"
while [ -n "$QUEUE" ]; do
checkqueue
sleep 0.5
done
wait
log ERROR "Forced stop."
exit 1
}
trap 'cleanstop' SIGINT SIGTERM
function addtoqueue {
NUM=$((NUM+1))
QUEUE="$QUEUE $1"
}
function regeneratequeue {
OLDREQUEUE=$QUEUE
QUEUE=""
NUM=0
for entry in $OLDREQUEUE; do
PID=$(echo "$entry" | cut -d: -f1)
if [ -d "/proc/$PID" ] ; then
QUEUE="$QUEUE $entry"
NUM=$((NUM+1))
fi
done
}
function checkqueue {
OLDCHQUEUE=$QUEUE
for entry in $OLDCHQUEUE; do
local _PID
_PID=$(echo "$entry" | cut -d: -f1)
if [ ! -d "/proc/$_PID" ] ; then
wait "$_PID"
local _exitcode=$?
local _UUID
_UUID=$(echo "$entry" | cut -d: -f2)
if [ "$_exitcode" != "0" ]; then
log WARN "Updating $_UUID failed."
FAILED=1
else
log SUCCESS "Updating $_UUID succeeded."
fi
regeneratequeue
break
fi
done
}
#
# MAIN
#
# Get the absolute script location
pushd "$(dirname "$0")" > /dev/null 2>&1
SCRIPTPATH=$(pwd)
popd > /dev/null 2>&1
# The script running the update on the device
UPDATE_SCRIPT="$SCRIPTPATH/${main_script_name}"
# Log timer
STARTTIME=$(date +%s)
# If no arguments passed, just display the help
if [ $# -eq 0 ]; then
help
exit 0
fi
# Parse arguments
while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
-h|--help)
help
exit 0
;;
--staging)
RESINHUP_ARGS+=( "--staging" )
;;
-u|--uuid)
if [ -z "$2" ]; then
log ERROR "\"$1\" argument needs a value."
fi
UUIDS="$UUIDS $2"
shift
;;
-s|--ssh-host)
if [ -z "$2" ]; then
log ERROR "\"$1\" argument needs a value."
fi
SSH_HOST=$2
shift
;;
-m|--max-threads)
if [ -z "$2" ]; then
log ERROR "\"$1\" argument needs a value."
fi
MAX_THREADS=$2
shift
;;
--hostos-version)
if [ -z "$2" ]; then
log ERROR "\"$1\" argument needs a value."
fi
HOSTOS_VERSION=$2
RESINHUP_ARGS+=( "--hostos-version $HOSTOS_VERSION" )
shift
;;
--supervisor-version)
if [ -z "$2" ]; then
log ERROR "\"$1\" argument needs a value."
fi
SUPERVISOR_VERSION=$2
RESINHUP_ARGS+=( "--supervisor-version $SUPERVISOR_VERSION" )
shift
;;
--no-reboot)
RESINHUP_ARGS+=( "--no-reboot" )
;;
--nolog)
RESINHUP_ARGS+=( "--nolog" )
;;
--no-colors)
NOCOLORS=yes
;;
*)
log WARN "Unrecognized option $1."
;;
esac
shift
done
# Check argument(s)
if [ -z "$UUIDS" ] || [ -z "$SSH_HOST" ]; then
log ERROR "No UUID and/or SSH_HOST specified."
fi
CURRENT_UPDATE=0
NR_UPDATES=$(echo "$UUIDS" | wc -w)
# 0 threads means Parallelise everything
if [ "$MAX_THREADS" -eq 0 ]; then
MAX_THREADS=$NR_UPDATES
fi
# Update each UUID
for uuid in $UUIDS; do
CURRENT_UPDATE=$((CURRENT_UPDATE+1))
log "[$CURRENT_UPDATE/$NR_UPDATES] Updating $uuid on $SSH_HOST."
log_filename="$uuid.upgrade1x2x.log"
if ! ssh "$SSH_HOST" host -s "${uuid}" "exit" > /dev/null 2>&1; then
log WARN "[$CURRENT_UPDATE/$NR_UPDATES] Can't connect to device. Skipping..."
continue
fi
# Connect to device
echo "Running ${main_script_name} ${RESINHUP_ARGS[*]} ..." >> "$log_filename"
ssh "$SSH_HOST" host -s "${uuid}" "bash -s" -- "${RESINHUP_ARGS[@]}" < "${UPDATE_SCRIPT}" >> "$log_filename" 2>&1 &
# Manage queue of threads
PID=$!
addtoqueue $PID:$uuid
while [ "$NUM" -ge "$MAX_THREADS" ]; do
checkqueue
sleep 0.5
done
done
# Wait for all threads
log "Waiting for all threads to finish..."
while [ -n "$QUEUE" ]; do
checkqueue
sleep 0.5
done
wait
if [ $FAILED -eq 1 ]; then
log ERROR "At least one device failed to update."
fi
# Success
exit 0