Class: ChefMetalFog::Providers::AWS

Inherits:
FogDriver
  • Object
show all
Defined in:
lib/chef_metal_fog/providers/aws.rb,
lib/chef_metal_fog/providers/aws/credentials.rb

Defined Under Namespace

Classes: Credentials

Constant Summary

Constants inherited from FogDriver

FogDriver::DEFAULT_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FogDriver

__new__, #allocate_machine, #allocate_machines, canonicalize_url, #compute, #compute_options, #connect_to_machine, #destroy_machine, from_provider, from_url, #image_for, inherited, #initialize, new, #provider, provider_class_for, #ready_machine, register_provider_class, #stop_machine, #transport_for

Constructor Details

This class inherits a constructor from ChefMetalFog::FogDriver

Class Method Details

.aws_account_info_for(aws_profile) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/chef_metal_fog/providers/aws.rb', line 218

def self.(aws_profile)
   ||= {}
  [aws_profile[:aws_access_key_id]] ||= begin
    options = {
      :aws_access_key_id => aws_profile[:aws_access_key_id],
      :aws_secret_access_key => aws_profile[:aws_secret_access_key],
      :aws_session_token => aws_profile[:aws_security_token],
      :host => URI(aws_profile[:iam_endpoint]).host,
      :scheme => URI(aws_profile[:iam_endpoint]).scheme,
      :port => URI(aws_profile[:iam_endpoint]).port,
      :path => URI(aws_profile[:iam_endpoint]).path
    }
    options.delete_if { |key, value| value.nil? }

    iam = Fog::AWS::IAM.new(options)
    arn = begin
            # TODO it would be nice if Fog let you do this normally ...
            iam.send(:request, {
              'Action'    => 'GetUser',
              :parser     => Fog::Parsers::AWS::IAM::GetUser.new
            }).body['User']['Arn']
          rescue Fog::AWS::IAM::Error
            # TODO Someone tell me there is a better way to find out your current
            # user ID than this!  This is what happens when you use an IAM user
            # with default privileges.
            if $!.message =~ /AccessDenied.+(arn:aws:iam::\d+:\S+)/
              arn = $1
            else
              raise
            end
          end
    arn_split = arn.split(':', 6)
    {
      :aws_account_id => arn_split[4],
      :aws_username => arn_split[5],
      :aws_user_arn => arn
    }
  end
end

.compute_options_for(provider, id, config) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/chef_metal_fog/providers/aws.rb', line 275

def self.compute_options_for(provider, id, config)
  new_compute_options = {}
  new_compute_options[:provider] = provider
  new_config = { :driver_options => { :compute_options => new_compute_options }}
  new_defaults = {
    :driver_options => { :compute_options => {} },
    :machine_options => { :bootstrap_options => {} }
  }
  result = Cheffish::MergedConfig.new(new_config, config, new_defaults)

  if id && id != ''
    # AWS canonical URLs are of the form fog:AWS:
    if id =~ /^(\d{12})(:(.+))?$/
      if $2
        id = $1
        new_compute_options[:region] = $3
      else
        Chef::Log.warn("Old-style AWS URL #{id} from an early beta of chef-metal (before 0.11-final) found. If you have servers in multiple regions on this account, you may see odd behavior like servers being recreated. To fix, edit any nodes with attribute metal.location.driver_url to include the region like so: fog:AWS:#{id}:<region> (e.g. us-east-1)")
      end
    else
      # Assume it is a profile name, and set that.
      aws_profile, region = id.split(':', 2)
      new_config[:driver_options][:aws_profile] = aws_profile
      new_compute_options[:region] = region
      id = nil
    end
  end

  aws_profile = get_aws_profile(result[:driver_options], id)
  new_compute_options[:aws_access_key_id] = aws_profile[:aws_access_key_id]
  new_compute_options[:aws_secret_access_key] = aws_profile[:aws_secret_access_key]
  new_compute_options[:aws_session_token] = aws_profile[:aws_security_token]
  new_defaults[:driver_options][:compute_options][:region] = aws_profile[:region]
  new_defaults[:driver_options][:compute_options][:endpoint] = aws_profile[:ec2_endpoint]

   = (result[:driver_options][:compute_options])
  new_config[:driver_options][:aws_account_info] = 
  id = "#{account_info[:aws_account_id]}:#{result[:driver_options][:compute_options][:region]}"

  [result, id]
end

.find_aws_profile_for_account_id(aws_credentials, aws_account_id) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/chef_metal_fog/providers/aws.rb', line 193

def self.(aws_credentials, )
  aws_profile = nil
  aws_credentials.each do |profile_name, profile|
    begin
       = (profile)
    rescue
      Chef::Log.warn("Could not connect to AWS profile #{aws_credentials[:name]}: #{$!}")
      Chef::Log.debug($!.backtrace.join("\n"))
      next
    end
    if [:aws_account_id] == 
      aws_profile = profile
      aws_profile[:name] = profile_name
      aws_profile = aws_profile.merge()
      break
    end
  end
  if aws_profile
    Chef::Log.info("Discovered AWS profile #{aws_profile[:name]} pointing at account #{aws_account_id}.  Using ...")
  else
    raise "No AWS profile leads to account ##{aws_account_id}.  Do you need to add profiles to ~/.aws/config?"
  end
  aws_profile
end

.get_aws_credentials(driver_options) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/chef_metal_fog/providers/aws.rb', line 258

def self.get_aws_credentials(driver_options)
  # Grab the list of possible credentials
  if driver_options[:aws_credentials]
    aws_credentials = driver_options[:aws_credentials]
  else
    aws_credentials = Credentials.new
    if driver_options[:aws_config_file]
      aws_credentials.load_ini(driver_options.delete(:aws_config_file))
    elsif driver_options[:aws_csv_file]
      aws_credentials.load_csv(driver_options.delete(:aws_csv_file))
    else
      aws_credentials.load_default
    end
  end
  aws_credentials
end

.get_aws_profile(driver_options, aws_account_id) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chef_metal_fog/providers/aws.rb', line 123

def self.get_aws_profile(driver_options, )
  aws_credentials = get_aws_credentials(driver_options)
  compute_options = driver_options[:compute_options] || {}

  # Order of operations:
  # compute_options[:aws_access_key_id] / compute_options[:aws_secret_access_key] / compute_options[:aws_security_token] / compute_options[:region]
  # compute_options[:aws_profile]
  # ENV['AWS_ACCESS_KEY_ID'] / ENV['AWS_SECRET_ACCESS_KEY'] / ENV['AWS_SECURITY_TOKEN'] / ENV['AWS_REGION']
  # ENV['AWS_PROFILE']
  # ENV['DEFAULT_PROFILE']
  # 'default'
  aws_profile = if compute_options[:aws_access_key_id]
      Chef::Log.debug("Using AWS driver access key options")
      {
        :aws_access_key_id => compute_options[:aws_access_key_id],
        :aws_secret_access_key => compute_options[:aws_secret_access_key],
        :aws_security_token => compute_options[:aws_session_token],
        :region => compute_options[:region]
      }
    elsif driver_options[:aws_profile]
      Chef::Log.debug("Using AWS profile #{driver_options[:aws_profile]}")
      aws_credentials[driver_options[:aws_profile]]
    elsif ENV['AWS_ACCESS_KEY_ID'] || ENV['AWS_ACCESS_KEY']
      Chef::Log.debug("Using AWS environment variable access keys")
      {
        :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'] || ENV['AWS_ACCESS_KEY'],
        :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_KEY'],
        :aws_security_token => ENV['AWS_SECURITY_TOKEN'],
        :region => ENV['AWS_REGION']
      }
    elsif ENV['AWS_PROFILE']
      Chef::Log.debug("Using AWS profile #{ENV['AWS_PROFILE']} from AWS_PROFILE environment variable")
      aws_credentials[ENV['AWS_PROFILE']]
    else
      Chef::Log.debug("Using AWS default profile")
      aws_credentials.default
    end
  # Endpoint configuration
  if compute_options[:ec2_endpoint]
    aws_profile[:ec2_endpoint] = compute_options[:ec2_endpoint]
  elsif ENV['EC2_URL']
    aws_profile[:ec2_endpoint] = ENV['EC2_URL']
  end
  if compute_options[:iam_endpoint]
    aws_profile[:iam_endpoint] = compute_options[:iam_endpoint]
  elsif ENV['AWS_IAM_URL']
    aws_profile[:iam_endpoint] = ENV['AWS_IAM_URL']
  else
    aws_profile[:iam_endpoint] = "https://iam.amazonaws.com/"
  end

  # Merge in account info for profile
  if aws_profile
    aws_profile = aws_profile.merge((aws_profile))
  end

  # If no profile is found (or the profile is not the right account), search
  # for a profile that matches the given account ID
  if  && (!aws_profile || aws_profile[:aws_account_id] != )
    aws_profile = (aws_credentials, )
  end

  if !aws_profile
    raise "No AWS profile specified!  Are you missing something in the Chef config or ~/.aws/config?"
  end

  aws_profile.delete_if { |key, value| value.nil? }
  aws_profile
end

Instance Method Details

#allocate_image(action_handler, image_spec, image_options, machine_spec) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef_metal_fog/providers/aws.rb', line 24

def allocate_image(action_handler, image_spec, image_options, machine_spec)
  if image_spec.location
    image = compute.images.get(image_spec.location['image_id'])
    if image
      raise "The image already exists, why are you asking me to create it?  I can't do that, Dave."
    end
  end
  action_handler.perform_action "Create image #{image_spec.name} from machine #{machine_spec.name} with options #{image_options.inspect}" do
    opt = image_options.dup
    response = compute.create_image(machine_spec.location['server_id'],
                                 image_spec.name,
                                 opt.delete(:description) || "The image formerly and currently named '#{image_spec.name}'",
                                 opt.delete(:no_reboot) || false,
                                 opt)
    image_spec.location = {
      'driver_url' => driver_url,
      'driver_version' => ChefMetalFog::VERSION,
      'image_id' => response.body['imageId'],
      'creator' => creator,
      'allocated_at' => Time.now.to_i
    }

    image_spec.machine_options ||= {}
    image_spec.machine_options.merge!({
      :bootstrap_options => {
          :image_id => image_spec.location['image_id']
      }
    })

  end
end

#bootstrap_options_for(action_handler, machine_spec, machine_options) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/chef_metal_fog/providers/aws.rb', line 82

def bootstrap_options_for(action_handler, machine_spec, machine_options)
  bootstrap_options = symbolize_keys(machine_options[:bootstrap_options] || {})

  if !bootstrap_options[:key_name]
    bootstrap_options[:key_name] = overwrite_default_key_willy_nilly(action_handler)
  end
  bootstrap_options.delete(:tags) # we handle these separately for performance reasons
  bootstrap_options
end

#convergence_strategy_for(machine_spec, machine_options) ⇒ Object



118
119
120
121
# File 'lib/chef_metal_fog/providers/aws.rb', line 118

def convergence_strategy_for(machine_spec, machine_options)
  machine_options[:convergence_options][:ohai_hints] = { 'ec2' => ''}
  super(machine_spec, machine_options)
end

#create_many_servers(num_servers, bootstrap_options, parallelizer) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/chef_metal_fog/providers/aws.rb', line 317

def create_many_servers(num_servers, bootstrap_options, parallelizer)
  # Create all the servers in one request if we have a version of fog that can do that
  if compute.servers.respond_to?(:create_many)
    servers = compute.servers.create_many(num_servers, num_servers, bootstrap_options)
    if block_given?
      parallelizer.parallelize(servers) do |server|
        yield server
      end.to_a
    end
    servers
  else
    super
  end
end

#create_servers(action_handler, specs_and_options, parallelizer) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef_metal_fog/providers/aws.rb', line 92

def create_servers(action_handler, specs_and_options, parallelizer)
  super(action_handler, specs_and_options, parallelizer) do |machine_spec, server|
    yield machine_spec, server if block_given?

    machine_options = specs_and_options[machine_spec]
    bootstrap_options = symbolize_keys(machine_options[:bootstrap_options] || {})
    tags = default_tags(machine_spec, bootstrap_options[:tags] || {})

    # Right now, not doing that in case manual tagging is going on
    server_tags = server.tags || {}
    extra_tags = tags.keys.select { |tag_name| !server_tags.has_key?(tag_name) }.to_a
    different_tags = server_tags.select { |tag_name, tag_value| tags.has_key?(tag_name) && tags[tag_name] != tag_value }.to_a
    if extra_tags.size > 0 || different_tags.size > 0
      tags_description = [ "Update tags for #{machine_spec.name} on #{driver_url}" ]
      tags_description += extra_tags.map { |tag| "  Add #{tag} = #{tags[tag].inspect}" }
      tags_description += different_tags.map { |tag_name, tag_value| "  Update #{tag_name} from #{tag_value.inspect} to #{tags[tag_name].inspect}"}
      action_handler.perform_action tags_description do
        # TODO should we narrow this down to just extra/different tags or
        # is it OK to just pass 'em all?  Certainly easier to do the
        # latter, and I can't think of a consequence for doing so offhand.
        compute.create_tags(server.identity, tags)
      end
    end
  end
end

#creatorObject



16
17
18
# File 'lib/chef_metal_fog/providers/aws.rb', line 16

def creator
  driver_options[:aws_account_info][:aws_username]
end

#default_ssh_usernameObject



20
21
22
# File 'lib/chef_metal_fog/providers/aws.rb', line 20

def default_ssh_username
  'ubuntu'
end

#destroy_image(action_handler, image_spec, image_options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef_metal_fog/providers/aws.rb', line 69

def destroy_image(action_handler, image_spec, image_options)
  if !image_spec.location
    return
  end
  image = compute.images.get(image_spec.location['image_id'])
  if !image
    return
  end
  delete_snapshots = image_options[:delete_snapshots]
  delete_snapshots = true if delete_snapshots.nil?
  image.deregister(delete_snapshots)
end

#ready_image(action_handler, image_spec, image_options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef_metal_fog/providers/aws.rb', line 56

def ready_image(action_handler, image_spec, image_options)
  if !image_spec.location
    raise "Cannot ready an image that does not exist"
  end
  image = compute.images.get(image_spec.location['image_id'])
  if !image.ready?
    action_handler.report_progress "Waiting for image to be ready ..."
    # TODO timeout
    image.wait_for { ready? }
    action_handler.report_progress "Image is ready!"
  end
end

#servers_for(machine_specs) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/chef_metal_fog/providers/aws.rb', line 332

def servers_for(machine_specs)
  # Grab all the servers in one request
  instance_ids = machine_specs.map { |machine_spec| (machine_spec.location || {})['server_id'] }.select { |id| !id.nil? }
  servers = compute.servers.all('instance-id' => instance_ids)
  result = {}
  machine_specs.each do |machine_spec|
    if machine_spec.location
      result[machine_spec] = servers.select { |s| s.id == machine_spec.location['server_id'] }.first
    else
      result[machine_spec] = nil
    end
  end
  result
end