vagrant-gq 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/Gemfile +14 -0
- data/LICENSE +22 -0
- data/README.md +169 -0
- data/Rakefile +21 -0
- data/example_box/README.md +13 -0
- data/example_box/metadata.json +3 -0
- data/greenqloud-generic.box +0 -0
- data/lib/vagrant-gq/action/connect_gq.rb +47 -0
- data/lib/vagrant-gq/action/is_created.rb +18 -0
- data/lib/vagrant-gq/action/is_stopped.rb +18 -0
- data/lib/vagrant-gq/action/message_already_created.rb +16 -0
- data/lib/vagrant-gq/action/message_not_created.rb +16 -0
- data/lib/vagrant-gq/action/message_will_not_destroy.rb +16 -0
- data/lib/vagrant-gq/action/read_ssh_info.rb +53 -0
- data/lib/vagrant-gq/action/read_state.rb +38 -0
- data/lib/vagrant-gq/action/run_instance.rb +247 -0
- data/lib/vagrant-gq/action/start_instance.rb +81 -0
- data/lib/vagrant-gq/action/stop_instance.rb +28 -0
- data/lib/vagrant-gq/action/sync_folders.rb +118 -0
- data/lib/vagrant-gq/action/terminate_instance.rb +47 -0
- data/lib/vagrant-gq/action/timed_provision.rb +21 -0
- data/lib/vagrant-gq/action/wait_for_state.rb +41 -0
- data/lib/vagrant-gq/action/warn_networks.rb +19 -0
- data/lib/vagrant-gq/action.rb +190 -0
- data/lib/vagrant-gq/config.rb +372 -0
- data/lib/vagrant-gq/errors.rb +31 -0
- data/lib/vagrant-gq/plugin.rb +73 -0
- data/lib/vagrant-gq/provider.rb +50 -0
- data/lib/vagrant-gq/util/timer.rb +17 -0
- data/lib/vagrant-gq/version.rb +5 -0
- data/lib/vagrant-gq.rb +18 -0
- data/locales/en.yml +122 -0
- data/spec/vagrant-gq/config_spec.rb +216 -0
- data/vagrant-gq.gemspec +58 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 246a2947e46f054d53627c48b086bb9f576f9d50
|
4
|
+
data.tar.gz: 41b65e5ac21205edad25dc2026ee200939b25e65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8a88b10952c45f78d1b65137f56aff9edef248a8847d32f3a6b22e12d30a6add2840d671e2d1bd97d0ac9ce81bece5b3204cad73ff1da5445dfb3f2e2c6e023
|
7
|
+
data.tar.gz: f25b9712c369852078109fcc5e74495ae47e05eca05305a34489b36c51b9390b0e42da2612777620624005c82cfd189750ec47f523f355727a19e549a524ebe5
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# OS-specific
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
# editors
|
5
|
+
*.swp
|
6
|
+
|
7
|
+
# Bundler/Rubygems
|
8
|
+
*.gem
|
9
|
+
.bundle
|
10
|
+
pkg/*
|
11
|
+
tags
|
12
|
+
Gemfile.lock
|
13
|
+
|
14
|
+
# Vagrant
|
15
|
+
.vagrant
|
16
|
+
Vagrantfile
|
17
|
+
!example_box/Vagrantfile
|
18
|
+
|
19
|
+
# RVM files for gemset/ruby setting
|
20
|
+
.ruby-*
|
21
|
+
.rvmrc
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
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
|
11
|
+
|
12
|
+
group :plugins do
|
13
|
+
gem "vagrant-gq", :path => "."
|
14
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright (c) 2014 GreenQloud
|
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.
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
Vagrant-gq is a fork of vagrant-aws, which bears the following license:
|
13
|
+
|
14
|
+
|
15
|
+
The MIT License (MIT)
|
16
|
+
Copyright (c) 2014 Mitchell Hashimoto
|
17
|
+
|
18
|
+
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:
|
19
|
+
|
20
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
21
|
+
|
22
|
+
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,169 @@
|
|
1
|
+
# Vagrant GQ Provider
|
2
|
+
|
3
|
+
This is a [Vagrant](http://www.vagrantup.com) 1.2+ plugin that adds a [GreenQloud](http://www.greenqloud.com)
|
4
|
+
provider to Vagrant, allowing Vagrant to control and provision machines in GreenQloud.
|
5
|
+
|
6
|
+
**NOTE:** This plugin requires Vagrant 1.2+,
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
* Boot GreenQloud instances.
|
11
|
+
* SSH into the instances.
|
12
|
+
* Provision the instances with any built-in Vagrant provisioner.
|
13
|
+
* Minimal synced folder support via `rsync`.
|
14
|
+
* Define region-specifc configurations so Vagrant can manage machines
|
15
|
+
in multiple regions.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Install using standard Vagrant 1.1+ plugin installation methods. After
|
20
|
+
installing, `vagrant up` and specify the `gq` provider. An example is
|
21
|
+
shown below.
|
22
|
+
|
23
|
+
```
|
24
|
+
$ vagrant plugin install vagrant-gq
|
25
|
+
...
|
26
|
+
$ vagrant up --provider=gq
|
27
|
+
...
|
28
|
+
```
|
29
|
+
|
30
|
+
Of course prior to doing this, you'll need to obtain an GreenQloud compatible
|
31
|
+
box file for Vagrant.
|
32
|
+
|
33
|
+
## Quick Start
|
34
|
+
|
35
|
+
After installing the plugin (instructions above), the quickest way to get
|
36
|
+
started is to actually use a generic GreenQloud box and specify all the details
|
37
|
+
manually within a `config.vm.provider` block, in your Vagrantfile:
|
38
|
+
|
39
|
+
```
|
40
|
+
Vagrant.configure("2") do |config|
|
41
|
+
config.vm.box = "greenqloud-generic"
|
42
|
+
config.vm.box_url = "https://vagrant.s.greenqloud.com/greenqloud-generic.box"
|
43
|
+
|
44
|
+
config.vm.provider :gq do |gq, override|
|
45
|
+
gq.access_key_id = "YOUR KEY"
|
46
|
+
gq.secret_access_key = "YOUR SECRET KEY"
|
47
|
+
gq.keypair_name = "KEYPAIR NAME"
|
48
|
+
|
49
|
+
gq.qmi = "qmi-60ce3bb8"
|
50
|
+
|
51
|
+
override.ssh.username = "ubuntu"
|
52
|
+
override.ssh.private_key_path = "PATH TO YOUR PRIVATE KEY"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
And then run `vagrant up --provider=gq`.
|
58
|
+
|
59
|
+
This will start an Ubuntu 14.04 instance in the within your account.
|
60
|
+
And assuming your SSH information was filled in properly within your
|
61
|
+
Vagrantfile, SSH and provisioning will work as well.
|
62
|
+
|
63
|
+
Note that normally a lot of this boilerplate is encoded within the box
|
64
|
+
file, but the box file used for the quick start, the "generic" box, has
|
65
|
+
no preconfigured defaults.
|
66
|
+
|
67
|
+
If you have issues with SSH connecting, make sure that the instances
|
68
|
+
are being launched with a security group that allows SSH access.
|
69
|
+
|
70
|
+
## Box Format
|
71
|
+
|
72
|
+
Every provider in Vagrant must introduce a custom box format. This
|
73
|
+
provider introduces `gq` boxes. You can view an example box in
|
74
|
+
the [example_box/ directory](https://github.com/greenqloud/vagrant-gq/tree/master/example_box).
|
75
|
+
That directory also contains instructions on how to build a box.
|
76
|
+
|
77
|
+
The box format is basically just the required `metadata.json` file
|
78
|
+
along with a `Vagrantfile` that does default settings for the
|
79
|
+
provider-specific configuration for this provider.
|
80
|
+
|
81
|
+
## Configuration
|
82
|
+
|
83
|
+
This provider exposes quite a few provider-specific configuration options:
|
84
|
+
|
85
|
+
* `access_key_id` - The access key for accessing GreenQloud
|
86
|
+
* `qmi` - The QMI id to boot, such as "qmi-12345678"
|
87
|
+
* `availability_zone` - The availability zone within the region to launch
|
88
|
+
the instance. If nil, it will use the default set by Amazon.
|
89
|
+
* `instance_ready_timeout` - The number of seconds to wait for the instance
|
90
|
+
to become "ready" in GreenQloud. Defaults to 120 seconds.
|
91
|
+
* `instance_type` - The type of instance, such as "m1.small". The default
|
92
|
+
value of this if not specified is "m1.small".
|
93
|
+
* `keypair_name` - The name of the keypair to use to bootstrap QMIs
|
94
|
+
which support it.
|
95
|
+
* `secret_access_key` - The secret access key for accessing GreenQloud
|
96
|
+
* `security_groups` - An array of security groups for the instance.
|
97
|
+
* `tags` - A hash of tags to set on the machine.
|
98
|
+
|
99
|
+
These can be set like typical provider-specific configuration:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
Vagrant.configure("2") do |config|
|
103
|
+
# ... other stuff
|
104
|
+
|
105
|
+
config.vm.provider :gq do |gq|
|
106
|
+
gq.access_key_id = "foo"
|
107
|
+
gq.secret_access_key = "bar"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
## Networks
|
113
|
+
|
114
|
+
Networking features in the form of `config.vm.network` are not
|
115
|
+
supported with `vagrant-gq`, currently. If any of these are
|
116
|
+
specified, Vagrant will emit a warning, but will otherwise boot
|
117
|
+
the GreenQloud machine.
|
118
|
+
|
119
|
+
## Synced Folders
|
120
|
+
|
121
|
+
There is minimal support for synced folders. Upon `vagrant up`,
|
122
|
+
`vagrant reload`, and `vagrant provision`, the GreenQloud provider will use
|
123
|
+
`rsync` (if available) to uni-directionally sync the folder to
|
124
|
+
the remote machine over SSH.
|
125
|
+
|
126
|
+
This is good enough for all built-in Vagrant provisioners (shell,
|
127
|
+
chef, and puppet) to work!
|
128
|
+
|
129
|
+
## Other Examples
|
130
|
+
|
131
|
+
### Tags
|
132
|
+
|
133
|
+
To use tags, simply define a hash of key/value for the tags you want to associate to your instance, like:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
Vagrant.configure("2") do |config|
|
137
|
+
# ... other stuff
|
138
|
+
|
139
|
+
config.vm.provider :gq do |gq|
|
140
|
+
gq.tags = {
|
141
|
+
'Name' => 'Some Name',
|
142
|
+
'Some Key' => 'Some Value'
|
143
|
+
}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
### User data
|
149
|
+
|
150
|
+
You can specify user data for the instance being booted.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
Vagrant.configure("2") do |config|
|
154
|
+
# ... other stuff
|
155
|
+
|
156
|
+
config.vm.provider :gq do |gq|
|
157
|
+
# Option 1: a single string
|
158
|
+
gq.user_data = "#!/bin/bash\necho 'got user data' > /tmp/user_data.log\necho"
|
159
|
+
|
160
|
+
# Option 2: use a file
|
161
|
+
gq.user_data = File.read("user_data.txt")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
## Acknowledgements
|
167
|
+
|
168
|
+
Vagrant-gq is a fork of vagrant-aws, copyright Mitchell Hashimoto. See the LICENSE file for further details.
|
169
|
+
|
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"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Vagrant GreenQloud 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 `gq` provider.
|
5
|
+
To turn this into a box:
|
6
|
+
|
7
|
+
```
|
8
|
+
$ tar cvzf gq.box ./metadata.json ./Vagrantfile
|
9
|
+
```
|
10
|
+
|
11
|
+
This box works by using Vagrant's built-in Vagrantfile merging to setup
|
12
|
+
defaults for GreenQloud. These defaults can easily be overwritten by higher-level
|
13
|
+
Vagrantfiles (such as project root Vagrantfiles).
|
Binary file
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "fog"
|
2
|
+
require "log4r"
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module GQ
|
6
|
+
module Action
|
7
|
+
# This action connects to GQ, verifies credentials work, and
|
8
|
+
# puts the GQ connection object into the `:gq_compute` key
|
9
|
+
# in the environment.
|
10
|
+
class ConnectGQ
|
11
|
+
def initialize(app, env)
|
12
|
+
@app = app
|
13
|
+
@logger = Log4r::Logger.new("vagrant_gq::action::connect_gq")
|
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
|
+
}
|
27
|
+
if region_config.use_iam_profile
|
28
|
+
fog_config[:use_iam_profile] = true
|
29
|
+
else
|
30
|
+
fog_config[:aws_access_key_id] = region_config.access_key_id
|
31
|
+
fog_config[:aws_secret_access_key] = region_config.secret_access_key
|
32
|
+
end
|
33
|
+
|
34
|
+
fog_config[:endpoint] = "https://api.greenqloud.com/"
|
35
|
+
|
36
|
+
# fog_config[:endpoint] = region_config.endpoint if region_config.endpoint
|
37
|
+
# fog_config[:version] = region_config.version if region_config.version
|
38
|
+
|
39
|
+
@logger.info("Connecting to GQ...")
|
40
|
+
env[:gq_compute] = Fog::Compute.new(fog_config)
|
41
|
+
|
42
|
+
@app.call(env)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module GQ
|
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,18 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module GQ
|
3
|
+
module Action
|
4
|
+
# This can be used with "Call" built-in to check if the machine
|
5
|
+
# is stopped and branch in the middleware.
|
6
|
+
class IsStopped
|
7
|
+
def initialize(app, env)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
env[:result] = env[:machine].state.id == :stopped
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module GQ
|
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_gq.already_status", :status => "created"))
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module GQ
|
3
|
+
module Action
|
4
|
+
class MessageWillNotDestroy
|
5
|
+
def initialize(app, env)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:ui].info(I18n.t("vagrant_gq.will_not_destroy", name: env[:machine].name))
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "log4r"
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module GQ
|
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_gq::action::read_ssh_info")
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_ssh_info] = read_ssh_info(env[:gq_compute], env[:machine])
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_ssh_info(gq, machine)
|
21
|
+
return nil if machine.id.nil?
|
22
|
+
|
23
|
+
# Find the machine
|
24
|
+
server = gq.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 attribute override
|
33
|
+
ssh_host_attribute = machine.provider_config.
|
34
|
+
get_region_config(machine.provider_config.region).ssh_host_attribute
|
35
|
+
# default host attributes to try. NOTE: Order matters!
|
36
|
+
ssh_attrs = [:public_ip_address, :dns_name, :private_ip_address]
|
37
|
+
ssh_attrs = (Array(ssh_host_attribute) + ssh_attrs).uniq if ssh_host_attribute
|
38
|
+
# try each attribute, get out on first value
|
39
|
+
host_value = nil
|
40
|
+
while !host_value and attr_name = ssh_attrs.shift
|
41
|
+
begin
|
42
|
+
host_value = server.send(attr_name)
|
43
|
+
rescue NoMethodError
|
44
|
+
@logger.info("SSH host attribute not found #{attr_name}")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
return { :host => host_value, :port => 22 }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "log4r"
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module GQ
|
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_gq::action::read_state")
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
env[:machine_state_id] = read_state(env[:gq_compute], env[:machine])
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_state(gq, machine)
|
21
|
+
return :not_created if machine.id.nil?
|
22
|
+
|
23
|
+
# Find the machine
|
24
|
+
server = gq.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
|