161 lines
4.0 KiB
Bash
Executable File
161 lines
4.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
source /etc/ulibauto || exit 1
|
|
|
|
function display_help() {
|
|
uecho "$scriptname: import/exporter une docker-machine
|
|
|
|
USAGE
|
|
$scriptname -d NAME
|
|
$scriptname -l ARCHIVE
|
|
$scriptname -a ARCHIVEs...
|
|
|
|
OPTIONS
|
|
-d, --dump
|
|
Exporter la machine
|
|
-l, --load
|
|
Importer la machine à partir de l'archive spécifiée
|
|
-a, --load-all
|
|
Importer toutes les machines depuis le répertoire spécifié"
|
|
}
|
|
|
|
function dump_machine() {
|
|
local name srcdir workdir destdir archive i
|
|
|
|
name="$1"
|
|
[ -n "$name" ] || die "Vous devez spécifier le nom de la machine"
|
|
|
|
srcdir="$HOME/.docker/machine/machines/$name"
|
|
[ -d "$srcdir" ] || die "$name: nom de machine incorrect"
|
|
|
|
ac_set_tmpdir workdir
|
|
destdir="$workdir/$name"
|
|
|
|
archive="${name}-machine.tar.gz"
|
|
|
|
estep "Copie des fichiers"
|
|
cp -r "$srcdir" "$workdir" || die
|
|
for i in ca-key.pem ca.pem key.pem cert.pem; do
|
|
[ -f "$destdir/$i" ] || {
|
|
cp "$HOME/.docker/machine/certs/$i" "$destdir" || die
|
|
}
|
|
done
|
|
|
|
estep "Correction des chemins"
|
|
sed -i "\
|
|
s|$HOME/.docker/machine|DOCKER_MACHINE_HOME|g
|
|
s|DOCKER_MACHINE_HOME/certs/|DOCKER_MACHINE_HOME/machines/$name/|g
|
|
" "$destdir/config.json" || die
|
|
|
|
estep "Génération de $archive"
|
|
tar czf "$archive" -C "$workdir" "$name" || die
|
|
|
|
ac_clean "$workdir"
|
|
}
|
|
|
|
function load_machine() {
|
|
local archive archivename destdir machinedir origdir workdir srcdir
|
|
|
|
archive="$1"
|
|
[ -n "$archive" ] || die "Vous devez spécifier l'archive à importer"
|
|
|
|
setx archivename=basename -- "$archive"
|
|
if [[ "$archivename" == *-machine.tar.gz ]]; then
|
|
name="${archivename%-machine.tar.gz}"
|
|
else
|
|
die "$archive: n'est pas une archive docker-machine"
|
|
fi
|
|
|
|
destdir="$HOME/.docker/machine/machines"
|
|
machinedir="$destdir/$name"
|
|
origdir="$HOME/${name}-machine.orig"
|
|
if [ -d "$machinedir" ]; then
|
|
if [ -n "$NOCLOBBER" ]; then
|
|
ewarn "$name: cette machine existe déjà"
|
|
return
|
|
else
|
|
ewarn "$name: une machine du même nom existe déjà"
|
|
ask_yesno "Voulez-vous l'écraser?" N || die
|
|
|
|
[ -d "$origdir" ] && rm -rf "$origdir"
|
|
mv "$machinedir" "$origdir"
|
|
fi
|
|
fi
|
|
|
|
ac_set_tmpdir workdir
|
|
srcdir="$workdir/$name"
|
|
|
|
estep "Extraction de l'archive"
|
|
tar xzf "$archive" -C "$workdir"
|
|
|
|
[ -d "$srcdir" ] || die "L'archive n'est pas pour la machine $name"
|
|
|
|
estep "Corriger les chemins"
|
|
sed -i "\
|
|
s|DOCKER_MACHINE_HOME|$HOME/.docker/machine|g
|
|
" "$srcdir/config.json" || die
|
|
|
|
estep "Installation de la machine"
|
|
mkdir -p "$destdir"
|
|
mv "$srcdir" "$destdir"
|
|
|
|
if [ -d "$origdir" ]; then
|
|
enote "Une fois que la machine aura été testée, vous pouvez supprimer ce répertoire:
|
|
$origdir"
|
|
fi
|
|
|
|
ac_clean "$workdir"
|
|
}
|
|
|
|
# faut-il ignorer les machines existantes
|
|
NOCLOBBER=
|
|
|
|
action=
|
|
args=(
|
|
--help '$exit_with display_help'
|
|
-d,--dump,-x,--export,-s,--save action=dump
|
|
-l,--load,-i,--import action=load
|
|
-a,--all,--load-all,--import-all action=loadall
|
|
)
|
|
parse_args "$@"; set -- "${args[@]}"
|
|
|
|
if [ -z "$action" ]; then
|
|
case "$1" in
|
|
d|dump|x|export|s|save) action=dump; shift;;
|
|
l|load|i|import) action=load; shift;;
|
|
a|all|loadall|importall) action=loadall; shift;;
|
|
*) die "Vous devez spécifier l'action à effectuer";;
|
|
esac
|
|
fi
|
|
|
|
case "$action" in
|
|
dump)
|
|
dump_machine "$1"
|
|
;;
|
|
load)
|
|
for archive in "$@"; do
|
|
etitle "$archive"
|
|
load_machine "$archive"
|
|
eend
|
|
done
|
|
;;
|
|
loadall)
|
|
[ $# -gt 0 ] || set -- .
|
|
archives=()
|
|
for file in "$@"; do
|
|
if [ -d "$file" ]; then
|
|
NOCLOBBER=1
|
|
array_lsfiles files "$file" "*-machine.tar.gz"
|
|
array_extend archives files
|
|
else
|
|
archives+=("$file")
|
|
fi
|
|
done
|
|
for archive in "${archives[@]}"; do
|
|
etitle "$archive"
|
|
load_machine "$archive"
|
|
eend
|
|
done
|
|
;;
|
|
esac
|