Skip to content

Commit

Permalink
Merge pull request #17 from trobrock/export
Browse files Browse the repository at this point in the history
Adding export and import
  • Loading branch information
trobrock committed Jun 4, 2014
2 parents 7799572 + f8e7cce commit 0572e57
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ configurations
current
*.un~
.vagrant
exports/
Empty file added exports/.gitkeep
Empty file.
37 changes: 37 additions & 0 deletions libexec/chefvm-export
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions libexec/chefvm-import
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions libexec/chefvm-prefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -e

echo $_CHEFVM_ROOT
62 changes: 62 additions & 0 deletions test/export_test.bats
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions test/import_test.bats
Original file line number Diff line number Diff line change
@@ -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" ]
}
12 changes: 12 additions & 0 deletions test/prefix_test.bats
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0572e57

Please sign in to comment.