tnargav-aws 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.gitsetup.yml +5 -0
- data/CHANGELOG.md +39 -0
- data/Gemfile +10 -0
- data/LICENSE +8 -0
- data/README.md +198 -0
- data/Rakefile +21 -0
- data/dummy.box +0 -0
- data/example_box/README.md +13 -0
- data/example_box/metadata.json +3 -0
- data/lib/vagrant-aws/action/connect_aws.rb +46 -0
- data/lib/vagrant-aws/action/is_created.rb +18 -0
- data/lib/vagrant-aws/action/message_already_created.rb +16 -0
- data/lib/vagrant-aws/action/message_not_created.rb +16 -0
- data/lib/vagrant-aws/action/read_ssh_info.rb +41 -0
- data/lib/vagrant-aws/action/read_state.rb +38 -0
- data/lib/vagrant-aws/action/run_instance.rb +161 -0
- data/lib/vagrant-aws/action/sync_folders.rb +67 -0
- data/lib/vagrant-aws/action/terminate_instance.rb +26 -0
- data/lib/vagrant-aws/action/timed_provision.rb +21 -0
- data/lib/vagrant-aws/action/warn_networks.rb +19 -0
- data/lib/vagrant-aws/action.rb +121 -0
- data/lib/vagrant-aws/config.rb +275 -0
- data/lib/vagrant-aws/errors.rb +23 -0
- data/lib/vagrant-aws/plugin.rb +73 -0
- data/lib/vagrant-aws/provider.rb +50 -0
- data/lib/vagrant-aws/util/timer.rb +17 -0
- data/lib/vagrant-aws/version.rb +5 -0
- data/lib/vagrant-aws.rb +18 -0
- data/locales/en.yml +73 -0
- data/spec/vagrant-aws/config_spec.rb +197 -0
- data/tnargav-aws.gemspec +58 -0
- data/vagrant-aws.gemspec +58 -0
- metadata +157 -0
data/.gitignore
ADDED
data/.gitsetup.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# 0.2.2 (April 18, 2013)
|
2
|
+
|
3
|
+
* Fix crashing bug with incorrect provisioner arguments.
|
4
|
+
|
5
|
+
# 0.2.1 (April 16, 2013)
|
6
|
+
|
7
|
+
* Got rid of extranneous references to old SSH settings.
|
8
|
+
|
9
|
+
# 0.2.0 (April 16, 2013)
|
10
|
+
|
11
|
+
* Add support for `vagrant ssh -c` [GH-42]
|
12
|
+
* Ability to specify a timeout for waiting for instances to become ready. [GH-44]
|
13
|
+
* Better error message if instance didn't become ready in time.
|
14
|
+
* Connection can now be done using IAM profiles. [GH-41]
|
15
|
+
|
16
|
+
# 0.1.3 (April 9, 2013)
|
17
|
+
|
18
|
+
* The `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` will be used if available
|
19
|
+
and no specific keys are set in the Vagrantfile. [GH-33]
|
20
|
+
* Fix issues with SSH on VPCs, the correct IP is used. [GH-30]
|
21
|
+
* Exclude the ".vagrant" directory from rsync.
|
22
|
+
* Implement `:disabled` flag support for shared folders. [GH-29]
|
23
|
+
* `aws.user_data` to specify user data on the instance. [GH-26]
|
24
|
+
|
25
|
+
# 0.1.2 (March 22, 2013)
|
26
|
+
|
27
|
+
* Choose the proper region when connecting to AWS. [GH-9]
|
28
|
+
* Configurable SSH port. [GH-13]
|
29
|
+
* Support other AWS-compatible API endpoints with `config.endpoint`
|
30
|
+
and `config.version`. [GH-6]
|
31
|
+
* Disable strict host key checking on rsync so known hosts aren't an issue. [GH-7]
|
32
|
+
|
33
|
+
# 0.1.1 (March 18, 2013)
|
34
|
+
|
35
|
+
* Up fog dependency for Vagrant 1.1.1
|
36
|
+
|
37
|
+
# 0.1.0 (March 14, 2013)
|
38
|
+
|
39
|
+
* Initial release.
|
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
# We depend on Vagrant for development, but we don't add it as a
|
7
|
+
# gem dependency because we expect to be installed within the
|
8
|
+
# Vagrant environment itself using `vagrant plugin`.
|
9
|
+
gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
|
10
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright (c) 2013 Mitchell Hashimoto
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
5
|
+
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
# Vagrant AWS Provider
|
2
|
+
|
3
|
+
This is a [Vagrant](http://www.vagrantup.com) 1.2+ plugin that adds an [AWS](http://aws.amazon.com)
|
4
|
+
provider to Vagrant, allowing Vagrant to control and provision machines in
|
5
|
+
EC2 and VPC.
|
6
|
+
|
7
|
+
**NOTE:** This plugin requires Vagrant 1.2+,
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
* Boot EC2 or VPC instances.
|
12
|
+
* SSH into the instances.
|
13
|
+
* Provision the instances with any built-in Vagrant provisioner.
|
14
|
+
* Minimal synced folder support via `rsync`.
|
15
|
+
* Define region-specifc configurations so Vagrant can manage machines
|
16
|
+
in multiple regions.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Install using standard Vagrant 1.1+ plugin installation methods. After
|
21
|
+
installing, `vagrant up` and specify the `aws` provider. An example is
|
22
|
+
shown below.
|
23
|
+
|
24
|
+
```
|
25
|
+
$ vagrant plugin install vagrant-aws
|
26
|
+
...
|
27
|
+
$ vagrant up --provider=aws
|
28
|
+
...
|
29
|
+
```
|
30
|
+
|
31
|
+
Of course prior to doing this, you'll need to obtain an AWS-compatible
|
32
|
+
box file for Vagrant.
|
33
|
+
|
34
|
+
## Quick Start
|
35
|
+
|
36
|
+
After installing the plugin (instructions above), the quickest way to get
|
37
|
+
started is to actually use a dummy AWS box and specify all the details
|
38
|
+
manually within a `config.vm.provider` block. So first, add the dummy
|
39
|
+
box using any name you want:
|
40
|
+
|
41
|
+
```
|
42
|
+
$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
|
43
|
+
...
|
44
|
+
```
|
45
|
+
|
46
|
+
And then make a Vagrantfile that looks like the following, filling in
|
47
|
+
your information where necessary.
|
48
|
+
|
49
|
+
```
|
50
|
+
Vagrant.configure("2") do |config|
|
51
|
+
config.vm.box = "dummy"
|
52
|
+
|
53
|
+
config.vm.provider :aws do |aws, override|
|
54
|
+
aws.access_key_id = "YOUR KEY"
|
55
|
+
aws.secret_access_key = "YOUR SECRET KEY"
|
56
|
+
aws.keypair_name = "KEYPAIR NAME"
|
57
|
+
|
58
|
+
aws.ami = "ami-7747d01e"
|
59
|
+
|
60
|
+
override.ssh.username = "ubuntu"
|
61
|
+
override.ssh.private_key_path = "PATH TO YOUR PRIVATE KEY"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
And then run `vagrant up --provider=aws`.
|
67
|
+
|
68
|
+
This will start an Ubuntu 12.04 instance in the us-east-1 region within
|
69
|
+
your account. And assuming your SSH information was filled in properly
|
70
|
+
within your Vagrantfile, SSH and provisioning will work as well.
|
71
|
+
|
72
|
+
Note that normally a lot of this boilerplate is encoded within the box
|
73
|
+
file, but the box file used for the quick start, the "dummy" box, has
|
74
|
+
no preconfigured defaults.
|
75
|
+
|
76
|
+
If you have issues with SSH connecting, make sure that the instances
|
77
|
+
are being launched with a security group that allows SSH access.
|
78
|
+
|
79
|
+
## Box Format
|
80
|
+
|
81
|
+
Every provider in Vagrant must introduce a custom box format. This
|
82
|
+
provider introduces `aws` boxes. You can view an example box in
|
83
|
+
the [example_box/ directory](https://github.com/mitchellh/vagrant-aws/tree/master/example_box).
|
84
|
+
That directory also contains instructions on how to build a box.
|
85
|
+
|
86
|
+
The box format is basically just the required `metadata.json` file
|
87
|
+
along with a `Vagrantfile` that does default settings for the
|
88
|
+
provider-specific configuration for this provider.
|
89
|
+
|
90
|
+
## Configuration
|
91
|
+
|
92
|
+
This provider exposes quite a few provider-specific configuration options:
|
93
|
+
|
94
|
+
* `access_key_id` - The access key for accessing AWS
|
95
|
+
* `ami` - The AMI id to boot, such as "ami-12345678"
|
96
|
+
* `availability_zone` - The availability zone within the region to launch
|
97
|
+
the instance. If nil, it will use the default set by Amazon.
|
98
|
+
* `instance_ready_timeout` - The number of seconds to wait for the instance
|
99
|
+
to become "ready" in AWS. Defaults to 120 seconds.
|
100
|
+
* `instance_type` - The type of instance, such as "m1.small". The default
|
101
|
+
value of this if not specified is "m1.small".
|
102
|
+
* `keypair_name` - The name of the keypair to use to bootstrap AMIs
|
103
|
+
which support it.
|
104
|
+
* `private_ip_address` - The private IP address to assign to an instance
|
105
|
+
within a [VPC](http://aws.amazon.com/vpc/)
|
106
|
+
* `region` - The region to start the instance in, such as "us-east-1"
|
107
|
+
* `secret_access_key` - The secret access key for accessing AWS
|
108
|
+
* `security_groups` - An array of security groups for the instance. If this
|
109
|
+
instance will be launched in VPC, this must be a list of security group
|
110
|
+
IDs.
|
111
|
+
* `subnet_id` - The subnet to boot the instance into, for VPC.
|
112
|
+
* `tags` - A hash of tags to set on the machine.
|
113
|
+
* `use_iam_profile` - If true, will use [IAM profiles](http://docs.aws.amazon.com/IAM/latest/UserGuide/instance-profiles.html)
|
114
|
+
for credentials.
|
115
|
+
|
116
|
+
These can be set like typical provider-specific configuration:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
Vagrant.configure("2") do |config|
|
120
|
+
# ... other stuff
|
121
|
+
|
122
|
+
config.vm.provider :aws do |aws|
|
123
|
+
aws.access_key_id = "foo"
|
124
|
+
aws.secret_access_key = "bar"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
In addition to the above top-level configs, you can use the `region_config`
|
130
|
+
method to specify region-specific overrides within your Vagrantfile. Note
|
131
|
+
that the top-level `region` config must always be specified to choose which
|
132
|
+
region you want to actually use, however. This looks like this:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
Vagrant.configure("2") do |config|
|
136
|
+
# ... other stuff
|
137
|
+
|
138
|
+
config.vm.provider :aws do |aws|
|
139
|
+
aws.access_key_id = "foo"
|
140
|
+
aws.secret_access_key = "bar"
|
141
|
+
aws.region = "us-east-1"
|
142
|
+
|
143
|
+
# Simple region config
|
144
|
+
aws.region_config "us-east-1", :ami => "ami-12345678"
|
145
|
+
|
146
|
+
# More comprehensive region config
|
147
|
+
aws.region_config "us-west-2" do |region|
|
148
|
+
region.ami = "ami-87654321"
|
149
|
+
region.keypair_name = "company-west"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
The region-specific configurations will override the top-level
|
156
|
+
configurations when that region is used. They otherwise inherit
|
157
|
+
the top-level configurations, as you would probably expect.
|
158
|
+
|
159
|
+
## Networks
|
160
|
+
|
161
|
+
Networking features in the form of `config.vm.network` are not
|
162
|
+
supported with `vagrant-aws`, currently. If any of these are
|
163
|
+
specified, Vagrant will emit a warning, but will otherwise boot
|
164
|
+
the AWS machine.
|
165
|
+
|
166
|
+
## Synced Folders
|
167
|
+
|
168
|
+
There is minimal support for synced folders. Upon `vagrant up`,
|
169
|
+
`vagrant reload`, and `vagrant provision`, the AWS provider will use
|
170
|
+
`rsync` (if available) to uni-directionally sync the folder to
|
171
|
+
the remote machine over SSH.
|
172
|
+
|
173
|
+
This is good enough for all built-in Vagrant provisioners (shell,
|
174
|
+
chef, and puppet) to work!
|
175
|
+
|
176
|
+
## Development
|
177
|
+
|
178
|
+
To work on the `vagrant-aws` plugin, clone this repository out, and use
|
179
|
+
[Bundler](http://gembundler.com) to get the dependencies:
|
180
|
+
|
181
|
+
```
|
182
|
+
$ bundle
|
183
|
+
```
|
184
|
+
|
185
|
+
Once you have the dependencies, verify the unit tests pass with `rake`:
|
186
|
+
|
187
|
+
```
|
188
|
+
$ bundle exec rake
|
189
|
+
```
|
190
|
+
|
191
|
+
If those pass, you're ready to start developing the plugin. You can test
|
192
|
+
the plugin without installing it into your Vagrant environment by just
|
193
|
+
creating a `Vagrantfile` in the top level of this directory (it is gitignored)
|
194
|
+
that uses it, and uses bundler to execute Vagrant:
|
195
|
+
|
196
|
+
```
|
197
|
+
$ bundle exec vagrant up --provider=aws
|
198
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
# Immediately sync all stdout so that tools like buildbot can
|
6
|
+
# immediately load in the output.
|
7
|
+
$stdout.sync = true
|
8
|
+
$stderr.sync = true
|
9
|
+
|
10
|
+
# Change to the directory of this file.
|
11
|
+
Dir.chdir(File.expand_path("../", __FILE__))
|
12
|
+
|
13
|
+
# This installs the tasks that help with gem creation and
|
14
|
+
# publishing.
|
15
|
+
Bundler::GemHelper.install_tasks
|
16
|
+
|
17
|
+
# Install the `spec` task so that we can run tests.
|
18
|
+
RSpec::Core::RakeTask.new
|
19
|
+
|
20
|
+
# Default task is to run the unit tests
|
21
|
+
task :default => "spec"
|
data/dummy.box
ADDED
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Vagrant AWS Example Box
|
2
|
+
|
3
|
+
Vagrant providers each require a custom provider-specific box format.
|
4
|
+
This folder shows the example contents of a box for the `aws` provider.
|
5
|
+
To turn this into a box:
|
6
|
+
|
7
|
+
```
|
8
|
+
$ tar cvzf aws.box ./metadata.json ./Vagrantfile
|
9
|
+
```
|
10
|
+
|
11
|
+
This box works by using Vagrant's built-in Vagrantfile merging to setup
|
12
|
+
defaults for AWS. These defaults can easily be overwritten by higher-level
|
13
|
+
Vagrantfiles (such as project root Vagrantfiles).
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "fog"
|
2
|
+
require "log4r"
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module AWS
|
6
|
+
module Action
|
7
|
+
# This action connects to AWS, verifies credentials work, and
|
8
|
+
# puts the AWS connection object into the `:aws_compute` key
|
9
|
+
# in the environment.
|
10
|
+
class ConnectAWS
|
11
|
+
def initialize(app, env)
|
12
|
+
@app = app
|
13
|
+
@logger = Log4r::Logger.new("vagrant_aws::action::connect_aws")
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
# Get the region we're going to booting up in
|
18
|
+
region = env[:machine].provider_config.region
|
19
|
+
|
20
|
+
# Get the configs
|
21
|
+
region_config = env[:machine].provider_config.get_region_config(region)
|
22
|
+
|
23
|
+
# Build the fog config
|
24
|
+
fog_config = {
|
25
|
+
:provider => :aws,
|
26
|
+
:region => region
|
27
|
+
}
|
28
|
+
if region_config.use_iam_profile
|
29
|
+
fog_config[:use_iam_profile] = True
|
30
|
+
else
|
31
|
+
fog_config[:aws_access_key_id] = region_config.access_key_id
|
32
|
+
fog_config[:aws_secret_access_key] = region_config.secret_access_key
|
33
|
+
end
|
34
|
+
|
35
|
+
fog_config[:endpoint] = region_config.endpoint if region_config.endpoint
|
36
|
+
fog_config[:version] = region_config.version if region_config.version
|
37
|
+
|
38
|
+
@logger.info("Connecting to AWS...")
|
39
|
+
env[:aws_compute] = Fog::Compute.new(fog_config)
|
40
|
+
|
41
|
+
@app.call(env)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module AWS
|
3
|
+
module Action
|
4
|
+
# This can be used with "Call" built-in to check if the machine
|
5
|
+
# is created and branch in the middleware.
|
6
|
+
class IsCreated
|
7
|
+
def initialize(app, env)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
env[:result] = env[:machine].state.id != :not_created
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module AWS
|
3
|
+
module Action
|
4
|
+
class MessageAlreadyCreated
|
5
|
+
def initialize(app, env)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:ui].info(I18n.t("vagrant_aws.already_created"))
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "log4r"
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module AWS
|
5
|
+
module Action
|
6
|
+
# This action reads the SSH info for the machine and puts it into the
|
7
|
+
# `:machine_ssh_info` key in the environment.
|
8
|
+
class ReadSSHInfo
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@logger = Log4r::Logger.new("vagrant_aws::action::read_ssh_info")
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_ssh_info] = read_ssh_info(env[:aws_compute], env[:machine])
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_ssh_info(aws, machine)
|
21
|
+
return nil if machine.id.nil?
|
22
|
+
|
23
|
+
# Find the machine
|
24
|
+
server = aws.servers.get(machine.id)
|
25
|
+
if server.nil?
|
26
|
+
# The machine can't be found
|
27
|
+
@logger.info("Machine couldn't be found, assuming it got destroyed.")
|
28
|
+
machine.id = nil
|
29
|
+
return nil
|
30
|
+
end
|
31
|
+
|
32
|
+
# Read the DNS info
|
33
|
+
return {
|
34
|
+
:host => server.dns_name || server.private_ip_address,
|
35
|
+
:port => 22
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "log4r"
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module AWS
|
5
|
+
module Action
|
6
|
+
# This action reads the state of the machine and puts it in the
|
7
|
+
# `:machine_state_id` key in the environment.
|
8
|
+
class ReadState
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@logger = Log4r::Logger.new("vagrant_aws::action::read_state")
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_state_id] = read_state(env[:aws_compute], env[:machine])
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_state(aws, machine)
|
21
|
+
return :not_created if machine.id.nil?
|
22
|
+
|
23
|
+
# Find the machine
|
24
|
+
server = aws.servers.get(machine.id)
|
25
|
+
if server.nil? || [:"shutting-down", :terminated].include?(server.state.to_sym)
|
26
|
+
# The machine can't be found
|
27
|
+
@logger.info("Machine not found or terminated, assuming it got destroyed.")
|
28
|
+
machine.id = nil
|
29
|
+
return :not_created
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the state
|
33
|
+
return server.state.to_sym
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require "log4r"
|
2
|
+
|
3
|
+
require 'vagrant/util/retryable'
|
4
|
+
|
5
|
+
require 'vagrant-aws/util/timer'
|
6
|
+
|
7
|
+
module VagrantPlugins
|
8
|
+
module AWS
|
9
|
+
module Action
|
10
|
+
# This runs the configured instance.
|
11
|
+
class RunInstance
|
12
|
+
include Vagrant::Util::Retryable
|
13
|
+
|
14
|
+
def initialize(app, env)
|
15
|
+
@app = app
|
16
|
+
@logger = Log4r::Logger.new("vagrant_aws::action::run_instance")
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
# Initialize metrics if they haven't been
|
21
|
+
env[:metrics] ||= {}
|
22
|
+
|
23
|
+
# Get the region we're going to booting up in
|
24
|
+
region = env[:machine].provider_config.region
|
25
|
+
|
26
|
+
# Get the configs
|
27
|
+
region_config = env[:machine].provider_config.get_region_config(region)
|
28
|
+
ami = region_config.ami
|
29
|
+
availability_zone = region_config.availability_zone
|
30
|
+
instance_type = region_config.instance_type
|
31
|
+
keypair = region_config.keypair_name
|
32
|
+
private_ip_address = region_config.private_ip_address
|
33
|
+
security_groups = region_config.security_groups
|
34
|
+
subnet_id = region_config.subnet_id
|
35
|
+
tags = region_config.tags
|
36
|
+
user_data = region_config.user_data
|
37
|
+
|
38
|
+
# If there is no keypair then warn the user
|
39
|
+
if !keypair
|
40
|
+
env[:ui].warn(I18n.t("vagrant_aws.launch_no_keypair"))
|
41
|
+
end
|
42
|
+
|
43
|
+
# If there is a subnet ID then warn the user
|
44
|
+
if subnet_id
|
45
|
+
env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Launch!
|
49
|
+
env[:ui].info(I18n.t("vagrant_aws.launching_instance"))
|
50
|
+
env[:ui].info(" -- Type: #{instance_type}")
|
51
|
+
env[:ui].info(" -- AMI: #{ami}")
|
52
|
+
env[:ui].info(" -- Region: #{region}")
|
53
|
+
env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
|
54
|
+
env[:ui].info(" -- Keypair: #{keypair}") if keypair
|
55
|
+
env[:ui].info(" -- Subnet ID: #{subnet_id}") if subnet_id
|
56
|
+
env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
|
57
|
+
env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?
|
58
|
+
|
59
|
+
begin
|
60
|
+
options = {
|
61
|
+
:availability_zone => availability_zone,
|
62
|
+
:flavor_id => instance_type,
|
63
|
+
:image_id => ami,
|
64
|
+
:key_name => keypair,
|
65
|
+
:private_ip_address => private_ip_address,
|
66
|
+
:subnet_id => subnet_id,
|
67
|
+
:tags => tags,
|
68
|
+
:user_data => user_data
|
69
|
+
}
|
70
|
+
|
71
|
+
if !security_groups.empty?
|
72
|
+
security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
|
73
|
+
options[security_group_key] = security_groups
|
74
|
+
end
|
75
|
+
|
76
|
+
server = env[:aws_compute].servers.create(options)
|
77
|
+
rescue Fog::Compute::AWS::NotFound => e
|
78
|
+
# Invalid subnet doesn't have its own error so we catch and
|
79
|
+
# check the error message here.
|
80
|
+
if e.message =~ /subnet ID/
|
81
|
+
raise Errors::FogError,
|
82
|
+
:message => "Subnet ID not found: #{subnet_id}"
|
83
|
+
end
|
84
|
+
|
85
|
+
raise
|
86
|
+
rescue Fog::Compute::AWS::Error => e
|
87
|
+
raise Errors::FogError, :message => e.message
|
88
|
+
end
|
89
|
+
|
90
|
+
# Immediately save the ID since it is created at this point.
|
91
|
+
env[:machine].id = server.id
|
92
|
+
|
93
|
+
# Wait for the instance to be ready first
|
94
|
+
env[:metrics]["instance_ready_time"] = Util::Timer.time do
|
95
|
+
tries = region_config.instance_ready_timeout / 2
|
96
|
+
|
97
|
+
env[:ui].info(I18n.t("vagrant_aws.waiting_for_ready"))
|
98
|
+
begin
|
99
|
+
retryable(:on => Fog::Errors::TimeoutError, :tries => tries) do
|
100
|
+
# If we're interrupted don't worry about waiting
|
101
|
+
next if env[:interrupted]
|
102
|
+
|
103
|
+
# Wait for the server to be ready
|
104
|
+
server.wait_for(2) { ready? }
|
105
|
+
end
|
106
|
+
rescue Fog::Errors::TimeoutError
|
107
|
+
# Delete the instance
|
108
|
+
terminate(env)
|
109
|
+
|
110
|
+
# Notify the user
|
111
|
+
raise Errors::InstanceReadyTimeout,
|
112
|
+
timeout: region_config.instance_ready_timeout
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
@logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")
|
117
|
+
|
118
|
+
if !env[:interrupted]
|
119
|
+
env[:metrics]["instance_ssh_time"] = Util::Timer.time do
|
120
|
+
# Wait for SSH to be ready.
|
121
|
+
env[:ui].info(I18n.t("vagrant_aws.waiting_for_ssh"))
|
122
|
+
while true
|
123
|
+
# If we're interrupted then just back out
|
124
|
+
break if env[:interrupted]
|
125
|
+
break if env[:machine].communicate.ready?
|
126
|
+
sleep 2
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
@logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")
|
131
|
+
|
132
|
+
# Ready and booted!
|
133
|
+
env[:ui].info(I18n.t("vagrant_aws.ready"))
|
134
|
+
end
|
135
|
+
|
136
|
+
# Terminate the instance if we were interrupted
|
137
|
+
terminate(env) if env[:interrupted]
|
138
|
+
|
139
|
+
@app.call(env)
|
140
|
+
end
|
141
|
+
|
142
|
+
def recover(env)
|
143
|
+
return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)
|
144
|
+
|
145
|
+
if env[:machine].provider.state.id != :not_created
|
146
|
+
# Undo the import
|
147
|
+
terminate(env)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def terminate(env)
|
152
|
+
destroy_env = env.dup
|
153
|
+
destroy_env.delete(:interrupted)
|
154
|
+
destroy_env[:config_validate] = false
|
155
|
+
destroy_env[:force_confirm_destroy] = true
|
156
|
+
env[:action_runner].run(Action.action_destroy, destroy_env)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|