risotto/ansible/sbin/update_images

88 lines
2.9 KiB
Text
Raw Normal View History

2022-10-01 22:33:11 +02:00
#!/bin/bash -e
2023-06-22 16:22:54 +02:00
TLS_SERVER=$1
if [ -z "$TLS_SERVER" ]; then
echo "$0 nom_tls_server"
exit 1
fi
DO_NOT_START=$2
REBOOT_EVERY_MONDAY=$3
2022-10-01 22:33:11 +02:00
# root dir configuration
RISOTTO_DIR="/var/lib/risotto"
RISOTTO_IMAGE_DIR="$RISOTTO_DIR/images"
# image configuration
IMAGE_BASE_RISOTTO_BASE_DIR="$RISOTTO_IMAGE_DIR/image_bases"
2022-12-21 16:35:58 +01:00
if [ -z "$1" ]; then
2023-01-23 20:23:32 +01:00
rm -f $IMAGE_BASE_RISOTTO_BASE_DIR*.build
2022-12-21 16:35:58 +01:00
fi
2023-01-23 20:23:32 +01:00
mkdir -p /var/log/risotto
ls /var/lib/risotto/images_files/ | while read image; do
if [ -d /var/lib/risotto/images_files/"$image" ]; then
echo
echo "Install image $image" | tee -a /var/log/risotto/update_images.log
2023-06-22 16:22:54 +02:00
/usr/local/sbin/build_image "$image" | tee -a /var/log/risotto/update_images.log || (echo "PROBLEME" | tee -a /var/log/risotto/update_images.log; true)
2023-01-23 20:23:32 +01:00
fi
done
2022-10-01 22:33:11 +02:00
2023-06-22 16:22:54 +02:00
idx=0
if [ -z "$DO_NOT_START" ]; then
machinectl reboot "$TLS_SERVER" || machinectl start "$TLS_SERVER"
while true; do
status=$(machinectl -q shell "$TLS_SERVER" /usr/bin/systemctl is-system-running 2>/dev/null || echo "not started")
if echo "$status" | grep -q degraded || echo "$status" | grep -q running; then
break
fi
idx=$((idx+1))
if [ $idx = 60 ]; then
echo "le serveur $TLS_SERVER n'a pas encore redémarré"
break
fi
sleep 2
done
fi
2022-10-01 22:33:11 +02:00
MACHINES=""
for nspawn in $(ls /etc/systemd/nspawn/*.nspawn); do
2023-06-22 16:22:54 +02:00
nspawn_file=$(basename "$nspawn")
2022-10-01 22:33:11 +02:00
machine=${nspawn_file%.*}
MACHINES="$MACHINES$machine "
MACHINE_MACHINES_DIR="/var/lib/machines/$machine"
2023-01-23 20:23:32 +01:00
IMAGE_NAME_RISOTTO_IMAGE_NAME="$(cat $RISOTTO_DIR/machines_informations/$machine.image)"
MACHINE_INFO="$RISOTTO_DIR/machines_informations/"
VERSION_MACHINE="$MACHINE_INFO/$machine.version"
2023-06-22 16:22:54 +02:00
if [ -n "$REBOOT_EVERY_MONDAY" ] && [ "$(date +%u)" = 1 ]; then
# update TLS certificate every monday, so stop container
machinectl stop "$machine" 2> /dev/null || true
while true; do
machinectl status "$machine" > /dev/null 2>&1 || break
sleep 1
done
fi
if [ ! -d "$MACHINE_MACHINES_DIR" ]; then
rm -f "$VERSION_MACHINE"
fi
2023-01-23 20:23:32 +01:00
diff -q "$RISOTTO_IMAGE_DIR/$IMAGE_NAME_RISOTTO_IMAGE_NAME".version "$VERSION_MACHINE" &> /dev/null || (
2022-10-17 18:48:32 +02:00
echo "Reinstall machine $machine"
2023-06-22 16:22:54 +02:00
machinectl stop "$machine" 2> /dev/null || true
2022-10-01 22:33:11 +02:00
while true; do
machinectl status "$machine" > /dev/null 2>&1 || break
sleep 1
done
rm -rf "$MACHINE_MACHINES_DIR"
mkdir "$MACHINE_MACHINES_DIR"
2023-06-22 16:22:54 +02:00
cp -a --reflink=auto "$RISOTTO_IMAGE_DIR/$IMAGE_NAME_RISOTTO_IMAGE_NAME/"* "$MACHINE_MACHINES_DIR"
2023-01-23 20:23:32 +01:00
cp -a --reflink=auto "$RISOTTO_IMAGE_DIR/$IMAGE_NAME_RISOTTO_IMAGE_NAME".version "$VERSION_MACHINE"
2022-10-01 22:33:11 +02:00
)
done
2023-06-22 16:22:54 +02:00
if [ -z "$DO_NOT_START" ]; then
echo "start $MACHINES"
2023-01-23 20:23:32 +01:00
machinectl start $MACHINES
2023-06-22 16:22:54 +02:00
sleep 5
journalctl -n 100 --no-pager
2023-01-23 20:23:32 +01:00
diagnose
fi
exit 0