Class: Chef::Provisioning::AWSDriver::Driver

Inherits:
Driver
  • Object
show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provisioning/aws_driver/driver.rb

Overview

Provisions machines using the AWS SDK

Constant Summary collapse

PORT_DEFAULTS =
{
  :http => 80,
  :https => 443,
}
PROTOCOL_DEFAULTS =
{
  25 => :tcp,
  80 => :http,
  443 => :https,
  465 => :ssl,
  587 => :tcp,
}
@@chef_default_lock =

For creating things like AWS keypairs exclusively

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver_url, config) ⇒ Driver

Returns a new instance of Driver.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 47

def initialize(driver_url, config)
  super

  _, profile_name, region = driver_url.split(':')
  profile_name = nil if profile_name && profile_name.empty?
  region = nil if region && region.empty?

  credentials = profile_name ? aws_credentials[profile_name] : aws_credentials.default
  @aws_config = AWS.config(
    access_key_id:     credentials[:aws_access_key_id],
    secret_access_key: credentials[:aws_secret_access_key],
    region: region || credentials[:region],
    proxy_uri: credentials[:proxy_uri] || nil,
    session_token: credentials[:aws_session_token] || nil,
    logger: Chef::Log.logger
  )
end

Instance Attribute Details

#aws_configObject (readonly)

Returns the value of attribute aws_config.



37
38
39
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 37

def aws_config
  @aws_config
end

Class Method Details

.canonicalize_url(driver_url, config) ⇒ Object



65
66
67
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 65

def self.canonicalize_url(driver_url, config)
  [ driver_url, config ]
end

.from_url(driver_url, config) ⇒ Object

URL scheme: aws:profilename:region TODO: migration path from fog:AWS - parse that URL canonical URL calls realpath on <path>



43
44
45
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 43

def self.from_url(driver_url, config)
  Driver.new(driver_url, config)
end

Instance Method Details

#account_idObject



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 562

def 
  begin
    # We've got an AWS account root credential or an IAM admin with access rights
    current_user = iam.client.get_user
    arn = current_user[:user][:arn]
  rescue AWS::IAM::Errors::AccessDenied => e
    # If we don't have access, the error message still tells us our account ID and user ...
    # https://forums.aws.amazon.com/thread.jspa?messageID=394344
    if e.to_s !~ /\b(arn:aws:iam::[0-9]{12}:\S*)/
      raise "IAM error response for GetUser did not include user ARN.  Can't retrieve account ID."
    end
    arn = $1
  end
  parse_arn(arn)[:account_id]
end

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

Image methods



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 362

def allocate_image(action_handler, image_spec, image_options, machine_spec, machine_options)
  actual_image = image_for(image_spec)
  aws_tags = image_options.delete(:aws_tags) || {}
  if actual_image.nil? || !actual_image.exists? || actual_image.state == :failed
    action_handler.perform_action "Create image #{image_spec.name} from machine #{machine_spec.name} with options #{image_options.inspect}" do
      image_options[:name] ||= image_spec.name
      image_options[:instance_id] ||= machine_spec.reference['instance_id']
      image_options[:description] ||= "Image #{image_spec.name} created from machine #{machine_spec.name}"
      Chef::Log.debug "AWS Image options: #{image_options.inspect}"
      actual_image = ec2.images.create(image_options.to_hash)
      image_spec.reference = {
        'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
        'image_id' => actual_image.id,
        'allocated_at' => Time.now.to_i
      }
      image_spec.driver_url = driver_url
    end
  end
  aws_tags['From-Instance'] = image_options[:instance_id] if image_options[:instance_id]
  converge_tags(actual_image, aws_tags, action_handler)
end

#allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) ⇒ Object

Load balancer methods



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 70

def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs)
  lb_options = AWSResource.lookup_options(lb_options || {}, managed_entry_store: lb_spec.managed_entry_store, driver: self)

  old_elb = nil
  actual_elb = load_balancer_for(lb_spec)
  if !actual_elb || !actual_elb.exists?
    lb_options[:listeners] ||= get_listeners(:http)
    if !lb_options[:subnets] && !lb_options[:availability_zones] && machine_specs
      lb_options[:subnets] = machine_specs.map { |s| ec2.instances[s.reference['instance_id']].subnet }.uniq
    end

    perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) }
    Chef::Log.debug "AWS Load Balancer options: #{lb_options.inspect}"

    updates = [ "create load balancer #{lb_spec.name} in #{aws_config.region}" ]
    updates << "  enable availability zones #{lb_options[:availability_zones]}" if lb_options[:availability_zones]
    updates << "  attach subnets #{lb_options[:subnets].join(', ')}" if lb_options[:subnets]
    updates << "  with listeners #{lb_options[:listeners]}" if lb_options[:listeners]
    updates << "  with security groups #{lb_options[:security_groups]}" if lb_options[:security_groups]
    updates << "  with tags #{lb_options[:aws_tags]}" if lb_options[:aws_tags]


    lb_aws_tags = lb_options[:aws_tags]
    lb_options.delete(:aws_tags)
    action_handler.perform_action updates do
      actual_elb = elb.load_balancers.create(lb_spec.name, lb_options)
      lb_options[:aws_tags] = lb_aws_tags

      lb_spec.reference = {
        'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
        'allocated_at' => Time.now.utc.to_s,
      }
      lb_spec.driver_url = driver_url
    end
  else
    # Header gets printed the first time we make an update
    perform_action = proc do |desc, &block|
      perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) }
      action_handler.perform_action [ "Update load balancer #{lb_spec.name} in #{aws_config.region}", desc ].flatten, &block
    end

    # TODO: refactor this whole giant method into many smaller method calls
    # TODO if we update scheme, we don't need to run any of the other updates.
    # Also, if things aren't specified (such as machines / listeners), we
    # need to grab them from the actual load balancer so we don't lose them.
    # i.e. load_balancer 'blah' do
    #   lb_options: { scheme: 'other_scheme' }
    # end
    # TODO we will leak the actual_elb if we fail to finish creating it
    # Update scheme - scheme is immutable once set, so if it is changing we need to delete the old
    # ELB and create a new one
    if lb_options[:scheme] && lb_options[:scheme].downcase != actual_elb.scheme
      desc = ["  updating scheme to #{lb_options[:scheme]}"]
      desc << "  WARN: scheme is immutable, so deleting and re-creating the ELB"
      perform_action.call(desc) do
        old_elb = actual_elb
        actual_elb = elb.load_balancers.create(lb_spec.name, lb_options)
      end
    end

    # Update security groups
    if lb_options[:security_groups]
      current = actual_elb.security_group_ids
      desired = lb_options[:security_groups]
      if current != desired
        perform_action.call("  updating security groups to #{desired.to_a}") do
          elb.client.apply_security_groups_to_load_balancer(
            load_balancer_name: actual_elb.name,
            security_groups: desired.to_a
          )
        end
      end
    end

    if lb_options[:availability_zones] || lb_options[:subnets]
      # A subnet always belongs to an availability zone.  When specifying a ELB spec, you can either
      # specify subnets OR AZs but not both.  You cannot specify multiple subnets in the same AZ.
      # You must specify at least 1 subnet or AZ.  On an update you cannot remove all subnets
      # or AZs - it must belong to one.
      if lb_options[:availability_zones] && lb_options[:subnets]
        # We do this check here because there is no atomic call we can make to specify both
        # subnets and AZs at the same time
        raise "You cannot specify both `availability_zones` and `subnets`"
      end

      # Users can switch from availability zones to subnets or vice versa.  To ensure we do not
      # unassign all (which causes an AWS error) we first add all available ones, then remove
      # an unecessary ones
      actual_zones_subnets = {}
      actual_elb.subnets.each do |subnet|
        actual_zones_subnets[subnet.id] = subnet.availability_zone.name
      end

      # Only 1 of subnet or AZ will be populated b/c of our check earlier
      desired_subnets_zones = {}
      if lb_options[:availability_zones]
        lb_options[:availability_zones].each do |zone|
          # If the user specifies availability zone, we find the default subnet for that
          # AZ because this duplicates the create logic
          zone = zone.downcase
          filters = [
            {:name => 'availabilityZone', :values => [zone]},
            {:name => 'defaultForAz', :values => ['true']}
          ]
          default_subnet = ec2.client.describe_subnets(:filters => filters)[:subnet_set]
          if default_subnet.size != 1
            raise "Could not find default subnet in availability zone #{zone}"
          end
          default_subnet = default_subnet[0]
          desired_subnets_zones[default_subnet[:subnet_id]] = zone
        end
      end
      unless lb_options[:subnets].nil? || lb_options[:subnets].empty?
        subnet_query = ec2.client.describe_subnets(:subnet_ids => lb_options[:subnets])[:subnet_set]
        # AWS raises an error on an unknown subnet, but not an unknown AZ
        subnet_query.each do |subnet|
          zone = subnet[:availability_zone].downcase
          desired_subnets_zones[subnet[:subnet_id]] = zone
        end
      end

      # We only bother attaching subnets, because doing this automatically attaches the AZ
      attach_subnets = desired_subnets_zones.keys - actual_zones_subnets.keys
      unless attach_subnets.empty?
        action = "  attach subnets #{attach_subnets.join(', ')}"
        enable_zones = (desired_subnets_zones.map {|s,z| z if attach_subnets.include?(s)}).compact
        action += " (availability zones #{enable_zones.join(', ')})"
        perform_action.call(action) do
          begin
            elb.client.attach_load_balancer_to_subnets(
              load_balancer_name: actual_elb.name,
              subnets: attach_subnets
            )
          rescue AWS::ELB::Errors::InvalidConfigurationRequest
            raise "You cannot currently move from 1 subnet to another in the same availability zone. " +
                "Amazon does not have an atomic operation which allows this.  You must create a new " +
                "ELB with the correct subnets and move instances into it.  Tried to attach subets " +
                "#{attach_subnets.join(', ')} (availability zones #{enable_zones.join(', ')}) to " +
                "existing ELB named #{actual_elb.name}"
          end
        end
      end

      detach_subnets = actual_zones_subnets.keys - desired_subnets_zones.keys
      unless detach_subnets.empty?
        action = "  detach subnets #{detach_subnets.join(', ')}"
        disable_zones = (actual_zones_subnets.map {|s,z| z if detach_subnets.include?(s)}).compact
        action += " (availability zones #{disable_zones.join(', ')})"
        perform_action.call(action) do
          elb.client.detach_load_balancer_from_subnets(
            load_balancer_name: actual_elb.name,
            subnets: detach_subnets
          )
        end
      end
    end

    # Update listeners - THIS IS NOT ATOMIC
    if lb_options[:listeners]
      add_listeners = {}
      lb_options[:listeners].each { |l| add_listeners[l[:port]] = l }
      actual_elb.listeners.each do |listener|
        desired_listener = add_listeners.delete(listener.port)
        if desired_listener

          # listener.(port|protocol|instance_port|instance_protocol) are immutable for the life
          # of the listener - must create a new one and delete old one
          immutable_updates = []
          if listener.protocol != desired_listener[:protocol].to_sym.downcase
            immutable_updates << "    update protocol from #{listener.protocol.inspect} to #{desired_listener[:protocol].inspect}"
          end
          if listener.instance_port != desired_listener[:instance_port]
            immutable_updates << "    update instance port from #{listener.instance_port.inspect} to #{desired_listener[:instance_port].inspect}"
          end
          if listener.instance_protocol != desired_listener[:instance_protocol].to_sym.downcase
            immutable_updates << "    update instance protocol from #{listener.instance_protocol.inspect} to #{desired_listener[:instance_protocol].inspect}"
          end
          if !immutable_updates.empty?
            perform_action.call(immutable_updates) do
              listener.delete
              actual_elb.listeners.create(desired_listener)
            end
          elsif listener.server_certificate != desired_listener[:server_certificate]
            # Server certificate is mutable - if no immutable changes required a full recreate, update cert
            perform_action.call("    update server certificate from #{listener.server_certificate} to #{desired_listener[:server_certificate]}") do
              listener.server_certificate = desired_listener[:server_certificate]
            end
          end

        else
          perform_action.call("  remove listener #{listener.port}") do
            listener.delete
          end
        end
      end
      add_listeners.values.each do |listener|
        updates = [ "  add listener #{listener[:port]}" ]
        updates << "    set protocol to #{listener[:protocol].inspect}"
        updates << "    set instance port to #{listener[:instance_port].inspect}"
        updates << "    set instance protocol to #{listener[:instance_protocol].inspect}"
        updates << "    set server certificate to #{listener[:server_certificate]}" if listener[:server_certificate]
        perform_action.call(updates) do
          actual_elb.listeners.create(listener)
        end
      end
    end
  end

  # GRRRR curse you AWS and your crappy tagging support for ELBs
  read_tags_block = lambda {|aws_object|
    resp = elb.client.describe_tags load_balancer_names: [aws_object.name]
    tags = {}
    resp.data[:tag_descriptions] && resp.data[:tag_descriptions].each do |td|
      td[:tags].each do |t|
        tags[t[:key]] = t[:value]
      end
    end
    tags
  }

  set_tags_block = lambda {|aws_object, desired_tags|
    aws_form_tags = []
    desired_tags.each do |k, v|
      aws_form_tags << {key: k, value: v}
    end
    elb.client.add_tags load_balancer_names: [aws_object.name], tags: aws_form_tags
  }

  delete_tags_block=lambda {|aws_object, tags_to_delete|
    aws_form_tags = []
    tags_to_delete.each do |k, v|
      aws_form_tags << {key: k}
    end
    elb.client.remove_tags load_balancer_names: [aws_object.name], tags: aws_form_tags
  }
  converge_tags(actual_elb, lb_options[:aws_tags], action_handler, read_tags_block, set_tags_block, delete_tags_block)

  # Update instance list, but only if there are machines specified
  if machine_specs
    actual_instance_ids = actual_elb.instances.map { |i| i.instance_id }

    instances_to_add = machine_specs.select { |s| !actual_instance_ids.include?(s.reference['instance_id']) }
    instance_ids_to_remove = actual_instance_ids - machine_specs.map { |s| s.reference['instance_id'] }

    if instances_to_add.size > 0
      perform_action.call("  add machines #{instances_to_add.map { |s| s.name }.join(', ')}") do
        instance_ids_to_add = instances_to_add.map { |s| s.reference['instance_id'] }
        Chef::Log.debug("Adding instances #{instance_ids_to_add.join(', ')} to load balancer #{actual_elb.name} in region #{aws_config.region}")
        actual_elb.instances.add(instance_ids_to_add)
      end
    end

    if instance_ids_to_remove.size > 0
      perform_action.call("  remove instances #{instance_ids_to_remove}") do
        actual_elb.instances.remove(instance_ids_to_remove)
      end
    end
  end

  # We have successfully switched all our instances to the (possibly) new LB
  # so it is safe to delete the old one.
  unless old_elb.nil?
    old_elb.delete
  end
ensure
  # Something went wrong before we could moved instances from the old ELB to the new one
  # Don't delete the old ELB, but warn users there could now be 2 ELBs with the same name
  unless old_elb.nil?
    Chef::Log.warn("It is possible there are now 2 ELB instances - #{old_elb.name} and #{actual_elb.name}. " +
    "Determine which is correct and manually clean up the other.")
  end
end

#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object

Machine methods



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 430

def allocate_machine(action_handler, machine_spec, machine_options)
  actual_instance = instance_for(machine_spec)
  bootstrap_options = bootstrap_options_for(action_handler, machine_spec, machine_options)

  if actual_instance == nil || !actual_instance.exists? || actual_instance.status == :terminated

    action_handler.perform_action "Create #{machine_spec.name} with AMI #{bootstrap_options[:image_id]} in #{aws_config.region}" do
      Chef::Log.debug "Creating instance with bootstrap options #{bootstrap_options}"

      actual_instance = ec2.instances.create(bootstrap_options.to_hash)

      # Make sure the instance is ready to be tagged
      Retryable.retryable(:tries => 12, :sleep => 5, :on => [AWS::EC2::Errors::InvalidInstanceID::NotFound, TimeoutError]) do
        raise TimeoutError unless actual_instance.status == :pending || actual_instance.status == :running
      end
      # TODO add other tags identifying user / node url (same as fog)
      actual_instance.tags['Name'] = machine_spec.name
      actual_instance.source_dest_check = machine_options[:source_dest_check] if machine_options.has_key?(:source_dest_check)
      machine_spec.reference = {
          'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
          'allocated_at' => Time.now.utc.to_s,
          'host_node' => action_handler.host_node,
          'image_id' => bootstrap_options[:image_id],
          'instance_id' => actual_instance.id
      }
      machine_spec.driver_url = driver_url
      machine_spec.reference['key_name'] = bootstrap_options[:key_name] if bootstrap_options[:key_name]
      %w(is_windows ssh_username sudo use_private_ip_for_ssh ssh_gateway).each do |key|
        machine_spec.reference[key] = machine_options[key.to_sym] if machine_options[key.to_sym]
      end
    end
  end
  # TODO because we don't want to add `provider_tags` as a base attribute,
  # we have to update the tags here in driver.rb instead of the providers
  converge_tags(actual_instance, machine_options[:aws_tags], action_handler)
end

#allocate_machines(action_handler, specs_and_options, parallelizer) ⇒ Object



467
468
469
470
471
472
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 467

def allocate_machines(action_handler, specs_and_options, parallelizer)
  create_servers(action_handler, specs_and_options, parallelizer) do |machine_spec, server|
    yield machine_spec
  end
  specs_and_options.keys
end

#auto_scalingObject



543
544
545
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 543

def auto_scaling
  @auto_scaling ||= AWS::AutoScaling.new(config: aws_config)
end

#aws_credentialsObject



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 663

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

#bootstrap_options_for(action_handler, machine_spec, machine_options) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 595

def bootstrap_options_for(action_handler, machine_spec, machine_options)
  bootstrap_options = (machine_options[:bootstrap_options] || {}).to_h.dup
  bootstrap_options[:instance_type] ||= default_instance_type
  image_id = bootstrap_options[:image_id] || machine_options[:image_id] || default_ami_for_region(aws_config.region)
  bootstrap_options[:image_id] = image_id
  if !bootstrap_options[:key_name]
    Chef::Log.debug('No key specified, generating a default one...')
    bootstrap_options[:key_name] = default_aws_keypair(action_handler, machine_spec)
  end

  if machine_options[:is_windows]
    Chef::Log.debug "Setting WinRM userdata..."
    bootstrap_options[:user_data] = user_data
  else
    Chef::Log.debug "Non-windows, not setting userdata"
  end

  bootstrap_options = AWSResource.lookup_options(bootstrap_options, managed_entry_store: machine_spec.managed_entry_store, driver: self)
  Chef::Log.debug "AWS Bootstrap options: #{bootstrap_options.inspect}"
  bootstrap_options
end

#build_arn(partition: 'aws', service: nil, region: aws_config.region, account_id: self.account_id, resource: nil) ⇒ Object



547
548
549
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 547

def build_arn(partition: 'aws', service: nil, region: aws_config.region, account_id: self., resource: nil)
  "arn:#{partition}:#{service}:#{region}:#{}:#{resource}"
end

#connect_to_machine(name, chef_server = nil) ⇒ Object



495
496
497
498
499
500
501
502
503
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 495

def connect_to_machine(name, chef_server = nil)
  if name.is_a?(MachineSpec)
    machine_spec = name
  else
    machine_spec = Chef::Provisioning::ChefMachineSpec.get(name, chef_server)
  end

  machine_for(machine_spec, machine_spec.reference)
end

#converge_tags(aws_object, desired_tags, action_handler, read_tags_block = lambda {|aws_object| aws_object.tags.to_h}, set_tags_block = lambda {|aws_object, desired_tags| aws_object.tags.set(desired_tags) }, delete_tags_block = lambda {|aws_object, tags_to_delete| aws_object.tags.delete(*tags_to_delete) }) ⇒ Object

TODO This is currently duplicated from AWS Provider Set the tags on the aws object to desired_tags, while ignoring any ‘Name` tag If no tags need to be modified, will not perform a write call on AWS



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 1017

def converge_tags(
  aws_object,
  desired_tags,
  action_handler,
  read_tags_block=lambda {|aws_object| aws_object.tags.to_h},
  set_tags_block=lambda {|aws_object, desired_tags| aws_object.tags.set(desired_tags) },
  delete_tags_block=lambda {|aws_object, tags_to_delete| aws_object.tags.delete(*tags_to_delete) }
)
  # If aws_tags were not provided we exit
  if desired_tags.nil?
    Chef::Log.debug "aws_tags not provided, nothing to converge"
    return
  end
  current_tags = read_tags_block.call(aws_object)
  # AWS always returns tags as strings, and we don't want to overwrite a
  # tag-as-string with the same tag-as-symbol
  desired_tags = Hash[desired_tags.map {|k, v| [k.to_s, v.to_s] }]
  tags_to_update = desired_tags.reject {|k,v| current_tags[k] == v}
  tags_to_delete = current_tags.keys - desired_tags.keys
  # We don't want to delete `Name`, just all other tags
  tags_to_delete.delete('Name')

  unless tags_to_update.empty?
    action_handler.perform_action "applying tags #{tags_to_update}" do
      set_tags_block.call(aws_object, tags_to_update)
    end
  end
  unless tags_to_delete.empty?
    action_handler.perform_action "deleting tags #{tags_to_delete.inspect}" do
      delete_tags_block.call(aws_object, tags_to_delete)
    end
  end
end

#convergence_strategy_for(machine_spec, machine_options) ⇒ Object



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 821

def convergence_strategy_for(machine_spec, machine_options)
  # Tell Ohai that this is an EC2 instance so that it runs the EC2 plugin
  convergence_options = Cheffish::MergedConfig.new(
    machine_options[:convergence_options] || {},
    ohai_hints: { 'ec2' => '' })

  # Defaults
  if !machine_spec.reference
    return Chef::Provisioning::ConvergenceStrategy::NoConverge.new(convergence_options, config)
  end

  if machine_spec.reference['is_windows']
    Chef::Provisioning::ConvergenceStrategy::InstallMsi.new(convergence_options, config)
  elsif machine_options[:cached_installer] == true
    Chef::Provisioning::ConvergenceStrategy::InstallCached.new(convergence_options, config)
  else
    Chef::Provisioning::ConvergenceStrategy::InstallSh.new(convergence_options, config)
  end
end

#create_many_instances(num_servers, bootstrap_options, parallelizer) ⇒ Object



1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 1004

def create_many_instances(num_servers, bootstrap_options, parallelizer)
  parallelizer.parallelize(1.upto(num_servers)) do |i|
    clean_bootstrap_options = Marshal.load(Marshal.dump(bootstrap_options))
    instance = ec2.instances.create(clean_bootstrap_options.to_hash)

    yield instance if block_given?
    instance
  end.to_a
end

#create_servers(action_handler, specs_and_options, parallelizer, &block) ⇒ Object



937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 937

def create_servers(action_handler, specs_and_options, parallelizer, &block)
  specs_and_servers = instances_for(specs_and_options.keys)

  by_bootstrap_options = {}
  specs_and_options.each do |machine_spec, machine_options|
    actual_instance = specs_and_servers[machine_spec]
    if actual_instance
      if actual_instance.status == :terminated
        Chef::Log.warn "Machine #{machine_spec.name} (#{actual_instance.id}) is terminated.  Recreating ..."
      else
        converge_tags(actual_instance, machine_options[:aws_tags], action_handler)
        yield machine_spec, actual_instance if block_given?
        next
      end
    elsif machine_spec.reference
      Chef::Log.warn "Machine #{machine_spec.name} (#{machine_spec.reference['instance_id']} on #{driver_url}) no longer exists.  Recreating ..."
    end

    bootstrap_options = bootstrap_options_for(action_handler, machine_spec, machine_options)
    by_bootstrap_options[bootstrap_options] ||= []
    by_bootstrap_options[bootstrap_options] << machine_spec
  end

  # Create the servers in parallel
  parallelizer.parallelize(by_bootstrap_options) do |bootstrap_options, machine_specs|
    machine_description = if machine_specs.size == 1
      "machine #{machine_specs.first.name}"
    else
      "machines #{machine_specs.map { |s| s.name }.join(", ")}"
    end
    description = [ "creating #{machine_description} on #{driver_url}" ]
    bootstrap_options.each_pair { |key,value| description << "  #{key}: #{value.inspect}" }
    action_handler.report_progress description
    if action_handler.should_perform_actions
      # Actually create the servers
      create_many_instances(machine_specs.size, bootstrap_options, parallelizer) do |instance|

        # Assign each one to a machine spec
        machine_spec = machine_specs.pop
        machine_options = specs_and_options[machine_spec]
        machine_spec.reference = {
          'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
          'allocated_at' => Time.now.utc.to_s,
          'host_node' => action_handler.host_node,
          'image_id' => bootstrap_options[:image_id],
          'instance_id' => instance.id
        }
        machine_spec.driver_url = driver_url
        instance.tags['Name'] = machine_spec.name
        instance.source_dest_check = machine_options[:source_dest_check] if machine_options.has_key?(:source_dest_check)
        converge_tags(instance, machine_options[:aws_tags], action_handler)
        machine_spec.reference['key_name'] = bootstrap_options[:key_name] if bootstrap_options[:key_name]
        %w(is_windows ssh_username sudo use_private_ip_for_ssh ssh_gateway).each do |key|
          machine_spec.reference[key] = machine_options[key.to_sym] if machine_options[key.to_sym]
        end
        action_handler.performed_action "machine #{machine_spec.name} created as #{instance.id} on #{driver_url}"

        yield machine_spec, instance if block_given?
      end

      if machine_specs.size > 0
        raise "Not all machines were created by create_servers"
      end
    end
  end.to_a
end

#create_ssh_transport(machine_spec, machine_options, instance) ⇒ Object



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 754

def create_ssh_transport(machine_spec, machine_options, instance)
  ssh_options = ssh_options_for(machine_spec, machine_options, instance)
  username = machine_spec.reference['ssh_username'] || machine_options[:ssh_username] || default_ssh_username
  if machine_options.has_key?(:ssh_username) && machine_options[:ssh_username] != machine_spec.reference['ssh_username']
    Chef::Log.warn("Server #{machine_spec.name} was created with SSH username #{machine_spec.reference['ssh_username']} and machine_options specifies username #{machine_options[:ssh_username]}.  Using #{machine_spec.reference['ssh_username']}.  Please edit the node and change the chef_provisioning.reference.ssh_username attribute if you want to change it.")
  end
  options = {}
  if machine_spec.reference[:sudo] || (!machine_spec.reference.has_key?(:sudo) && username != 'root')
    options[:prefix] = 'sudo '
  end

  remote_host = determine_remote_host(machine_spec, instance)

  #Enable pty by default
  options[:ssh_pty_enable] = true
  options[:ssh_gateway] = machine_spec.reference['ssh_gateway'] if machine_spec.reference.has_key?('ssh_gateway')

  Chef::Provisioning::Transport::SSH.new(remote_host, username, ssh_options, options, config)
end

#create_winrm_transport(machine_spec, machine_options, instance) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 707

def create_winrm_transport(machine_spec, machine_options, instance)
  remote_host = determine_remote_host(machine_spec, instance)

  port = machine_spec.reference['winrm_port'] || 5985
  endpoint = "http://#{remote_host}:#{port}/wsman"
  type = :plaintext
  pem_bytes = get_private_key(instance.key_name)
  encrypted_admin_password = wait_for_admin_password(machine_spec)

  decoded = Base64.decode64(encrypted_admin_password)
  private_key = OpenSSL::PKey::RSA.new(pem_bytes)
  decrypted_password = private_key.private_decrypt decoded

  winrm_options = {
    :user => machine_spec.reference['winrm_username'] || 'Administrator',
    :pass => decrypted_password,
    :disable_sspi => true,
    :basic_auth_only => true
  }

  Chef::Provisioning::Transport::WinRM.new("#{endpoint}", type, winrm_options, {})
end

#default_ami_for_region(region) ⇒ Object



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 680

def default_ami_for_region(region)
  Chef::Log.debug("Choosing default AMI for region '#{region}'")

  case region
    when 'ap-northeast-1'
      'ami-6cbca76d'
    when 'ap-southeast-1'
      'ami-04c6ec56'
    when 'ap-southeast-2'
      'ami-c9eb9ff3'
    when 'eu-west-1'
      'ami-5f9e1028'
    when 'eu-central-1'
      'ami-56c2f14b'
    when 'sa-east-1'
      'ami-81f14e9c'
    when 'us-east-1'
      'ami-12793a7a'
    when 'us-west-1'
      'ami-6ebca42b'
    when 'us-west-2'
      'ami-b9471c89'
    else
      raise 'Unsupported region!'
  end
end

#default_aws_keypair(action_handler, machine_spec) ⇒ Object



918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 918

def default_aws_keypair(action_handler, machine_spec)
  driver = self
  default_key_name = default_aws_keypair_name(machine_spec)
  updated = @@chef_default_lock.synchronize do
    Provisioning.inline_resource(action_handler) do
      aws_key_pair default_key_name do
        driver driver
        allow_overwrite true
      end
    end
  end

  # Only warn the first time
  default_warning = 'Using default key, which is not shared between machines!  It is recommended to create an AWS key pair with the aws_key_pair resource, and set :bootstrap_options => { :key_name => <key name> }'
  Chef::Log.warn(default_warning) if updated

  default_key_name
end

#default_aws_keypair_name(machine_spec) ⇒ Object



909
910
911
912
913
914
915
916
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 909

def default_aws_keypair_name(machine_spec)
  if machine_spec.reference &&
      Gem::Version.new(machine_spec.reference['driver_version']) < Gem::Version.new('0.10')
    'metal_default'
  else
    'chef_default'
  end
end

#default_instance_typeObject



1108
1109
1110
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 1108

def default_instance_type
  't2.micro'
end

#default_ssh_usernameObject



617
618
619
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 617

def default_ssh_username
  'ubuntu'
end

#destroy_image(action_handler, image_spec, image_options) ⇒ Object



398
399
400
401
402
403
404
405
406
407
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 398

def destroy_image(action_handler, image_spec, image_options)
  # TODO the driver should automatically be set by `inline_resource`
  d = self
  Provisioning.inline_resource(action_handler) do
    aws_image image_spec.name do
      action :destroy
      driver d
    end
  end
end

#destroy_load_balancer(action_handler, lb_spec, lb_options) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 346

def destroy_load_balancer(action_handler, lb_spec, lb_options)
  return if lb_spec == nil

  actual_elb = load_balancer_for(lb_spec)
  if actual_elb && actual_elb.exists?
    # Remove ELB from AWS
    action_handler.perform_action "Deleting EC2 ELB #{lb_spec.id}" do
      actual_elb.delete
    end
  end

  # Remove LB spec from databag
  lb_spec.delete(action_handler)
end

#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 505

def destroy_machine(action_handler, machine_spec, machine_options)
  d = self
  Provisioning.inline_resource(action_handler) do
    aws_instance machine_spec.name do
      action :destroy
      driver d
    end
  end

  # TODO move this into the aws_instance provider somehow
  strategy = convergence_strategy_for(machine_spec, machine_options)
  strategy.cleanup_convergence(action_handler, machine_spec)
end

#determine_remote_host(machine_spec, instance) ⇒ Object



774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 774

def determine_remote_host(machine_spec, instance)
  if machine_spec.reference['use_private_ip_for_ssh']
    instance.private_ip_address
  elsif !instance.public_ip_address
    Chef::Log.warn("Server #{machine_spec.name} has no public ip address.  Using private ip '#{instance.private_ip_address}'.  Set driver option 'use_private_ip_for_ssh' => true if this will always be the case ...")
    instance.private_ip_address
  elsif instance.public_ip_address
    instance.public_ip_address
  else
    raise "Server #{instance.id} has no private or public IP address!"
  end
end

#ec2Object



519
520
521
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 519

def ec2
  @ec2 ||= AWS::EC2.new(config: aws_config)
end

#elbObject



523
524
525
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 523

def elb
  @elb ||= AWS::ELB.new(config: aws_config)
end

#get_listener(listener) ⇒ Object



1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 1072

def get_listener(listener)
  result = {}

  case listener
  when Hash
    result.merge!(listener)
  when Array
    result[:port] = listener[0] if listener.size >= 1
    result[:protocol] = listener[1] if listener.size >= 2
  when Symbol,String
    result[:protocol] = listener
  when Integer
    result[:port] = listener
  else
    raise "Invalid listener #{listener}"
  end

  # If either port or protocol are set, set the other
  if result[:port] && !result[:protocol]
    result[:protocol] = PROTOCOL_DEFAULTS[result[:port]]
  elsif result[:protocol] && !result[:port]
    result[:port] = PORT_DEFAULTS[result[:protocol]]
  end
  if result[:instance_port] && !result[:instance_protocol]
    result[:instance_protocol] = PROTOCOL_DEFAULTS[result[:instance_port]]
  elsif result[:instance_protocol] && !result[:instance_port]
    result[:instance_port] = PORT_DEFAULTS[result[:instance_protocol]]
  end

  # If instance_port is still unset, copy port/protocol over
  result[:instance_port] ||= result[:port]
  result[:instance_protocol] ||= result[:protocol]

  result
end

#get_listeners(listeners) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 1051

def get_listeners(listeners)
  case listeners
  when Hash
    listeners.map do |from, to|
      from = get_listener(from)
      from.delete(:instance_port)
      from.delete(:instance_protocol)
      to = get_listener(to)
      to.delete(:port)
      to.delete(:protocol)
      to.merge(from)
    end
  when Array
    listeners.map { |listener| get_listener(listener) }
  when nil
    nil
  else
    [ get_listener(listeners) ]
  end
end

#iamObject



527
528
529
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 527

def iam
  @iam ||= AWS::IAM.new(config: aws_config)
end

#image_for(image_spec) ⇒ Object



651
652
653
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 651

def image_for(image_spec)
  Chef::Resource::AwsImage.get_aws_object(image_spec.name, driver: self, managed_entry_store: image_spec.managed_entry_store, required: false)
end

#instance_for(machine_spec) ⇒ Object



636
637
638
639
640
641
642
643
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 636

def instance_for(machine_spec)
  if machine_spec.reference
    if machine_spec.driver_url != driver_url
      raise "Switching a machine's driver from #{machine_spec.driver_url} to #{driver_url} is not currently supported!  Use machine :destroy and then re-create the machine on the new driver."
    end
    Chef::Resource::AwsInstance.get_aws_object(machine_spec.reference['instance_id'], driver: self, managed_entry_store: machine_spec.managed_entry_store, required: false)
  end
end

#instances_for(machine_specs) ⇒ Object



645
646
647
648
649
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 645

def instances_for(machine_specs)
  result = {}
  machine_specs.each { |machine_spec| result[machine_spec] = instance_for(machine_spec) }
  result
end

#keypair_for(bootstrap_options) ⇒ Object



621
622
623
624
625
626
627
628
629
630
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 621

def keypair_for(bootstrap_options)
  if bootstrap_options[:key_name]
    keypair_name = bootstrap_options[:key_name]
    actual_key_pair = ec2.key_pairs[keypair_name]
    if !actual_key_pair.exists?
      ec2.key_pairs.create(keypair_name)
    end
    actual_key_pair
  end
end

#load_balancer_for(lb_spec) ⇒ Object



632
633
634
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 632

def load_balancer_for(lb_spec)
  Chef::Resource::AwsLoadBalancer.get_aws_object(lb_spec.name, driver: self, managed_entry_store: lb_spec.managed_entry_store, required: false)
end

#machine_for(machine_spec, machine_options, instance = nil) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 581

def machine_for(machine_spec, machine_options, instance = nil)
  instance ||= instance_for(machine_spec)

  if !instance
    raise "Instance for node #{machine_spec.name} has not been created!"
  end

  if machine_spec.reference['is_windows']
    Chef::Provisioning::Machine::WindowsMachine.new(machine_spec, transport_for(machine_spec, machine_options, instance), convergence_strategy_for(machine_spec, machine_options))
  else
    Chef::Provisioning::Machine::UnixMachine.new(machine_spec, transport_for(machine_spec, machine_options, instance), convergence_strategy_for(machine_spec, machine_options))
  end
end

#parse_arn(arn) ⇒ Object



551
552
553
554
555
556
557
558
559
560
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 551

def parse_arn(arn)
  parts = arn.split(':', 6)
  {
    partition: parts[1],
    service: parts[2],
    region: parts[3],
    account_id: parts[4],
    resource: parts[5]
  }
end

#ready_image(action_handler, image_spec, image_options) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 384

def ready_image(action_handler, image_spec, image_options)
  actual_image = image_for(image_spec)
  if actual_image.nil? || !actual_image.exists?
    raise 'Cannot ready an image that does not exist'
  else
    if actual_image.state != :available
      action_handler.report_progress 'Waiting for image to be ready ...'
      wait_until_ready_image(action_handler, image_spec, actual_image)
    else
      action_handler.report_progress "Image #{image_spec.name} is ready!"
    end
  end
end

#ready_load_balancer(action_handler, lb_spec, lb_options, machine_spec) ⇒ Object



343
344
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 343

def ready_load_balancer(action_handler, lb_spec, lb_options, machine_spec)
end

#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 474

def ready_machine(action_handler, machine_spec, machine_options)
  instance = instance_for(machine_spec)

  if instance.nil?
    raise "Machine #{machine_spec.name} does not have an instance associated with it, or instance does not exist."
  end

  if instance.status != :running
    wait_until_machine(action_handler, machine_spec, instance) { instance.status != :stopping }
    if instance.status == :stopped
      action_handler.perform_action "Start #{machine_spec.name} (#{machine_spec.reference['instance_id']}) in #{aws_config.region} ..." do
        instance.start
      end
    end
    wait_until_ready_machine(action_handler, machine_spec, instance)
  end

  wait_for_transport(action_handler, machine_spec, machine_options)
  machine_for(machine_spec, machine_options, instance)
end

#s3Object



531
532
533
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 531

def s3
  @s3 ||= AWS::S3.new(config: aws_config)
end

#snsObject



535
536
537
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 535

def sns
  @sns ||= AWS::SNS.new(config: aws_config)
end

#sqsObject



539
540
541
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 539

def sqs
  @sqs ||= AWS::SQS.new(config: aws_config)
end

#ssh_options_for(machine_spec, machine_options, instance) ⇒ Object



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 787

def ssh_options_for(machine_spec, machine_options, instance)
  result = {
    # TODO create a user known hosts file
    #          :user_known_hosts_file => vagrant_ssh_config['UserKnownHostsFile'],
    #          :paranoid => true,
    :auth_methods => [ 'publickey' ],
    :keys_only => true,
    :host_key_alias => "#{instance.id}.AWS"
  }.merge(machine_options[:ssh_options] || {})
  if instance.respond_to?(:private_key) && instance.private_key
    result[:key_data] = [ instance.private_key ]
  elsif instance.respond_to?(:key_name) && instance.key_name
    key = get_private_key(instance.key_name)
    unless key
      raise "Server has key name '#{instance.key_name}', but the corresponding private key was not found locally.  Check if the key is in Chef::Config.private_key_paths: #{Chef::Config.private_key_paths.join(', ')}"
    end
    result[:key_data] = [ key ]
  elsif machine_spec.reference['key_name']
    key = get_private_key(machine_spec.reference['key_name'])
    unless key
      raise "Server was created with key name '#{machine_spec.reference['key_name']}', but the corresponding private key was not found locally.  Check if the key is in Chef::Config.private_key_paths: #{Chef::Config.private_key_paths.join(', ')}"
    end
    result[:key_data] = [ key ]
  elsif machine_options[:bootstrap_options] && machine_options[:bootstrap_options][:key_path]
    result[:key_data] = [ IO.read(machine_options[:bootstrap_options][:key_path]) ]
  elsif machine_options[:bootstrap_options] && machine_options[:bootstrap_options][:key_name]
    result[:key_data] = [ get_private_key(machine_options[:bootstrap_options][:key_name]) ]
  else
    # TODO make a way to suggest other keys to try ...
    raise "No key found to connect to #{machine_spec.name} (#{machine_spec.reference.inspect})!"
  end
  result
end

#transport_for(machine_spec, machine_options, instance) ⇒ Object



655
656
657
658
659
660
661
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 655

def transport_for(machine_spec, machine_options, instance)
  if machine_spec.reference['is_windows']
    create_winrm_transport(machine_spec, machine_options, instance)
  else
    create_ssh_transport(machine_spec, machine_options, instance)
  end
end

#user_dataObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 409

def user_data
  # TODO: Make this use HTTPS at some point.
  <<EOD
<powershell>
winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

net stop winrm
sc config winrm start=auto
net start winrm
</powershell>
EOD
end

#wait_for_admin_password(machine_spec) ⇒ Object



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 730

def wait_for_admin_password(machine_spec)
  time_elapsed = 0
  sleep_time = 10
  max_wait_time = 900 # 15 minutes
  encrypted_admin_password = nil
  instance_id = machine_spec.reference['instance_id']

  Chef::Log.info "waiting for #{machine_spec.name}'s admin password to be available..."
  while time_elapsed < max_wait_time && encrypted_admin_password.nil?
    response = ec2.client.get_password_data({ :instance_id => instance_id })
    encrypted_admin_password = response['password_data'.to_sym]

    if encrypted_admin_password.nil?
      Chef::Log.info "#{time_elapsed}/#{max_wait_time}s elapsed -- sleeping #{sleep_time} for #{machine_spec.name}'s admin password."
      sleep(sleep_time)
      time_elapsed += sleep_time
    end
  end

  Chef::Log.info "#{machine_spec.name}'s admin password is available!"

  encrypted_admin_password
end

#wait_for_transport(action_handler, machine_spec, machine_options) ⇒ Object



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 889

def wait_for_transport(action_handler, machine_spec, machine_options)
  instance = instance_for(machine_spec)
  time_elapsed = 0
  sleep_time = 10
  max_wait_time = 120
  transport = transport_for(machine_spec, machine_options, instance)
  unless transport.available?
    if action_handler.should_perform_actions
      action_handler.report_progress "waiting for #{machine_spec.name} (#{instance.id} on #{driver_url}) to be connectable (transport up and running) ..."
      while time_elapsed < max_wait_time && !transport.available?
        action_handler.report_progress "been waiting #{time_elapsed}/#{max_wait_time} -- sleeping #{sleep_time} seconds for #{machine_spec.name} (#{instance.id} on #{driver_url}) to be connectable ..."
        sleep(sleep_time)
        time_elapsed += sleep_time
      end

      action_handler.report_progress "#{machine_spec.name} is now connectable"
    end
  end
end

#wait_until_image(action_handler, image_spec, image = nil, &block) ⇒ Object



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 845

def wait_until_image(action_handler, image_spec, image=nil, &block)
  image ||= image_for(image_spec)
  time_elapsed = 0
  sleep_time = 10
  max_wait_time = 300
  if !yield(image)
    action_handler.report_progress "waiting for #{image_spec.name} (#{image.id} on #{driver_url}) to be ready ..."
    while time_elapsed < max_wait_time && !yield(image)
      action_handler.report_progress "been waiting #{time_elapsed}/#{max_wait_time} -- sleeping #{sleep_time} seconds for #{image_spec.name} (#{image.id} on #{driver_url}) to be ready ..."
      sleep(sleep_time)
      time_elapsed += sleep_time
    end
    unless yield(image)
      raise "Image #{image.id} did not become ready within #{max_wait_time} seconds"
    end
    action_handler.report_progress "Image #{image_spec.name} is now ready"
  end
end

#wait_until_machine(action_handler, machine_spec, instance = nil, &block) ⇒ Object



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 868

def wait_until_machine(action_handler, machine_spec, instance=nil, &block)
  instance ||= instance_for(machine_spec)
  time_elapsed = 0
  sleep_time = 10
  max_wait_time = 120
  if !yield(instance)
    if action_handler.should_perform_actions
      action_handler.report_progress "waiting for #{machine_spec.name} (#{instance.id} on #{driver_url}) to be ready ..."
      while time_elapsed < max_wait_time && !yield(instance)
        action_handler.report_progress "been waiting #{time_elapsed}/#{max_wait_time} -- sleeping #{sleep_time} seconds for #{machine_spec.name} (#{instance.id} on #{driver_url}) to be ready ..."
        sleep(sleep_time)
        time_elapsed += sleep_time
      end
      unless yield(instance)
        raise "Image #{instance.id} did not become ready within #{max_wait_time} seconds"
      end
      action_handler.report_progress "#{machine_spec.name} is now ready"
    end
  end
end

#wait_until_ready_image(action_handler, image_spec, image = nil) ⇒ Object



841
842
843
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 841

def wait_until_ready_image(action_handler, image_spec, image=nil)
  wait_until_image(action_handler, image_spec, image) { image.state == :available }
end

#wait_until_ready_machine(action_handler, machine_spec, instance = nil) ⇒ Object



864
865
866
# File 'lib/chef/provisioning/aws_driver/driver.rb', line 864

def wait_until_ready_machine(action_handler, machine_spec, instance=nil)
  wait_until_machine(action_handler, machine_spec, instance) { instance.status == :running }
end