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..1ed0593 --- /dev/null +++ b/libexec/chefvm-export @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# 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 + +__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 + +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 + +if [ "$include_keys" = "1" ]; then + tar czf $export_file ./ +else + tar czf $export_file --exclude '*.pem' ./ +fi + +popd > /dev/null 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/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/export_test.bats b/test/export_test.bats new file mode 100644 index 0000000..787cbdd --- /dev/null +++ b/test/export_test.bats @@ -0,0 +1,62 @@ +#!/usr/bin/env bats + +setup() { + 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" { + 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" ] +} + +@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 +} + +@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 +} 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" ] +} 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 +}