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
|
#!/bin/bash -e
show_help() {
echo "usage: remote.refresh snap [--channel CHANNEL] <SNAPNAME>"
echo " remote.refresh full [--channel CHANNEL]"
echo " remote.refresh disable-refreshes"
echo ""
echo "SNAPNAME: allowed options are: kernel, core, base, gadget, snapd or the snap name"
echo ""
echo "Available options:"
echo " -h --help show this help message."
echo ""
}
check_refresh_after_reboot() {
local refresh_channel=$1
local snap=$2
echo "remote.refresh: checking snap \"$snap\" refresh after reboot"
remote.wait-for reboot
remote.wait-for snap-command
remote.retry -n 10 --wait 2 "snap info $snap | grep -q -E \"tracking: +(latest/${refresh_channel}|${refresh_channel})\""
echo "remote.refresh: snap \"$snap\" refreshed correctly"
}
check_refresh() {
local refresh_channel=$1
local snap=$2
echo "remote.refresh: checking snap \"$snap\" refresh with no reboot"
remote.wait-for ssh -n 60 --wait 2
remote.wait-for snap-command
remote.retry -n 10 --wait 2 "snap info $snap | grep -q -E \"tracking: +(latest/${refresh_channel}|${refresh_channel})\""
echo "remote.refresh: snap \"$snap\" refreshed correctly"
}
check_waiting_for_reboot() {
remote.exec "sudo journalctl -n 30" | grep -q "Waiting for system reboot"
}
check_ready_to_refresh() {
remote.wait-for ssh
if check_waiting_for_reboot; then
echo "remote.refresh: waiting for system reboot"
remote.wait-for reboot
fi
remote.wait-for refresh
}
do_refresh() {
local snap_name=$1
local refresh_channel=$2
echo "remote.refresh: triggering refresh for snap \"$snap_name\" on channel \"$refresh_channel\""
output=$(remote.exec "sudo snap refresh --channel ${refresh_channel} $snap_name 2>&1" || echo "snapd is about to reboot the system")
if echo "$output" | grep -E "(no updates available|cannot refresh \"$snap_name\"|is not installed)"; then
echo "remote.refresh: snap \"$snap_name\" has no updates available"
elif echo "$output" | grep -E "snapd is about to reboot the system"; then
remote.exec --timeout 3 "sudo reboot" || true
check_refresh_after_reboot "$refresh_channel" "$snap_name"
else
check_refresh "$refresh_channel" "$snap_name"
fi
}
process_refresh() {
local snap_name=$1
local refresh_channel=$2
if [ -z "$refresh_channel" ]; then
# Tracking is retrieved from snap info command because in old versions of
# snapd the tracking is not included in the snap list output
refresh_channel="$(remote.exec "snap info $snap_name" | grep -E '^tracking:' | awk '{ print $2 }')"
fi
echo "remote.refresh: Refreshing $snap_name snap from $refresh_channel channel"
do_refresh "$snap_name" "$refresh_channel"
}
refresh_fundamental() {
snap_name=$1
regex=$2
refresh_channel=$3
echo "remote.refresh: starting $snap_name refresh process"
snap_name="$(remote.exec "snap list" | grep -E "$regex" | awk '{ print $1 }')"
if [ -z "$snap_name" ]; then
echo "remote.refresh: no $snap_name snap to update"
return
fi
if [ "$(echo "$snap_name" | wc -l)" -gt 1 ]; then
echo "remote.refresh: there is more than 1 $snap_name snap to update, skipping"
return 0
fi
process_refresh "$snap_name" "$refresh_channel"
}
refresh_kernel() {
local refresh_channel=$1
local snap_name regex
snap_name='kernel'
regex='(kernel$|kernel,)'
refresh_fundamental "$snap_name" "$regex" "$refresh_channel"
}
refresh_gadget() {
local refresh_channel=$1
local snap_name regex
snap_name='gadget'
regex='(gadget$|gadget,)'
refresh_fundamental "$snap_name" "$regex" "$refresh_channel"
}
refresh_snapd() {
local refresh_channel=$1
local snap_name regex
snap_name='snapd'
regex='^snapd.*(snapd$|snapd,)'
refresh_fundamental "$snap_name" "$regex" "$refresh_channel"
}
refresh_core() {
local refresh_channel=$1
local snap_name regex
snap_name='core'
regex='^core.*(core$|core,)'
refresh_fundamental "$snap_name" "$regex" "$refresh_channel"
}
refresh_core_base() {
local refresh_channel=$1
local snap_name regex
snap_name='core base'
regex='^core.* (base$|base,)'
refresh_fundamental "$snap_name" "$regex" "$refresh_channel"
}
refresh_snap() {
local snapname=$1
local refresh_channel=$2
if [ -z "$snapname" ]; then
echo "remote.refresh: snap name to refresh is not specified"
return 1
fi
if ! remote.exec "snap list $snapname"; then
echo "remote.refresh: no $snapname snap to update"
return 0
fi
snap_line="$(remote.exec "snap list $snapname" | tail -1)"
process_refresh "$snap_line" "$refresh_channel"
}
refresh_all() {
# Run update and make "|| true" to continue when the connection is closed by remote host or not any snap to update
remote.exec "sudo snap refresh" || true
remote.wait-for ssh
}
get_boot_id() {
remote.exec "cat /proc/sys/kernel/random/boot_id"
}
prevent_autorefresh() {
remote.wait-for ssh
state_path="$(remote.exec 'find /writable -name state.json 2>/dev/null' || true)"
if [ -z "$state_path" ]; then
echo "remote.refresh: state file not found in writable"
state_path="$(remote.exec 'find / -name state.json 2>/dev/null' || true)"
fi
if [ -z "$state_path" ]; then
echo "remote.refresh: state.json file not found"
exit 1
fi
remote.exec "sudo cp $state_path /tmp/state.json"
remote.exec "sudo chmod 644 /tmp/state.json"
remote.pull /tmp/state.json state.json
echo "remote.refresh: state file retrieved"
jq ".data[\"last-refresh\"] = \"$(date +%Y-%m-%dT%H:%M:%S%:z)\"" state.json > state.json.new
echo "remote.refresh: state file just updated"
remote.push state.json.new /tmp/state.json.new
remote.exec "sudo cp /tmp/state.json.new $state_path"
remote.exec "sudo chmod 600 $state_path"
echo "remote.refresh: updated state file restored"
rm -f state.json state.json.new
}
disable_refreshes() {
if ! command -v jq &>/dev/null; then
snap install --devmode jq
fi
echo "remote.refresh: modifying state to make it look like the last refresh just happened"
remote.exec "sudo systemctl stop snapd.socket snapd.service"
prevent_autorefresh
remote.exec "sudo systemctl start snapd.socket snapd.service"
echo "remote.refresh: minimizing risk of hitting refresh schedule"
remote.exec "sudo snap set core refresh.schedule=00:00-23:59"
remote.exec "sudo snap refresh --time --abs-time" | grep -Eq "last: 2[0-9]{3}"
}
full_refresh() {
echo "remote.refresh: starting full refresh"
check_ready_to_refresh
disable_refreshes
check_ready_to_refresh
refresh_core
check_ready_to_refresh
refresh_core_base
check_ready_to_refresh
refresh_snapd
check_ready_to_refresh
refresh_kernel
check_ready_to_refresh
refresh_all
check_ready_to_refresh
}
snap_refresh() {
local channel snapname
while [ $# -gt 0 ]; do
case "$1" in
--channel)
channel=$2
shift 2
;;
*)
snapname=$1
shift
;;
esac
done
if [ -z "$snapname" ]; then
echo "remote.refresh: snap name to refresh is not specified"
return 1
fi
case "$snapname" in
core)
refresh_core "$channel"
;;
base)
refresh_core_base "$channel"
;;
snapd)
refresh_snapd "$channel"
;;
kernel)
refresh_kernel "$channel"
;;
gadget)
refresh_gadget "$channel"
;;
*)
refresh_snap "$snapname" "$channel"
;;
esac
}
main() {
if [ $# -eq 0 ]; then
show_help
exit
fi
local action
case "$1" in
-h|--help)
show_help
exit
;;
full)
action=full_refresh
shift
;;
snap)
action=snap_refresh
shift
;;
disable-refreshes)
action=disable_refreshes
shift
;;
*)
echo "remote.refresh: unsupported parameter $1" >&2
exit 1
;;
esac
if [ -z "$(declare -f "$action")" ]; then
echo "remote.refresh: no such command: $action"
show_help
exit 1
fi
"$action" "$@"
}
main "$@"
|