-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-lxc-containers.sh
executable file
·67 lines (50 loc) · 1.74 KB
/
update-lxc-containers.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
#!/bin/bash
if ! which lxc >/dev/null; then
exit 0 #no containers
fi
. update-snap.sh lib
lxc list 2>/dev/null >/dev/null
if [ ! $? -eq 0 ]; then
echo "Error in executing lxc list. Perhaps lxc was not installed properly" >/dev/stderr
exit 0 #error when accessing containers. We are not going to fix it here, so just return.
fi
lxc_list_stopped=$(lxc list --format csv | grep -E '^[^,]+,STOPPED' | grep -o -E '^[^,]+')
lxc_list_running=( $(lxc list --format csv | grep -E '^[^,]+,RUNNING' | grep -o -E '^[^,]+') )
function update_running_lxc {
local lxc_name=$1
ips=( $(eval echo $(lxc list -c4 --format csv $lxc_name | sed -E "s/ \([^\)]+\) ?//g") ) )
for ip in "${ips[@]}"; do
if ssh -o PreferredAuthentications=publickey ${ip} /bin/true >/dev/null; then
install_run_update_all ${ip} ${lxc_name}
break
fi
done
}
function install_run_update_all {
local ip=$1
local lxc_name=$2
local homedir=$(ssh $ip pwd)
if ! [ $? -eq 0 ]; then
return 1; # cannot access the container
fi
homedir="${homedir}/tmp/update-all"
if ! ssh $ip ls "${homedir}/.git" 2>/dev/null >/dev/null; then
ssh $ip mkdir -p "${homedir}"
for file in $(pwd)/*.sh; do
lxc file push "${file}" "$2/${homedir}/"
done
fi
ssh $ip "cd $homedir; ./update-all.sh"
}
host_is_disabled=$(is_host_disabled api.snapcraft.io)
if [ "host_is_disabled" = "1" ]; then
echo "Enabling api.snapcraft.io..."
enable_host api.snapcraft.io 127.0.0.1
fi
for lxc_name in "${lxc_list_running[@]}"; do
update_running_lxc "${lxc_name}"
done
if [ "$host_is_disabled" = "1" ]; then
echo "Disabling back api.snapcraft.io..."
disable_host api.snapcraft.io 127.0.0.1
fi