Class: VCenterDriver::Datastore
Overview
Constant Summary
Constants inherited
from Storage
Storage::CURLBIN
Instance Attribute Summary collapse
Attributes inherited from Storage
#item
Class Method Summary
collapse
Instance Method Summary
collapse
-
#all_images ⇒ Object
-
#copy_virtual_disk(src_path, target_ds, target_path, new_size = nil) ⇒ Object
-
#dc_path ⇒ Object
-
#delete_file(img_name) ⇒ Object
-
#delete_virtual_disk(img_name) ⇒ Object
-
#descriptor?(remote_path) ⇒ Boolean
-
#dir_empty?(path) ⇒ Boolean
-
#download_file(source_path, target_path) ⇒ Object
-
#download_to_stdout(remote_path) ⇒ Object
-
#generate_file_url(path) ⇒ Object
-
#get_search_params(ds_name, img_path = nil, img_name = nil) ⇒ Object
-
#get_text_file(remote_path) ⇒ Object
-
#initialize(item, vi_client = nil) ⇒ Datastore
constructor
A new instance of Datastore.
-
#move_virtual_disk(disk, dest_path, dest_dsid, vi_client = nil) ⇒ Object
-
#rm_directory(directory) ⇒ Object
-
#stat(img_str) ⇒ Object
Get file size for image handling.
-
#upload_file(source_path, target_path) ⇒ Object
Methods inherited from Storage
#create_directory, #create_virtual_disk, disk_or_iso?, exists_one_by_ref_dc_and_type?, get_image_import_template, get_one_image_ds_by_ref_and_dc, #monitor, #obtain_dc, #obtain_fm, #obtain_vdm, #to_one, #to_one_template
Methods included from Memoize
#[], #[]=, #clear, #clear_all
Constructor Details
#initialize(item, vi_client = nil) ⇒ Datastore
Returns a new instance of Datastore.
383
384
385
386
387
388
389
390
|
# File 'lib/datastore.rb', line 383
def initialize(item, vi_client = nil)
check_item(item, RbVmomi::VIM::Datastore)
@vi_client = vi_client
@item = item
@one_item = {}
super()
end
|
Instance Attribute Details
#one_item ⇒ Object
Returns the value of attribute one_item.
381
382
383
|
# File 'lib/datastore.rb', line 381
def one_item
@one_item
end
|
Class Method Details
.detach_and_destroy(disk, vm, disk_id, prev_ds_ref, vi_client) ⇒ Object
detach disk from vCenter vm if possible, destroy the disk on FS
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
|
# File 'lib/datastore.rb', line 844
def self.detach_and_destroy(disk, vm, disk_id, prev_ds_ref, vi_client)
is_cd = !(disk['CLONE'].nil? || disk['CLONE'] == 'YES')
begin
ds_ref, img_path = vm.detach_disk(disk)
return if is_cd
if !(ds_ref && img_path && !img_path.empty?)
img_path = vm.disk_real_path(disk, disk_id)
ds_ref = prev_ds_ref
end
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
search_params = ds.get_search_params(ds['name'],
File.dirname(img_path),
File.basename(img_path))
search_task = ds['browser']
.SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
ds.delete_virtual_disk(img_path)
img_dir = File.dirname(img_path)
ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
rescue StandardError => e
if !e.message.start_with?('FileNotFound')
raise e.message end
end
end
|
.new_from_ref(ref, vi_client) ⇒ Object
839
840
841
|
# File 'lib/datastore.rb', line 839
def self.new_from_ref(ref, vi_client)
new(RbVmomi::VIM::Datastore.new(vi_client.vim, ref), vi_client)
end
|
Instance Method Details
#all_images ⇒ Object
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
# File 'lib/datastore.rb', line 698
def all_images
images = {}
imid = -1
ds_id = nil
ds_name = self['name']
ipool = VCenterDriver::VIHelper
.one_pool(OpenNebula::ImagePool, false)
if ipool.respond_to?(:message)
raise "Could not get OpenNebula ImagePool: #{pool.message}"
end
dpool = VCenterDriver::VIHelper
.one_pool(OpenNebula::DatastorePool, false)
if dpool.respond_to?(:message)
raise "Could not get OpenNebula DatastorePool: #{pool.message}"
end
ds_id = @one_item['ID']
begin
search_params = get_search_params(ds_name)
search_task = self['browser']
.SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
search_task.info.result.each do |result|
folderpath = ''
size = result.folderPath.size
if result.folderPath[-1] != ']'
if result.folderPath[-1] != '/'
result.folderPath[size] = '/'
end
folderpath = result
.folderPath.sub(/^\[#{ds_name}\] /, '')
end
result.file.each do |image|
image_path = ''
next unless ['FloppyImageFileInfo',
'IsoImageFileInfo',
'VmDiskFileInfo'].include? image.class.to_s
image_path << folderpath << image.path
image_name = File.basename(image.path)
.reverse.sub('kdmv.', '').reverse
if image.class.to_s == 'VmDiskFileInfo'
image_type = 'OS'
else
image_type = 'CDROM'
end
image_size = image.capacityKb / 1024 rescue nil
image_size ||= image.fileSize / 1024 / 1024 rescue nil
controller = image.controllerType rescue nil
if controller
if controller == 'VirtualIDEController'
disk_prefix = 'hd'
else
disk_prefix = 'sd'
end
else
disk_prefix = VCenterDriver::VIHelper
.get_default(
'IMAGE/TEMPLATE/DEV_PREFIX'
)
end
key = "#{image_name}#{ds_name}#{image_path}"
import_name = VCenterDriver::VIHelper
.one_name(
OpenNebula::ImagePool,
image_name,
key,
ipool
)
one_image = "NAME=\"#{import_name}\"\n"
one_image << "PATH=\"vcenter://#{image_path}\"\n"
one_image << "PERSISTENT=\"NO\"\n"
one_image << "TYPE=\"#{image_type}\"\n"
unless CONFIG[:delete_images]
one_image << "VCENTER_IMPORTED=\"YES\"\n"
end
one_image << "DEV_PREFIX=\"#{disk_prefix}\"\n"
image_found = VCenterDriver::VIHelper
.find_image_by(
'SOURCE',
OpenNebula::ImagePool,
image_path,
ds_id,
ipool
)
next if image_found
images[import_name] = {
:import_id => imid+=1,
:name => import_name,
:ref => import_name,
:path => image_path,
:size => image_size.to_s,
:type => image.class.to_s,
:dsid => ds_id,
:one => one_image
}
end
end
rescue StandardError => e
raise "Could not find images. \
Reason: #{e.message}/#{e.backtrace}"
end
vname = @vi_client.vc_name || ''
{ vname => images }
end
|
#copy_virtual_disk(src_path, target_ds, target_path, new_size = nil) ⇒ Object
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/datastore.rb', line 427
def copy_virtual_disk(src_path, target_ds, target_path, new_size = nil)
source_ds_name = self['name']
target_ds_name = target_ds['name']
leading_dirs = target_path.split('/')[0..-2]
if !leading_dirs.empty?
if source_ds_name == target_ds_name
create_directory(leading_dirs.join('/'))
else
target_ds.create_directory(leading_dirs.join('/'))
end
end
copy_params = {
:sourceName => "[#{source_ds_name}] #{src_path}",
:sourceDatacenter => obtain_dc.item
}
if File.extname(src_path) == '.vmdk'
copy_params[:destName] = "[#{target_ds_name}] #{target_path}"
obtain_vdm.CopyVirtualDisk_Task(copy_params).wait_for_completion
if new_size
resize_spec = {
:name => "[#{target_ds_name}] #{target_path}",
:datacenter => target_ds.obtain_dc.item,
:newCapacityKb => new_size,
:eagerZero => false
}
obtain_vdm.ExtendVirtualDisk_Task(
resize_spec
).wait_for_completion
end
else
copy_params[:destinationName] =
"[#{target_ds_name}] #{target_path}"
obtain_fm.CopyDatastoreFile_Task(
copy_params
).wait_for_completion
end
target_path
end
|
#dc_path ⇒ Object
616
617
618
619
620
621
622
623
624
625
626
|
# File 'lib/datastore.rb', line 616
def dc_path
dc = obtain_dc
p = dc.item.parent
path = [dc.item.name]
while p.instance_of? RbVmomi::VIM::Folder
path.unshift(p.name)
p = p.parent
end
path.delete_at(0) path.join('/')
end
|
#delete_file(img_name) ⇒ Object
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/datastore.rb', line 409
def delete_file(img_name)
ds_name = self['name']
begin
obtain_fm.DeleteDatastoreFile_Task(
:name => "[#{ds_name}] #{img_name}",
:datacenter => obtain_dc.item
).wait_for_completion
rescue StandardError => e
if !e.message.start_with?('ManagedObjectNotFound') &&
!e.message.start_with?('FileNotFound')
raise e
end
end
end
|
#delete_virtual_disk(img_name) ⇒ Object
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
# File 'lib/datastore.rb', line 392
def delete_virtual_disk(img_name)
ds_name = self['name']
begin
obtain_vdm.DeleteVirtualDisk_Task(
:name => "[#{ds_name}] #{img_name}",
:datacenter => obtain_dc.item
).wait_for_completion
rescue StandardError => e
if !e.message.start_with?('ManagedObjectNotFound') &&
!e.message.start_with?('FileNotFound')
raise e
end
end
end
|
#descriptor?(remote_path) ⇒ Boolean
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
# File 'lib/datastore.rb', line 657
def descriptor?(remote_path)
url = generate_file_url(remote_path)
rout, wout = IO.pipe
pid = spawn(CURLBIN,
'-I', '-k', '--noproxy', '*', '-f',
'-b', self['_connection.cookie'],
url,
:out => wout,
:err => '/dev/null')
Process.waitpid(pid, 0)
raise 'read image header failed' unless $?.success?
wout.close
size = rout.readlines.select do |l|
l.downcase.start_with?('content-length')
end[0].downcase.sub('content-length: ', '')
rout.close
size.chomp.to_i < 4096 end
|
#dir_empty?(path) ⇒ Boolean
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
# File 'lib/datastore.rb', line 516
def dir_empty?(path)
ds_name = self['name']
spec = RbVmomi::VIM::HostDatastoreBrowserSearchSpec.new
search_params = {
'datastorePath' => "[#{ds_name}] #{path}",
'searchSpec' => spec
}
begin
search_task = self['browser']
.SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
!search_task.info.result.nil? &&
search_task.info.result.length == 1 &&
search_task.info.result.first.file.empty?
rescue StandardError
false
end
end
|
#download_file(source_path, target_path) ⇒ Object
542
543
544
|
# File 'lib/datastore.rb', line 542
def download_file(source_path, target_path)
@item.download(source_path, target_path)
end
|
#download_to_stdout(remote_path) ⇒ Object
646
647
648
649
650
651
652
653
654
655
|
# File 'lib/datastore.rb', line 646
def download_to_stdout(remote_path)
url = generate_file_url(remote_path)
pid = spawn(CURLBIN,
'-k', '--noproxy', '*', '-f',
'-b', self['_connection.cookie'],
url)
Process.waitpid(pid, 0)
raise 'download failed' unless $?.success? end
|
#generate_file_url(path) ⇒ Object
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
# File 'lib/datastore.rb', line 628
def generate_file_url(path)
if self['_connection.http.use_ssl?']
protocol = 'https://'
else
protocol = 'http://'
end
hostname = self['_connection.http.address']
port = self['_connection.http.port']
dcpath = dc_path
url_path = "folder/#{path}?dcPath=#{dcpath}&dsName=#{self['name']}"
url = "#{protocol}#{hostname}:#{port}/#{url_path}"
URI.escape(url) end
|
#get_search_params(ds_name, img_path = nil, img_name = nil) ⇒ Object
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
# File 'lib/datastore.rb', line 583
def get_search_params(ds_name, img_path = nil, img_name = nil)
spec = RbVmomi::VIM::HostDatastoreBrowserSearchSpec.new
vmdisk_query = RbVmomi::VIM::VmDiskFileQuery.new
vmdisk_query.details = RbVmomi::VIM::VmDiskFileQueryFlags(
:diskType => true,
:capacityKb => true,
:hardwareVersion => true,
:controllerType => true
)
spec.query = [vmdisk_query,
RbVmomi::VIM::IsoImageFileQuery.new]
spec.details = RbVmomi::VIM::FileQueryFlags(
:fileOwner => true,
:fileSize => true,
:fileType => true,
:modification => true
)
if img_name.nil?
spec.matchPattern = []
else
spec.matchPattern = [img_name]
end
datastore_path = "[#{ds_name}]"
datastore_path << " #{img_path}" unless img_path.nil?
{ 'datastorePath' => datastore_path,
'searchSpec' => spec }
end
|
#get_text_file(remote_path) ⇒ Object
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
# File 'lib/datastore.rb', line 679
def get_text_file(remote_path)
url = generate_file_url(remote_path)
rout, wout = IO.pipe
pid = spawn CURLBIN, '-k', '--noproxy', '*', '-f',
'-b', self['_connection.cookie'],
url,
:out => wout,
:err => '/dev/null'
Process.waitpid(pid, 0)
raise 'get text file failed' unless $?.success?
wout.close
output = rout.readlines
rout.close
output
end
|
#move_virtual_disk(disk, dest_path, dest_dsid, vi_client = nil) ⇒ Object
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
# File 'lib/datastore.rb', line 472
def move_virtual_disk(disk, dest_path, dest_dsid, vi_client = nil)
vi_client ||= @vi_client
target_ds = VCenterDriver::VIHelper.one_item(
OpenNebula::Datastore,
dest_dsid,
false
)
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']
target_ds_vc = VCenterDriver::Datastore
.new_from_ref(
target_ds_ref,
vi_client
)
dest_name = target_ds_vc['name']
target_ds_vc.create_directory(File.dirname(dest_path))
dpath_ds = "[#{dest_name}] #{dest_path}"
orig_path = "[#{self['name']}] #{disk.path}"
move_params = {
:sourceName => orig_path,
:sourceDatacenter => obtain_dc.item,
:destName => dpath_ds,
:force => true
}
obtain_vdm.MoveVirtualDisk_Task(move_params).wait_for_completion
end
|
#rm_directory(directory) ⇒ Object
503
504
505
506
507
508
509
510
511
512
513
514
|
# File 'lib/datastore.rb', line 503
def rm_directory(directory)
ds_name = self['name']
rm_directory_params = {
:name => "[#{ds_name}] #{directory}",
:datacenter => obtain_dc.item
}
obtain_fm.DeleteDatastoreFile_Task(
rm_directory_params
).wait_for_completion
end
|
#stat(img_str) ⇒ Object
Get file size for image handling
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
# File 'lib/datastore.rb', line 547
def stat(img_str)
ds_name = self['name']
img_path = File.dirname img_str
img_name = File.basename img_str
search_params = get_search_params(ds_name, img_path, img_name)
begin
search_task = self['browser']
.SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
size = search_task
.info.result[0].file[0].capacityKb / 1024 rescue nil
size ||= search_task
.info
.result[0].file[0].fileSize / 1024 / 1024 rescue nil
raise 'Could not get file size or capacity' if size.nil?
size
rescue StandardError => e
message = "Could not find file. Reason: \"#{e.message}\"."
if VCenterDriver::CONFIG[:debug_information]
message += ' ' + e.backtrace.to_s
end
raise message
end
end
|
#upload_file(source_path, target_path) ⇒ Object
538
539
540
|
# File 'lib/datastore.rb', line 538
def upload_file(source_path, target_path)
@item.upload(target_path, source_path)
end
|