Class: EC2Launcher::Launcher

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

Instance Method Summary collapse

Methods included from BackoffRunner

#run_with_backoff, #test_with_backoff

Methods included from AWSInitializer

#initialize_aws

Constructor Details

#initializeLauncher

Returns a new instance of Launcher.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ec2launcher.rb', line 41

def initialize()
  spec = Gem::Specification.find_by_name("ec2launcher")
  @gem_install_dir = spec.gem_dir
  @startup_scripts_dir = File.join(@gem_install_dir, "startup-scripts")
  @run_url_script_cache = nil
  @setup_script_cache = nil
  @setup_instance_script_cache = nil

  begin
    @log = Log4r::Logger['ec2launcher']
    unless @log
      @log = Log4r::Logger.new 'ec2launcher'
      log_output = Log4r::Outputter.stdout
      log_output.formatter = PatternFormatter.new :pattern => "%m"
      @log.outputters = log_output
    end
  rescue
  end
end

Instance Method Details

#attach_to_elb(instance, elb_name, subnet = nil) ⇒ Object

Attaches an instance to the specified ELB.

Parameters:

  • instance (AWS::EC2::Instance)

    newly created EC2 instance.

  • elb_name (String)

    name of ELB.

  • subnet (String) (defaults to: nil)

    subnet name or id. Defaults to nil.



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
532
533
534
535
# File 'lib/ec2launcher.rb', line 499

def attach_to_elb(instance, elb_name, subnet = nil)
  begin
    @log.info ""
    @log.info "Adding to ELB: #{elb_name}"
    elb = AWS::ELB.new
    AWS.memoize do
      unless subnet
        # 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
      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_commands(commands) ⇒ String

Builds a bash script snipp containing a list of commands to execute.

Parameters:

  • commands (Array<String>)

    List of commands to run. Can be nil.

Returns:

  • (String)

    String containing newline separated bash commands to run or an empty string if no commands.



910
911
912
913
914
915
916
917
# File 'lib/ec2launcher.rb', line 910

def build_commands(commands)
  command_str = ""
  unless commands.nil? || commands.empty?
    processed_commands = commands.collect {|cmd| substitute_command_variables(cmd) }
    command_str = "\n" + processed_commands.join("\n") + "\n"
  end
  command_str
end

#build_launch_command(launch_options) ⇒ String

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

launch_options =

:ami
:availability_zone
:aws_keyfile
:block_device_mappings
:block_device_tags
:chef_validation_pem_url
:email_notifications
:fqdn
:gems
:iam_profile
:instance_type
:key
:packages
:roles
:security_group_ids
:short_name
:subnet

Returns:

  • (String)

    Launch commands to pass into new instance as userdata



782
783
784
785
786
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
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/ec2launcher.rb', line 782

def build_launch_command(launch_options)
  # Build JSON for setup scripts

  # Require ec2launcher gem if cloning and using provisioned IOPS
  setup_json = {
    'hostname' => launch_options[:fqdn],
    'short_hostname' => launch_options[:short_name],
    'block_device_mappings' => launch_options[:block_device_mappings],
    'roles' => launch_options[:roles],
    'chef_server_url' => @environment.chef_server_url,
    'chef_validation_pem_url' => launch_options[:chef_validation_pem_url],
    'aws_keyfile' => launch_options[:aws_keyfile],
    'gems' => launch_options[:gems],
    'packages' => launch_options[:packages],
    'provisioned_iops' => false,
    'dynamic_name' => @options.dynamic_name,
    'domain_name' => @environment.domain_name
  }
  if @options.dynamic_name
    # setup_json['dynamic_name_prefix'] = @hostname_generator.prefix
    setup_json['dynamic_name_suffix'] = "#{@hostname_generator.prefix}.#{@hostname_generator.suffix}"
    setup_json['route53_zone_id'] = @route53_zone_id
  end

  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

    @application.block_devices.each do |bd|
      if bd.provisioned_iops?
        setup_json['provisioned_iops'] = true
        break
      end
    end
  end
  unless launch_options[:email_notifications].nil?
    setup_json['email_notifications'] = launch_options[:email_notifications]
  end

  ##############################
  # Build launch command
  json_text = @options.pretty_print ? JSON.pretty_generate(setup_json) : setup_json.to_json
  user_data = <<EOF
#!/bin/bash
cat > /tmp/setup.json <<End-Of-Message-JSON
#{json_text}
End-Of-Message-JSON
EOF
  if @environment.use_rvm or @application.use_rvm
    user_data += <<-EOF
export HOME=/root
if [[ -s "/etc/profile.d/rvm.sh" ]] ; then
  source "/etc/profile.d/rvm.sh"
fi
EOF
  end

  # pre-commands, if necessary
  user_data += build_commands(@environment.precommand)
  user_data += build_commands(@application.precommand)

  unless @options.skip_setup
    if @run_url_script_cache.nil?
      @run_url_script_cache = load_and_encode_file_with_path(@startup_scripts_dir, "runurl")
    end

    if @setup_script_cache.nil?
      @setup_script_cache = load_and_encode_file_with_path(@startup_scripts_dir, "setup.rb")
    end

    if @setup_instance_script_cache.nil?
      @setup_instance_script_cache = load_and_encode_file_with_path(@startup_scripts_dir, "setup_instance.rb")
    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 scripts
    user_data += "\ncat > /tmp/setup.rb.gz.base64 <<End-Of-Message2\n"
    user_data += @setup_script_cache
    user_data += "End-Of-Message2"

    user_data += "\ncat > /tmp/setup_instance.rb.gz.base64 <<End-Of-Message3\n"
    user_data += @setup_instance_script_cache
    user_data += "End-Of-Message3"

    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 += "\nbase64 -d /tmp/setup_instance.rb.gz.base64 | gunzip > /tmp/setup_instance.rb"
    user_data += "\nchmod +x /tmp/setup_instance.rb"
    user_data += "\nrm -f /tmp/setup_instance.rb.gz.base64"

    user_data += "\ngem install ec2launcher --no-ri --no-rdoc"

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

  # Add extra requested commands to the launch sequence
  user_data += build_commands(@options.commands)

  # Post commands
  user_data += build_commands(@environment.postcommand)
  user_data += build_commands(@application.postcommand)

  user_data
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:



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
587
588
# File 'lib/ec2launcher.rb', line 545

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

#get_instance_dns(ec2_instance, public = true) ⇒ Object



599
600
601
602
# File 'lib/ec2launcher.rb', line 599

def get_instance_dns(ec2_instance, public = true)
  dns_name = public ? ec2_instance.public_dns_name : ec2_instance.private_dns_name
  dns_name.nil? ? "n/a" : dns_name
end

#launch(options) ⇒ Object



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
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
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
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
# File 'lib/ec2launcher.rb', line 62

def launch(options)
  @options = options

  @log.info "ec2launcher v#{EC2Launcher::VERSION}"

  @log.level = case @options.verbosity 
    when :quiet then WARN
    when :verbose then DEBUG
    else INFO
  end
  
  # Load configuration data
  config_wrapper = ConfigWrapper.new(@options.directory)

  @config = config_wrapper.config
  @environments = config_wrapper.environments
  @applications = config_wrapper.applications

  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(@options.access_key, @options.secret)
  @ec2 = AWS::EC2.new

  ##############################
  # Create Route53 connection
  ##############################
  @route53 = nil
  @route53_zone_id = nil
  @route53_domain_name = nil
  if @environment.route53_zone_id
    aws_route53 = AWS::Route53.new 
    @route53 = EC2Launcher::Route53.new(aws_route53, @environment.route53_zone_id, @log)
    @route53_zone_id = @environment.route53_zone_id
    route53_zone = aws_route53.client.get_hosted_zone({:id => @environment.route53_zone_id})
    @route53_domain_name = route53_zone[:hosted_zone][:name].chop
  end

  ##############################
  # 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|
    next if ec2_subnet.nil? && sg.vpc_id
    next if ec2_subnet && ec2_subnet.vpc_id != sg.vpc_id
    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|
    if sg_map.has_key?(sg_name)
      security_group_ids << sg_map[sg_name].security_group_id
    else
      missing_security_groups << sg_name
    end
  end

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

  ##############################
  # IAM PROFILE
  ##############################
  iam_profile = @application.iam_profile_for_environment(@environment.name)
  iam_profile ||= @environment.iam_profile

  ##############################
  # 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 = nil
  run_with_backoff(60, 1, "searching for ami") do
    ami = find_ami(instance_architecture, instance_virtualization, ami_name_match, @options.ami_id)
  end

  ##############################
  # DOMAIN NAME
  ##############################

  # Note: Route53 domain names override domain names specified in the environments
  @domain_name = @route53_domain_name
  @domain_name ||= @environment.domain_name

  ##############################
  # HOSTNAME
  ##############################
  @hostname_generator = EC2Launcher::HostnameGenerator.new(@ec2, @environment, @application)
  short_hostnames = []
  fqdn_names = []
  unless @options.dynamic_name
    if @options.count > 1
      1.upto(@options.count).each do |i|
        short_hostname = @hostname_generator.generate_hostname()
        long_hostname = @hostname_generator.generate_fqdn(short_hostname, @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_fqdn(short_hostname, @domain_name)
      else
        long_hostname = @options.hostname
        short_hostname = @hostname_generator.generate_short_name(long_hostname, @environment.domain_name)
        if long_hostname == short_hostname
          long_hostname = @hostname_generator.generate_fqdn(short_hostname, @environment.domain_name)
        end
      end
      short_hostnames << short_hostname
      fqdn_names << long_hostname
    end
  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?
  gems << {"name" => "ec2launcher", "version" => VERSION}

  ##############################
  # 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 "IAM profile         : #{iam_profile}" if iam_profile
  @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 "Route53 Zone        : #{@route53_domain_name}" if @route53_domain_name
  @log.info "Chef PEM            : #{chef_validation_pem_url}"
  @log.info "AWS key file        : #{aws_keyfile}"
  @log.info "Roles               : #{roles.join(', ')}"
  
  gem_list = []
  gems.each do |gem_info|
    if gem_info.kind_of? Hash
      gem_name = gem_info[:name]
      gem_version = gem_info[:version]
      next unless gem_name

      gem_details = gem_name
      gem_details += " (#{gem_version})" if gem_version

      gem_list << gem_details
    else
      gem_list << gem_info
    end
  end
  @log.info "Gems                : #{gem_list.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 ""
  if @options.dynamic_name
      @log.info "Name                : **dynamic**"
  else
    fqdn_names.each do |fqdn|
      @log.info "Name                : #{fqdn}"
    end
  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
          block_device_text = "  Block device   : #{key}, #{block_device_mappings[key][:volume_size]}GB, "
          block_device_text += "#{block_device_mappings[key][:snapshot_id]}" if block_device_mappings[key][:snapshot_id]
          block_device_text += ", (#{block_device_mappings[key][:delete_on_termination] ? 'auto-delete' : 'no delete'}), "
          block_device_text += "(#{block_device_mappings[key][:iops].nil? ? 'standard' : block_device_mappings[key][:iops].to_s} IOPS)"
          @log.info block_device_text
      end
    end
  end

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

  # Launch options
  launch_options = {
    :ami => ami.ami_id,
    :availability_zone => availability_zone,
    :aws_keyfile => aws_keyfile,
    :block_device_mappings => block_device_mappings,
    :chef_validation_pem_url => chef_validation_pem_url,
    :email_notifications => email_notifications,
    :environment => @environment.name,
    :gems => gems, 
    :iam_profile => iam_profile,
    :instance_type => instance_type,
    :key => key_name,
    :packages => packages,
    :provisioned_iops => @application.has_provisioned_iops?(),
    :roles => roles, 
    :security_group_ids => security_group_ids,
    :subnet => subnet
  }

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

  if @options.show_user_data
    custom_launch_options = launch_options
    unless @options.dynamic_name
      custom_launch_options = launch_options.merge({
        :fqdn => fqdn_names[0],
        :short_name => short_hostnames[0]
      })
    end

    user_data = build_launch_command(custom_launch_options)

    @log.info ""
    @log.info "---user-data---"
    @log.info user_data
    @log.info "---user-data---"

    exit 0
  end

  ##############################
  # Launch the new intance
  ##############################
  @log.warn ""
  instances = []
  new_instance_names = []
  0.upto(@options.count - 1).each do |i|
    fqdn = nil
    short_hostname = nil

    unless @options.dynamic_name
      fqdn = fqdn_names[i]
      short_hostname = short_hostnames[i]
    end

    block_device_tags = @block_device_builder.generate_device_tags(fqdn, short_hostname, @environment.name, @application.block_devices)
    launch_options.merge!({
      :fqdn => fqdn,
      :short_name => short_hostname,
      :block_device_tags => block_device_tags,
    })
    user_data = build_launch_command(launch_options)

    instance = launch_instance(launch_options, user_data)
    instances << instance

    if @options.dynamic_name
      short_hostname = @hostname_generator.generate_dynamic_hostname(instance.id)
      fqdn = @hostname_generator.generate_fqdn(short_hostname, @environment.domain_name)
    end

    public_dns_name = get_instance_dns(instance, true)
    private_dns_name = get_instance_dns(instance, false)
    @log.info "Launched #{fqdn} (#{instance.id}) [#{public_dns_name} / #{private_dns_name} / #{instance.private_ip_address} ]"

    new_instance_names << fqdn
  end

  @log.info "********************"    
  new_instance_names.each_index do |i|
    public_dns_name = get_instance_dns(instances[i], true)
    private_dns_name = get_instance_dns(instances[i], false)
    @log.warn "** New instance: #{new_instance_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, ec2_subnet) }
  end

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

#launch_instance(launch_options, user_data) ⇒ Object



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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/ec2launcher.rb', line 628

def launch_instance(launch_options, user_data)
  @log.warn "Launching instance... #{launch_options[:fqdn]}"
  new_instance = nil
  run_with_backoff(30, 1, "launching instance") do
    launch_mapping = {
        :image_id => launch_options[:ami],
        :availability_zone => launch_options[:availability_zone],
        :key_name => launch_options[:key],
        :security_group_ids => launch_options[:security_group_ids],
        :user_data => user_data,
        :instance_type => launch_options[:instance_type]
    }
    unless launch_options[:block_device_mappings].nil? || launch_options[:block_device_mappings].keys.empty?
      if launch_options[:provisioned_iops]
        # Only include ephemeral devices if we're using provisioned IOPS for the EBS volumes
        launch_mapping[:block_device_mappings] = {}
        launch_options[:block_device_mappings].keys.sort.each do |block_device_name|
          if launch_options[:block_device_mappings][block_device_name] =~ /^ephemeral/
            launch_mapping[:block_device_mappings][block_device_name] = launch_options[:block_device_mappings][block_device_name]
          end
        end
      else
        launch_mapping[:block_device_mappings] = launch_options[:block_device_mappings]
      end

      # Remove the block_device_mappings entry if it's empty. Otherwise the AWS API will throw an error.
      launch_mapping.delete(:block_device_mappings) if launch_mapping[:block_device_mappings].keys.empty?
    end

    launch_mapping[:iam_instance_profile] = launch_options[:iam_profile] if launch_options[:iam_profile]
    launch_mapping[:subnet] = launch_options[:subnet] if launch_options[:subnet]

    new_instance = @ec2.instances.create(launch_mapping)
  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

  if @options.dynamic_name
    launch_options[:short_name] = @hostname_generator.generate_dynamic_hostname(new_instance.id)
    launch_options[:fqdn] = @hostname_generator.generate_fqdn(launch_options[:short_name], @environment.domain_name)
    launch_options[:block_device_tags] = @block_device_builder.generate_device_tags(launch_options[:fqdn], launch_options[:short_name], @environment.name, @application.block_devices)
  end

  ##############################
  # Tag instance
  @log.info "Tagging instance..."
  run_with_backoff(30, 1, "tag #{new_instance.id}, tag: name, value: #{launch_options[:fqdn]}") { new_instance.add_tag("Name", :value => launch_options[:fqdn]) }
  run_with_backoff(30, 1, "tag #{new_instance.id}, tag: short_name, value: #{launch_options[:short_name]}") { new_instance.add_tag("short_name", :value => launch_options[:short_name]) }
  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) }
  if @options.clone_host
    run_with_backoff(30, 1, "tag #{new_instance.id}, tag: cloned_from, value: #{@options.clone_host}") { new_instance.add_tag("cloned_from", :value => @options.clone_host) }
  end

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

  ##############################
  # Add to Route53
  if @route53
    @log.info "Adding A record to Route53: #{launch_options[:fqdn]} => #{new_instance.private_ip_address}"
    @route53.create_record(launch_options[:fqdn], new_instance.private_ip_address, 'A')
  end

  new_instance
end

#load_and_encode_file(pathname) ⇒ Object



750
751
752
# File 'lib/ec2launcher.rb', line 750

def load_and_encode_file(pathname)
  `cat #{pathname} |sed '/^[ \t]*$/d'|sed '/^[ \t]*#/d'|gzip -f |base64`
end

#load_and_encode_file_with_path(base_path, filename) ⇒ Object



754
755
756
757
# File 'lib/ec2launcher.rb', line 754

def load_and_encode_file_with_path(base_path, filename)
  pathname = File.join(base_path, filename)
  load_and_encode_file(pathname)
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



735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/ec2launcher.rb', line 735

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