79 lines
1.9 KiB
Bash
Executable File
79 lines
1.9 KiB
Bash
Executable File
#!/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
|
|
containersList="${currentPath}/containers.csv"
|
|
|
|
# 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 "- $ct:"
|
|
echo " target: $ct:"
|
|
echo " processing:"
|
|
|
|
# Arrêter
|
|
echo -n " stop: "
|
|
lxc stop $ct 2> /dev/null
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
|
|
# Déplacer
|
|
echo -n " move: "
|
|
lxc move --target $target $ct 2> /dev/null
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
|
|
# Démarrer
|
|
echo -n " start: "
|
|
lxc start $ct 2> /dev/null
|
|
[[ $? -eq 0 ]] && echo "success" || echo "failed"
|
|
}
|
|
|
|
# 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 2> /dev/null | 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 2> /dev/null | grep $(hostname)); do
|
|
ct=$(echo $line | awk -F',' '{print $2}')
|
|
move_ct
|
|
done
|
|
;;
|
|
*)
|
|
show_usage;
|
|
esac
|