From 0f4424e2f0a7ed31f74d16928dfb15f2797180ac Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 08:23:26 -0700 Subject: [PATCH 1/6] Add prefix command support for grabbing the chefvm install dir --- libexec/chefvm-prefix | 5 +++++ test/prefix_test.bats | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100755 libexec/chefvm-prefix create mode 100644 test/prefix_test.bats diff --git a/libexec/chefvm-prefix b/libexec/chefvm-prefix new file mode 100755 index 0000000..3a259fa --- /dev/null +++ b/libexec/chefvm-prefix @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e + +echo $_CHEFVM_ROOT diff --git a/test/prefix_test.bats b/test/prefix_test.bats new file mode 100644 index 0000000..2d288a4 --- /dev/null +++ b/test/prefix_test.bats @@ -0,0 +1,12 @@ +#!/usr/bin/env bats + +@test "should print the chefvm root" { + run chefvm prefix + + [ "$status" -eq 0 ] + if [ ! -z $TRAVIS_BUILD_DIR ]; then + [ "$output" = "$TRAVIS_BUILD_DIR" ] + else + [ "$output" = "/root/.chefvm" ] + fi +} From 754983a471612d0a220b74210692986363a72fec Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 08:53:39 -0700 Subject: [PATCH 2/6] First pass at export, still need to ignore keys optionally --- .gitignore | 1 + exports/.gitkeep | 0 libexec/chefvm-export | 24 ++++++++++++++++++++++++ test/export_test.bats | 16 ++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 exports/.gitkeep create mode 100755 libexec/chefvm-export create mode 100644 test/export_test.bats diff --git a/.gitignore b/.gitignore index 6bf471d..8f85c90 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ configurations current *.un~ .vagrant +exports/ diff --git a/exports/.gitkeep b/exports/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/libexec/chefvm-export b/libexec/chefvm-export new file mode 100755 index 0000000..be7c38f --- /dev/null +++ b/libexec/chefvm-export @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Usage: chefvm export CONFIG +# Summary: Export a configuration +# Help: This command will export a configuration to a file so it can be imported on another machine. + +set -e + +__chefvm_config $1 + +if [ -z "$config" ]; then + echo "No name provided" + exit 1 +fi + +if [ ! -d $_CHEFVM_ROOT/$config_path ]; then + echo "No configuration named $1 found." + chefvm list + exit 1 +fi + +echo "Exporting: $config" +pushd $_CHEFVM_ROOT > /dev/null +tar czf $_CHEFVM_ROOT/exports/${config}.tar.gz $config_path +popd > /dev/null diff --git a/test/export_test.bats b/test/export_test.bats new file mode 100644 index 0000000..27c246c --- /dev/null +++ b/test/export_test.bats @@ -0,0 +1,16 @@ +#!/usr/bin/env bats + +@test "should have the exports directory" { + chefvm_root=$(chefvm prefix) + + [ -d "$chefvm_root/exports" ] +} + +@test "should create a tar file for the export" { + chefvm_root=$(chefvm prefix) + run chefvm export example + + [ "$status" -eq 0 ] + [ "$output" = "Exporting: example" ] + [ -f "$chefvm_root/exports/example.tar.gz" ] +} From 8256af4f9be9f76c00e0cf48a321bb9195737a6f Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 09:22:02 -0700 Subject: [PATCH 3/6] Ignore key files by default --- libexec/chefvm-export | 6 ++++-- test/export_test.bats | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/libexec/chefvm-export b/libexec/chefvm-export index be7c38f..eefaf58 100755 --- a/libexec/chefvm-export +++ b/libexec/chefvm-export @@ -19,6 +19,8 @@ if [ ! -d $_CHEFVM_ROOT/$config_path ]; then fi echo "Exporting: $config" -pushd $_CHEFVM_ROOT > /dev/null -tar czf $_CHEFVM_ROOT/exports/${config}.tar.gz $config_path +pushd $_CHEFVM_ROOT/$config_path > /dev/null +export_file="$_CHEFVM_ROOT/exports/${config}.tar.gz" +rm -f $export_file +tar czf $export_file --exclude '*.pem' ./ popd > /dev/null diff --git a/test/export_test.bats b/test/export_test.bats index 27c246c..b0cf1a1 100644 --- a/test/export_test.bats +++ b/test/export_test.bats @@ -1,5 +1,11 @@ #!/usr/bin/env bats +setup() { + touch $chefvm_root/configurations/example/knife.rb + touch $chefvm_root/configurations/example/myuser.pem + touch $chefvm_root/configurations/example/myvalidator.pem +} + @test "should have the exports directory" { chefvm_root=$(chefvm prefix) @@ -14,3 +20,23 @@ [ "$output" = "Exporting: example" ] [ -f "$chefvm_root/exports/example.tar.gz" ] } + +@test "should ignore key files by default" { + chefvm_root=$(chefvm prefix) + run chefvm export example + + [ "$status" -eq 0 ] + + tempfile=$(mktemp -t chefvm-export-ignore-keys.XXXXXXXXXX) + rm $tempfile + mkdir -p $tempfile + pushd $tempfile > /dev/null + tar xzf $chefvm_root/exports/example.tar.gz + + [ -f "knife.rb" ] + [ ! -f "myuser.pem" ] + [ ! -f "myvalidator.pem" ] + + popd > /dev/null + rm -rf $tempfile +} From dc451332a149f364da572e81b5a0ccb971af2e49 Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 09:29:05 -0700 Subject: [PATCH 4/6] Add support for optionally including the key files --- libexec/chefvm-export | 13 ++++++++++++- test/export_test.bats | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libexec/chefvm-export b/libexec/chefvm-export index eefaf58..97245c3 100755 --- a/libexec/chefvm-export +++ b/libexec/chefvm-export @@ -18,9 +18,20 @@ if [ ! -d $_CHEFVM_ROOT/$config_path ]; then exit 1 fi +if [ "$2" = "--include-keys" ]; then + include_keys=1 +fi + echo "Exporting: $config" + pushd $_CHEFVM_ROOT/$config_path > /dev/null export_file="$_CHEFVM_ROOT/exports/${config}.tar.gz" rm -f $export_file -tar czf $export_file --exclude '*.pem' ./ + +if [ "$include_keys" = "1" ]; then + tar czf $export_file ./ +else + tar czf $export_file --exclude '*.pem' ./ +fi + popd > /dev/null diff --git a/test/export_test.bats b/test/export_test.bats index b0cf1a1..787cbdd 100644 --- a/test/export_test.bats +++ b/test/export_test.bats @@ -1,9 +1,9 @@ #!/usr/bin/env bats setup() { - touch $chefvm_root/configurations/example/knife.rb - touch $chefvm_root/configurations/example/myuser.pem - touch $chefvm_root/configurations/example/myvalidator.pem + touch $(chefvm prefix)/configurations/example/knife.rb + touch $(chefvm prefix)/configurations/example/myuser.pem + touch $(chefvm prefix)/configurations/example/myvalidator.pem } @test "should have the exports directory" { @@ -40,3 +40,23 @@ setup() { popd > /dev/null rm -rf $tempfile } + +@test "should be able to include key files" { + chefvm_root=$(chefvm prefix) + run chefvm export example --include-keys + + [ "$status" -eq 0 ] + + tempfile=$(mktemp -t chefvm-export-ignore-keys.XXXXXXXXXX) + rm $tempfile + mkdir -p $tempfile + pushd $tempfile > /dev/null + tar xzf $chefvm_root/exports/example.tar.gz + + [ -f "knife.rb" ] + [ -f "myuser.pem" ] + [ -f "myvalidator.pem" ] + + popd > /dev/null + rm -rf $tempfile +} From ad4150ec08da94e2e79a29a9fe77aad12c7b451a Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 09:30:10 -0700 Subject: [PATCH 5/6] Better documentation --- libexec/chefvm-export | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/chefvm-export b/libexec/chefvm-export index 97245c3..1ed0593 100755 --- a/libexec/chefvm-export +++ b/libexec/chefvm-export @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Usage: chefvm export CONFIG -# Summary: Export a configuration +# Usage: chefvm export CONFIG [--include-keys] +# Summary: Export a configuration, ignoring key files (unless specified) # Help: This command will export a configuration to a file so it can be imported on another machine. set -e From f8e7cce25410be76abe10ea1b82a94c0ff3e2f81 Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Wed, 4 Jun 2014 10:23:30 -0700 Subject: [PATCH 6/6] Add import ability --- libexec/chefvm-import | 26 ++++++++++++++++++++++++++ test/import_test.bats | 22 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100755 libexec/chefvm-import create mode 100644 test/import_test.bats diff --git a/libexec/chefvm-import b/libexec/chefvm-import new file mode 100755 index 0000000..092869b --- /dev/null +++ b/libexec/chefvm-import @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Usage: chefvm import FILE CONFIG_NAME +# Summary: Import a configuration +# Help: This command will import a file that was exported from chefvm to a new configuration. + +set -e + +file=$(readlink -f $1) +if [ -z "$file" ] || [ ! -f "$file" ]; then + echo "$file does not exist" + exit 1 +fi + +if [ -z "$2" ]; then + echo "No new config name specified" + exit 1 +fi + +__chefvm_config $2 + +echo "Importing '$1' as '$2'" + +mkdir -p $_CHEFVM_ROOT/$config_path +pushd $_CHEFVM_ROOT/$config_path > /dev/null +tar xzf $file +popd > /dev/null diff --git a/test/import_test.bats b/test/import_test.bats new file mode 100644 index 0000000..13c9a0f --- /dev/null +++ b/test/import_test.bats @@ -0,0 +1,22 @@ +#!/usr/bin/env bats + +setup() { + touch $(chefvm prefix)/configurations/example/knife.rb + touch $(chefvm prefix)/configurations/example/myuser.pem + chefvm export example > /dev/null +} + +teardown() { + chefvm delete new_example > /dev/null +} + +@test "should be able to import a configuration" { + run chefvm import $(chefvm prefix)/exports/example.tar.gz new_example + + [ "$status" -eq 0 ] + [ "$output" = "Importing '$(chefvm prefix)/exports/example.tar.gz' as 'new_example'" ] + + run chefvm list + [ "${lines[0]}" = "*= example" ] + [ "${lines[1]}" = " new_example" ] +}