Class: Cucumber::Chef::Provider::AWS

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/providers/aws.rb

Constant Summary collapse

INVALID_STATES =
%w(terminated pending).map(&:to_sym)
RUNNING_STATES =
%w(running starting-up).map(&:to_sym)
SHUTDOWN_STATES =
%w(shutdown stopping stopped shutting-down).map(&:to_sym)
VALID_STATES =
RUNNING_STATES+SHUTDOWN_STATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui = ZTK::UI.new) ⇒ AWS

Returns a new instance of AWS.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cucumber/chef/providers/aws.rb', line 37

def initialize(ui=ZTK::UI.new)
  @ui = ui

  @connection = Fog::Compute.new(
    :provider => 'AWS',
    :aws_access_key_id => Cucumber::Chef::Config.aws[:aws_access_key_id],
    :aws_secret_access_key => Cucumber::Chef::Config.aws[:aws_secret_access_key],
    :region => Cucumber::Chef::Config.aws[:region]
  )
  ensure_security_group

  @server = filter_servers(@connection.servers, VALID_STATES)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



28
29
30
# File 'lib/cucumber/chef/providers/aws.rb', line 28

def connection
  @connection
end

#serverObject

Returns the value of attribute server.



28
29
30
# File 'lib/cucumber/chef/providers/aws.rb', line 28

def server
  @server
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/cucumber/chef/providers/aws.rb', line 169

def alive?
  (exists? && RUNNING_STATES.include?(self.state))
end

#createObject

CREATE



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cucumber/chef/providers/aws.rb', line 55

def create
  if (exists? && alive?)
    @ui.stdout.puts("A test lab already exists using the #{Cucumber::Chef::Config.provider.upcase} credentials you have supplied; attempting to reprovision it.")
  else
    server_definition = {
      :image_id => Cucumber::Chef::Config.aws_image_id,
      :groups => Cucumber::Chef::Config.aws[:aws_security_group],
      :flavor_id => Cucumber::Chef::Config.aws[:aws_instance_type],
      :key_name => Cucumber::Chef::Config.aws[:aws_ssh_key_id],
      :availability_zone => Cucumber::Chef::Config.aws[:availability_zone],
      :tags => { "purpose" => "cucumber-chef", "cucumber-chef-mode" => Cucumber::Chef::Config.mode },
      :identity_file => Cucumber::Chef::Config.aws[:identity_file]
    }

    if (@server = @connection.servers.create(server_definition))
      ZTK::Benchmark.bench(:message => "Creating #{Cucumber::Chef::Config.provider.upcase} instance", :mark => "completed in %0.4f seconds.", :ui => @ui) do
        @server.wait_for { ready? }
        tag_server
        ZTK::TCPSocketCheck.new(:host => self.ip, :port => self.port, :wait => 120).wait
      end
    end
  end

  self

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { "Backtrace:\n#{e.backtrace.join("\n")}" }
  raise AWSError, e.message
end

#dead?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/cucumber/chef/providers/aws.rb', line 173

def dead?
  (exists? && SHUTDOWN_STATES.include?(self.state))
end

#destroyObject

DESTROY



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cucumber/chef/providers/aws.rb', line 90

def destroy
  if exists?
    @server.destroy
  else
    raise AWSError, "We could not find a test lab!"
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise AWSError, e.message
end

#downObject

HALT



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cucumber/chef/providers/aws.rb', line 129

def down
  if (exists? && alive?)
    if !@server.stop
      raise AWSError, "Failed to halt the test lab!"
    end
  else
    raise AWSError, "We could not find a running test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise AWSError, e.message
end

#exists?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/cucumber/chef/providers/aws.rb', line 165

def exists?
  !!@server
end

#idObject



179
180
181
# File 'lib/cucumber/chef/providers/aws.rb', line 179

def id
  @server.id
end

#ipObject



191
192
193
# File 'lib/cucumber/chef/providers/aws.rb', line 191

def ip
  @server.public_ip_address
end

#portObject



195
196
197
# File 'lib/cucumber/chef/providers/aws.rb', line 195

def port
  22
end

#reloadObject

RELOAD



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cucumber/chef/providers/aws.rb', line 148

def reload
  if (exists? && alive?)
    if !@server.restart
      raise AWSError, "Failed to reload the test lab!"
    end
  else
    raise AWSError, "We could not find a running test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise AWSError, e.message
end

#stateObject



183
184
185
# File 'lib/cucumber/chef/providers/aws.rb', line 183

def state
  @server.state.to_sym
end

#upObject

UP



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cucumber/chef/providers/aws.rb', line 107

def up
  if (exists? && dead?)
    if @server.start
      @server.wait_for { ready? }
      ZTK::TCPSocketCheck.new(:host => self.ip, :port => self.port, :wait => 120).wait
    else
      raise AWSError, "Failed to boot the test lab!"
    end
  else
    raise AWSError, "We could not find a powered off test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise AWSError, e.message
end

#usernameObject



187
188
189
# File 'lib/cucumber/chef/providers/aws.rb', line 187

def username
  @server.username
end