Class: EC2Launcher::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2launcher.rb

Instance Method Summary collapse

Instance Method Details

#attach_to_elb(instance, elb_name) ⇒ Object

Attaches an instance to the specified ELB.

Parameters:

  • instance (AWS::EC2::Instance)

    newly created EC2 instance.

  • elb_name (String)

    name of ELB.



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/ec2launcher.rb', line 400

def attach_to_elb(instance, elb_name)
  begin
    puts ""
    puts "Adding to ELB: #{elb_name}"
    elb = AWS::ELB.new
    AWS.memoize do
      # Build list of availability zones for any existing instances
      zones = { }
      zones[instance.availability_zone] = instance.availability_zone
      elb.load_balancers[elb_name].instances.each do |elb_instance|
        zones[elb_instance.availability_zone] = elb_instance.availability_zone
      end
  
      # Build list of existing zones
      existing_zones = { }
      elb.load_balancers[elb_name].availability_zones.each do |zone|
        existing_zones[zone.name] = zone
      end
  
      # Enable zones
      zones.keys.each do |zone_name|
        elb.load_balancers[elb_name].availability_zones.enable(zones[zone_name])
      end
  
      # Disable zones
      existing_zones.keys.each do |zone_name|
        elb.load_balancers[elb_name].availability_zones.disable(existing_zones[zone_name]) unless zones.has_key?(zone_name)
      end
  
      elb.load_balancers[elb_name].instances.register(instance)
    end
  rescue StandardError => bang
    puts "Error adding to load balancers: " + bang.to_s
  end
end

#build_list_of_valid_directories(directories) ⇒ Array<String>

Given a list of possible directories, build a list of directories that actually exist.

Parameters:

  • directories (Array<String>)

    list of possible directories

Returns:

  • (Array<String>)

    directories that exist or an empty array if none of the directories exist.



441
442
443
444
445
446
447
448
449
450
451
# File 'lib/ec2launcher.rb', line 441

def build_list_of_valid_directories(directories)
  dirs = []
  unless directories.nil?
    if directories.kind_of? Array
      directories.each {|d| dirs << d if File.directory?(d) }
    else
      dirs << directories if File.directory?(directories)
    end
  end
  dirs
end

#find_ami(arch, virtualization, ami_name_match, id = nil) ⇒ AmiDetails

Searches for the most recent AMI matching the criteria.

Parameters:

  • arch (String)

    system archicture, ‘i386` or `x86_64`

  • virtualization (String)

    virtualization type, ‘paravirtual` or `hvm`

  • ami_name_match (Regex)

    regular expression that describes the AMI.

  • id (String, nil) (defaults to: nil)

    id of an AMI. If not nil, ami_name_match is ignored.

Returns:



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/ec2launcher.rb', line 461

def find_ami(arch, virtualization, ami_name_match, id = nil)
  puts "Searching for AMI..."
  ami_name = ""
  ami_id = ""

  # Retrieve list of AMIs
  my_images = @ec2.images.with_owner('self')

  if id.nil?
    # Search for latest AMI with the right architecture and virtualization
    my_images.each do |ami|
      next if arch != ami.architecture.to_s
      next if virtualization != ami.virtualization_type.to_s
      next unless ami.state == :available

      next if ! ami.name.match(ami_name_match)

      if ami.name > ami_name
          ami_name = ami.name
          ami_id = ami.id
      end
    end
  else
    # Look for specified AMI
    ami_arch = nil
    my_images.each do |ami|
      next if ami.id != id
      ami_id = id
      ami_name = ami.name
      ami_arch = ami.architecture
    end

    # Check that AMI exists
    if ami_id.nil?
      abort("AMI id not found: #{ami_id}")
    end

    if arch != ami_arch.to_s
      abort("Invalid AMI selection. Architecture for instance type (#{instance_type} - #{instance_architecture} - #{instance_virtualization}) does not match AMI arch (#{ami_arch.to_s}).")
    end
  end

  AmiDetails.new(ami_name, ami_id)
end

#generate_hostnameObject

Generates a new hostname based on:

* application base name
* application name
* application suffix
* environment short name
* environment name


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/ec2launcher.rb', line 533

def generate_hostname()
  puts "Calculating host name..."

  prefix = @application.basename
  prefix ||= @application.name

  env_suffix = @environment.short_name
  env_suffix ||= @environment.name
  
  suffix = env_suffix
  unless @application.name_suffix.nil?
    suffix = "#{@application.name_suffix}.#{env_suffix}"
  end

  regex = Regexp.new("#{prefix}(\\d+)[.]#{suffix.gsub(/[.]/, "[.]")}.*")

  server_numbers = []

  highest_server_number = 0
  lowest_server_number = 32768
  AWS.memoize do
    server_instances = @ec2.instances.filter("tag:Name", "#{prefix}*#{suffix}*")
    server_instances.each do |i|
      next if i.status == :terminated
      server_name = i.tags[:Name]
      unless regex.match(server_name).nil?
        server_num = $1.to_i
        server_numbers << server_num
      end
    end
    highest_server_number = server_numbers.max
  end

  # If the highest number server is less than 10, just add
  # 1 to it. Otherwise, find the first available
  # server number starting at 1.
  host_number = 0
  if highest_server_number.nil?
    host_number = 1
  elsif highest_server_number < 10
    host_number = highest_server_number + 1
  else
    # Try to start over with 1 and find the
    # first available host number
    server_number_set = Set.new(server_numbers)
    host_number = 1
    while server_number_set.include?(host_number) do
        host_number += 1
    end
  end

  short_hostname = "#{prefix}#{host_number}.#{suffix}"
  short_hostname
end

#initialize_awsObject

Initializes connections to the AWS SDK



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/ec2launcher.rb', line 508

def initialize_aws()
  aws_access_key = @options.access_key
  aws_access_key ||= ENV['AWS_ACCESS_KEY']

  aws_secret_access_key = @options.secret
  aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY']

  if aws_access_key.nil? || aws_secret_access_key.nil?
    abort("You MUST either set the AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY environment variables or use the command line options.")
  end

  puts "Initializing AWS connection..."
  AWS.config({
    :access_key_id => aws_access_key,
    :secret_access_key => aws_secret_access_key
  })
end

#launch(options) ⇒ Object



51
52
53
54
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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/ec2launcher.rb', line 51

def launch(options)
  @options = options
  
  # Load configuration data
  @config = load_config_file

  environments_directories = process_directory_list(@config.environments, "environments", "Environments", false)
  applications_directories = process_directory_list(@config.applications, "applications", "Applications", true)

  # Attempt to load default environment data
  @default_environment = nil
  environments_directories.each do |env_dir|
    filename = File.join(env_dir, "default.rb")
    @default_environment = load_environment_file(filename)
    unless @default_environment.nil?
      validate_environment(filename, @default_environment)
      break
    end
  end
  @default_environment ||= EC2Launcher::Environment.new

  # Load other environments
  @environments = { }
  environments_directories.each do |env_dir|
    Dir.entries(env_dir).each do |env_name|
      filename = File.join(env_dir, env_name)
      next if File.directory?(filename)
      next if filename == "default.rb"

      new_env = load_environment_file(filename, @default_environment)
      validate_environment(filename, new_env)

      @environments[new_env.name] = new_env
      new_env.aliases.each {|env_alias| @environments[env_alias] = new_env }
    end
  end

  # Load applications
  @applications = {}
  applications_directories.each do |app_dir|
    Dir.entries(app_dir).each do |application_name|
      filename = File.join(app_dir, application_name)
      next if File.directory?(filename)

      apps = ApplicationDSL.execute(File.read(filename)).applications
      apps.each do |new_application|
        @applications[new_application.name] = new_application
        validate_application(filename, new_application)
      end
    end
  end

  # Process inheritance rules for environments
  @environments.values.each do |env|
    next if env.inherit.nil?

    new_env = process_environment_inheritance(env)
    @environments[new_env.name] = new_env
  end

  # Process inheritance rules for applications
  @applications.values.each do |app|
    next if app.inherit.nil?

    new_app = process_application_inheritance(app)
    @applications[new_app.name] = new_app
  end

  if @options.list
    puts ""
    env_names = @environments.keys.sort.join(", ")
    puts "Environments: #{env_names}"

    app_names = @applications.keys.sort.join(", ")
    puts "Applications: #{app_names}"
    exit 0
  end

  ##############################
  # ENVIRONMENT
  ##############################
  unless @environments.has_key? options.environ
    puts "Environment not found: #{options.environ}"
    exit 2
  end
  @environment = @environments[options.environ]

  ##############################
  # APPLICATION
  ##############################
  unless @applications.has_key? options.application
    puts "Application not found: #{options.application}"
    exit 3
  end
  @application = @applications[options.application]

  # Initialize AWS and create EC2 connection
  initialize_aws()
  @ec2 = AWS::EC2.new

  ##############################
  # AVAILABILITY ZONES
  ##############################
  availability_zone = options.zone
  if availability_zone.nil?
    availability_zone = @application.availability_zone
    availability_zone ||= @environment.availability_zone
    availability_zone ||= @default_environment.availability_zone
    availability_zone ||= "us-east-1a"
  end

  ##############################
  # SSH KEY
  ##############################
  key_name = @environment.key_name
  key_name ||= @default_environment.key_name
  if key_name.nil?
    puts "Unable to determine SSH key name."
    exit 4
  end

  ##############################
  # SECURITY GROUPS
  ##############################
  security_groups = []
  security_groups += @environment.security_groups unless @environment.security_groups.nil?
  security_groups += @application.security_groups_for_environment(@environment.name)

  ##############################
  # INSTANCE TYPE
  ##############################
  instance_type = options.instance_type
  instance_type ||= @application.instance_type
  instance_type ||= "m1.small"

  ##############################
  # ARCHITECTURE
  ##############################
  instance_architecture = "x86_64"

  instance_virtualization = case instance_type
    when "cc1.4xlarge" then "hvm"
    when "cc2.8xlarge" then "hvm"
    when "cg1.4xlarge" then "hvm"
    else "paravirtual"
  end

  ##############################
  # AMI
  ##############################
  ami_name_match = @application.ami_name
  ami_name_match ||= @environment.ami_name
  ami = find_ami(instance_architecture, instance_virtualization, ami_name_match, @options.ami_id)

  ##############################
  # HOSTNAME
  ##############################
  hostname = @options.hostname
  if hostname.nil?
    short_hostname = generate_hostname()
    hostname = short_hostname

    unless @environment.domain_name.nil?
      hostname += ".#{@environment.domain_name}"
    end
  else
    short_hostname = hostname
    unless @environment.domain_name.nil?
      short_hostname = hostname.gsub(/.#{@environment.domain_name}/, '')
    end
  end

  ##############################
  # Block devices
  ##############################
  builder = EC2Launcher::BlockDeviceBuilder.new(@ec2, @options.volume_size)
  builder.generate_block_devices(hostname, short_hostname, instance_type, @environment, @application, @options.clone_host)
  block_device_mappings = builder.block_device_mappings
  block_device_tags = builder.block_device_tags

  ##############################
  # ELB
  ##############################
  elb_name = nil
  elb_name = @application.elb_for_environment(@environment.name) unless @application.elb.nil?

  ##############################
  # Roles
  ##############################
  roles = []
  roles += @environment.roles unless @environment.roles.nil?
  roles += @application.roles_for_environment(@environment.name)

  ##############################
  # Packages - preinstall
  ##############################
  subnet = nil
  subnet = @application.subnet unless @application.subnet.nil?
  subnet ||= @environment.subnet unless @environment.subnet.nil?

  ##############################
  # Gems - preinstall
  ##############################
  gems = []
  gems += @environment.gems unless @environment.gems.nil?
  gems += @application.gems unless @application.gems.nil?

  ##############################
  # Packages - preinstall
  ##############################
  packages = []
  packages += @environment.packages unless @environment.packages.nil?
  packages += @application.packages unless @application.packages.nil?

  ##############################
  # Email Notification
  ##############################
  email_notifications = nil
  email_notifications = @application.email_notifications
  email_notifications ||= @environment.email_notifications

  ##############################
  # Chef Validation PEM
  ##############################
  chef_validation_pem_url = nil
  chef_validation_pem_url = @options.chef_validation_url
  chef_validation_pem_url ||= @environment.chef_validation_pem_url

  ##############################
  # File on new instance containing AWS keys
  ##############################
  aws_keyfile = @environment.aws_keyfile

  ##############################
  # Build JSON for setup scripts
  ##############################
  setup_json = {
    'hostname' => hostname,
    'short_hostname' => short_hostname,
    'roles' => roles,
    'chef_server_url' => @environment.chef_server_url,
    'chef_validation_pem_url' => chef_validation_pem_url,
    'aws_keyfile' => aws_keyfile,
    'gems' => gems,
    'packages' => packages
  }
  unless @application.block_devices.nil? || @application.block_devices.empty?
    setup_json['block_devices'] = @application.block_devices
  end
  unless email_notifications.nil?
    setup_json['email_notifications'] = email_notifications
  end

  ##############################
  # Build launch command
  user_data = "#!/bin/sh
export HOME=/root
echo '#{setup_json.to_json}' > /tmp/setup.json"

  # pre-commands, if necessary
  unless @environment.precommands.nil? || @environment.precommands.empty?
    precommands = @environment.precommands.join("\n")
    user_data += "\n" + precommands
  end

  # Primary setup script
  user_data += "\ncurl http://bazaar.launchpad.net/~alestic/runurl/trunk/download/head:/runurl-20090817053347-o2e56z7xwq8m9tt6-1/runurl -o /tmp/runurl
chmod +x /tmp/runurl
/tmp/runurl https://raw.github.com/StudyBlue/ec2launcher/master/startup-scripts/setup.rb -e #{@environment.name} -a #{@application.name} -h #{hostname} /tmp/setup.json > /var/log/cloud-startup.log
rm -f /tmp/runurl"
  user_data += " -c #{options.clone_host}" unless options.clone_host.nil?

  # Add extra requested commands to the launch sequence
  options.commands.each {|extra_cmd| user_data += "\n#{extra_cmd}" }

  # Post commands
  unless @environment.postcommands.nil? || @environment.postcommands.empty?
    postcommands = @environment.postcommands.join("\n")
    user_data += "\n" + postcommands
  end

  ##############################
  puts
  puts "Availability zone: #{availability_zone}"
  puts "Key name         : #{key_name}"
  puts "Security groups  : #{security_groups.join(", ")}"
  puts "Instance type    : #{instance_type}"
  puts "Architecture     : #{instance_architecture}"
  puts "AMI name         : #{ami.ami_name}"
  puts "AMI id           : #{ami.ami_id}"
  puts "Name             : #{hostname}"
  puts "ELB              : #{elb_name}" if elb_name
  puts "Chef PEM         : #{chef_validation_pem_url}"
  puts "AWS key file     : #{aws_keyfile}"
  puts "Roles            : #{roles.join(', ')}"
  puts "Gems             : #{gems.join(', ')}"
  puts "Packages         : #{packages.join(', ')}"
  puts "VPC Subnet       : #{subnet}" if subnet

  unless block_device_mappings.empty?
    block_device_mappings.keys.sort.each do |key|
      if block_device_mappings[key] =~ /^ephemeral/
          puts "  Block device   : #{key}, #{block_device_mappings[key]}"
      else
          puts "  Block device   : #{key}, #{block_device_mappings[key][:volume_size]}GB, " +
             "#{block_device_mappings[key][:snapshot_id]}, " +
             "(#{block_device_mappings[key][:delete_on_termination] ? 'auto-delete' : 'no delete'})"
      end
    end
  end

  puts "User data:"
  puts user_data
  puts

  if chef_validation_pem_url.nil?
    puts "***ERROR*** Missing the URL For the Chef Validation PEM file."
    exit 3
  end

  # Quit if we're only displaying the defaults
  exit 0 if @options.show_defaults

  ##############################
  # Launch the new intance
  ##############################
  instance = launch_instance(hostname, ami.ami_id, availability_zone, key_name, security_groups, instance_type, user_data, block_device_mappings, block_device_tags, subnet)

  ##############################
  # ELB
  ##############################
  attach_to_elb(instance, elb_name) unless elb_name.nil?

  ##############################
  # COMPLETED
  ##############################
  puts ""
  puts "Hostname   : #{hostname}"
  puts "Instance id: #{instance.id}"
  puts "Public dns : #{instance.public_dns_name}"
  puts "Private dns: #{instance.private_dns_name}"
  puts "********************"    
end

#launch_instance(hostname, ami_id, availability_zone, key_name, security_groups, instance_type, user_data, block_device_mappings = nil, block_device_tags = nil, vpc_subnet = nil) ⇒ AWS::EC2::Instance

Launches an EC2 instance.

Parameters:

  • FQDN (String)

    for the new host.

  • ami_id (String)

    id for the AMI to use.

  • availability_zone (String)

    EC2 availability zone to use

  • key_name (String)

    EC2 SSH key to use.

  • security_groups (Array<String>)

    list of security groups.

  • instance_type (String)

    EC2 instance type.

  • user_data (String)

    command data to store pass to the instance in the EC2 user-data field.

  • Hash<String,Hash<String, (Hash<String,Hash<String, String>, nil] block_device_mappings mapping of device names to block device details. See http://docs.amazonwebservices.com/AWSRubySDK/latest/AWS/EC2/InstanceCollection.html#create-instance_method.)

    String>, nil] block_device_mappings mapping of device names to block device details. See docs.amazonwebservices.com/AWSRubySDK/latest/AWS/EC2/InstanceCollection.html#create-instance_method.

  • block_device_tags (Hash<String,Hash<String, String>>, nil) (defaults to: nil)

    mapping of device names to hash objects with tags for the new EBS block devices.

Returns:

  • (AWS::EC2::Instance)

    newly created EC2 instance or nil if the launch failed.



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/ec2launcher.rb', line 602

def launch_instance(hostname, ami_id, availability_zone, key_name, security_groups, instance_type, user_data, block_device_mappings = nil, block_device_tags = nil, vpc_subnet = nil)
  puts "Launching instance..."
  new_instance = nil
  run_with_backoff(30, 1, "launching instance") do
    new_instance = @ec2.instances.create(
      :image_id => ami_id,
      :availability_zone => availability_zone,
      :key_name => key_name,
      :security_groups => security_groups,
      :user_data => user_data,
      :instance_type => instance_type,
      :block_device_mappings => block_device_mappings,
      :subnet => vpc_subnet
    )
  end
  sleep 5

  puts "  Waiting for instance to start up..."
  sleep 2
  instance_ready = false
  until instance_ready
    sleep 1
    begin
      instance_ready = new_instance.status != :pending
    rescue
    end
  end

  unless new_instance.status == :running
    puts "Instance launch failed. Aborting."
    exit 5
  end

  ##############################
  # Tag instance
  puts "Tagging instance..."
  run_with_backoff(30, 1, "tag #{new_instance.id}, tag: name, value: #{hostname}") { new_instance.add_tag("Name", :value => hostname) }
  run_with_backoff(30, 1, "tag #{new_instance.id}, tag: environment, value: #{@environment.name}") { new_instance.add_tag("environment", :value => @environment.name) }
  run_with_backoff(30, 1, "tag #{new_instance.id}, tag: application, value: #{@application.name}") { new_instance.add_tag("application", :value => @application.name) }

  ##############################
  # Tag volumes
  unless block_device_tags.empty?
    puts "Tagging volumes..."
    AWS.start_memoizing
    block_device_tags.keys.each do |device|
      v = new_instance.block_device_mappings[device].volume
      block_device_tags[device].keys.each do |tag_name|
        run_with_backoff(30, 1, "tag #{v.id}, tag: #{tag_name}, value: #{block_device_tags[device][tag_name]}") do
          v.add_tag(tag_name, :value => block_device_tags[device][tag_name])
        end
      end
    end
    AWS.stop_memoizing
  end

  new_instance
end

#load_config_fileEC2Launcher::Config

Read in the configuration file stored in the workspace directory. By default this will be ‘./config.rb’.

Returns:



665
666
667
668
669
670
# File 'lib/ec2launcher.rb', line 665

def load_config_file()
  # Load configuration file
  config_filename = File.join(@options.directory, "config.rb")
  abort("Unable to find 'config.rb' in '#{@options.directory}'") unless File.exists?(config_filename)
  ConfigDSL.execute(File.read(config_filename)).config
end

#load_environment_file(name, default_environment = nil, fail_on_missing = false) ⇒ EC2Launcher::Environment

Load and parse an environment file

Parameters:

  • name (String)

    full pathname of the environment file to load

  • default_environment (EC2Launcher::Environment, nil) (defaults to: nil)

    the default environment, which will be used as the base for the new environment. Optional.

  • fail_on_missing (Boolean) (defaults to: false)

    print an error and exit if the file does not exist.

Returns:



681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/ec2launcher.rb', line 681

def load_environment_file(name, default_environment = nil, fail_on_missing = false)
  unless File.exists?(name)
    abort("Unable to read environment: #{name}") if fail_on_missing
    return nil
  end

  new_env = Marshal::load(Marshal.dump(default_environment)) unless default_environment.nil?
  new_env ||= EC2Launcher::Environment.new

  new_env.load(File.read(name))
  new_env
end

#process_application_inheritance(app) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/ec2launcher.rb', line 724

def process_application_inheritance(app)
    return app if app.inherit.nil?

    # Find base application
    base_app = @applications[app.inherit]
    abort("Invalid inheritance '#{app.inherit}' in #{app.name}") if base_app.nil?

    new_app = nil
    if base_app.inherit.nil?
      # Clone base application
      new_app = Marshal::load(Marshal.dump(base_app))
    else
      new_app = process_application_inheritance(base_app)
    end
    new_app.merge(app)
    new_app
end

#process_directory_list(target_directories, default_directory, name, fail_on_error = false) ⇒ Array<String] list of directories that exist

Attempts to build a list of valid directories.

Parameters:

  • target_directories (Array<String>, nil)

    list of possible directories

  • default_directory (String)

    directory to use if the target_directories list is empty or nil

  • name (String)

    name of the type of directory. Used only for error messages.

  • fail_on_error (Boolean) (defaults to: false)

    exit with an error if the list of valid directories is empty

Returns:

  • (Array<String] list of directories that exist)

    Array<String] list of directories that exist



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/ec2launcher.rb', line 703

def process_directory_list(target_directories, default_directory, name, fail_on_error = false)
  dirs = []
  if target_directories.nil?
    dirs << File.join(@options.directory, default_directory)
  else
    target_directories.each {|d| dirs << File.join(@options.directory, d) }
  end
  valid_directories = build_list_of_valid_directories(dirs)

  if valid_directories.empty?
    temp_dirs = dirs.each {|d| "'#{d}'"}.join(", ")
    if fail_on_error
      abort("ERROR - #{name} directories not found: #{temp_dirs}")
    else
      puts "WARNING - #{name} directories not found: #{temp_dirs}"
    end
  end

  valid_directories
end

#process_environment_inheritance(env) ⇒ Object



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/ec2launcher.rb', line 742

def process_environment_inheritance(env)
    return env if env.inherit.nil?

    # Find base environment
    base_env = @environments[env.inherit]
    abort("Invalid inheritance '#{env.inherit}' in #{env.name}") if base_env.nil?

    new_env = nil
    if base_env.inherit.nil?
      # Clone base environment
      new_env = Marshal::load(Marshal.dump(base_env))
    else
      new_env = process_environment_inheritance(base_env)
    end
    new_env.merge(env)
    new_env
end

#run_with_backoff(max_time, sleep_time, message, &block) ⇒ Object

Runs an AWS request inside a Ruby block with an exponential backoff in case we exceed the allowed AWS RequestLimit.

Parameters:

  • max_time (Integer)

    maximum amount of time to sleep before giving up.

  • sleep_time (Integer)

    the initial amount of time to sleep before retrying.

  • message (message)

    message to display if we get an exception.

  • block (Block)

    Ruby code block to execute.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ec2launcher.rb', line 35

def run_with_backoff(max_time, sleep_time, message, &block)
  if sleep_time > max_time
    puts "AWS::EC2::Errors::RequestLimitExceeded ... failed #{message}"
    return false
  end
  
  begin
    yield
  rescue AWS::EC2::Errors::RequestLimitExceeded
    puts "AWS::EC2::Errors::RequestLimitExceeded ... retrying #{message} in #{sleep_time} seconds"
    sleep sleep_time
    run_with_backoff(max_time, sleep_time * 2, message, &block)
  end  
  true
end

#validate_application(filename, application) ⇒ Object

Validates all settings in an application file

Parameters:

  • filename (String)

    name of the application file

  • application (EC2Launcher::Application)

    application object to validate



765
766
767
768
769
770
771
772
773
# File 'lib/ec2launcher.rb', line 765

def validate_application(filename, application)
  unless application.availability_zone.nil? || AVAILABILITY_ZONES.include?(application.availability_zone)
    abort("Invalid availability zone '#{application.availability_zone}' in application '#{application.name}' (#{filename})")
  end

  unless application.instance_type.nil? || INSTANCE_TYPES.include?(application.instance_type)
    abort("Invalid instance type '#{application.instance_type}' in application '#{application.name}' (#{filename})")
  end
end

#validate_environment(filename, environment) ⇒ Object

Validates all settings in an environment file

Parameters:

  • filename (String)

    name of the environment file

  • environment (EC2Launcher::Environment)

    environment object to validate



780
781
782
783
784
# File 'lib/ec2launcher.rb', line 780

def validate_environment(filename, environment)
  unless environment.availability_zone.nil? || AVAILABILITY_ZONES.include?(environment.availability_zone)
    abort("Invalid availability zone '#{environment.availability_zone}' in environment '#{environment.name}' (#{filename})")
  end
end