Revision e6a57b0f src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb
1 1
module VCenterDriver
2

  
2
require 'set'
3 3
class DatacenterFolder
4 4
    attr_accessor :items
5 5

  
......
38 38
        @vi_client.vim.serviceContent.about.instanceUuid
39 39
    end
40 40

  
41
    def get_vcenter_instance_name
42
        @vi_client.vim.serviceContent.setting.setting.select{|set| set.key == 'VirtualCenter.InstanceName'}.first.value rescue nil
43
    end
44

  
45 41
    def get_vcenter_api_version
46 42
        @vi_client.vim.serviceContent.about.apiVersion
47 43
    end
48 44

  
49
    def get_clusters
50

  
51
        clusters = {}
52

  
53
        vcenter_uuid = get_vcenter_instance_uuid
54

  
55
        pool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool, false)
56
        if pool.respond_to?(:message)
57
            raise "Could not get OpenNebula Pool: #{pool.message}"
58
        end
59

  
60
        fetch! if @items.empty? #Get datacenters
61

  
62
        # Add datacenter to hash and store in an array all clusters
63
        @items.values.each do |dc|
64
            dc_name = dc.item.name
65
            clusters[dc_name] = []
66

  
67
            host_folder = dc.host_folder
68
            host_folder.fetch_clusters!
69

  
70
            host_folder.items.values.each do |ccr|
71
                cluster = {}
72
                cluster[:ref]  = ccr['_ref']
73
                cluster[:name] = ccr['name']
74
                attribute = "TEMPLATE/VCENTER_CCR_REF"
75
                one_host = VCenterDriver::VIHelper.find_by_ref(OpenNebula::HostPool,
76
                                                               attribute,
77
                                                               ccr['_ref'],
78
                                                               vcenter_uuid,
79
                                                               pool)
80

  
81
                next if one_host.nil? #Cluster hasn't been imported'
82

  
83
                cluster[:host_id] = one_host['ID']
84
                clusters[dc_name] << cluster
85
            end
86
        end
87

  
88
        clusters
89
    end
90

  
91
    def get_unimported_hosts(hpool)
45
    def get_unimported_hosts(hpool, vcenter_instance_name)
92 46
        host_objects = {}
93 47

  
94 48
        vcenter_uuid = get_vcenter_instance_uuid
......
116 70
                rpools = cluster.get_resource_pool_list.select {|rp| !rp[:name].empty?}
117 71

  
118 72
                host_info = {}
119
                host_info[:cluster_name]     = ccr['name'].tr(" ", "_")
73
                cluster_name = "[#{vcenter_instance_name} - #{dc_name}] #{ccr['name'].tr(" ", "_")}"
74
                host_info[:cluster_name]     = cluster_name
120 75
                host_info[:cluster_ref]      = ccr['_ref']
121 76
                host_info[:vcenter_uuid]     = vcenter_uuid
122 77
                host_info[:vcenter_version]  = vcenter_version
......
129 84
        return host_objects
130 85
    end
131 86

  
132
    def get_unimported_datastores(hpool)
87
    def get_unimported_datastores(dpool, vcenter_instance_name, hpool)
88

  
133 89
        ds_objects = {}
134 90

  
135 91
        vcenter_uuid = get_vcenter_instance_uuid
136 92

  
137
        vcenter_instance_name = get_vcenter_instance_name
138

  
139 93
        fetch! if @items.empty? #Get datacenters
140 94

  
141
        one_clusters = get_clusters
95
        one_clusters = {}
142 96

  
143 97
        @items.values.each do |dc|
144 98
            dc_name = dc.item.name
99

  
100
            one_clusters[dc_name] = []
101

  
102
            host_folder = dc.host_folder
103
            host_folder.fetch_clusters!
104

  
105
            host_folder.items.values.each do |ccr|
106
                cluster = {}
107
                cluster[:ref]  = ccr['_ref']
108
                cluster[:name] = ccr['name']
109
                attribute = "TEMPLATE/VCENTER_CCR_REF"
110
                one_host = VCenterDriver::VIHelper.find_by_ref(OpenNebula::HostPool,
111
                                                               attribute,
112
                                                               ccr['_ref'],
113
                                                               vcenter_uuid,
114
                                                               hpool)
115

  
116
                if !!one_host
117
                    one_clusters[:host_id] = one_host['ID']
118
                    one_clusters[dc_name] << cluster
119
                end
120
            end
121

  
145 122
            next if one_clusters[dc_name].empty? #No clusters imported, continue
146 123
            ds_objects[dc_name] = []
147 124

  
......
155 132
                    clusters_in_ds = {}
156 133

  
157 134
                    hosts_in_ds.each do |host|
158
                        if !clusters_in_ds[host.key.parent._ref.to_s]
159
                            clusters_in_ds[host.key.parent._ref.to_s] = host.key.parent.name
135
                        cluster_ref = host.key.parent._ref
136
                        if !clusters_in_ds[cluster_ref]
137
                            clusters_in_ds[cluster_ref] = host.key.parent.name
160 138
                        end
161 139
                    end
162 140

  
163 141
                    clusters_in_ds.each do |ccr_ref, ccr_name|
164
                        already_image_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "IMAGE_DS", hpool)
142
                        already_image_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "IMAGE_DS", dpool)
165 143

  
166 144
                        if !already_image_ds
167 145
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "IMAGE_DS", vcenter_uuid, vcenter_instance_name, dc_name)
168 146
                            ds_objects[dc_name] << object if !object.nil?
169 147
                        end
170 148

  
171
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", hpool)
149
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", dpool)
172 150

  
173 151
                        if !already_system_ds
174 152
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "SYSTEM_DS", vcenter_uuid, vcenter_instance_name, dc_name)
......
184 162
                    ds_in_spod.each do |sp_ds|
185 163
                        hosts_in_ds = sp_ds.host
186 164
                        hosts_in_ds.each do |host|
187
                            if !clusters_in_spod[host.key.parent._ref.to_s]
188
                                clusters_in_spod[host.key.parent._ref.to_s] = host.key.parent.name
165
                            cluster_ref = host.key.parent._ref
166
                            if !clusters_in_spod[cluster_ref]
167
                                clusters_in_spod[cluster_ref] = host.key.parent.name
189 168
                            end
190 169
                        end
191 170
                    end
192 171

  
193 172
                    clusters_in_spod.each do |ccr_ref, ccr_name|
194
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", hpool)
173
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", dpool)
195 174

  
196 175
                        if !already_system_ds
197 176
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "SYSTEM_DS", vcenter_uuid, vcenter_instance_name, dc_name)
......
201 180
                end
202 181
            end
203 182
        end
204

  
205 183
        ds_objects
206 184
    end
207 185

  
......
209 187
        template_objects = {}
210 188
        vcenter_uuid = get_vcenter_instance_uuid
211 189

  
190
        vcenter_instance_name = vi_client.vim.host
191

  
212 192
        fetch! if @items.empty? #Get datacenters
213 193

  
214 194
        @items.values.each do |dc|
215

  
195
            rp_cache = {}
216 196
            dc_name = dc.item.name
217 197
            template_objects[dc_name] = []
218 198

  
219
            #Get templates defined in a datacenter
220
            vm_folder = dc.vm_folder
221
            vm_folder.fetch_templates!
222
            vm_folder.items.values.each do |template|
199

  
200
            view = vi_client.vim.serviceContent.viewManager.CreateContainerView({
201
                container: dc.item.vmFolder,
202
                type:      ['VirtualMachine'],
203
                recursive: true
204
            })
205

  
206
            pc = vi_client.vim.serviceContent.propertyCollector
207

  
208
            filterSpec = RbVmomi::VIM.PropertyFilterSpec(
209
            :objectSet => [
210
                :obj => view,
211
                :skip => true,
212
                :selectSet => [
213
                RbVmomi::VIM.TraversalSpec(
214
                    :name => 'traverseEntities',
215
                    :type => 'ContainerView',
216
                    :path => 'view',
217
                    :skip => false
218
                )
219
                ]
220
            ],
221
            :propSet => [
222
                { :type => 'VirtualMachine', :pathSet => ['config.template'] }
223
            ]
224
            )
225

  
226
            result = pc.RetrieveProperties(:specSet => [filterSpec])
227

  
228
            vms = {}
229
                result.each do |r|
230
                vms[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::VirtualMachine)
231
            end
232
            templates = []
233
            vms.each do |ref,value|
234
                if value["config.template"]
235
                    templates << VCenterDriver::Template.new_from_ref(ref, vi_client)
236
                end
237
            end
238

  
239
            view.DestroyView # Destroy the view
240

  
241

  
242
            templates.each do |template|
243

  
223 244
                one_template = VCenterDriver::VIHelper.find_by_ref(OpenNebula::TemplatePool,
224 245
                                                                   "TEMPLATE/VCENTER_TEMPLATE_REF",
225 246
                                                                   template['_ref'],
226 247
                                                                   vcenter_uuid,
227 248
                                                                   tpool)
228
                next if one_template #If the template has been already imported
229 249

  
230
                template_ccr  = template['runtime.host.parent']
231

  
232
                # Check if template has nics or disks to be imported later
233
                has_nics_and_disks = false
250
                next if one_template #If the template has been already imported
234 251

  
235
                template["config.hardware.device"].each do |device|
236
                    if VCenterDriver::Storage.is_disk_or_iso?(device)
237
                        has_nics_and_disks = true
238
                        break
239
                    end
252
                one_template = VCenterDriver::Template.get_xml_template(template, vcenter_uuid, vi_client, vcenter_instance_name, dc_name, rp_cache)
240 253

  
241
                    if VCenterDriver::Network.is_nic?(device)
242
                        has_nics_and_disks = true
243
                        break
244
                    end
245
                end
254
                template_objects[dc_name] << one_template if !!one_template
255
            end
256
        end
246 257

  
247
                #Get resource pools
248
                rp_cache = {}
249
                if !rp_cache[template_ccr.name.to_s]
250
                    tmp_cluster = VCenterDriver::ClusterComputeResource.new_from_ref(template_ccr._ref, vi_client)
251
                    rp_list = tmp_cluster.get_resource_pool_list
252
                    rp = ""
253
                    if !rp_list.empty?
254
                        rp_name_list = []
255
                        rp_list.each do |rp_hash|
256
                            rp_name_list << rp_hash[:name]
257
                        end
258
                        rp =  "O|list|Which resource pool you want this VM to run in? "
259
                        rp << "|#{rp_name_list.join(",")}" #List of RP
260
                        rp << "|#{rp_name_list.first}" #Default RP
261
                    end
262
                    rp_cache[template_ccr.name.to_s] = rp
263
                end
264
                rp = rp_cache[template_ccr.name.to_s]
265

  
266
                object = template.to_one_template(template,
267
                                                  has_nics_and_disks,
268
                                                  rp,
269
                                                  rp_list,
270
                                                  vcenter_uuid)
271

  
272
                template_objects[dc_name] << object if !object.nil?
273
            end #template loop
274
        end #datacenter loop
275
        return template_objects
258
        template_objects
276 259
    end
277 260

  
278
    def get_unimported_networks(npool)
261
    def get_unimported_networks(npool,vcenter_instance_name)
279 262

  
280 263
        network_objects = {}
281 264
        vcenter_uuid = get_vcenter_instance_uuid
282 265

  
266
        pc = @vi_client.vim.serviceContent.propertyCollector
267

  
268
        #Get all port groups and distributed port groups in vcenter instance
269
        view = @vi_client.vim.serviceContent.viewManager.CreateContainerView({
270
                container: @vi_client.vim.rootFolder,
271
                type:      ['Network','DistributedVirtualPortgroup'],
272
                recursive: true
273
        })
274

  
275
        filterSpec = RbVmomi::VIM.PropertyFilterSpec(
276
            :objectSet => [
277
                :obj => view,
278
                :skip => true,
279
                :selectSet => [
280
                RbVmomi::VIM.TraversalSpec(
281
                    :name => 'traverseEntities',
282
                    :type => 'ContainerView',
283
                    :path => 'view',
284
                    :skip => false
285
                )
286
                ]
287
            ],
288
            :propSet => [
289
                { :type => 'Network', :pathSet => ['name'] },
290
                { :type => 'DistributedVirtualPortgroup', :pathSet => ['name'] }
291
            ]
292
        )
293

  
294
        result = pc.RetrieveProperties(:specSet => [filterSpec])
295

  
296
        networks = {}
297
            result.each do |r|
298
            networks[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) || r.obj.is_a?(RbVmomi::VIM::Network)
299
            networks[r.obj._ref][:network_type] = r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) ? "Distributed Port Group" : "Port Group"
300
        end
301

  
302
        view.DestroyView # Destroy the view
303

  
283 304
        fetch! if @items.empty? #Get datacenters
284 305

  
285 306
        @items.values.each do |dc|
......
287 308
            dc_name = dc.item.name
288 309
            network_objects[dc_name] = []
289 310

  
290
            #Get networks defined in a datacenter
291
            network_folder = dc.network_folder
292
            network_folder.fetch!
293
            network_folder.items.values.each do |network|
311
            view = @vi_client.vim.serviceContent.viewManager.CreateContainerView({
312
                container: dc.item,
313
                type:      ['ClusterComputeResource'],
314
                recursive: true
315
            })
316

  
317
            filterSpec = RbVmomi::VIM.PropertyFilterSpec(
318
                :objectSet => [
319
                    :obj => view,
320
                    :skip => true,
321
                    :selectSet => [
322
                    RbVmomi::VIM.TraversalSpec(
323
                        :name => 'traverseEntities',
324
                        :type => 'ContainerView',
325
                        :path => 'view',
326
                        :skip => false
327
                    )
328
                    ]
329
                ],
330
                :propSet => [
331
                    { :type => 'ClusterComputeResource', :pathSet => ['name','network'] }
332
                ]
333
            )
334

  
335
            result = pc.RetrieveProperties(:specSet => [filterSpec])
336

  
337
            clusters = {}
338
                result.each do |r|
339
                clusters[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::ClusterComputeResource)
340
            end
341

  
342
            view.DestroyView # Destroy the view
343

  
344
            clusters.each do |ref, info|
294 345

  
295
                next if network.instance_of? VCenterDriver::DistributedVirtualSwitch
346
                network_obj = info['network']
296 347

  
297
                one_network = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
298
                                                                  "TEMPLATE/VCENTER_NET_REF",
299
                                                                  network['_ref'],
300
                                                                  vcenter_uuid,
301
                                                                  npool)
302
                next if one_network #If the network has been already imported
348
                network_obj.each do |n|
349
                    network_ref  = n._ref
350
                    network_name = networks[network_ref]['name']
351
                    network_type = networks[network_ref][:network_type]
303 352

  
304
                network_name = network['name']
305
                network_ref  = network['_ref']
353
                    one_network = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
354
                                                                    "TEMPLATE/VCENTER_NET_REF",
355
                                                                    network_ref,
356
                                                                    vcenter_uuid,
357
                                                                    npool)
358
                    next if one_network #If the network has been already imported
306 359

  
307
                network.clusters.each do |ccr_ref, ccr_name|
308 360
                    one_vnet = VCenterDriver::Network.to_one_template(network_name,
309
                                                                      network_ref,
310
                                                                      network.network_type,
311
                                                                      ccr_ref,
312
                                                                      ccr_name,
313
                                                                      vcenter_uuid)
361
                                                                        network_ref,
362
                                                                        network_type,
363
                                                                        ref,
364
                                                                        info['name'],
365
                                                                        vcenter_uuid,
366
                                                                        vcenter_instance_name,
367
                                                                        dc_name)
314 368
                    network_objects[dc_name] << one_vnet
315
                end #network clusters loop
369
                end
370

  
316 371
            end # network loop
317 372
        end #datacenters loop
318 373

  

Also available in: Unified diff