Revision 918bce77 src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
978 978
        end
979 979
    end
980 980

  
981
    #  Checks if a RbVmomi::VIM::VirtualDevice is a disk
981
    def get_vcenter_disks
982
        disks = []
983
        self["config.hardware.device"].each do |device|
984
            disk = {}
985
            if is_disk_or_iso?(device)
986
                disk[:datastore] = device.backing.datastore
987
                disk[:path]      = device.backing.fileName
988
                disk[:type]      = is_disk?(device) ? "OS" : "CDROM"
989
                disks << disk
990
            end
991
        end
992
        return disks
993
    end
994

  
995
    def import_vcenter_disks(vc_uuid, dpool, ipool)
996

  
997
        disk_info = ""
998
        error = ""
999

  
1000
        ccr_ref = self["runtime.host.parent._ref"]
1001

  
1002
        #Get disks and info required
1003
        vc_disks = get_vcenter_disks
1004

  
1005
        # Track allocated images
1006
        allocated_images = []
1007

  
1008
        vc_disks.each do |disk|
1009

  
1010
            datastore_found = VCenterDriver::Storage.get_one_image_ds_by_ref_and_ccr(disk[:datastore]._ref,
1011
                                                                                     ccr_ref,
1012
                                                                                     vc_uuid,
1013
                                                                                     dpool)
1014
            if datastore_found.nil?
1015
                error = "    Error datastore #{disk[:datastore].name}: has to be imported first as an image datastore!\n"
1016

  
1017
                #Rollback delete disk images
1018
                allocated_images.each do |i|
1019
                    i.delete
1020
                end
1021

  
1022
                break
1023
            end
1024

  
1025
            image_template = VCenterDriver::Datastore.get_image_import_template(disk[:datastore].name,
1026
                                                                                disk[:path],
1027
                                                                                disk[:type], ipool)
1028
            if !image_template.empty?
1029
                # Then the image is created
1030
                one_i = VCenterDriver::VIHelper.new_one_item(OpenNebula::Image)
1031

  
1032
                allocated_images << one_i
1033

  
1034
                rc = one_i.allocate(image_template, datastore_found['ID'].to_i)
1035

  
1036
                if ::OpenNebula.is_error?(rc)
1037
                    error = "    Error creating disk from template: #{rc.message}. Cannot import the template\n"
1038

  
1039
                    #Rollback delete disk images
1040
                    allocated_images.each do |i|
1041
                        i.delete
1042
                    end
1043

  
1044
                    break
1045
                end
1046

  
1047
                #Add info for One template
1048
                one_i.info
1049
                disk_info << "DISK=[\n"
1050
                disk_info << "IMAGE=\"#{one_i["NAME"]}\",\n"
1051
                disk_info << "IMAGE_UNAME=\"#{one_i["UNAME"]}\"\n"
1052
                disk_info << "]\n"
1053
            end
1054
        end
1055

  
1056
        return error, disk_info
1057

  
1058
    end
1059

  
1060
    #  Checks if a RbVmomi::VIM::VirtualDevice is a disk or a cdrom
982 1061
    def is_disk_or_cdrom?(device)
983 1062
        is_disk  = !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
984 1063
        is_cdrom = !(device.class.ancestors.index(RbVmomi::VIM::VirtualCdrom)).nil?
985 1064
        is_disk || is_cdrom
986 1065
    end
987 1066

  
1067
    #  Checks if a RbVmomi::VIM::VirtualDevice is a disk or an iso file
1068
    def is_disk_or_iso?(device)
1069
        is_disk  = !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
1070
        is_iso = device.backing.is_a? RbVmomi::VIM::VirtualCdromIsoBackingInfo
1071
        is_disk || is_iso
1072
    end
1073

  
1074
    #  Checks if a RbVmomi::VIM::VirtualDevice is a disk
1075
    def is_disk?(device)
1076
        !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
1077
    end
1078

  
988 1079
    def find_free_controller(position=0)
989 1080
        free_scsi_controllers = []
990 1081
        available_controller  = nil
......
1298 1389
        return str
1299 1390
    end
1300 1391

  
1301
    def to_one_template(template_name, template_ref, template_ccr, cluster_name,
1302
                        ds, ds_list, default_ds, rp, rp_list, vcenter_uuid)
1392
    def to_one_template(template, ds, ds_list, default_ds,
1393
                        rp, rp_list, vcenter_uuid)
1394

  
1395
        template_name = template['name']
1396
        template_ref  = template['_ref']
1397
        template_ccr  = template['runtime.host.parent']
1398
        cluster_name  = template['runtime.host.parent.name']
1399

  
1303 1400
        one_tmp = {}
1304 1401
        one_tmp[:name]                  = "#{template_name} - #{cluster_name}"
1305
        one_tmp[:vcenter_ccr_ref]       = template_ccr
1402
        one_tmp[:template_name]         = template_name
1403
        one_tmp[:vcenter_ccr_ref]       = template_ccr._ref
1306 1404
        one_tmp[:one]                   = to_one(true)
1307 1405
        one_tmp[:vcenter_ref]           = template_ref
1308 1406
        one_tmp[:vcenter_instance_uuid] = vcenter_uuid
......
1311 1409
        one_tmp[:ds_list]               = ds_list
1312 1410
        one_tmp[:default_ds]            = default_ds
1313 1411
        one_tmp[:rp]                    = rp
1314
        one_tmp[:rp_list]                = rp_list
1412
        one_tmp[:rp_list]               = rp_list
1413
        one_tmp[:template]              = template
1315 1414
        return one_tmp
1316 1415
    end
1317 1416

  

Also available in: Unified diff