Revision 918bce77

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb
281 281
                end
282 282
                rp = rp_cache[template_ccr.name.to_s]
283 283

  
284
                object = template.to_one_template(template_name,
285
                                                  template_ref,
286
                                                  template_ccr._ref,
287
                                                  cluster_name,
284
                object = template.to_one_template(template,
288 285
                                                  ds,
289 286
                                                  ds_list,
290 287
                                                  default_ds,
src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb
68 68
        end
69 69
    end
70 70

  
71
    def self.get_image_import_template(ds_name, image_path, image_type, ipool)
72
        one_image = ""
73

  
74
        # Remove ds info from path
75
        image_path.sub!(/^\[#{ds_name}\] /, "")
76

  
77
        # Get image name
78
        file_name = File.basename(image_path).reverse.sub("kdmv.","").reverse
79
        image_name = "#{file_name} - #{ds_name}"
80

  
81
        #Chek if the image has already been imported
82
        if VCenterDriver::VIHelper.find_by_name(OpenNebula::ImagePool,
83
                                                image_name,
84
                                                ipool,
85
                                                false).nil?
86
            #Set template
87
            one_image << "NAME=\"#{image_name}\"\n"
88
            one_image << "PATH=\"vcenter://#{image_path}\"\n"
89
            one_image << "TYPE=\"#{image_type}\"\n"
90
            one_image << "PERSISTENT=\"NO\"\n"
91
            one_image << "OPENNEBULA_MANAGED=\"YES\"\n"
92
        end
93

  
94
        return one_image
95
    end
96

  
97
    def self.get_one_image_ds_by_ref_and_ccr(ref, ccr_ref, vcenter_uuid, pool = nil)
98
        pool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false) if pool.nil?
99
        element = pool.select{|e|
100
            e["TEMPLATE/TYPE"] == "IMAGE_DS" &&
101
            e["TEMPLATE/VCENTER_DS_REF"] == ref &&
102
            e["TEMPLATE/VCENTER_CCR_REF"] == ccr_ref &&
103
            e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid}.first rescue nil
104

  
105
        return element
106
    end
107

  
108

  
71 109
    def monitor
72 110
        summary = self['summary']
73 111

  
src/vmm_mad/remotes/lib/vcenter_driver/importer.rb
74 74

  
75 75
        STDOUT.print "done!\n"
76 76

  
77
        # Create OpenNebula pools
78
        dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool)
79
        ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
80

  
81
        # Get vcenter intance uuid as moref is unique for each vcenter
82
        vc_uuid = vi_client.vim.serviceContent.about.instanceUuid
83

  
77 84
        rs.each {|dc, tmps|
78 85
            STDOUT.print "\nDo you want to process datacenter #{dc}"\
79 86
                            " (y/[n])? "
......
95 102

  
96 103
                next if STDIN.gets.strip.downcase != 'y'
97 104

  
105
=begin
106
                ## Add existing disks to template (OPENNEBULA_MANAGED)
107

  
108
                template = t[:template]
109

  
110
                error, template_disks = template.import_vcenter_disks(vc_uuid,
111
                                                                      dpool,
112
                                                                      ipool)
113
                if error.empty?
114
                    t[:one] << template_disks
115
                else
116
                    STDOUT.puts error
117
                    next
118
                end
119
=end
98 120

  
121
                # Datastore placement
99 122
                ds_input = ""
100 123

  
101 124
                STDOUT.print "\n    This template is currently set to be "\
......
246 269
                        end
247 270

  
248 271
                        list_str = "\n    [Index] Resource pool :"\
249
                                    "\n    #{dashes}\n"
272
                                   "\n    #{dashes}\n"
250 273

  
251 274
                        STDOUT.print list_str
252 275

  
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