Skip to content

Instantly share code, notes, and snippets.

@gordlea
Last active May 23, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordlea/a2df1d22c1b0f46daad69be0d1948dc0 to your computer and use it in GitHub Desktop.
Save gordlea/a2df1d22c1b0f46daad69be0d1948dc0 to your computer and use it in GitHub Desktop.
Update/snapshot all lxc containers (debian, ubuntu, fedora, centos and alpine supported)
#!/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
#!/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