Revision 95313155

View differences:

src/vmm_mad/remotes/ec2/ec2_driver.rb
28 28
EC2_DRIVER_CONF = "#{ETC_LOCATION}/ec2_driver.conf"
29 29
EC2_DRIVER_DEFAULT = "#{ETC_LOCATION}/ec2_driver.default"
30 30

  
31
gem 'aws-sdk', '=1.66'
31
gem 'aws-sdk', '>= 2.0'
32 32

  
33 33
# Load EC2 credentials and environment
34 34
require 'yaml'
......
196 196

  
197 197
    # EC2 attributes that will be retrieved in a polling action
198 198
    EC2_POLL_ATTRS = [
199
        :dns_name,
199
        :public_dns_name,
200 200
        :private_dns_name,
201 201
        :key_name,
202
        :availability_zone,
202
        # not available as a method, should get placement/availability_zone
203
        # :availability_zone,
203 204
        :platform,
204 205
        :vpc_id,
205 206
        :private_ip_address,
206
        :ip_address,
207
        :public_ip_address,
207 208
        :subnet_id,
208 209
        :security_groups,
209 210
        :instance_type,
......
229 230
        raise "secret_access_key not defined for #{host}" if @region['secret_access_key'].nil?
230 231
        raise "region_name not defined for #{host}" if @region['region_name'].nil?
231 232

  
232
        AWS.config(
233
            'access_key_id'     => @region['access_key_id'],
234
            'secret_access_key' => @region['secret_access_key'],
235
            'region'            => @region['region_name'])
233
        Aws.config.merge!({
234
            :access_key_id      => @region['access_key_id'],
235
            :secret_access_key  => @region['secret_access_key'],
236
            :region             => @region['region_name']
237
        })
236 238

  
237 239
        if (proxy_uri = public_cloud_ec2_conf['proxy_uri'])
238
            AWS.config(:proxy_uri => proxy_uri)
240
            Aws.config(:proxy_uri => proxy_uri)
239 241
        end
240 242

  
241
        @ec2 = AWS.ec2
243
        @ec2 = Aws::EC2::Resource.new
242 244
    end
243 245

  
244 246
    # DEPLOY action, also sets ports and ip if needed
......
280 282
            end
281 283
        end
282 284

  
285
        begin
286
            instances = @ec2.create_instances(opts)
287
            instance = instances.first
288
        rescue => e
289
            STDERR.puts(e.message)
290
            exit(-1)
291
        end
292

  
283 293
        index = 0
284 294

  
285 295
        while index < 5
286 296
            begin
287
                instance.status
297
                instance.state
298
                break
288 299
            rescue
289 300
            end
290 301
            sleep 2
291 302
            index = index + 1
292 303
        end
293 304

  
294
        begin
295
            instance = AWS.ec2.instances.create(opts)
296
        rescue => e
297
            STDERR.puts(e.message)
298
            exit(-1)
299
        end
300

  
301 305
        tags = generate_options(:tags, ec2_info)['tags'] || {}
302 306

  
303 307
        tags['ONE_ID'] = id
308

  
309
        tag_array = []
304 310
        tags.each{ |key,value|
305
            begin
306
                instance.add_tag(key, :value => value)
307
            rescue => e
308
                STDERR.puts(e.message)
309
                exit(-1)
310
            end
311
            tag_array << {
312
                :key => key,
313
                :value => value
314
            }
311 315
        }
312 316

  
317
        begin
318
            instance.create_tags(:tags => tag_array)
319
        rescue => e
320
            STDERR.puts(e.message)
321
            exit(-1)
322
        end
323

  
313 324
        if ec2_value(ec2_info, 'ELASTICIP')
314 325
            begin
315 326
                start_time = Time.now
316
                while instance.status == :pending
327
                while instance.state.name == 'pending'
317 328
                    break if Time.now - start_time > @state_change_timeout
318 329
                    sleep 5
319 330
                end
......
421 432
        vpool.each{|vm| onevm_info[vm.deploy_id] = vm }
422 433

  
423 434
        begin
424
            AWS.ec2.instances.each do |i|
425
                next if i.status != :pending && i.status != :running
435
            @ec2.instances.each do |i|
436
                next if i.state.name != 'pending' && i.state.name != 'running'
426 437

  
427
                one_id = i.tags['ONE_ID']
438
                one_id = i.tags.find {|t| t.key == 'ONE_ID' }
439
                one_id = one_id.value if one_id
428 440

  
429
                poll_data=parse_poll(i, onevm_info[i.instance_id],do_cw, cw_mon_time)
441
                poll_data=parse_poll(i, onevm_info[i.id], do_cw, cw_mon_time)
430 442

  
431 443
                vm_template_to_one = vm_to_one(i)
432 444
                vm_template_to_one = Base64.encode64(vm_template_to_one).gsub("\n","")
......
521 533
    # Retrieve the vm information from the EC2 instance
522 534
    def parse_poll(instance, onevm, do_cw, cw_mon_time)
523 535
        begin
524
            onevm.info
525
            if onevm && do_cw
526
              cloudwatch_str = cloudwatch_monitor_info(instance.instance_id,
527
                                                       onevm,
528
                                                       cw_mon_time)
529
            else
530
              previous_cpu   = onevm["/VM/MONITORING/CPU"] ? onevm["/VM/MONITORING/CPU"] : 0
531
              previous_netrx = onevm["/VM/MONITORING/NETRX"] ? onevm["/VM/MONITORING/NETRX"] : 0
532
              previous_nettx = onevm["/VM/MONITORING/NETTX"] ? onevm["/VM/MONITORING/NETTX"] : 0
536
            if onevm
537
                if do_cw
538
                    onevm.info
539
                    cloudwatch_str = cloudwatch_monitor_info(instance.instance_id,
540
                                                           onevm,
541
                                                           cw_mon_time)
542
                else
543
                  previous_cpu   = onevm["/VM/MONITORING/CPU"] || 0
544
                  previous_netrx = onevm["/VM/MONITORING/NETRX"] || 0
545
                  previous_nettx = onevm["/VM/MONITORING/NETTX"] || 0
533 546

  
534
              cloudwatch_str = "CPU=#{previous_cpu} "\
535
                               "NETTX=#{previous_nettx} "\
536
                               "NETRX=#{previous_netrx} "
547
                  cloudwatch_str = "CPU=#{previous_cpu} "\
548
                                   "NETTX=#{previous_nettx} "\
549
                                   "NETRX=#{previous_netrx} "
550
                end
551
            else
552
                cloudwatch_str = ""
537 553
            end
538 554

  
539 555
            info =  "#{POLL_ATTRIBUTE[:memory]}=0 #{cloudwatch_str}"
......
542 558
            if !instance.exists?
543 559
                state = VM_STATE[:deleted]
544 560
            else
545
                state = case instance.status
546
                when :pending
561
                state = case instance.state.name
562
                when 'pending'
547 563
                    VM_STATE[:active]
548
                when :running
564
                when 'running'
549 565
                    VM_STATE[:active]
550
                when :'shutting-down', :terminated
566
                when 'shutting-down', 'terminated'
551 567
                    VM_STATE[:deleted]
552 568
                else
553 569
                    VM_STATE[:unknown]
......
560 576
                if !value.nil? && !value.empty?
561 577
                    if value.is_a?(Array)
562 578
                        value = value.map {|v|
563
                            v.security_group_id if v.is_a?(AWS::EC2::SecurityGroup)
579
                            v.group_id if v.is_a?(Aws::EC2::Types::GroupIdentifier)
564 580
                        }.join(",")
565 581
                    end
566 582

  
......
602 618
                if str
603 619
                    tmp = opts
604 620
                    last_key = nil
605
                    v[:opt].split('/').each { |k|
621
                    v[:opt].split('/').each { |key|
622
                        k = key.to_sym
606 623
                        tmp = tmp[last_key] if last_key
607 624
                        tmp[k] = {}
608 625
                        last_key = k
......
642 659
    def load_default_template_values
643 660
        @defaults = Hash.new
644 661

  
645
        if File.exists?(EC2_DRIVER_DEFAULT)
662
        if File.exist?(EC2_DRIVER_DEFAULT)
646 663
            fd  = File.new(EC2_DRIVER_DEFAULT)
647 664
            xml = REXML::Document.new fd
648 665
            fd.close()
......
656 673
            EC2.each {|action, hash|
657 674
                if hash[:args]
658 675
                    hash[:args].each { |key, value|
659
                        @defaults[key] = value_from_xml(ec2, key)
676
                        @defaults[key.to_sym] = value_from_xml(ec2, key)
660 677
                    }
661 678
                end
662 679
            }
......
666 683
    # Retrive the instance from EC2
667 684
    def get_instance(id)
668 685
        begin
669
            instance = AWS.ec2.instances[id]
686
            instance = @ec2.instance(id)
670 687
            if instance.exists?
671 688
                return instance
672 689
            else
......
706 723
    # Extract monitoring information from Cloud Watch
707 724
    # CPU, NETTX and NETRX
708 725
    def cloudwatch_monitor_info(id, onevm, cw_mon_time)
709
        cw=AWS::CloudWatch::Client.new
726
        cw=Aws::CloudWatch::Client.new
710 727

  
711 728
        # CPU
712 729
        begin

Also available in: Unified diff