Script de déplacement automatisé des CT
This commit is contained in:
commit
10101fcc97
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
balance-containers.csv
|
68
balance-containers.bash
Normal file
68
balance-containers.bash
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/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}/$(echo $scriptName | awk -F'.' '{print $1}').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 "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
|
Loading…
Reference in New Issue
Block a user