Revision 4944a4ae

View differences:

src/onedb/vcenter_one54_pre.rb
16 16
# limitations under the License.                                             #
17 17
#--------------------------------------------------------------------------- #
18 18

  
19
#!/usr/bin/env ruby
20

  
21
ONE_LOCATION=ENV["ONE_LOCATION"]
19
ONE_LOCATION = ENV["ONE_LOCATION"]
22 20

  
23 21
if !ONE_LOCATION
24
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
25
    REMOTES_LOCATION="/var/lib/one/remotes/"
22
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
23
    REMOTES_LOCATION  = "/var/lib/one/remotes/"
26 24
else
27
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
28
    REMOTES_LOCATION=ONE_LOCATION+"/var/remotes/"
25
    RUBY_LIB_LOCATION = ONE_LOCATION+"/lib/ruby"
26
    REMOTES_LOCATION  = ONE_LOCATION+"/var/remotes/"
29 27
end
30 28

  
31 29
$: << RUBY_LIB_LOCATION
......
41 39
require 'opennebula'
42 40

  
43 41
TEMP_DIR="/var/tmp/vcenter_one54"
42

  
44 43
FileUtils.mkdir_p TEMP_DIR
45 44

  
46 45
def banner(msg, header=false, extended=nil)
......
257 256
    raise rc.message if OpenNebula.is_error?(rc)
258 257

  
259 258
    STDOUT.puts "Datastore \e[96m#{ds_name}\e[39m is now also a SYSTEM datastore with ID: #{one_ds["ID"]}\n"
259
    one_ds
260 260
end
261 261

  
262 262
def get_dc(item)
......
362 362

  
363 363
    cluster_id = one_clusters[host_id]
364 364

  
365
    devices.each do |device|
366
        rc = vnpool.info_all
367
        raise "\n    ERROR! Could not update vnpool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
368

  
369
        rc = ipool.info_all
370
        raise "\n    ERROR! Could not update ipool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
371

  
372
        rc = dspool.info
373
        raise "\n    ERROR! Could not update dspool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
374

  
375
        if defined?(RbVmomi::VIM::VirtualIDEController) &&
376
           device.is_a?(RbVmomi::VIM::VirtualIDEController)
377
            ide_controlled += device.device
378
            next
379
        end
380

  
381
        if defined?(RbVmomi::VIM::VirtualSATAController) &&
382
           device.is_a?(RbVmomi::VIM::VirtualSATAController)
383
            sata_controlled += device.device
384
            next
385
        end
386

  
387
        if defined?(RbVmomi::VIM::VirtualSCSIController) &&
388
           device.is_a?(RbVmomi::VIM::VirtualSCSIController)
389
            scsi_controlled += device.device
390
            next
391
        end
365
    if !vm_wild && template_xml
366
        devices.each do |device|
367
            rc = vnpool.info_all
368
            raise "\n    ERROR! Could not update vnpool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
392 369

  
393
        #cluster_id = xml_doc.root.xpath("HISTORY_RECORDS/HISTORY[last()]/CID").text
370
            rc = ipool.info_all
371
            raise "\n    ERROR! Could not update ipool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
394 372

  
395
        # If CDROM
396
        if !(device.class.ancestors.index(RbVmomi::VIM::VirtualCdrom)).nil?
397
            ds_name       = device.backing.datastore.name
398
            ds_ref        = device.backing.datastore._ref
399
            image_path    = device.backing.fileName.sub(/^\[(.*?)\] /, "")
373
            rc = dspool.info
374
            raise "\n    ERROR! Could not update dspool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
400 375

  
401
            image = find_image(ipool, ds_name, image_path)
376
            if defined?(RbVmomi::VIM::VirtualIDEController) &&
377
               device.is_a?(RbVmomi::VIM::VirtualIDEController)
378
                ide_controlled += device.device
379
                next
380
            end
402 381

  
403
            if image
404
                # It's a persistent disk if it already exists
405
                xml_template     = xml_doc.root.at_xpath("TEMPLATE")
406
                existing_disk    = existing_disks[managed_disk_index]
382
            if defined?(RbVmomi::VIM::VirtualSATAController) &&
383
               device.is_a?(RbVmomi::VIM::VirtualSATAController)
384
                sata_controlled += device.device
385
                next
386
            end
407 387

  
408
                # Replace DISK_ID
409
                existind_disk_id = existing_disk.at_xpath("DISK_ID")
410
                existind_disk_id.content = disk_index
388
            if defined?(RbVmomi::VIM::VirtualSCSIController) &&
389
               device.is_a?(RbVmomi::VIM::VirtualSCSIController)
390
                scsi_controlled += device.device
391
                next
392
            end
411 393

  
412
                disk = xml_template.add_child(existing_disks[managed_disk_index])
394
            #cluster_id = xml_doc.root.xpath("HISTORY_RECORDS/HISTORY[last()]/CID").text
413 395

  
414
                # Add VCENTER_DS_REF
415
                disk.add_child(xml_doc.create_element("VCENTER_DS_REF")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{ds_ref}"))
396
            # If CDROM
397
            if !(device.class.ancestors.index(RbVmomi::VIM::VirtualCdrom)).nil?
398
                device_backing_datastore = device.backing.datastore rescue nil
399
                if device_backing_datastore
400
                    ds_name       = device.backing.datastore.name
401
                    ds_ref        = device.backing.datastore._ref
402
                    image_path    = device.backing.fileName.sub(/^\[(.*?)\] /, "")
416 403

  
417
                STDOUT.puts "--- Added VCENTER_DS_REF=#{ds_ref} to CDROM (IMAGE_ID=#{image["ID"]})"
404
                    image = find_image(ipool, ds_name, image_path)
418 405

  
419
                # Update indexes
420
                managed_disk_index = managed_disk_index + 1
421
                disk_index = disk_index + 1
422
            end
423
        end
406
                    if image
407
                        # It's a persistent disk if it already exists
408
                        xml_template     = xml_doc.root.at_xpath("TEMPLATE")
409
                        existing_disk    = existing_disks[managed_disk_index]
424 410

  
425
        # If Virtual Disk
426
        if !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
427
            ds_name       = device.backing.datastore.name
428
            ds_ref        = device.backing.datastore._ref
429
            image_type    = "OS"
430
            image_path    = device.backing.fileName.sub(/^\[(.*?)\] /, "")
431
            image_prefix  = "hd" if ide_controlled.include?(device.key)
432
            image_prefix  = "sd" if scsi_controlled.include?(device.key)
433
            image_prefix  = "sd" if sata_controlled.include?(device.key)
434
            file_name     = File.basename(image_path).gsub(/\.vmdk$/,"")
435
            image_name    = "#{file_name} - #{ds_name}"
411
                        # Replace DISK_ID
412
                        existind_disk_id = existing_disk.at_xpath("DISK_ID")
413
                        existind_disk_id.content = disk_index
436 414

  
437
            #Check if the image already exists
438
            one_image = find_image(ipool, ds_name, image_path)
415
                        disk = xml_template.add_child(existing_disks[managed_disk_index])
439 416

  
440
            if !one_image
441
                #Check if the IMAGE DS is there
442
                ds = find_datastore(dspool, ds_ref, dc_ref, vcenter_uuid, "IMAGE_DS")
417
                        # Add VCENTER_DS_REF
418
                        disk.add_child(xml_doc.create_element("VCENTER_DS_REF")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{ds_ref}"))
443 419

  
444
                #Create IMAGE and SYSTEM DS if datastore is not found
445
                if !ds
446
                    ds = create_image_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, ccr_name, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
420
                        STDOUT.puts "--- Added VCENTER_DS_REF=#{ds_ref} to CDROM (IMAGE_ID=#{image["ID"]})"
447 421

  
448
                    create_system_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
422
                        # Update indexes
423
                        managed_disk_index = managed_disk_index + 1
424
                        disk_index = disk_index + 1
425
                    end
449 426
                end
427
            end
450 428

  
451
                ds_id = ds["ID"].to_i
452

  
453
                if vm_wild || !template_xml
454
                    #Create images for unmanaged disks
455

  
456
                    template = ""
457
                    template << "NAME=\"#{image_name}\"\n"
458
                    template << "PATH=\"vcenter://#{image_path}\"\n"
459
                    template << "TYPE=\"#{image_type}\"\n"
460
                    template << "PERSISTENT=\"NO\"\n"
461
                    template << "VCENTER_IMPORTED=\"YES\"\n"
462
                    template << "DEV_PREFIX=\"#{image_prefix}\"\n"
463

  
464
                    one_image = OpenNebula::Image.new(OpenNebula::Image.build_xml, one_client)
465
                    rc = one_image.allocate(template, ds_id)
466
                    raise "\n    ERROR! Could not create image for wild vm. Reason #{rc.message}" if OpenNebula.is_error?(rc)
429
            # If Virtual Disk
430
            if !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
431
                ds_name       = device.backing.datastore.name
432
                ds_ref        = device.backing.datastore._ref
433
                image_type    = "OS"
434
                image_path    = device.backing.fileName.sub(/^\[(.*?)\] /, "")
435
                image_prefix  = "hd" if ide_controlled.include?(device.key)
436
                image_prefix  = "sd" if scsi_controlled.include?(device.key)
437
                image_prefix  = "sd" if sata_controlled.include?(device.key)
438
                file_name     = File.basename(image_path).gsub(/\.vmdk$/,"")
439
                image_name    = "#{file_name} - #{ds_name}"
440

  
441
                #Check if the image already exists
442
                one_image = find_image(ipool, ds_name, image_path)
443

  
444
                if !one_image
445
                    #Check if the IMAGE DS is there
446
                    ds = find_datastore(dspool, ds_ref, dc_ref, vcenter_uuid, "IMAGE_DS")
467 447

  
468
                    loop do
469
                        rc = one_image.info
470
                        raise "\n    ERROR! Could not get image info for wild vm disk. Reason #{rc.message}" if OpenNebula.is_error?(rc)
471
                        break if one_image["SOURCE"] && !one_image["SOURCE"].empty? #Get out of loop if image is not locked
472
                        sleep(1)
473
                    end
448
                    #Create IMAGE and SYSTEM DS if datastore is not found
449
                    if !ds
450
                        ds = create_image_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, ccr_name, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
474 451

  
475
                    if one_image["STATE"] == 5
476
                        raise "\n    ERROR! The image created for wild vm is in ERROR state"
452
                        create_system_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
477 453
                    end
478 454

  
479
                    vcenter_ids[:image] << one_image["ID"]
455
                    ds_id = ds["ID"].to_i
480 456

  
481
                    STDOUT.puts "--- Image #{one_image["NAME"]} with ID #{one_image["ID"]} has been created"
482
                else
483 457
                    template_disk = template_xml.xpath("VMTEMPLATE/TEMPLATE/DISK")[unmanaged_disk_index] rescue nil
484 458
                    raise "Cannot find unmanaged disk inside template" if !template_disk
485 459

  
......
499 473
                    ds_name = ds["NAME"]
500 474

  
501 475
                    STDOUT.puts "--- Image #{one_image["NAME"]} with ID #{one_image["ID"]} already exists"
502
                end
503

  
504
                # Create unmanaged disk element for vm template
505
                # Get disk size (capacity)
506
                image_name   = one_image["NAME"]
507
                image_source = one_image["SOURCE"]
508
                image_id     = one_image["ID"]
509

  
510
                # Add new disk attributes
511
                create_disk(xml_doc, image_name, image_source, image_prefix, image_id,
512
                            disk_index, cluster_id, ds, ds_ref, ds_name, vi_client)
513

  
514
                STDOUT.puts "--- Added unmanaged disk to xml template (IMAGE_ID=#{one_image["ID"]})"
515

  
516
                reference = {}
517
                reference[:key]   = "opennebula.disk.#{unmanaged_disk_index}"
518
                reference[:value] = "#{device.key}"
519
                extraconfig << reference
520

  
521
                unmanaged_disk_index = unmanaged_disk_index + 1
522
                disk_index = disk_index + 1
523
            else
524

  
525
                if one_image["TEMPLATE/VCENTER_IMPORTED"] == "YES"
526

  
527
                    #Check if the IMAGE DS is there
528
                    ds = find_datastore(dspool, ds_ref, dc_ref, vcenter_uuid, "IMAGE_DS")
529

  
530
                    #Create IMAGE and SYSTEM DS if datastore is not found
531
                    if !ds
532
                        ds = create_image_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, ccr_name, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
533

  
534
                        create_system_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
535
                    end
536 476

  
537
                    ds_id = ds["ID"].to_i
538

  
539
                    #Create unmanaged disk element for vm template
540
                    image_id     = one_image["ID"]
477
                    # Create unmanaged disk element for vm template
478
                    # Get disk size (capacity)
541 479
                    image_name   = one_image["NAME"]
542 480
                    image_source = one_image["SOURCE"]
481
                    image_id     = one_image["ID"]
543 482

  
483
                    # Add new disk attributes
544 484
                    create_disk(xml_doc, image_name, image_source, image_prefix, image_id,
545 485
                                disk_index, cluster_id, ds, ds_ref, ds_name, vi_client)
546 486

  
547
                    STDOUT.puts "--- Added unmanaged disk in wild vm (IMAGE_ID=#{one_image["ID"]})"
487
                    STDOUT.puts "--- Added unmanaged disk to xml template (IMAGE_ID=#{one_image["ID"]})"
548 488

  
549 489
                    reference = {}
550 490
                    reference[:key]   = "opennebula.disk.#{unmanaged_disk_index}"
551 491
                    reference[:value] = "#{device.key}"
552 492
                    extraconfig << reference
553 493

  
554
                    # Update indexes
555 494
                    unmanaged_disk_index = unmanaged_disk_index + 1
556 495
                    disk_index = disk_index + 1
557

  
558 496
                else
559
                    # It's a persistent disk if it already exists
560
                    xml_template     = xml_doc.root.at_xpath("TEMPLATE")
561
                    existing_disk    = existing_disks[managed_disk_index]
497
                    if one_image["TEMPLATE/VCENTER_IMPORTED"] == "YES"
498
                        # This is (probably) a wild VM. The code should not reach this.
562 499

  
563
                    # Replace DISK_ID
564
                    existing_disk_image_id = existing_disk.xpath("IMAGE_ID").text
565
                    existing_disk_id = existing_disk.at_xpath("DISK_ID")
566
                    existing_disk_id.content = disk_index
500
                        #Check if the IMAGE DS is there
501
                        ds = find_datastore(dspool, ds_ref, dc_ref, vcenter_uuid, "IMAGE_DS")
567 502

  
568
                    disk = xml_template.add_child(existing_disks[managed_disk_index])
503
                        #Create IMAGE and SYSTEM DS if datastore is not found
504
                        if !ds
505
                            ds = create_image_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, ccr_name, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
569 506

  
570
                    # Add VCENTER_DISK_TYPE and VCENTER_ADAPTER_TYPE if found
571
                    if !existing_disk.xpath("DISK_TYPE").text.empty?
572
                        disk.add_child(xml_doc.create_element("VCENTER_DISK_TYPE")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{existing_disk.xpath("DISK_TYPE").text}"))
573
                        STDOUT.puts "--- Added VCENTER_DISK_TYPE=#{existing_disk.xpath("DISK_TYPE").text} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
574
                    end
507
                            create_system_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
508
                        end
575 509

  
576
                    if !existing_disk.xpath("ADAPTER_TYPE").text.empty?
577
                        disk.add_child(xml_doc.create_element("VCENTER_ADAPTER_TYPE")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{existing_disk.xpath("ADAPTER_TYPE").text}"))
578
                        STDOUT.puts "--- Added VCENTER_ADAPTER_TYPE=#{existing_disk.xpath("ADAPTER_TYPE").text} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
579
                    end
510
                        ds_id = ds["ID"].to_i
580 511

  
581
                    # Add VCENTER_DS_REF
582
                    disk.add_child(xml_doc.create_element("VCENTER_DS_REF")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{ds_ref}"))
583
                    STDOUT.puts "--- Added VCENTER_DS_REF=#{ds_ref} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
512
                        #Create unmanaged disk element for vm template
513
                        image_id     = one_image["ID"]
514
                        image_name   = one_image["NAME"]
515
                        image_source = one_image["SOURCE"]
584 516

  
585
                    # Update indexes
586
                    managed_disk_index = managed_disk_index + 1
587
                    disk_index = disk_index + 1
588
                end
589
            end
590
        end
517
                        create_disk(xml_doc, image_name, image_source, image_prefix, image_id,
518
                                    disk_index, cluster_id, ds, ds_ref, ds_name, vi_client)
591 519

  
592
        # If VirtualEthernetCard
593
        if !device.class.ancestors.index(RbVmomi::VIM::VirtualEthernetCard).nil?
594
            network_bridge = device.backing.network.name
595
            network_ref    = device.backing.network._ref
596
            network_name   = "#{network_bridge} [#{vm_name}]"
597
            network_type   = device.backing.network.instance_of?(RbVmomi::VIM::DistributedVirtualPortgroup) ? "Distributed Port Group" : "Port Group"
520
                        STDOUT.puts "--- Added unmanaged disk in wild vm (IMAGE_ID=#{one_image["ID"]})"
598 521

  
599
            # Create network if doesn't exist
600
            network = find_network(vnpool, network_ref, ccr_ref, template_ref, vcenter_uuid)
522
                        reference = {}
523
                        reference[:key]   = "opennebula.disk.#{unmanaged_disk_index}"
524
                        reference[:value] = "#{device.key}"
525
                        extraconfig << reference
601 526

  
602
            if !network
603
                one_net = ""
604
                one_net << "NAME=\"#{network_name}\"\n"
605
                one_net << "BRIDGE=\"#{network_bridge}\"\n"
606
                one_net << "VN_MAD=\"dummy\"\n"
607
                one_net << "VCENTER_PORTGROUP_TYPE=\"#{network_type}\"\n"
608
                one_net << "VCENTER_NET_REF=\"#{network_ref}\"\n"
609
                one_net << "VCENTER_CCR_REF=\"#{ccr_ref}\"\n"
610
                one_net << "VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
611
                one_net << "VCENTER_TEMPLATE_REF=\"#{template_ref}\"\n"
612
                one_net << "OPENNEBULA_MANAGED=\"NO\"\n"
613
                one_net << "AR=[\n"
614
                one_net << "TYPE=\"ETHER\",\n"
615
                one_net << "SIZE=\"255\"\n"
616
                one_net << "]\n"
527
                        # Update indexes
528
                        unmanaged_disk_index = unmanaged_disk_index + 1
529
                        disk_index = disk_index + 1
617 530

  
618
                one_vn = OpenNebula::VirtualNetwork.new(OpenNebula::VirtualNetwork.build_xml, one_client)
619
                rc = one_vn.allocate(one_net, cluster_id.to_i)
620
                raise "\n    ERROR! Could not create vnet for vm #{vm_name}. Reason #{rc.message}" if OpenNebula.is_error?(rc)
531
                    else
532
                        # It's a persistent disk if it already exists
533
                        xml_template  = xml_doc.root.at_xpath("TEMPLATE")
534
                        existing_disk = existing_disks[managed_disk_index]
621 535

  
622
                rc = one_vn.info
623
                raise "\n    ERROR! Could not get network info for vnet #{network_name}. Reason #{rc.message}" if OpenNebula.is_error?(rc)
624
                network = one_vn
625
                STDOUT.puts "--- Network #{one_vn["NAME"]} with ID #{one_vn["ID"]} has been created"
626
            else
627
                STDOUT.puts "--- Network #{network["NAME"]} with ID #{network["ID"]} already exists"
628
            end
536
                        # Replace DISK_ID
537
                        existing_disk_image_id = existing_disk.xpath("IMAGE_ID").text
538
                        existing_disk_id = existing_disk.at_xpath("DISK_ID")
539
                        existing_disk_id.content = disk_index
540

  
541
                        disk = xml_template.add_child(existing_disks[managed_disk_index])
629 542

  
630
            existing_macs = []
631
            existing_nics.xpath("MAC").each do |mac|
632
                existing_macs << mac.text
543
                        # Add VCENTER_DISK_TYPE and VCENTER_ADAPTER_TYPE if found
544
                        if !existing_disk.xpath("DISK_TYPE").text.empty?
545
                            disk.add_child(xml_doc.create_element("VCENTER_DISK_TYPE")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{existing_disk.xpath("DISK_TYPE").text}"))
546
                            STDOUT.puts "--- Added VCENTER_DISK_TYPE=#{existing_disk.xpath("DISK_TYPE").text} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
547
                        end
548

  
549
                        if !existing_disk.xpath("ADAPTER_TYPE").text.empty?
550
                            disk.add_child(xml_doc.create_element("VCENTER_ADAPTER_TYPE")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{existing_disk.xpath("ADAPTER_TYPE").text}"))
551
                            STDOUT.puts "--- Added VCENTER_ADAPTER_TYPE=#{existing_disk.xpath("ADAPTER_TYPE").text} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
552
                        end
553

  
554
                        # Add VCENTER_DS_REF
555
                        disk.add_child(xml_doc.create_element("VCENTER_DS_REF")).add_child(Nokogiri::XML::CDATA.new(xml_doc,"#{ds_ref}"))
556
                        STDOUT.puts "--- Added VCENTER_DS_REF=#{ds_ref} to existing disk (IMAGE_ID=#{existing_disk_image_id})"
557

  
558
                        # Update indexes
559
                        managed_disk_index = managed_disk_index + 1
560
                        disk_index = disk_index + 1
561
                    end
562
                end
633 563
            end
634 564

  
635
            mac_address = device.macAddress
636
            if !existing_macs.include?(mac_address)
637
                # Unmanaged nic
638
                create_nic(xml_doc, network, mac_address, cluster_id, nic_index)
565
            # If VirtualEthernetCard
566
            if !device.class.ancestors.index(RbVmomi::VIM::VirtualEthernetCard).nil?
567
                network_bridge = device.backing.network.name
568
                network_ref    = device.backing.network._ref
569
                network_name   = "#{network_bridge} [#{vm_name}]"
570
                network_type   = device.backing.network.instance_of?(RbVmomi::VIM::DistributedVirtualPortgroup) ? "Distributed Port Group" : "Port Group"
571

  
572
                # Create network if doesn't exist
573
                network = find_network(vnpool, network_ref, ccr_ref, template_ref, vcenter_uuid)
574

  
575
                if !network
576
                    one_net = ""
577
                    one_net << "NAME=\"#{network_name}\"\n"
578
                    one_net << "BRIDGE=\"#{network_bridge}\"\n"
579
                    one_net << "VN_MAD=\"dummy\"\n"
580
                    one_net << "VCENTER_PORTGROUP_TYPE=\"#{network_type}\"\n"
581
                    one_net << "VCENTER_NET_REF=\"#{network_ref}\"\n"
582
                    one_net << "VCENTER_CCR_REF=\"#{ccr_ref}\"\n"
583
                    one_net << "VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
584
                    one_net << "VCENTER_TEMPLATE_REF=\"#{template_ref}\"\n"
585
                    one_net << "OPENNEBULA_MANAGED=\"NO\"\n"
586
                    one_net << "AR=[\n"
587
                    one_net << "TYPE=\"ETHER\",\n"
588
                    one_net << "SIZE=\"255\"\n"
589
                    one_net << "]\n"
590

  
591
                    one_vn = OpenNebula::VirtualNetwork.new(OpenNebula::VirtualNetwork.build_xml, one_client)
592
                    rc = one_vn.allocate(one_net, cluster_id.to_i)
593
                    raise "\n    ERROR! Could not create vnet for vm #{vm_name}. Reason #{rc.message}" if OpenNebula.is_error?(rc)
594

  
595
                    rc = one_vn.info
596
                    raise "\n    ERROR! Could not get network info for vnet #{network_name}. Reason #{rc.message}" if OpenNebula.is_error?(rc)
597
                    network = one_vn
598
                    STDOUT.puts "--- Network #{one_vn["NAME"]} with ID #{one_vn["ID"]} has been created"
599
                else
600
                    STDOUT.puts "--- Network #{network["NAME"]} with ID #{network["ID"]} already exists"
601
                end
639 602

  
640
                #Update indexes
641
                nic_index = nic_index + 1
642
            else
643
                # Managed nic
644
                managed_nic_index = existing_macs.index(mac_address)
645
                xml_template      = xml_doc.root.at_xpath("TEMPLATE")
646
                existing_nic      = existing_nics[managed_nic_index]
647

  
648
                # Replace NIC_ID
649
                existind_nic_id = existing_nic.at_xpath("NIC_ID")
650
                existind_nic_id.content = nic_index
651

  
652
                # Add existing NIC to XML template
653
                nic = xml_template.add_child(existing_nics[managed_nic_index])
654
                create_cdata_element(nic, xml_doc, "VCENTER_NET_REF", "#{network["TEMPLATE/VCENTER_NET_REF"]}")
655
                create_cdata_element(nic, xml_doc, "VCENTER_CCR_REF", "#{network["TEMPLATE/VCENTER_CCR_REF"]}")
656
                create_cdata_element(nic, xml_doc, "VCENTER_INSTANCE_ID", "#{network["TEMPLATE/VCENTER_INSTANCE_ID"]}")
657
                create_cdata_element(nic, xml_doc, "VCENTER_PORTGROUP_TYPE", "#{network["TEMPLATE/VCENTER_PORTGROUP_TYPE"]}")
658

  
659
                #Update indexes
660
                nic_index = nic_index + 1
603
                existing_macs = []
604
                existing_nics.xpath("MAC").each do |mac|
605
                    existing_macs << mac.text
606
                end
607

  
608
                mac_address = device.macAddress
609
                if !existing_macs.include?(mac_address)
610
                    # Unmanaged nic
611
                    create_nic(xml_doc, network, mac_address, cluster_id, nic_index)
612

  
613
                    #Update indexes
614
                    nic_index = nic_index + 1
615
                else
616
                    # Managed nic
617
                    managed_nic_index = existing_macs.index(mac_address)
618
                    xml_template      = xml_doc.root.at_xpath("TEMPLATE")
619
                    existing_nic      = existing_nics[managed_nic_index]
620

  
621
                    # Replace NIC_ID
622
                    existind_nic_id = existing_nic.at_xpath("NIC_ID")
623
                    existind_nic_id.content = nic_index
624

  
625
                    # Add existing NIC to XML template
626
                    nic = xml_template.add_child(existing_nics[managed_nic_index])
627
                    create_cdata_element(nic, xml_doc, "VCENTER_NET_REF", "#{network["TEMPLATE/VCENTER_NET_REF"]}")
628
                    create_cdata_element(nic, xml_doc, "VCENTER_CCR_REF", "#{network["TEMPLATE/VCENTER_CCR_REF"]}")
629
                    create_cdata_element(nic, xml_doc, "VCENTER_INSTANCE_ID", "#{network["TEMPLATE/VCENTER_INSTANCE_ID"]}")
630
                    create_cdata_element(nic, xml_doc, "VCENTER_PORTGROUP_TYPE", "#{network["TEMPLATE/VCENTER_PORTGROUP_TYPE"]}")
631

  
632
                    #Update indexes
633
                    nic_index = nic_index + 1
634
                end
661 635
            end
662 636
        end
663 637
    end
......
716 690
            if !vc_vmachines.key? vm_ref
717 691
                raise "Could not find vcenter vm using ref #{vm_ref} in order to assign a datastore"
718 692
            else
719
                ds_ref = vc_vmachines[vm_ref]["datastore"].first._ref rescue nil
720
                raise "Could not get ds ref in order to assign a datastore in history records" if !ds_ref
693
                vc_system_ds = vc_vmachine["datastore"].first rescue nil
694
                raise "Could not find Datastore associated with the VM" if vc_system_ds.nil?
695

  
696
                ds_ref = vc_system_ds._ref
721 697

  
722 698
                ds = find_datastore(dspool, ds_ref, dc_ref, vcenter_uuid, "SYSTEM_DS")
699
                if !ds
700
                    ds_name = vc_system_ds.name
701
                    ds = create_system_ds(ds_name, ds_ref, vcenter_name, vcenter_uuid, dc_name, dc_ref, one_client, vcenter_user, vcenter_pass, vcenter_host, cluster_id)
702
                end
723 703
                ds_id = ds["ID"]
724 704
            end
725 705
        else
......
2101 2081
                end
2102 2082
            end
2103 2083

  
2104
            # Try to get moref using the templates uuid note that that uuid
2084
            # Try to get moref using the templates uuid. Note that that uuid
2105 2085
            # is not unique
2086
            templates_same_uuid = {}
2106 2087
            if !template_ref && template_uuid
2107 2088
                templates_found = 0
2108 2089
                vc_templates[vcenter_uuid].each do |ref, value|
2109 2090
                    if value["config.uuid"] == template_uuid
2110 2091
                        templates_found += 1
2092
                        templates_same_uuid[ref] = value
2111 2093
                        if templates_found > 1
2112 2094
                            template_ref = nil
2113 2095
                        else
......
2125 2107
                index = 0
2126 2108
                template_refs  = []
2127 2109

  
2128
                vc_templates[vcenter_uuid].each do |ref, t|
2110
                if templates_same_uuid.length > 1
2111
                    # Choose only between those that have the same UUID
2112
                    templates_list = templates_same_uuid
2113
                else
2114
                    templates_list = vc_templates[vcenter_uuid]
2115
                end
2116

  
2117
                templates_list.each do |ref, t|
2129 2118
                    item = RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref)
2130 2119

  
2131 2120
                    folders = []
......
2165 2154
                STDOUT.puts "-" * 80
2166 2155
            end
2167 2156

  
2157
            # Get OpenNebulas's template
2158
            one_template = OpenNebula::Template.new_with_id(template["ID"], one_client)
2168 2159
            STDOUT.puts
2169 2160

  
2170 2161
            if !template_ref
2171
                raise "Template #{template_name} could not be updated, cannot find template's MOREF"
2162
                STDOUT.print "Could not upgrade this template. Not found in vCenter. Do you want to remove it? (y/n) "
2163
                loop do
2164
                    option = STDIN.gets.strip
2165
                    case option
2166
                    when "y"
2167
                        # delete
2168
                        rc = one_template.delete
2169
                        raise "Template #{template["ID"]}: '#{template["NAME"]}' could not be deleted. Reason #{rc.message}" if OpenNebula.is_error?(rc)
2170

  
2171
                        STDOUT.puts("\nTemplate #{template["ID"]}: '#{template["NAME"]}' has been \e[93mdeleted\e[39m.")
2172
                        break
2173
                    when "n"
2174
                        STDOUT.puts("\nTemplate #{template["ID"]}: '#{template["NAME"]}' is \e[93mbroken\e[39m. Please inspect it manually after the upgrade.")
2175
                        STDOUT.puts("\nPress any key to continue.\n")
2176
                        STDIN.gets
2177
                        break
2178
                    end
2179
                end
2180
                next
2172 2181
            end
2173 2182

  
2174
            # Get OpenNebulas's template
2175
            one_template = OpenNebula::Template.new_with_id(template["ID"], one_client)
2176
            rc   = one_template.info
2183
            rc  = one_template.info
2177 2184
            raise "Could not get info for template #{template["ID"]}. Reason: #{rc.message}" if OpenNebula.is_error?(rc)
2178 2185

  
2179 2186
            # Find vcenter template in vc_templates
......
2372 2379
            # Refresh pools
2373 2380
            rc = vnpool.info_all
2374 2381
            raise "\n    ERROR! Could not update vnpool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
2382

  
2375 2383
            rc = ipool.info_all
2376 2384
            raise "\n    ERROR! Could not update ipool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
2385

  
2377 2386
            rc = dspool.info
2378 2387
            raise "\n    ERROR! Could not update dspool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
2379 2388

  
......
2498 2507

  
2499 2508
                # Try to get moref using the templates uuid note that that uuid
2500 2509
                # is not unique
2510
                templates_same_uuid = {}
2501 2511
                if !template_ref && template_uuid
2502 2512
                    templates_found = 0
2503 2513
                    vc_templates[vcenter_uuid].each do |ref, value|
2504 2514
                        if value["config.uuid"] == template_uuid
2505 2515
                            templates_found += 1
2516
                            templates_same_uuid[ref] = value
2506 2517
                            if templates_found > 1
2507 2518
                                template_ref = nil
2508 2519
                            else
......
2520 2531
                    index = 0
2521 2532
                    template_refs  = []
2522 2533

  
2523
                    vc_templates[vcenter_uuid].each do |ref, t|
2534
                    if templates_same_uuid.length > 1
2535
                        # Choose only between those that have the same UUID
2536
                        templates_list = templates_same_uuid
2537
                    else
2538
                        templates_list = vc_templates[vcenter_uuid]
2539
                    end
2540

  
2541
                    templates_list.each do |ref, t|
2524 2542
                        item = RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref)
2525 2543

  
2526 2544
                        folders = []
......
2772 2790
                vc_clusters[vcenter_uuid]   = retrieve_vcenter_clusters(vi_client)
2773 2791
                vc_datastores[vcenter_uuid] = retrieve_vcenter_datastores(vi_client)
2774 2792
                vc_networks[vcenter_uuid]   = retrieve_vcenter_networks(vi_client)
2775
                vc_vmachines[vcenter_uuid], vc_templates[vcenter_uuid]  = retrieve_vcenter_vms(vi_client)
2793
                vc_vmachines[vcenter_uuid], vc_templates[vcenter_uuid] = retrieve_vcenter_vms(vi_client)
2776 2794

  
2777 2795
                STDOUT.puts "--- #{host["TEMPLATE/VCENTER_HOST"]} \e[92mobjects have been retrieved\e[39m"
2778 2796
            end

Also available in: Unified diff