112 lines
2.8 KiB
Bash
Executable File
112 lines
2.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
## Récupérer le nom du script
|
|
scriptName=$(basename $0)
|
|
|
|
# Définir l'emplacement de la liste
|
|
containersList="${HOME}/containers.csv"
|
|
|
|
# Récupérer le nom d'hôte LXD
|
|
nodename=$(/snap/bin/lxc info | grep "server_name" | awk -F':' '{print $NF}' | sed 's# ##g')
|
|
|
|
# SI le nom d'hôte LXD n'est pas défini, le mettre à hostname
|
|
[[ -z '${nodename}' ]] && nodename=$(hostname)
|
|
|
|
# Définir l'aide
|
|
show_usage() {
|
|
echo "Usage: ${currentPath}/${scriptName} [options [parameters]]"
|
|
echo
|
|
echo "Options:"
|
|
echo " --balance: move containers"
|
|
echo " --list: generate list of current balance (used by --balance)"
|
|
echo " --migrate <target>: migrate all local containers to target"
|
|
echo " --move <target> <container>: move container to target"
|
|
}
|
|
|
|
move_ct() {
|
|
echo " move: pending"
|
|
lxc move --target $target $ct 2> /dev/null
|
|
lxc_move_result=$?
|
|
echo -n " move: "
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
}
|
|
|
|
migrate_ct() {
|
|
echo "- container: $ct"
|
|
echo " target: $target"
|
|
echo " processing:"
|
|
|
|
# SI le conteneur est démarré
|
|
if [[ "$(lxc list --columns s --format csv $ct 2> /dev/null)" == "RUNNING" ]]; then
|
|
# Arrêter
|
|
echo -n " stop: "
|
|
lxc stop $ct 2> /dev/null
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
|
|
# Déplacer
|
|
move_ct
|
|
|
|
# Démarrer
|
|
echo -n " start: "
|
|
lxc start $ct 2> /dev/null
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
else
|
|
echo " stop: skipped"
|
|
move_ct
|
|
echo " start: skipped"
|
|
fi
|
|
}
|
|
|
|
# Boucler sur les arguments
|
|
case "${1}" in
|
|
--list)
|
|
lxc list --columns nL --format csv | tee $containersList
|
|
;;
|
|
--balance)
|
|
while read line; do
|
|
# Découper les informations
|
|
ct=$(echo $line | awk -F',' '{print $1}')
|
|
target=$(echo $line | awk -F',' '{print $2}')
|
|
|
|
# Récupérer l'hôte courrant
|
|
host=$(lxc list --columns nL --format csv $ct 2> /dev/null | grep $target)
|
|
|
|
# Traiter le conteneur s'il n'est pas sur l'hôte cible
|
|
if [[ -z "${host}" ]]; then
|
|
migrate_ct
|
|
fi
|
|
done < $containersList
|
|
;;
|
|
--migrate)
|
|
# Vérifier la présence d'une cible
|
|
[[ -z "${2}" ]] && show_usage && exit
|
|
|
|
# Stocker la cible
|
|
target="${2}"
|
|
|
|
# Parcourir la liste de conteneur locaux
|
|
for line in $(lxc list --columns Ln --format csv 2> /dev/null | grep ${nodename}); do
|
|
ct=$(echo $line | awk -F',' '{print $2}')
|
|
migrate_ct
|
|
done
|
|
;;
|
|
--move)
|
|
# Vérifier la présence d'une cible
|
|
[[ -z "${2}" ]] && show_usage && exit
|
|
|
|
# Stocker la cible
|
|
target="${2}"
|
|
|
|
# Vérifier la présence d'un conteneur
|
|
[[ -z "${3}" ]] && show_usage && exit
|
|
|
|
# Stocker le conteneur
|
|
ct="${3}"
|
|
|
|
# Effectuer la migration
|
|
migrate_ct
|
|
;;
|
|
*)
|
|
show_usage;
|
|
esac
|