Last active
May 23, 2019 15:31
-
-
Save gordlea/a2df1d22c1b0f46daad69be0d1948dc0 to your computer and use it in GitHub Desktop.
Update/snapshot all lxc containers (debian, ubuntu, fedora, centos and alpine supported)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
snapshot_containers () { | |
running_containers=() | |
while IFS= read -r line; do | |
running_containers+=( "$line" ) | |
done < <( lxc list --columns=ns --format=json \ | |
| jq --raw-output 'map(select(.status | contains("Running")) | .name) | .[]' ) | |
for i in "${running_containers[@]}"; do | |
echo "taking snapshot of apt based container: $i" | |
lxc stop $i | |
sleep 1s | |
lxc snapshot $i | |
lxc start $i | |
done | |
} | |
snapshot_containers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
apt_containers=() | |
while IFS= read -r line; do | |
apt_containers+=( "$line" ) | |
done < <( lxc list --columns=ns --format=json \ | |
| jq --raw-output \ | |
'map(select(.config["image.os"] | contains("ubuntu") or contains("debian")) | .name) | .[]' ) | |
for i in "${apt_containers[@]}"; do | |
echo "updating apt based container: $i" | |
lxc exec $i --force-noninteractive -- sh -c "apt-get --yes update" | |
lxc exec $i --force-noninteractive -- sh -c "apt-get --yes full-upgrade" | |
lxc exec $i --force-noninteractive -- sh -c "apt-get --yes autoremove" | |
done | |
alpine_containers=() | |
while IFS= read -r line; do | |
alpine_containers+=( "$line" ) | |
done < <( lxc list --columns=ns --format=json \ | |
| jq --raw-output \ | |
'map(select(.config["image.os"] | contains("Alpine")) | .name) | .[]' ) | |
for i in "${alpine_containers[@]}"; do | |
echo "updating alpine apk based container: $i" | |
lxc exec $i --force-noninteractive -- sh -c "apk update && apk upgrade" | |
done | |
yum_containers=() | |
while IFS= read -r line; do | |
yum_containers+=( "$line" ) | |
done < <( lxc list --columns=ns --format=json \ | |
| jq --raw-output \ | |
'map(select(.config["image.os"] | contains("Fedora") or contains("Centos")) | .name) | .[]' ) | |
for i in "${yum_containers[@]}" | |
do | |
echo "updating Fedora rpm based container: $i" | |
lxc exec $i --force-noninteractive -- bash -c "[[ \"$(command -v dnf >/dev/null 2>&1; echo $?)\" = \"1\" ]] && yum -y update || dnf -y upgrade" | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment