mirror of
https://github.com/YunoHost/yunohost_demo.git
synced 2024-09-03 19:56:44 +02:00
70 lines
2.5 KiB
Bash
Executable file
70 lines
2.5 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Script d'upgrade des 2 conteneurs de demo.
|
|
# Ce script n'a vocation qu'a être dans un cron
|
|
|
|
# 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
|
|
|
|
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)
|
|
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)
|
|
TIME_TO_SWITCH=$(cat "$script_dir/demo_lxc_build.sh" | grep TIME_TO_SWITCH= | cut -d '=' -f2)
|
|
|
|
IP_UPGRADE=$PLAGE_IP.150
|
|
|
|
UPGRADE_DEMO_CONTAINER () { # Démarrage, upgrade et snapshot
|
|
MACHINE=$1
|
|
IP_MACHINE=$2
|
|
# Attend que la machine soit éteinte.
|
|
# Timeout à $TIME_TO_SWITCH +5 minutes, en seconde
|
|
TIME_OUT=$(($TIME_TO_SWITCH * 60 + 300))
|
|
sudo lxc-wait -n $MACHINE -s 'STOPPED' -t $TIME_OUT
|
|
|
|
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
|
|
|
|
# Restaure le snapshot
|
|
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
|
|
|
|
# Démarre le conteneur
|
|
sudo lxc-start -n $MACHINE -d > /dev/null
|
|
sleep 10
|
|
|
|
# Update
|
|
update_apt=0
|
|
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
|
|
|
|
# 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" ]
|
|
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
|
|
}
|
|
|
|
UPGRADE_DEMO_CONTAINER $LXC_NAME1 $IP_LXC1
|
|
UPGRADE_DEMO_CONTAINER $LXC_NAME2 $IP_LXC2
|