Method: CloudMaker::EC2#launch

Defined in:
lib/cloud_maker/ec2.rb

#launch(cloud_maker_config) ⇒ Object

Public: Launches a new EC2 instance, associates any specified elastic IPS with it, adds any specified tags, and archives the launch details to S3.

Returns an AWS::EC2 object for the launched instance.



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
# File 'lib/cloud_maker/ec2.rb', line 109

def launch(cloud_maker_config)
  user_data = cloud_maker_config.to_user_data

  if !cloud_maker_config['availability_zone'].nil? && !cloud_maker_config['availability_zone'].empty?
    region = find_region(cloud_maker_config['availability_zone'])
  else
    region = ec2 # .instances.create will just put things in the default region
  end

  config = {
    :image_id => cloud_maker_config['ami'],
    :iam_instance_profile => cloud_maker_config['iam_role'],
    :security_groups => cloud_maker_config['security_group'],
    :instance_type => cloud_maker_config['instance_type'],
    :key_name => cloud_maker_config['key_pair'],
    :availability_zone => cloud_maker_config['availability_zone'],
    :user_data => user_data
  }
  config[:block_device_mappings] = cloud_maker_config['block_device_mappings'] if cloud_maker_config['block_device_mappings']

  instance = region.instances.create(config)

  begin
    if cloud_maker_config['tags']
      # we can only set tags to be strings so make sure we convert all of our tags
      # to strings
      string_tags = cloud_maker_config['tags'].inject({}) do |hash, pair|
        hash[pair[0]] = pair[1].to_s
        hash
      end
      instance.tags.set(string_tags)
    end
  rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e
    retries ||= 0
    if retries < 5
      sleep(2**retries)
      retries += 1
      retry
    end
  end

  if cloud_maker_config.elastic_ip? || cloud_maker_config.cname?
    while instance.status == :pending
      #wait
    end
    instance.associate_elastic_ip(cloud_maker_config["elastic_ip"]) if cloud_maker_config.elastic_ip?

    if cloud_maker_config.cname?
      r53 = AWS::Route53::Client.new(:access_key_id => self.aws_access_key_id, :secret_access_key => self.aws_secret_access_key)

      zone = r53.list_hosted_zones[:hosted_zones].select {|zone|
        cloud_maker_config['cname'] + '.' =~ /#{Regexp.escape(zone[:name])}$/
      }.first

      r53.change_resource_record_sets(
        :hosted_zone_id => zone[:id],
        :change_batch => {
          :comment => "CloudMaker initialization of #{instance.instance_id}.", :changes => [{
            :action => "CREATE", :resource_record_set => {
              :name => cloud_maker_config['cname'],
              :type => 'CNAME',
              :ttl => 60,
              :resource_records => [{:value => instance.dns_name}]
            }
          }]
        }
      )
    end
  end

  if cloud_maker_config.elb?
    while instance.status == :pending
      #wait
    end
    elb_interface = ElbInterface.new(
      :instance => instance,
      :aws_access_key_id => self.aws_access_key_id,
      :aws_secret_access_key => self.aws_secret_access_key,
      :elb_name => cloud_maker_config["elb"]
    ).attach
  end
  
  archiver = S3Archiver.new(
    :instance_id => instance.id,
    :aws_access_key_id => self.aws_access_key_id,
    :aws_secret_access_key => self.aws_secret_access_key,
    :bucket_name => cloud_maker_config["tags"][BUCKET_TAG]
  )
  archiver.store_archive(cloud_maker_config, self.class.instance_to_hash(instance))

  instance
end