Class: EC2Launcher::Launcher

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

Instance Method Summary collapse

Methods included from BackoffRunner

#run_with_backoff

Constructor Details

#initializeLauncher

Returns a new instance of Launcher.



38
39
40
41
42
43
44
45
46
# File 'lib/ec2launcher.rb', line 38

def initialize()
  @run_url_script_cache = nil
  @setup_script_cache = nil

  @log = Logger.new 'ec2launcher'
  log_output = Outputter.stdout
  log_output.formatter = PatternFormatter.new :pattern => "%m"
  @log.outputters = log_output
end

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.



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/ec2launcher.rb', line 414

def attach_to_elb(instance, elb_name)
  begin
    @log.info ""
    @log.info "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
    @log.error "Error adding to load balancers: " + bang.to_s
  end
end

#build_launch_command(fqdn, short_hostname, roles, chef_validation_pem_url, aws_keyfile, gems, packages, email_notifications) ⇒ String

Builds the launch scripts that should run on the new instance.

Parameters:

  • fqdn (String)

    Fully qualified hostname

  • short_name (String)

    Short hostname without the domain

  • chef_validation_pem_url (String)

    URL For the Chef validation pem file

  • aws_keyfile (String)

    Name of the AWS key to use

  • gems (Array<String>)

    List of gems to pre-install

  • packages (Array<String>)

    List of packages to pre-install

  • email_notifications (EC2Launcher::DSL::EmailNotifications)

    Email notification settings for launch updates

Returns:

  • (String)

    Launch commands to pass into new instance as userdata



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'lib/ec2launcher.rb', line 799

def build_launch_command(fqdn, short_hostname, roles, chef_validation_pem_url, aws_keyfile, gems, packages, email_notifications)
  # Build JSON for setup scripts
  setup_json = {
    'hostname' => fqdn,
    '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
  }
  setup_json["gem_path"] = @instance_paths.gem_path
  setup_json["ruby_path"] = @instance_paths.ruby_path
  setup_json["chef_path"] = @instance_paths.chef_path
  setup_json["knife_path"] = @instance_paths.knife_path

  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"
  user_data += "\nexport HOME=/root"
  user_data += "\necho '#{setup_json.to_json}' > /tmp/setup.json"

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

  user_data += "\n"

  unless @options.skip_setup
    if @run_url_script_cache.nil?
      puts "Downloading runurl script from #{RUN_URL_SCRIPT}"
      @run_url_script_cache = `curl -s #{RUN_URL_SCRIPT} |gzip -f |base64`
    end

    if @setup_script_cache.nil?
      puts "Downloading setup script from #{SETUP_SCRIPT}"
      @setup_script_cache = `curl -s #{SETUP_SCRIPT} |gzip -f |base64`
    end

    # runurl script
    user_data += "cat > /tmp/runurl.gz.base64 <<End-Of-Message\n"
    user_data += @run_url_script_cache
    user_data += "End-Of-Message"

    # setup script
    user_data += "\ncat > /tmp/setup.rb.gz.base64 <<End-Of-Message2\n"
    user_data += @setup_script_cache
    user_data += "End-Of-Message2"

    user_data += "\nbase64 -d /tmp/runurl.gz.base64 | gunzip > /tmp/runurl"
    user_data += "\nchmod +x /tmp/runurl"
    # user_data += "\nrm -f /tmp/runurl.gz.base64"

    user_data += "\nbase64 -d /tmp/setup.rb.gz.base64 | gunzip > /tmp/setup.rb"
    user_data += "\nchmod +x /tmp/setup.rb"
    # user_data += "\nrm -f /tmp/setup.rb.gz.base64"

    user_data += "\n#{setup_json['ruby_path']} /tmp/setup.rb -e #{@environment.name} -a #{@application.name} -h #{fqdn} /tmp/setup.json"
    user_data += " -c #{@options.clone_host}" unless @options.clone_host.nil?
    user_data += " 2>&1 > /var/log/cloud-startup.log"
  end

  # Add extra requested commands to the launch sequence
  unless @options.commands.nil?
    commands = @options.commands.collect {|cmd| substitute_command_variables(cmd) }
    user_data += "\n" + commands.join("\n")
  end

  # Post commands
  unless @environment.postcommands.nil? || @environment.postcommands.empty?
    commands = @environment.postcommands.collect {|cmd| substitute_command_variables(cmd) }
    user_data += "\n" + commands.join("\n")
  end
  user_data
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.



455
456
457
458
459
460
461
462
463
464
465
# File 'lib/ec2launcher.rb', line 455

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

#build_path(instance_path, executable, default_path) ⇒ Object

Builds the path to an executable.



468
469
470
471
472
473
474
475
476
477
478
# File 'lib/ec2launcher.rb', line 468

def build_path(instance_path, executable, default_path)
  app_path = default_path
  unless instance_path.nil?
    if instance_path =~ /#{executable}$/
      app_path = instance_path
    else
      app_path = File.join(instance_path, executable)
    end
  end
  app_path
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:



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/ec2launcher.rb', line 488

def find_ami(arch, virtualization, ami_name_match, id = nil)
  @log.info "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

#initialize_awsObject

Initializes connections to the AWS SDK



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/ec2launcher.rb', line 535

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

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

#launch(options) ⇒ Object



48
49
50
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/ec2launcher.rb', line 48

def launch(options)
  @options = options

  @log.level = case @options.verbosity 
    when :quiet then WARN
    when :verbose then DEBUG
    else INFO
  end
  
  # 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)

  # 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)

      new_env = load_environment_file(filename)
      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 = EC2Launcher::DSL::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|
    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
    @log.fatal "Environment not found: #{options.environ}"
    exit 2
  end
  @environment = @environments[options.environ]

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

  ##############################
  # INSTANCE PATHS
  ##############################
  @instance_paths = EC2Launcher::InstancePathsConfig.new(@environment)

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

  ##############################
  # SUBNET
  ##############################
  subnet = nil
  subnet = @application.subnet unless @application.subnet.nil?
  subnet ||= @environment.subnet unless @environment.subnet.nil?

  ec2_subnet = nil
  unless subnet.nil?
    # Find the matching EC2 subnet
    ec2_subnet = @ec2.subnets[subnet]
  end

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

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

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

  # Build mapping of existing security group names to security group objects
  sg_map = { }
  AWS.start_memoizing
  @ec2.security_groups.each do |sg|
    if ec2_subnet.nil?
      next unless sg.vpc_id.nil?
    else
      next unless ec2_subnet.vpc_id == sg.vpc_id
    end
    sg_map[sg.name] = sg
  end
  AWS.stop_memoizing

  # Convert security group names to security group ids
  security_group_ids = []
  missing_security_groups = []
  security_groups.each do |sg_name|
    missing_security_groups << sg_name unless sg_map.has_key?(sg_name)
    security_group_ids << sg_map[sg_name].security_group_id
  end

  if missing_security_groups.length > 0
    @log.fatal "ERROR: Missing security groups: #{missing_security_groups.join(', ')}"
    exit 3
  end

  ##############################
  # 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_generator = EC2Launcher::HostnameGenerator.new(@ec2, @environment, @application)
  short_hostnames = []
  fqdn_names = []
  if @options.count > 1
    1.upto(@options.count).each do |i|
      short_hostname = hostname_generator.generate_hostname()
      long_hostname = hostname_generator.generate_long_name(short_hostname, @environment.domain_name)
      short_hostnames << short_hostname
      fqdn_names << long_hostname
    end
  else
    if @options.hostname.nil?
      short_hostname = hostname_generator.generate_hostname()
      long_hostname = hostname_generator.generate_long_name(short_hostname, @environment.domain_name)
    else
      long_hostname = @options.hostname
      short_hostname = hostname_generator.generate_short_name(short_hostname, @environment.domain_name)
      if long_hostname == short_hostname
        long_hostname = hostname_generator.generate_long_name(short_hostname, @environment.domain_name)
      end
    end
    short_hostnames << short_hostname
    fqdn_names << long_hostname
  end

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

  ##############################
  # 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)

  ##############################
  # 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

  ##############################
  @log.info
  @log.info "Availability zone: #{availability_zone}"
  @log.info "Key name            : #{key_name}"
  @log.info "Security groups     : " + security_groups.collect {|name| "#{name} (#{sg_map[name].security_group_id})"}.join(", ")
  @log.info "Instance type       : #{instance_type}"
  @log.info "Architecture        : #{instance_architecture}"
  @log.info "AMI name            : #{ami.ami_name}"
  @log.info "AMI id              : #{ami.ami_id}"
  @log.info "ELB                 : #{elb_name}" if elb_name
  @log.info "Chef PEM            : #{chef_validation_pem_url}"
  @log.info "AWS key file        : #{aws_keyfile}"
  @log.info "Roles               : #{roles.join(', ')}"
  @log.info "Gems                : #{gems.join(', ')}"
  @log.info "Packages            : #{packages.join(', ')}"
  if subnet
    cidr_block = @ec2.subnets[subnet].cidr_block
    @log.info "VPC Subnet          : #{subnet} (#{cidr_block})"
  end
  @log.info ""
  fqdn_names.each do |fqdn|
    @log.info "Name                : #{fqdn}"
  end

  unless block_device_mappings.empty?
    @log.info ""
    @log.info "Block devices     :"
    block_device_mappings.keys.sort.each do |key|
      if block_device_mappings[key] =~ /^ephemeral/
          @log.info "  Block device   : #{key}, #{block_device_mappings[key]}"
      else
          @log.info "  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

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

  # Quit if we're only displaying the defaults
  if @options.show_defaults || @options.show_user_data
    if @options.show_user_data
      user_data = build_launch_command(fqdn_names[0], short_hostnames[0], roles, chef_validation_pem_url, aws_keyfile, gems, packages, email_notifications)
      @log.info ""
      @log.info "---user-data---"
      @log.info user_data
      @log.info "---user-data---"
    end
    exit 0
  end

  ##############################
  # Launch the new intance
  ##############################
  @log.warn ""
  instances = []
  fqdn_names.each_index do |i|
    block_device_tags = block_device_builder.generate_device_tags(fqdn_names[i], short_hostnames[i], @environment.name, @application.block_devices)
    user_data = build_launch_command(fqdn_names[i], short_hostnames[i], roles, chef_validation_pem_url, aws_keyfile, gems, packages, email_notifications)

    instance = launch_instance(fqdn_names[i], ami.ami_id, availability_zone, key_name, security_group_ids, instance_type, user_data, block_device_mappings, block_device_tags, subnet)
    instances << instance

    public_dns_name = instance.public_dns_name.nil? ? "no public dns" : instance.public_dns_name
    private_dns_name = instance.private_dns_name.nil? ? "no private dns" : instance.private_dns_name
    @log.info "Launched #{fqdn_names[i]} (#{instance.id}) [#{public_dns_name} / #{private_dns_name} / #{instance.private_ip_address} ]"
  end

  @log.info "********************"    
  fqdn_names.each_index do |i|
    public_dns_name = instances[i].public_dns_name.nil? ? "n/a" : instances[i].public_dns_name
    private_dns_name = instances[i].private_dns_name.nil? ? "n/a" : instances[i].private_dns_name
    @log.warn "** New instance: #{fqdn_names[i]} | #{instances[i].id} | #{public_dns_name} | #{private_dns_name} | #{instances[i].private_ip_address}"
  end

  ##############################
  # ELB
  ##############################
  unless elb_name.nil?
    instances.each {|instance| attach_to_elb(instance, elb_name) }
  end

  ##############################
  # COMPLETED
  ##############################
  @log.info "********************"    
end

#launch_instance(hostname, ami_id, availability_zone, key_name, security_group_ids, 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_group_ids (Array<String>)

    list of security groups ids

  • 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.



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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
# File 'lib/ec2launcher.rb', line 567

def launch_instance(hostname, ami_id, availability_zone, key_name, security_group_ids, instance_type, user_data, block_device_mappings = nil, block_device_tags = nil, vpc_subnet = nil)
  @log.warn "Launching instance... #{hostname}"
  new_instance = nil
  run_with_backoff(30, 1, "launching instance") do
    if block_device_mappings.nil? || block_device_mappings.keys.empty?
      new_instance = @ec2.instances.create(
        :image_id => ami_id,
        :availability_zone => availability_zone,
        :key_name => key_name,
        :security_group_ids => security_group_ids,
        :user_data => user_data,
        :instance_type => instance_type,
        :subnet => vpc_subnet
      )
    else
      new_instance = @ec2.instances.create(
        :image_id => ami_id,
        :availability_zone => availability_zone,
        :key_name => key_name,
        :security_group_ids => security_group_ids,
        :user_data => user_data,
        :instance_type => instance_type,
        :block_device_mappings => block_device_mappings,
        :subnet => vpc_subnet
      )
    end
  end
  sleep 5

  @log.info "  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
    @log.fatal "Instance launch failed. Aborting."
    exit 5
  end

  ##############################
  # Tag instance
  @log.info "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?
    @log.info "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:

  • (EC2Launcher::Config)

    the parsed configuration object



642
643
644
645
646
647
# File 'lib/ec2launcher.rb', line 642

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)
  EC2Launcher::DSL::ConfigDSL.execute(File.read(config_filename)).config
end

#load_environment_file(name, 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)

    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:

  • (EC2Launcher::Environment)

    the new environment loaded from the specified file.



658
659
660
661
662
663
664
665
666
667
# File 'lib/ec2launcher.rb', line 658

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

  load_env = EC2Launcher::DSL::Environment.new
  load_env.load(File.read(name))
  load_env
end

#process_application_inheritance(app) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/ec2launcher.rb', line 699

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



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/ec2launcher.rb', line 678

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
      @log.warn "WARNING - #{name} directories not found: #{temp_dirs}"
    end
  end

  valid_directories
end

#process_environment_inheritance(env) ⇒ Object



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/ec2launcher.rb', line 717

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

#substitute_command_variables(cmd) ⇒ String

Given a string containing a command to run, replaces any inline variables. Supported variables include:

* @APPLICATION@ - name of the application
* @APP@ - name of the application
* @ENVIRONMENT@ - name of the environment
* @ENV@ - name of the environment
* @RUBY@ - Full pathname to the ruby executable
* @GEM@ - Full pathname to the gem executable
* @CHEF@ - Full pathname to the chef-client executable
* @KNIFE@ - Full pathname to the knife executable

Returns:

  • (String)

    command with variables replaced



747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/ec2launcher.rb', line 747

def substitute_command_variables(cmd)
  substitutions = {
    /@APPLICATION@/ => @application.name,
    /@APP@/ => @application.name,
    /@ENVIRONMENT@/ => @environment.name,
    /@ENV@/ => @environment.name,
    /@RUBY@/ => @instance_paths.ruby_path,
    /@GEM@/ => @instance_paths.gem_path,
    /@CHEF@/ => @instance_paths.chef_path,
    /@KNIFE@/ => @instance_paths.knife_path
  }
  substitutions.keys.each {|key| cmd.gsub!(key, substitutions[key]) }
  cmd
end

#validate_application(filename, application) ⇒ Object

Validates all settings in an application file

Parameters:



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

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:



782
783
784
785
786
# File 'lib/ec2launcher.rb', line 782

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