Revision 347968ad src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb
77 77

  
78 78
        "USED_MB=#{used_mb}\nFREE_MB=#{free_mb} \nTOTAL_MB=#{total_mb}"
79 79
    end
80

  
81
    def self.exists_one_by_ref_ccr_and_type?(ref, ccr_ref, vcenter_uuid, type, pool = nil)
82
        pool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false) if pool.nil?
83
        elements = pool.select{|e|
84
            e["TEMPLATE/TYPE"] == type &&
85
            e["TEMPLATE/VCENTER_DS_REF"] == ref &&
86
            e["TEMPLATE/VCENTER_CCR_REF"] == ccr_ref &&
87
            e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid}
88

  
89
        return elements.size == 1
90
    end
91

  
92
    def to_one(ds_name, vcenter_uuid, ccr_ref, host_id)
93
        one = ""
94
        one << "NAME=\"#{ds_name}\"\n"
95
        one << "TM_MAD=vcenter\n"
96
        one << "VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
97
        one << "VCENTER_CCR_REF=\"#{ccr_ref}\"\n"
98
        one << "VCENTER_DS_REF=\"#{self['_ref']}\"\n"
99
        one << "VCENTER_ONE_HOST_ID=\"#{host_id}\"\n"
100

  
101
        return one
102
    end
103

  
104
    def to_one_template(one_clusters, ccr_ref, ccr_name, type, vcenter_uuid, one)
105

  
106
        one_cluster = one_clusters.select { |ccr| ccr[:ref] == ccr_ref }.first rescue nil
107

  
108
        return nil if one_cluster.nil?
109

  
110
        ds_name = ""
111

  
112
        if type == "IMAGE_DS"
113
            ds_name = "#{self['name']} - #{ccr_name} (IMG)"
114
        else
115
            ds_name = "#{self['name']} - #{ccr_name} (SYS)"
116
        end
117

  
118
        one_tmp = {
119
            :name     => ds_name,
120
            :total_mb => ((self['summary.capacity'].to_i / 1024) / 1024),
121
            :free_mb  => ((self['summary.freeSpace'].to_i / 1024) / 1024),
122
            :cluster  => ccr_name,
123
            :one  => to_one(ds_name, vcenter_uuid, ccr_ref, one_cluster[:host_id])
124
        }
125

  
126
        if type == "SYSTEM_DS"
127
            one_tmp[:one] << "TYPE=SYSTEM_DS\n"
128
        else
129
            one_tmp[:one] << "DS_MAD=vcenter\n"
130
            one_tmp[:one] << "TYPE=IMAGE_DS\n"
131
        end
132

  
133
        return one_tmp
134
    end
135

  
136

  
80 137
end # class Storage
81 138

  
82 139
class StoragePod < Storage
......
215 272
    # Get file size for image handling
216 273
    def stat(img_str)
217 274
        ds_name = self['name']
218

  
219 275
        img_path = File.dirname img_str
220 276
        img_name = File.basename img_str
221 277

  
222 278
        # Create Search Spec
223
        spec         = RbVmomi::VIM::HostDatastoreBrowserSearchSpec.new
224
        spec.query   = [RbVmomi::VIM::VmDiskFileQuery.new,
225
                        RbVmomi::VIM::IsoImageFileQuery.new]
226
        spec.details = RbVmomi::VIM::FileQueryFlags(:fileOwner    => true,
227
                                                    :fileSize     => true,
228
                                                    :fileType     => true,
229
                                                    :modification => true)
230
        spec.matchPattern=[img_name]
231

  
232
        search_params = {'datastorePath' => "[#{ds_name}] #{img_path}",
233
                         'searchSpec'    => spec}
279
        search_params = get_search_params(ds_name, img_path, img_name)
234 280

  
235 281
        # Perform search task and return results
236 282
        begin
......
250 296
        end
251 297
    end
252 298

  
299
    def get_search_params(ds_name, img_path=nil, img_name=nil)
300
        spec         = RbVmomi::VIM::HostDatastoreBrowserSearchSpec.new
301
        spec.query   = [RbVmomi::VIM::VmDiskFileQuery.new,
302
                        RbVmomi::VIM::IsoImageFileQuery.new]
303
        spec.details = RbVmomi::VIM::FileQueryFlags(:fileOwner    => true,
304
                                                    :fileSize     => true,
305
                                                    :fileType     => true,
306
                                                    :modification => true)
307

  
308

  
309
        spec.matchPattern = img_name.nil? ? [] : [img_name]
310

  
311
        datastore_path = "[#{ds_name}]"
312
        datastore_path << " #{img_path}" if !img_path.nil?
313

  
314
        search_params = {'datastorePath' => datastore_path,
315
                         'searchSpec'    => spec}
316

  
317
        return search_params
318
    end
319

  
253 320
    def get_fm
254 321
        self['_connection.serviceContent.fileManager']
255 322
    end
......
347 414
        return output
348 415
    end
349 416

  
417
    def get_images(vcenter_uuid)
418
        img_templates = []
419
        ds_id = nil
420
        ds_name = self['name']
421

  
422
        img_types = ["FloppyImageFileInfo",
423
                     "IsoImageFileInfo",
424
                     "VmDiskFileInfo"]
425

  
426
        ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool, false)
427
        if ipool.respond_to?(:message)
428
            raise "Could not get OpenNebula ImagePool: #{pool.message}"
429
        end
430

  
431
        dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false)
432
        if dpool.respond_to?(:message)
433
            raise "Could not get OpenNebula DatastorePool: #{pool.message}"
434
        end
435

  
436
        begin
437
            one_ds = VCenterDriver::VIHelper.find_by_ref(OpenNebula::DatastorePool,
438
                                                        "TEMPLATE/VCENTER_DS_REF",
439
                                                        self["_ref"],
440
                                                        vcenter_uuid,
441
                                                        dpool)
442
            raise "Could not find OpenNebula Datastore" if one_ds.nil?
443
            ds_id = one_ds["ID"]
444
        rescue Exception => e
445
            raise "Error: #{e.message}"
446
        end
447

  
448
        begin
449
            # Create Search Spec
450
            search_params = get_search_params(ds_name)
451

  
452
            # Perform search task and return results
453
            search_task = self['browser'].
454
                SearchDatastoreSubFolders_Task(search_params)
455
            search_task.wait_for_completion
456

  
457
            search_task.info.result.each { |image|
458
                folderpath = ""
459
                if image.folderPath[-1] != "]"
460
                    folderpath = image.folderPath.sub(/^\[#{ds_name}\] /, "")
461
                end
462

  
463
                image = image.file.first
464

  
465
                # Skip not relevant files
466
                next if !img_types.include? image.class.to_s
467

  
468
                # Get image path and name
469
                image_path = folderpath
470
                image_path << image.path
471
                image_name = File.basename(image.path).reverse.sub("kdmv.","").reverse
472

  
473
                # Get image and disk type
474
                image_type = image.class.to_s == "VmDiskFileInfo" ? "OS" : "CDROM"
475
                disk_type = image.class.to_s == "VmDiskFileInfo" ? image.diskType : nil
476

  
477
                #Set template
478
                one_image =  "NAME=\"#{image_name} - #{ds_name}\"\n"
479
                one_image << "PATH=\"vcenter://#{image_path}\"\n"
480
                one_image << "PERSISTENT=\"YES\"\n"
481
                one_image << "TYPE=\"#{image_type}\"\n"
482
                one_image << "DISK_TYPE=\"#{disk_type}\"\n" if disk_type
483

  
484
                if VCenterDriver::VIHelper.find_by_name(OpenNebula::ImagePool,
485
                                                        "#{image_name} - #{ds_name}",
486
                                                        ipool,
487
                                                        false).nil?
488
                    img_templates << {
489
                        :name        => "#{image_name} - #{ds_name}",
490
                        :path        => image_path,
491
                        :size        => (image.fileSize / 1024).to_s,
492
                        :type        => image.class.to_s,
493
                        :dsid        => ds_id,
494
                        :one         => one_image
495
                    }
496
                end
497
            }
498

  
499
        rescue
500
            raise "Could not find images."
501
        end
502

  
503
        return img_templates
504
    end
505

  
350 506
    # This is never cached
351 507
    def self.new_from_ref(ref, vi_client)
352 508
        self.new(RbVmomi::VIM::Datastore.new(vi_client.vim, ref), vi_client)

Also available in: Unified diff