2021-04-17 16:29:29 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
## Récupérer l'emplacement actuel
|
|
|
|
currentPath=$(dirname $0)
|
|
|
|
|
|
|
|
## Récupérer le nom du script
|
|
|
|
scriptName=$(basename $0)
|
|
|
|
|
|
|
|
# Définir l'emplacement de la liste
|
2021-06-04 22:39:24 +00:00
|
|
|
containersList="${currentPath}/containers.csv"
|
2021-04-17 16:29:29 +00:00
|
|
|
|
|
|
|
# Définir l'aide
|
|
|
|
show_usage() {
|
|
|
|
echo "Usage: ${currentPath}/${scriptName} [options [parameters]]"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -b or --balance: move containers"
|
|
|
|
echo " -l or --list: generate list of current balance (used by --balance)"
|
|
|
|
echo " -m or --migrate <target>: move containers on local host to target"
|
|
|
|
}
|
|
|
|
|
|
|
|
move_ct() {
|
|
|
|
# echo "lxc move --target $target $ct"
|
|
|
|
|
|
|
|
# Arrêter le conteneur
|
|
|
|
lxc stop $ct;
|
|
|
|
|
|
|
|
# Déplacer et démarrer le conteneur
|
|
|
|
lxc move --target $target $ct &&\
|
|
|
|
lxc start $ct
|
|
|
|
}
|
|
|
|
|
|
|
|
# Boucler sur les arguments
|
|
|
|
case "${1}" in
|
|
|
|
-l|--list)
|
|
|
|
lxc list --columns Ln --format csv | tee $containersList
|
|
|
|
;;
|
|
|
|
-b|--balance)
|
|
|
|
while read line; do
|
|
|
|
# Découper les informations
|
|
|
|
target=$(echo $line | awk -F',' '{print $1}')
|
|
|
|
ct=$(echo $line | awk -F',' '{print $2}')
|
|
|
|
|
|
|
|
# Récupérer l'hôte courrant
|
|
|
|
host=$(lxc list --columns Ln --format csv $ct | grep $target)
|
|
|
|
|
|
|
|
# Traiter le conteneur s il n'est pas sur l'hôte cible
|
|
|
|
if [[ -z "${host}" ]]; then
|
|
|
|
move_ct
|
|
|
|
fi
|
|
|
|
done < $containersList
|
|
|
|
;;
|
|
|
|
-m|--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 | grep $(hostname)); do
|
|
|
|
ct=$(echo $line | awk -F',' '{print $2}')
|
|
|
|
move_ct
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
show_usage;
|
|
|
|
esac
|