yunohost_demo/demo_upgrade.sh

71 lines
2.5 KiB
Bash
Raw Normal View History

2016-08-12 17:35:14 +02:00
#!/bin/bash
2016-08-13 01:08:44 +02:00
# Script d'upgrade des 2 conteneurs de demo.
# Ce script n'a vocation qu'a être dans un cron
2016-08-12 17:35:14 +02:00
# Récupère le dossier du script
if [ "${0:0:1}" == "/" ]; then script_dir="$(dirname "$0")"; else script_dir="$PWD/$(dirname "$0" | cut -d '.' -f2)"; fi
2016-08-13 01:08:44 +02:00
LXC_NAME1=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME1= | cut -d '=' -f2)
LXC_NAME2=$(cat "$script_dir/demo_lxc_build.sh" | grep LXC_NAME2= | cut -d '=' -f2)
2016-08-13 22:05:38 +02:00
IP_LXC1=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC1= | cut -d '=' -f2)
IP_LXC2=$(cat "$script_dir/demo_lxc_build.sh" | grep IP_LXC2= | cut -d '=' -f2)
PLAGE_IP=$(cat "$script_dir/demo_lxc_build.sh" | grep PLAGE_IP= | cut -d '=' -f2)
2016-08-13 01:08:44 +02:00
TIME_TO_SWITCH=$(cat "$script_dir/demo_lxc_build.sh" | grep TIME_TO_SWITCH= | cut -d '=' -f2)
2016-08-12 17:35:14 +02:00
2016-08-13 22:05:38 +02:00
IP_UPGRADE=$PLAGE_IP.150
2016-08-12 17:35:14 +02:00
UPGRADE_DEMO_CONTAINER () { # Démarrage, upgrade et snapshot
MACHINE=$1
2016-08-13 22:05:38 +02:00
IP_MACHINE=$2
2016-08-12 17:35:14 +02:00
# Attend que la machine soit éteinte.
2016-08-13 22:05:38 +02:00
# Timeout à $TIME_TO_SWITCH +5 minutes, en seconde
TIME_OUT=$(($TIME_TO_SWITCH * 60 + 300))
2016-08-12 18:07:01 +02:00
sudo lxc-wait -n $MACHINE -s 'STOPPED' -t $TIME_OUT
2016-08-12 17:35:14 +02:00
2016-08-13 22:05:38 +02:00
while sudo test -e /var/lib/lxc/$MACHINE/lock_file; do
sleep 1 # Attend que le conteneur soit libéré par le script switch.
done
2016-08-12 17:35:14 +02:00
# Restaure le snapshot
2016-08-13 22:05:38 +02:00
sudo lxc-snapshot -r snap0 -n $MACHINE
# Change l'ip du conteneur le temps de l'upgrade. Pour empêcher HAProxy de basculer sur le conteneur.
sudo sed -i "s@address $IP_MACHINE@address $IP_UPGRADE@" /var/lib/lxc/$MACHINE/rootfs/etc/network/interfaces
2016-08-12 17:35:14 +02:00
# Démarre le conteneur
2016-08-13 19:31:36 +02:00
sudo lxc-start -n $MACHINE -d > /dev/null
2016-08-12 17:35:14 +02:00
sleep 10
# Update
2016-08-13 22:05:38 +02:00
update_apt=0
2016-08-12 17:35:14 +02:00
sudo lxc-attach -n $MACHINE -- apt-get update
sudo lxc-attach -n $MACHINE -- apt-get dist-upgrade --dry-run | grep -q "^Inst " # Vérifie si il y aura des mises à jour.
if [ "$?" -eq 0 ]; then
update_apt=1
fi
# Upgrade
sudo lxc-attach -n $MACHINE -- apt-get dist-upgrade
# Clean
sudo lxc-attach -n $MACHINE -- apt-get autoremove
sudo lxc-attach -n $MACHINE -- apt-get autoclean
# Arrêt de la machine virtualisée
sudo lxc-stop -n $MACHINE
2016-08-13 22:05:38 +02:00
# Restaure l'ip d'origine du conteneur.
sudo sed -i "s@address $IP_UPGRADE@address $IP_MACHINE@" /var/lib/lxc/$MACHINE/rootfs/etc/network/interfaces
if [ "$update_apt" -eq "1" ]
2016-08-12 17:35:14 +02:00
then
# Archivage du snapshot
sudo tar -cz --acls --xattrs -f /var/lib/lxcsnaps/$MACHINE/snap0.tar.gz /var/lib/lxcsnaps/$MACHINE/snap0
# Remplacement du snapshot
sudo lxc-snapshot -n $MACHINE -d snap0
sudo lxc-snapshot -n $MACHINE
fi
}
2016-08-13 22:05:38 +02:00
UPGRADE_DEMO_CONTAINER $LXC_NAME1 $IP_LXC1
UPGRADE_DEMO_CONTAINER $LXC_NAME2 $IP_LXC2