Revision 3a17103e src/vmm_mad/remotes/ec2/ec2_driver.rb

View differences:

src/vmm_mad/remotes/ec2/ec2_driver.rb
243 243

  
244 244
    # DEPLOY action, also sets ports and ip if needed
245 245
    def deploy(id, host, xml_text, lcm_state, deploy_id)
246
      if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE"
247
        ec2_info = get_deployment_info(host, xml_text)
246
        if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE"
247
            ec2_info = get_deployment_info(host, xml_text)
248 248

  
249
        load_default_template_values
249
            load_default_template_values
250 250

  
251
        if !ec2_value(ec2_info, 'AMI')
252
            STDERR.puts("Cannot find AMI in deployment file")
253
            exit(-1)
254
        end
251
            if !ec2_value(ec2_info, 'AMI')
252
                STDERR.puts("Cannot find AMI in deployment file")
253
                exit(-1)
254
            end
255 255

  
256
        opts = generate_options(:run, ec2_info, {
256
            opts = generate_options(:run, ec2_info, {
257 257
                :min_count => 1,
258 258
                :max_count => 1})
259 259

  
260
        # The OpenNebula context will be only included if not USERDATA
261
        #   is provided by the user
262
        if !ec2_value(ec2_info, 'USERDATA')
263
            xml = OpenNebula::XMLElement.new
264
            xml.initialize_xml(xml_text, 'VM')
265

  
266
            if xml.has_elements?('TEMPLATE/CONTEXT')
267
                # Since there is only 1 level ',' will not be added
268
                context_str = xml.template_like_str('TEMPLATE/CONTEXT')
269

  
270
                if xml['TEMPLATE/CONTEXT/TOKEN'] == 'YES'
271
                    # TODO use OneGate library
272
                    token_str = generate_onegate_token(xml)
273
                    if token_str
274
                        context_str << "\nONEGATE_TOKEN=\"#{token_str}\""
260
            # The OpenNebula context will be only included if not USERDATA
261
            #   is provided by the user
262
            if !ec2_value(ec2_info, 'USERDATA')
263
                xml = OpenNebula::XMLElement.new
264
                xml.initialize_xml(xml_text, 'VM')
265

  
266
                if xml.has_elements?('TEMPLATE/CONTEXT')
267
                    # Since there is only 1 level ',' will not be added
268
                    context_str = xml.template_like_str('TEMPLATE/CONTEXT')
269

  
270
                    if xml['TEMPLATE/CONTEXT/TOKEN'] == 'YES'
271
                        # TODO use OneGate library
272
                        token_str = generate_onegate_token(xml)
273
                        if token_str
274
                            context_str << "\nONEGATE_TOKEN=\"#{token_str}\""
275
                        end
275 276
                    end
276
                end
277 277

  
278
                userdata_key = EC2[:run][:args]["USERDATA"][:opt]
279
                opts[userdata_key] = Base64.encode64(context_str)
278
                    userdata_key = EC2[:run][:args]["USERDATA"][:opt]
279
                    opts[userdata_key] = Base64.encode64(context_str)
280
                end
280 281
            end
281
        end
282

  
283
        index = 0
284 282

  
285
        while index < 5
286 283
            begin
287
                instance.status
288
            rescue
289
            end
290
            sleep 2
291
            index = index + 1
292
        end
284
                instance = AWS.ec2.instances.create(opts)
285
            rescue => e
286
                exception_error = e.message.gsub("\n","; ")
293 287

  
294
        begin
295
            instance = AWS.ec2.instances.create(opts)
296
        rescue => e
297
            STDERR.puts(e.message)
298
            exit(-1)
299
        end
288
                instance_id   = instance.id rescue nil
289
                instance_id_s = instance_id || "nil"
300 290

  
301
        tags = generate_options(:tags, ec2_info)['tags'] || {}
291
                error_msg = "Failed instance create (instance_id=#{instance_id_s}): #{exception_error}"
292
                OpenNebula::log_error error_msg
293

  
294
                if instance_id
295
                    OpenNebula::log_error "Cancelling instance."
296
                    cancel(instance_id)
297
                end
302 298

  
303
        tags['ONE_ID'] = id
304
        tags.each{ |key,value|
305
            begin
306
                instance.add_tag(key, :value => value)
307
            rescue => e
308
                STDERR.puts(e.message)
309 299
                exit(-1)
310 300
            end
311
        }
312 301

  
313
        if ec2_value(ec2_info, 'ELASTICIP')
314
            begin
315
                start_time = Time.now
316
                while instance.status == :pending
317
                    break if Time.now - start_time > @state_change_timeout
318
                    sleep 5
302
            status_success = false
303
            index = 0
304
            while index < 5 && status_success == false
305
                begin
306
                    instance.status
307
                rescue
308
                    OpenNebula::log_info "Retrying status check"
309
                    sleep 2
310
                else
311
                    status_success = true
319 312
                end
320
                instance.associate_elastic_ip(ec2_value(ec2_info, 'ELASTICIP'))
321
            rescue => e
322
                STDERR.puts(e.message)
313
                index = index + 1
314
            end
315

  
316
            if !status_success
317
                OpenNebula::log_error "Giving up, could not get status. Trying to cancel instance"
318

  
319
                instance_id = instance.id rescue nil
320

  
321
                if instance_id
322
                    OpenNebula::log_error "Cancelling instance #{instance_id}"
323
                    cancel(instance_id)
324
                else
325
                    OpenNebula::log_error "Did not get an instance id."
326
                end
327

  
323 328
                exit(-1)
324 329
            end
325
        end
326 330

  
327
        puts(instance.id)
328
      else
329
        restore(deploy_id)
330
        deploy_id
331
      end
331
            tags = generate_options(:tags, ec2_info)['tags'] || {}
332

  
333
            tags['ONE_ID'] = id
334
            tags.each{ |key,value|
335
                begin
336
                    instance.add_tag(key, :value => value)
337
                rescue => e
338
                    STDERR.puts(e.message)
339
                    exit(-1)
340
                end
341
            }
342

  
343
            if ec2_value(ec2_info, 'ELASTICIP')
344
                begin
345
                    start_time = Time.now
346
                    while instance.status == :pending
347
                        break if Time.now - start_time > @state_change_timeout
348
                        sleep 5
349
                    end
350
                    instance.associate_elastic_ip(ec2_value(ec2_info, 'ELASTICIP'))
351
                rescue => e
352
                    STDERR.puts(e.message)
353
                    exit(-1)
354
                end
355
            end
356
            puts(instance.id)
357
        else
358
            restore(deploy_id)
359
            deploy_id
360
        end
332 361
    end
333 362

  
334 363
    # Shutdown a EC2 instance

Also available in: Unified diff