vagrant-aws-stsmith 0.5.0.dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/CHANGELOG.md +76 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE +8 -0
  6. data/README.md +251 -0
  7. data/Rakefile +21 -0
  8. data/dummy.box +0 -0
  9. data/example_box/README.md +13 -0
  10. data/example_box/metadata.json +3 -0
  11. data/lib/vagrant-aws.rb +18 -0
  12. data/lib/vagrant-aws/action.rb +190 -0
  13. data/lib/vagrant-aws/action/connect_aws.rb +46 -0
  14. data/lib/vagrant-aws/action/is_created.rb +18 -0
  15. data/lib/vagrant-aws/action/is_stopped.rb +18 -0
  16. data/lib/vagrant-aws/action/message_already_created.rb +16 -0
  17. data/lib/vagrant-aws/action/message_not_created.rb +16 -0
  18. data/lib/vagrant-aws/action/message_will_not_destroy.rb +16 -0
  19. data/lib/vagrant-aws/action/read_ssh_info.rb +53 -0
  20. data/lib/vagrant-aws/action/read_state.rb +38 -0
  21. data/lib/vagrant-aws/action/run_instance.rb +247 -0
  22. data/lib/vagrant-aws/action/start_instance.rb +81 -0
  23. data/lib/vagrant-aws/action/stop_instance.rb +28 -0
  24. data/lib/vagrant-aws/action/sync_folders.rb +118 -0
  25. data/lib/vagrant-aws/action/terminate_instance.rb +47 -0
  26. data/lib/vagrant-aws/action/timed_provision.rb +21 -0
  27. data/lib/vagrant-aws/action/wait_for_state.rb +41 -0
  28. data/lib/vagrant-aws/action/warn_networks.rb +19 -0
  29. data/lib/vagrant-aws/config.rb +372 -0
  30. data/lib/vagrant-aws/errors.rb +31 -0
  31. data/lib/vagrant-aws/plugin.rb +73 -0
  32. data/lib/vagrant-aws/provider.rb +50 -0
  33. data/lib/vagrant-aws/util/timer.rb +17 -0
  34. data/lib/vagrant-aws/version.rb +5 -0
  35. data/locales/en.yml +122 -0
  36. data/spec/vagrant-aws/config_spec.rb +216 -0
  37. data/vagrant-aws.gemspec +59 -0
  38. metadata +149 -0
@@ -0,0 +1,31 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module AWS
5
+ module Errors
6
+ class VagrantAWSError < Vagrant::Errors::VagrantError
7
+ error_namespace("vagrant_aws.errors")
8
+ end
9
+
10
+ class FogError < VagrantAWSError
11
+ error_key(:fog_error)
12
+ end
13
+
14
+ class InternalFogError < VagrantAWSError
15
+ error_key(:internal_fog_error)
16
+ end
17
+
18
+ class InstanceReadyTimeout < VagrantAWSError
19
+ error_key(:instance_ready_timeout)
20
+ end
21
+
22
+ class RsyncError < VagrantAWSError
23
+ error_key(:rsync_error)
24
+ end
25
+
26
+ class MkdirError < VagrantAWSError
27
+ error_key(:mkdir_error)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,73 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "The Vagrant AWS plugin must be run within Vagrant."
5
+ end
6
+
7
+ # This is a sanity check to make sure no one is attempting to install
8
+ # this into an early Vagrant version.
9
+ if Vagrant::VERSION < "1.2.0"
10
+ raise "The Vagrant AWS plugin is only compatible with Vagrant 1.2+"
11
+ end
12
+
13
+ module VagrantPlugins
14
+ module AWS
15
+ class Plugin < Vagrant.plugin("2")
16
+ name "AWS"
17
+ description <<-DESC
18
+ This plugin installs a provider that allows Vagrant to manage
19
+ machines in AWS (EC2/VPC).
20
+ DESC
21
+
22
+ config(:aws, :provider) do
23
+ require_relative "config"
24
+ Config
25
+ end
26
+
27
+ provider(:aws, parallel: true) do
28
+ # Setup logging and i18n
29
+ setup_logging
30
+ setup_i18n
31
+
32
+ # Return the provider
33
+ require_relative "provider"
34
+ Provider
35
+ end
36
+
37
+ # This initializes the internationalization strings.
38
+ def self.setup_i18n
39
+ I18n.load_path << File.expand_path("locales/en.yml", AWS.source_root)
40
+ I18n.reload!
41
+ end
42
+
43
+ # This sets up our log level to be whatever VAGRANT_LOG is.
44
+ def self.setup_logging
45
+ require "log4r"
46
+
47
+ level = nil
48
+ begin
49
+ level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
50
+ rescue NameError
51
+ # This means that the logging constant wasn't found,
52
+ # which is fine. We just keep `level` as `nil`. But
53
+ # we tell the user.
54
+ level = nil
55
+ end
56
+
57
+ # Some constants, such as "true" resolve to booleans, so the
58
+ # above error checking doesn't catch it. This will check to make
59
+ # sure that the log level is an integer, as Log4r requires.
60
+ level = nil if !level.is_a?(Integer)
61
+
62
+ # Set the logging level on all "vagrant" namespaced
63
+ # logs as long as we have a valid level.
64
+ if level
65
+ logger = Log4r::Logger.new("vagrant_aws")
66
+ logger.outputters = Log4r::Outputter.stderr
67
+ logger.level = level
68
+ logger = nil
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,50 @@
1
+ require "log4r"
2
+ require "vagrant"
3
+
4
+ module VagrantPlugins
5
+ module AWS
6
+ class Provider < Vagrant.plugin("2", :provider)
7
+ def initialize(machine)
8
+ @machine = machine
9
+ end
10
+
11
+ def action(name)
12
+ # Attempt to get the action method from the Action class if it
13
+ # exists, otherwise return nil to show that we don't support the
14
+ # given action.
15
+ action_method = "action_#{name}"
16
+ return Action.send(action_method) if Action.respond_to?(action_method)
17
+ nil
18
+ end
19
+
20
+ def ssh_info
21
+ # Run a custom action called "read_ssh_info" which does what it
22
+ # says and puts the resulting SSH info into the `:machine_ssh_info`
23
+ # key in the environment.
24
+ env = @machine.action("read_ssh_info")
25
+ env[:machine_ssh_info]
26
+ end
27
+
28
+ def state
29
+ # Run a custom action we define called "read_state" which does
30
+ # what it says. It puts the state in the `:machine_state_id`
31
+ # key in the environment.
32
+ env = @machine.action("read_state")
33
+
34
+ state_id = env[:machine_state_id]
35
+
36
+ # Get the short and long description
37
+ short = I18n.t("vagrant_aws.states.short_#{state_id}")
38
+ long = I18n.t("vagrant_aws.states.long_#{state_id}")
39
+
40
+ # Return the MachineState object
41
+ Vagrant::MachineState.new(state_id, short, long)
42
+ end
43
+
44
+ def to_s
45
+ id = @machine.id.nil? ? "new" : @machine.id
46
+ "AWS (#{id})"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module AWS
3
+ module Util
4
+ class Timer
5
+ # A basic utility method that times the execution of the given
6
+ # block and returns it.
7
+ def self.time
8
+ start_time = Time.now.to_f
9
+ yield
10
+ end_time = Time.now.to_f
11
+
12
+ end_time - start_time
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module AWS
3
+ VERSION = "0.5.0.dev"
4
+ end
5
+ end
@@ -0,0 +1,122 @@
1
+ en:
2
+ vagrant_aws:
3
+ already_status: |-
4
+ The machine is already %{status}.
5
+ launching_instance: |-
6
+ Launching an instance with the following settings...
7
+ launch_no_keypair: |-
8
+ Warning! You didn't specify a keypair to launch your instance with.
9
+ This can sometimes result in not being able to access your instance.
10
+ launch_vpc_warning: |-
11
+ Warning! You're launching this instance into a VPC without an
12
+ elastic IP. Please verify you're properly connected to a VPN so
13
+ you can access this machine, otherwise Vagrant will not be able
14
+ to SSH into it.
15
+ not_created: |-
16
+ Instance is not created. Please run `vagrant up` first.
17
+ ready: |-
18
+ Machine is booted and ready for use!
19
+ rsync_not_found_warning: |-
20
+ Warning! Folder sync disabled because the rsync binary is missing in the %{side}.
21
+ Make sure rsync is installed and the binary can be found in the PATH.
22
+ rsync_folder: |-
23
+ Rsyncing folder: %{hostpath} => %{guestpath}
24
+ starting: |-
25
+ Starting the instance...
26
+ stopping: |-
27
+ Stopping the instance...
28
+ terminating: |-
29
+ Terminating the instance...
30
+ waiting_for_ready: |-
31
+ Waiting for instance to become "ready"...
32
+ waiting_for_ssh: |-
33
+ Waiting for SSH to become available...
34
+ warn_networks: |-
35
+ Warning! The AWS provider doesn't support any of the Vagrant
36
+ high-level network configurations (`config.vm.network`). They
37
+ will be silently ignored.
38
+ warn_ssh_access: |-
39
+ Warning! Vagrant might not be able to SSH into the instance.
40
+ Please check your security groups settings.
41
+ will_not_destroy: |-
42
+ The instance '%{name}' will not be destroyed, since the confirmation
43
+ was declined.
44
+
45
+ config:
46
+ access_key_id_required: |-
47
+ An access key ID must be specified via "access_key_id"
48
+ ami_required: |-
49
+ An AMI must be configured via "ami" (region: #{region})
50
+ private_key_missing: |-
51
+ The specified private key for AWS could not be found
52
+ region_required: |-
53
+ A region must be specified via "region"
54
+ secret_access_key_required: |-
55
+ A secret access key is required via "secret_access_key"
56
+ subnet_id_required_with_public_ip: |-
57
+ If you assign a public IP address to an instance in a VPC, a subnet must be specifed via "subnet_id"
58
+
59
+ errors:
60
+ fog_error: |-
61
+ There was an error talking to AWS. The error message is shown
62
+ below:
63
+
64
+ %{message}
65
+ internal_fog_error: |-
66
+ There was an error talking to AWS. The error message is shown
67
+ below:
68
+
69
+ Error: %{error}
70
+ Response: %{response}
71
+ instance_ready_timeout: |-
72
+ The instance never became "ready" in AWS. The timeout currently
73
+ set waiting for the instance to become ready is %{timeout} seconds.
74
+ Please verify that the machine properly boots. If you need more time
75
+ set the `instance_ready_timeout` configuration on the AWS provider.
76
+ rsync_error: |-
77
+ There was an error when attempting to rsync a share folder.
78
+ Please inspect the error message below for more info.
79
+
80
+ Host path: %{hostpath}
81
+ Guest path: %{guestpath}
82
+ Error: %{stderr}
83
+ mkdir_error: |-
84
+ There was an error when attempting to create a shared host folder.
85
+ Please inspect the error message below for more info.
86
+
87
+ Host path: %{hostpath}
88
+ Error: %{err}
89
+
90
+ states:
91
+ short_not_created: |-
92
+ not created
93
+ long_not_created: |-
94
+ The EC2 instance is not created. Run `vagrant up` to create it.
95
+
96
+ short_stopped: |-
97
+ stopped
98
+ long_stopped: |-
99
+ The EC2 instance is stopped. Run `vagrant up` to start it.
100
+
101
+ short_stopping: |-
102
+ stopping
103
+ long_stopping: |-
104
+ The EC2 instance is stopping. Wait until is completely stopped to
105
+ run `vagrant up` and start it.
106
+
107
+ short_pending: |-
108
+ pending
109
+ long_pending: |-
110
+ The EC2 instance is pending a start (i.e. this is a transition state).
111
+
112
+ short_running: |-
113
+ running
114
+ long_running: |-
115
+ The EC2 instance is running. To stop this machine, you can run
116
+ `vagrant halt`. To destroy the machine, you can run `vagrant destroy`.
117
+
118
+ short_pending: |-
119
+ pending
120
+ long_pending: |-
121
+ The EC2 instance is still being initialized. To destroy this machine,
122
+ you can run `vagrant destroy`.
@@ -0,0 +1,216 @@
1
+ require "vagrant-aws/config"
2
+
3
+ describe VagrantPlugins::AWS::Config do
4
+ let(:instance) { described_class.new }
5
+
6
+ # Ensure tests are not affected by AWS credential environment variables
7
+ before :each do
8
+ ENV.stub(:[] => nil)
9
+ end
10
+
11
+ describe "defaults" do
12
+ subject do
13
+ instance.tap do |o|
14
+ o.finalize!
15
+ end
16
+ end
17
+
18
+ its("access_key_id") { should be_nil }
19
+ its("ami") { should be_nil }
20
+ its("availability_zone") { should be_nil }
21
+ its("instance_ready_timeout") { should == 120 }
22
+ its("instance_type") { should == "m1.small" }
23
+ its("keypair_name") { should be_nil }
24
+ its("private_ip_address") { should be_nil }
25
+ its("region") { should == "us-east-1" }
26
+ its("secret_access_key") { should be_nil }
27
+ its("security_groups") { should == [] }
28
+ its("subnet_id") { should be_nil }
29
+ its("iam_instance_profile_arn") { should be_nil }
30
+ its("iam_instance_profile_name") { should be_nil }
31
+ its("tags") { should == {} }
32
+ its("user_data") { should be_nil }
33
+ its("use_iam_profile") { should be_false }
34
+ its("block_device_mapping") {should == [] }
35
+ its("elastic_ip") { should be_nil }
36
+ its("terminate_on_shutdown") { should == false }
37
+ its("ssh_host_attribute") { should be_nil }
38
+ its("monitoring") { should == false }
39
+ its("ebs_optimized") { should == false }
40
+ its("associate_public_ip") { should == false }
41
+ end
42
+
43
+ describe "overriding defaults" do
44
+ # I typically don't meta-program in tests, but this is a very
45
+ # simple boilerplate test, so I cut corners here. It just sets
46
+ # each of these attributes to "foo" in isolation, and reads the value
47
+ # and asserts the proper result comes back out.
48
+ [:access_key_id, :ami, :availability_zone, :instance_ready_timeout,
49
+ :instance_type, :keypair_name, :ssh_host_attribute, :ebs_optimized,
50
+ :region, :secret_access_key, :monitoring, :associate_public_ip,
51
+ :subnet_id, :tags, :elastic_ip, :terminate_on_shutdown,
52
+ :iam_instance_profile_arn, :iam_instance_profile_name,
53
+ :use_iam_profile, :user_data, :block_device_mapping].each do |attribute|
54
+
55
+ it "should not default #{attribute} if overridden" do
56
+ instance.send("#{attribute}=".to_sym, "foo")
57
+ instance.finalize!
58
+ instance.send(attribute).should == "foo"
59
+ end
60
+ end
61
+ it "should not default security_groups if overridden" do
62
+ instance.security_groups = "foo"
63
+ instance.finalize!
64
+ instance.security_groups.should == ["foo"]
65
+ end
66
+ end
67
+
68
+ describe "getting credentials from environment" do
69
+ context "without EC2 credential environment variables" do
70
+ subject do
71
+ instance.tap do |o|
72
+ o.finalize!
73
+ end
74
+ end
75
+
76
+ its("access_key_id") { should be_nil }
77
+ its("secret_access_key") { should be_nil }
78
+ end
79
+
80
+ context "with EC2 credential environment variables" do
81
+ before :each do
82
+ ENV.stub(:[]).with("AWS_ACCESS_KEY").and_return("access_key")
83
+ ENV.stub(:[]).with("AWS_SECRET_KEY").and_return("secret_key")
84
+ end
85
+
86
+ subject do
87
+ instance.tap do |o|
88
+ o.finalize!
89
+ end
90
+ end
91
+
92
+ its("access_key_id") { should == "access_key" }
93
+ its("secret_access_key") { should == "secret_key" }
94
+ end
95
+ end
96
+
97
+ describe "region config" do
98
+ let(:config_access_key_id) { "foo" }
99
+ let(:config_ami) { "foo" }
100
+ let(:config_instance_type) { "foo" }
101
+ let(:config_keypair_name) { "foo" }
102
+ let(:config_region) { "foo" }
103
+ let(:config_secret_access_key) { "foo" }
104
+
105
+ def set_test_values(instance)
106
+ instance.access_key_id = config_access_key_id
107
+ instance.ami = config_ami
108
+ instance.instance_type = config_instance_type
109
+ instance.keypair_name = config_keypair_name
110
+ instance.region = config_region
111
+ instance.secret_access_key = config_secret_access_key
112
+ end
113
+
114
+ it "should raise an exception if not finalized" do
115
+ expect { instance.get_region_config("us-east-1") }.
116
+ to raise_error
117
+ end
118
+
119
+ context "with no specific config set" do
120
+ subject do
121
+ # Set the values on the top-level object
122
+ set_test_values(instance)
123
+
124
+ # Finalize so we can get the region config
125
+ instance.finalize!
126
+
127
+ # Get a lower level region
128
+ instance.get_region_config("us-east-1")
129
+ end
130
+
131
+ its("access_key_id") { should == config_access_key_id }
132
+ its("ami") { should == config_ami }
133
+ its("instance_type") { should == config_instance_type }
134
+ its("keypair_name") { should == config_keypair_name }
135
+ its("region") { should == config_region }
136
+ its("secret_access_key") { should == config_secret_access_key }
137
+ end
138
+
139
+ context "with a specific config set" do
140
+ let(:region_name) { "hashi-region" }
141
+
142
+ subject do
143
+ # Set the values on a specific region
144
+ instance.region_config region_name do |config|
145
+ set_test_values(config)
146
+ end
147
+
148
+ # Finalize so we can get the region config
149
+ instance.finalize!
150
+
151
+ # Get the region
152
+ instance.get_region_config(region_name)
153
+ end
154
+
155
+ its("access_key_id") { should == config_access_key_id }
156
+ its("ami") { should == config_ami }
157
+ its("instance_type") { should == config_instance_type }
158
+ its("keypair_name") { should == config_keypair_name }
159
+ its("region") { should == region_name }
160
+ its("secret_access_key") { should == config_secret_access_key }
161
+ end
162
+
163
+ describe "inheritance of parent config" do
164
+ let(:region_name) { "hashi-region" }
165
+
166
+ subject do
167
+ # Set the values on a specific region
168
+ instance.region_config region_name do |config|
169
+ config.ami = "child"
170
+ end
171
+
172
+ # Set some top-level values
173
+ instance.access_key_id = "parent"
174
+ instance.ami = "parent"
175
+
176
+ # Finalize and get the region
177
+ instance.finalize!
178
+ instance.get_region_config(region_name)
179
+ end
180
+
181
+ its("access_key_id") { should == "parent" }
182
+ its("ami") { should == "child" }
183
+ end
184
+
185
+ describe "shortcut configuration" do
186
+ subject do
187
+ # Use the shortcut configuration to set some values
188
+ instance.region_config "us-east-1", :ami => "child"
189
+ instance.finalize!
190
+ instance.get_region_config("us-east-1")
191
+ end
192
+
193
+ its("ami") { should == "child" }
194
+ end
195
+
196
+ describe "merging" do
197
+ let(:first) { described_class.new }
198
+ let(:second) { described_class.new }
199
+
200
+ it "should merge the tags and block_device_mappings" do
201
+ first.tags["one"] = "one"
202
+ second.tags["two"] = "two"
203
+ first.block_device_mapping = [{:one => "one"}]
204
+ second.block_device_mapping = [{:two => "two"}]
205
+
206
+ third = first.merge(second)
207
+ third.tags.should == {
208
+ "one" => "one",
209
+ "two" => "two"
210
+ }
211
+ third.block_device_mapping.index({:one => "one"}).should_not be_nil
212
+ third.block_device_mapping.index({:two => "two"}).should_not be_nil
213
+ end
214
+ end
215
+ end
216
+ end