From d962eb2735f893d4879c9d82cfd6793551f5bd3e Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Mon, 1 Aug 2016 17:54:37 -0700 Subject: [PATCH] Allow `package` command to just take a snapshot Currently, the snapshot sub-command is not supported for this provider, but the ability to take ami snapshots is supported inside of the package command. This patch adds an option: `package_only_snapshot` that allows you to use the package option to only take a snapshot. --- README.md | 2 + lib/vagrant-aws/action/package_instance.rb | 44 ++++++++++++---------- lib/vagrant-aws/config.rb | 7 ++++ spec/vagrant-aws/config_spec.rb | 1 + 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 31bbf8cd..e7005a5e 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,8 @@ This provider exposes quite a few provider-specific configuration options: * `tenancy` - When running in a VPC configure the tenancy of the instance. Supports 'default' and 'dedicated'. * `tags` - A hash of tags to set on the machine. * `package_tags` - A hash of tags to set on the ami generated during the package operation. +* `package_only_snapshot` - A boolean that indicates whether the package command should only + create an AMI snapshop. Defaults to false. * `use_iam_profile` - If true, will use [IAM profiles](http://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html) for credentials. * `block_device_mapping` - Amazon EC2 Block Device Mapping Property diff --git a/lib/vagrant-aws/action/package_instance.rb b/lib/vagrant-aws/action/package_instance.rb index 81092151..a97f9407 100644 --- a/lib/vagrant-aws/action/package_instance.rb +++ b/lib/vagrant-aws/action/package_instance.rb @@ -92,26 +92,30 @@ def call(env) raise Errors::FogError, :message => e.message end - # Handles inclusions from --include and --vagrantfile options - setup_package_files(env) - - # Setup the temporary directory for the tarball files - @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s) - env["export.temp_dir"] = @temp_dir - FileUtils.mkpath(env["export.temp_dir"]) - - # Create the Vagrantfile and metadata.json files from templates to go in the box - create_vagrantfile(env) - create_metadata_file(env) - - # Just match up a couple environmental variables so that - # the superclass will do the right thing. Then, call the - # superclass to actually create the tarball (.box file) - env["package.directory"] = env["export.temp_dir"] - general_call(env) - - # Always call recover to clean up the temp dir - clean_temp_dir + region=env[:machine].provider_config.region + region_config=env[:machine].provider_config.get_region_config(region) + if !region_config.package_only_snapshot + # Handles inclusions from --include and --vagrantfile options + setup_package_files(env) + + # Setup the temporary directory for the tarball files + @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s) + env["export.temp_dir"] = @temp_dir + FileUtils.mkpath(env["export.temp_dir"]) + + # Create the Vagrantfile and metadata.json files from templates to go in the box + create_vagrantfile(env) + create_metadata_file(env) + + # Just match up a couple environmental variables so that + # the superclass will do the right thing. Then, call the + # superclass to actually create the tarball (.box file) + env["package.directory"] = env["export.temp_dir"] + general_call(env) + + # Always call recover to clean up the temp dir + clean_temp_dir + end end protected diff --git a/lib/vagrant-aws/config.rb b/lib/vagrant-aws/config.rb index 30a19cb4..9c7ff26e 100644 --- a/lib/vagrant-aws/config.rb +++ b/lib/vagrant-aws/config.rb @@ -114,6 +114,12 @@ class Config < Vagrant.plugin("2", :config) # @return [Hash] attr_accessor :package_tags + # Whether to create a vagrant package or just take a snapshot when running + # the package subcommand. + # + # @return [Boolean] + attr_accessor :package_only_snapshot + # Use IAM Instance Role for authentication to AWS instead of an # explicit access_id and secret_access_key # @@ -215,6 +221,7 @@ def initialize(region_specific=false) @subnet_id = UNSET_VALUE @tags = {} @package_tags = {} + @package_only_snapshot = false @user_data = UNSET_VALUE @use_iam_profile = UNSET_VALUE @block_device_mapping = [] diff --git a/spec/vagrant-aws/config_spec.rb b/spec/vagrant-aws/config_spec.rb index eb309743..eaa34c97 100644 --- a/spec/vagrant-aws/config_spec.rb +++ b/spec/vagrant-aws/config_spec.rb @@ -46,6 +46,7 @@ its("iam_instance_profile_name") { should be_nil } its("tags") { should == {} } its("package_tags") { should == {} } + its("package_only_snapshot") { should == false } its("user_data") { should be_nil } its("use_iam_profile") { should be false } its("block_device_mapping") {should == [] }