Last active
December 13, 2016 11:42
-
-
Save pugnascotia/203c93754dfba786b42fedbd9435ece2 to your computer and use it in GitHub Desktop.
Import docker-machine configuration archive from Rancher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# With inspiration from https://gist.github.com/schickling/2c48da462a7def0a577e | |
use strict; | |
use warnings; | |
use File::Path qw( make_path ); | |
my $machine_archive = shift @ARGV; | |
usage() unless $machine_archive; | |
usage() unless $machine_archive =~ /^ (.*?) \. ( zip | tar\.gz ) $/xi; | |
my $machine_name = $1; | |
my $is_zip = 'zip' eq lc $2; | |
my $machine_storage_path = $ENV{MACHINE_STORAGE_PATH} ? $ENV{MACHINE_STORAGE_PATH} : $ENV{HOME} . '/.docker/machine'; | |
my $machine_path = "$machine_storage_path/machines/$machine_name"; | |
die "$machine_path already exists\n" if -d $machine_path; | |
my @files; | |
if ($is_zip) { | |
@files = map { s/^\s+//; chomp; $_ } | |
grep !m|/$|, | |
grep m|/$machine_name/|, | |
qx(unzip $machine_archive); | |
} | |
else { | |
@files = qx(tar tf $machine_archive); | |
} | |
my ($guid) = split m|/|, $files[0]; | |
$guid or die "Failed to determine machine GUID! Is this a rancher config export?\n"; | |
make_path($machine_path) or die "Failed to mkdir $machine_path: $!\n"; | |
if ($is_zip) { | |
0 == system( 'unzip', '-j', '-q', $machine_archive, @files, '-d', $machine_path ) | |
or die "Failed to unzip files. unzip exited with $?"; | |
} | |
else { | |
0 == system( 'tar', 'xf', $machine_archive, '-C', $machine_path, '--strip-components=3' ) | |
or die "Failed to untar files. tar exited with $?"; | |
} | |
0 == system( 'perl', '-p', '-i', '-e', "s|/var/lib/cattle/machine/$guid|$machine_storage_path|g", "$machine_path/config.json" ) | |
or die "In-place edit failed - perl exited with $?"; | |
print "Imported $machine_name to $machine_path\n"; | |
sub usage { | |
print STDERR <<EOF; | |
Usage: rancher-machine-import [ machine.zip | machine.tar.gz ] | |
Imports a machine from a machine file, exported from Rancher. | |
EOF | |
exit 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment