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

View differences:

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

  
17 17
        dc_folder = VCenterDriver::DatacenterFolder.new(vi_client)
18 18

  
19
        # Get vcenter intance uuid as moref is unique for each vcenter
20
        vc_uuid = vi_client.vim.serviceContent.about.instanceUuid
21

  
22
        # Get vcenter API version
23
        vc_version = vi_client.vim.serviceContent.about.apiVersion
19
        vcenter_instance_name = vi_client.vim.host
24 20

  
25 21
        # OpenNebula's ClusterPool
26 22
        cpool = VCenterDriver::VIHelper.one_pool(OpenNebula::ClusterPool, false)
......
29 25
            raise "Could not get OpenNebula ClusterPool: #{cpool.message}"
30 26
        end
31 27

  
32
        cpool.info
33

  
34 28
        cluster_list = {}
35 29
        cpool.each do |c|
36 30
            cluster_list[c["ID"]] = c["NAME"]
......
43 37
            raise "Could not get OpenNebula HostPool: #{hpool.message}"
44 38
        end
45 39

  
46
        rs = dc_folder.get_unimported_hosts(hpool)
40
        rs = dc_folder.get_unimported_hosts(hpool,vcenter_instance_name)
47 41

  
48 42
        STDOUT.print "done!\n\n"
49 43

  
......
175 169
            end
176 170

  
177 171
            tmps.each{ |t|
172
                template = nil
173
                template_copy_ref = nil
174
                template_xml = nil
178 175

  
179 176
                if !use_defaults
180 177
                    STDOUT.print "\n  * VM Template found:\n"\
......
186 183
                    next if STDIN.gets.strip.downcase != 'y'
187 184
                end
188 185

  
186
                # Linked Clones
187
                if !use_defaults
188

  
189
                    template = VCenterDriver::Template.new_from_ref(t[:vcenter_ref], vi_client)
190

  
191
                    STDOUT.print "\n    For faster deployment operations"\
192
                                 " and lower disk usage, OpenNebula"\
193
                                 " can create new VMs as linked clones."\
194
                                 "\n    Would you like to use Linked Clones with VMs based on this template (y/[n])? "
195

  
196
                    if STDIN.gets.strip.downcase == 'y'
197

  
198
                        STDOUT.print "\n    Linked clones requires that delta"\
199
                                     " disks must be created for each disk in the template."\
200
                                     " This operation may change the template contents."\
201
                                     " \n    Do you want OpenNebula to create a copy of the template,"\
202
                                     " so the original template remains untouched ([y]/n)? "
203

  
204
                        template = t[:template]
205
                        if STDIN.gets.strip.downcase != 'n'
206

  
207
                            STDOUT.print "\n    The new template will be named"\
208
                                         " adding a one- prefix to the name"\
209
                                         " of the original template. \n"\
210
                                         "    If you prefer a different name"\
211
                                         " please specify or press Enter"\
212
                                         " to use defaults: "
213

  
214
                            template_name = STDIN.gets.strip.downcase
215

  
216
                            STDOUT.print "\n    WARNING!!! The cloning operation can take some time"\
217
                                         " depending on the size of disks. Please wait...\n"
218

  
219

  
220
                            error, template_copy_ref = template.create_template_copy(template_name)
221

  
222
                            if template_copy_ref
223

  
224
                                template = VCenterDriver::Template.new_from_ref(template_copy_ref, vi_client)
225

  
226
                                one_template = VCenterDriver::Template.get_xml_template(template, vc_uuid, vi_client, options[:vcenter], dc)
227

  
228
                                if one_template
229
                                    #Now create delta disks
230
                                    STDOUT.print "\n    Delta disks are being created, please be patient..."
231

  
232
                                    lc_error, use_lc = template.create_delta_disks
233
                                    if lc_error
234
                                        STDOUT.print "\n    ERROR. Something was wrong with the create delta disks on the template operation: #{lc_error}.\n"\
235
                                                    "\n    Linked Clones will not be used with this template.\n"
236
                                    else
237
                                        one_template[:one] << "\nVCENTER_LINKED_CLONES=\"YES\"\n"
238
                                        t = one_template
239
                                    end
240
                                else
241
                                    STDOUT.print "\n    ERROR. Something was wrong obtaining the info from the template's copy.\n"\
242
                                                 "\n    Linked Clones will not be used with this template.\n"
243
                                    template.delete_template if template_copy_ref
244
                                end
245

  
246
                            else
247
                                STDOUT.print "\n    ERROR. #{error}\n"
248
                            end
249

  
250
                        else
251
                            # Create linked clones on top of the existing template
252
                            # Create a VirtualMachine object from the template_copy_ref
253
                            STDOUT.print "\n    Delta disks are being created, please be patient..."
254

  
255
                            lc_error, use_lc = template.create_delta_disks
256
                            if lc_error
257
                                STDOUT.print "\n    ERROR. Something was wrong with the create delta disks on the template operation: #{lc_error}.\n"\
258
                                             "\n    Linked Clones will not be used with this template.\n"
259
                            end
260
                            t[:one] << "\nVCENTER_LINKED_CLONES=\"YES\"\n" if use_lc
261
                        end
262
                    end
263
                end
264

  
265
                vcenter_vm_folder = ""
266
                if !use_defaults
267
                    STDOUT.print "\n\n    Do you want to specify a folder where"\
268
                                    " the deployed VMs based on this template will appear"\
269
                                    " in vSphere's VM and Templates section?"\
270
                                    "\n    If no path is set, VMs will be placed in the same"\
271
                                    " location where the template lives."\
272
                                    "\n    Please specify a path using slashes to separate folders"\
273
                                    " e.g /Management/VMs or press Enter to use defaults: "\
274

  
275
                    vcenter_vm_folder = STDIN.gets.strip
276
                    t[:one] << "VCENTER_VM_FOLDER=\"#{vcenter_vm_folder}\"\n" if !vcenter_vm_folder.empty?
277
                end
278

  
189 279
                ## Add existing disks to template (OPENNEBULA_MANAGED)
190 280

  
191
                template = t[:template]
281
                STDOUT.print "\n    The existing disks and networks in the template"\
282
                             " are being imported, please be patient..."
283

  
284
                template = t[:template] if !template
285

  
192 286

  
193 287
                error, template_disks = template.import_vcenter_disks(vc_uuid,
194 288
                                                                      dpool,
......
198 292
                    t[:one] << template_disks
199 293
                else
200 294
                    STDOUT.puts error
295
                    template.delete_template if template_copy_ref
201 296
                    next
202 297
                end
203 298

  
204 299
                error, template_nics = template.import_vcenter_nics(vc_uuid,
205
                                                                    npool)
300
                                                                   npool)
206 301
                if error.empty?
207 302
                    t[:one] << template_nics
208 303
                else
209 304
                    STDOUT.puts error
305
                    template.delete_template if template_copy_ref
210 306
                    next
211 307
                end
212 308

  
......
217 313
                if !use_defaults
218 314

  
219 315
                    if rp_split.size > 3
220
                        STDOUT.print "\n    This template is currently set to "\
316
                        STDOUT.print "\n\n    This template is currently set to "\
221 317
                            "launch VMs in the default resource pool."\
222 318
                            "\n    Press y to keep this behaviour, n to select"\
223 319
                            " a new resource pool or d to delegate the choice"\
......
238 334
                                        "\"#{list_of_rp}\""
239 335
                            input_str+= "\n    Press y to agree, or input a comma"\
240 336
                                        " separated list of resource pools to edit "\
241
                                        "[y/comma separated list] "
337
                                        "([y]/comma separated list) "
242 338
                            STDOUT.print input_str
243 339

  
244 340
                            answer = STDIN.gets.strip
245 341

  
246
                            if answer.downcase == 'y'
247
                                rp_input += rp_split[3] + "|"
248
                            else
342
                            if !answer.empty? && answer.downcase != 'y'
249 343
                                rp_input += answer + "|"
344
                            else
345
                                rp_input += rp_split[3] + "|"
250 346
                            end
251 347

  
252 348
                            # Default
......
254 350
                                            "to the end user is set to"\
255 351
                                            " \"#{default_rp}\"."
256 352
                            input_str+= "\n    Press y to agree, or input a new "\
257
                                        "resource pool [y/resource pool name] "
353
                                        "resource pool ([y]/resource pool name) "
258 354
                            STDOUT.print input_str
259 355

  
260 356
                            answer = STDIN.gets.strip
261 357

  
262
                            if answer.downcase == 'y'
263
                                rp_input += rp_split[4]
264
                            else
358
                            if !answer.empty? && answer.downcase != 'y'
265 359
                                rp_input += answer
360
                            else
361
                                rp_input += rp_split[4]
266 362
                            end
267 363
                        when 'n'
268 364

  
269 365
                            list_of_rp   = rp_split[-2]
270 366

  
271
                            input_str = "    The list of available resource pools is:\n"
272

  
273
                            STDOUT.print input_str
274

  
275
                            dashes = ""
276
                            100.times do
277
                                dashes << "-"
278
                            end
279

  
280
                            list_str = "\n    [Index] Resource pool :"\
281
                                    "\n    #{dashes}\n"
282

  
283
                            STDOUT.print list_str
367
                            STDOUT.print "    The list of available resource pools is:\n\n"
284 368

  
285 369
                            index = 1
286
                            t[:rp_list].each do |rp|
287
                                list_str = "    [#{index}] #{rp[:name]}\n"
370
                            t[:rp_list].each do |r|
371
                                list_str = "    - #{r[:name]}\n"
288 372
                                index += 1
289 373
                                STDOUT.print list_str
290 374
                            end
291 375

  
292 376
                            input_str = "\n    Please input the new default"\
293
                                        " resource pool index in the list (e.g 1): "
377
                                        " resource pool name: "
294 378

  
295 379
                            STDOUT.print input_str
296 380

  
297 381
                            answer = STDIN.gets.strip
298 382

  
299
                            t[:one] << "VCENTER_RESOURCE_POOL=\"#{t[:rp_list][answer.to_i - 1][:name]}\"\n"
383
                            t[:one] << "VCENTER_RESOURCE_POOL=\"#{answer}\"\n"
300 384
                        end
301 385
                    end
302 386
                end
......
314 398

  
315 399
                if ::OpenNebula.is_error?(rc)
316 400
                    STDOUT.puts "    Error creating template: #{rc.message}\n"
401
                    template.delete_template if template_copy_ref
317 402
                else
318 403
                    STDOUT.puts "    OpenNebula template #{one_t.id} created!\n"
319 404
                end
......
343 428

  
344 429
        dc_folder = VCenterDriver::DatacenterFolder.new(vi_client)
345 430

  
346
        # OpenNebula's ClusterPool
347
        cpool = VCenterDriver::VIHelper.one_pool(OpenNebula::ClusterPool, false)
348

  
349
        if cpool.respond_to?(:message)
350
            raise "Could not get OpenNebula ClusterPool: #{cpool.message}"
351
        end
352

  
353
        cpool.info
354

  
355
        cluster_list = {}
356
        cpool.each do |c|
357
            cluster_list[c["ID"]] = c["NAME"]
358
        end
359

  
360 431
        # OpenNebula's VirtualNetworkPool
361 432
        npool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool, false)
362 433

  
......
364 435
            raise "Could not get OpenNebula VirtualNetworkPool: #{npool.message}"
365 436
        end
366 437

  
367
        rs = dc_folder.get_unimported_networks(npool)
438
        rs = dc_folder.get_unimported_networks(npool,options[:vcenter])
368 439

  
369 440
        STDOUT.print "done!\n"
370 441

  
......
393 464
                    STDOUT.print print_str
394 465

  
395 466
                    next if STDIN.gets.strip.downcase != 'y'
396

  
397
                    if cluster_list.size > 1
398
                        STDOUT.print "\n    In which OpenNebula cluster do you want the network to be included?\n "
399

  
400
                        cluster_list_str = "\n"
401
                        cluster_list.each do |key, value|
402
                            cluster_list_str << "      - ID: " << key << " - NAME: " << value << "\n"
403
                        end
404

  
405
                        STDOUT.print "\n    #{cluster_list_str}"
406
                        STDOUT.print "\n    Specify the ID of the cluster or Enter to use the default cluster: "
407

  
408
                        answer = STDIN.gets.strip
409
                        one_cluster_id = answer if !answer.empty?
410
                    end
411

  
412 467
                end
413 468

  
414 469
                size="255"
......
499 554

  
500 555
                one_vn = VCenterDriver::VIHelper.new_one_item(OpenNebula::VirtualNetwork)
501 556

  
502
                if one_cluster_id
503
                    rc = one_vn.allocate(n[:one],one_cluster_id.to_i)
504
                else
505
                    rc = one_vn.allocate(n[:one])
506
                end
557
                rc = one_vn.allocate(n[:one])
507 558

  
508 559
                if ::OpenNebula.is_error?(rc)
509 560
                    STDOUT.puts "\n    Error creating virtual network: " +
......
538 589

  
539 590
        dc_folder = VCenterDriver::DatacenterFolder.new(vi_client)
540 591

  
541
        # OpenNebula's ClusterPool
542
        cpool = VCenterDriver::VIHelper.one_pool(OpenNebula::ClusterPool, false)
592
        dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false)
543 593

  
544
        if cpool.respond_to?(:message)
545
            raise "Could not get OpenNebula ClusterPool: #{cpool.message}"
594
        if dpool.respond_to?(:message)
595
            raise "Could not get OpenNebula DatastorePool: #{dpool.message}"
546 596
        end
547 597

  
548
        cpool.info
549

  
550
        cluster_list = {}
551
        cpool.each do |c|
552
            cluster_list[c["ID"]] = c["NAME"]
553
        end
554

  
555
        hpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false)
598
        # Get OpenNebula's host pool
599
        hpool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool, false)
556 600

  
557 601
        if hpool.respond_to?(:message)
558
            raise "Could not get OpenNebula DatastorePool: #{hpool.message}"
602
            raise "Could not get OpenNebula HostPool: #{hpool.message}"
559 603
        end
560 604

  
561
        rs = dc_folder.get_unimported_datastores(hpool)
605
        rs = dc_folder.get_unimported_datastores(dpool, options[:vcenter], hpool)
562 606

  
563 607
        STDOUT.print "done!\n"
564 608

  
......
585 629
                                    "    Import this as Datastore [y/n]? "
586 630

  
587 631
                    next if STDIN.gets.strip.downcase != 'y'
588

  
589
                    if cluster_list.size > 1
590
                        STDOUT.print "\n    In which OpenNebula cluster do you want the datastore to be included?\n "
591

  
592
                        cluster_list_str = "\n"
593
                        cluster_list.each do |key, value|
594
                            cluster_list_str << "      - ID: " << key << " - NAME: " << value << "\n"
595
                        end
596

  
597
                        STDOUT.print "\n    #{cluster_list_str}"
598
                        STDOUT.print "\n    Specify the ID of the cluster or Enter to use the default cluster: "
599

  
600
                        answer = STDIN.gets.strip
601
                        one_cluster_id = answer if !answer.empty?
602
                    end
603 632
                end
604 633

  
605 634
                one_d = VCenterDriver::VIHelper.new_one_item(OpenNebula::Datastore)

Also available in: Unified diff