Class: VsphereHelper
- Inherits:
-
Object
- Object
- VsphereHelper
- Defined in:
- lib/beaker/hypervisor/vsphere_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #find_customization(name) ⇒ Object
- #find_datastore(dc, datastorename) ⇒ Object
- #find_folder(dc, foldername) ⇒ Object
- #find_pool(dc, poolname) ⇒ Object
- #find_snapshot(vm, snapname) ⇒ Object
-
#find_vms(names, connection = @connection) ⇒ Object
an easier wrapper around the horrid PropertyCollector interface, necessary for searching VMs in all Datacenters that may be nested within folders of arbitrary depth returns a hash array of <name> => <VirtualMachine ManagedObjects>.
- #get_base_vm_container_from(connection) ⇒ Object
-
#initialize(vInfo) ⇒ VsphereHelper
constructor
A new instance of VsphereHelper.
- #search_child_snaps(tree, snapname) ⇒ Object
- #wait_for_tasks(tasks, try, attempts) ⇒ Object
Constructor Details
#initialize(vInfo) ⇒ VsphereHelper
Returns a new instance of VsphereHelper.
6 7 8 9 10 11 12 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 6 def initialize vInfo @logger = vInfo[:logger] || Beaker::Logger.new @connection = RbVmomi::VIM.connect :host => vInfo[:server], :user => vInfo[:user], :password => vInfo[:pass], :insecure => true end |
Class Method Details
.load_config(dot_fog = '.fog') ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 14 def self.load_config(dot_fog = '.fog') default = get_fog_credentials(dot_fog) vsphere_credentials = {} vsphere_credentials[:server] = default[:vsphere_server] vsphere_credentials[:user] = default[:vsphere_username] vsphere_credentials[:pass] = default[:vsphere_password] return vsphere_credentials end |
Instance Method Details
#close ⇒ Object
187 188 189 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 187 def close @connection.close end |
#find_customization(name) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 45 def find_customization name csm = @connection.serviceContent.customizationSpecManager begin customizationSpec = csm.GetCustomizationSpec({:name => name}).spec rescue customizationSpec = nil end return customizationSpec end |
#find_datastore(dc, datastorename) ⇒ Object
108 109 110 111 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 108 def find_datastore(dc,datastorename) datacenter = @connection.serviceInstance.find_datacenter(dc) datacenter.find_datastore(datastorename) end |
#find_folder(dc, foldername) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 113 def find_folder(dc,foldername) datacenter = @connection.serviceInstance.find_datacenter(dc) base = datacenter.vmFolder.traverse(foldername) if base != nil base else abort "Failed to find folder #{foldername}" end end |
#find_pool(dc, poolname) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 123 def find_pool(dc,poolname) datacenter = @connection.serviceInstance.find_datacenter(dc) base = datacenter.hostFolder pools = poolname.split('/') pools.each do |pool| case base when RbVmomi::VIM::Folder base = base.childEntity.find { |f| f.name == pool } when RbVmomi::VIM::ClusterComputeResource base = base.resourcePool.resourcePool.find { |f| f.name == pool } when RbVmomi::VIM::ResourcePool base = base.resourcePool.find { |f| f.name == pool } else abort "Unexpected object type encountered (#{base.class}) while finding resource pool" end end base = base.resourcePool unless base.is_a?(RbVmomi::VIM::ResourcePool) and base.respond_to?(:resourcePool) base end |
#find_snapshot(vm, snapname) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 25 def find_snapshot vm, snapname if vm.snapshot search_child_snaps vm.snapshot.rootSnapshotList, snapname else raise "vm #{vm.name} has no snapshots to revert to" end end |
#find_vms(names, connection = @connection) ⇒ Object
an easier wrapper around the horrid PropertyCollector interface, necessary for searching VMs in all Datacenters that may be nested within folders of arbitrary depth returns a hash array of <name> => <VirtualMachine ManagedObjects>
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 61 def find_vms names, connection = @connection names = names.is_a?(Array) ? names : [ names ] containerView = get_base_vm_container_from connection propertyCollector = connection.propertyCollector objectSet = [{ :obj => containerView, :skip => true, :selectSet => [ RbVmomi::VIM::TraversalSpec.new({ :name => 'gettingTheVMs', :path => 'view', :skip => false, :type => 'ContainerView' }) ] }] propSet = [{ :pathSet => [ 'name' ], :type => 'VirtualMachine' }] results = propertyCollector.RetrievePropertiesEx({ :specSet => [{ :objectSet => objectSet, :propSet => propSet }], :options => { :maxObjects => nil } }) vms = {} results.objects.each do |result| name = result.propSet.first.val next unless names.include? name vms[name] = result.obj end while results.token do results = propertyCollector.ContinueRetrievePropertiesEx({:token => results.token}) results.objects.each do |result| name = result.propSet.first.val next unless names.include? name vms[name] = result.obj end end vms end |
#get_base_vm_container_from(connection) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 144 def get_base_vm_container_from connection viewManager = connection.serviceContent.viewManager viewManager.CreateContainerView({ :container => connection.serviceContent.rootFolder, :recursive => true, :type => [ 'VirtualMachine' ] }) end |
#search_child_snaps(tree, snapname) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 33 def search_child_snaps tree, snapname snapshot = nil tree.each do |child| if child.name == snapname snapshot ||= child.snapshot else snapshot ||= search_child_snaps child.childSnapshotList, snapname end end snapshot end |
#wait_for_tasks(tasks, try, attempts) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 153 def wait_for_tasks tasks, try, attempts obj_set = tasks.map { |task| { :obj => task } } filter = @connection.propertyCollector.CreateFilter( :spec => { :propSet => [{ :type => 'Task', :all => false, :pathSet => ['info.state']}], :objectSet => obj_set }, :partialUpdates => false ) ver = '' while true result = @connection.propertyCollector.WaitForUpdates(:version => ver) ver = result.version complete = 0 tasks.each do |task| if ['success', 'error'].member? task.info.state complete += 1 end end break if (complete == tasks.length) if try <= attempts sleep 5 try += 1 else raise "unable to complete Vsphere tasks before timeout" end end filter.DestroyPropertyFilter tasks end |