147 lines
3.7 KiB
Bash
Executable File
147 lines
3.7 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
|
|
|
|
OPTIONS
|
|
-d, --dump
|
|
Exporter la machine
|
|
-l, --load
|
|
Import la machine
|
|
-a, --load-all
|
|
Importer toutes les machines"
|
|
}
|
|
|
|
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) load_machine "$1";;
|
|
loadall)
|
|
if [ $# -gt 0 ]; then
|
|
archives=("$@")
|
|
else
|
|
NOCLOBBER=1
|
|
array_lsfiles archives . "*-machine.tar.gz"
|
|
fi
|
|
for archive in "${archives[@]}"; do
|
|
etitle "$archive"
|
|
load_machine "$archive"
|
|
eend
|
|
done
|
|
;;
|
|
esac
|