Module: Deltacloud::Drivers::VSphere::Helper
- Included in:
- Deltacloud::Drivers::Vsphere::VsphereDriver
- Defined in:
- lib/deltacloud/drivers/vsphere/vsphere_client.rb
Instance Method Summary collapse
- #extract_architecture(text) ⇒ Object
-
#find_datastore(credentials, name) ⇒ Object
This helper will try to find a Datastore object in all Datacenters.
-
#find_resource_pool(credentials, name) ⇒ Object
Find a ResourcePool object associated by given Datastore ResourcePool is defined for Datacenter and is used for launching a new instance.
-
#find_vm(credentials, name) ⇒ Object
Find a VirtualMachine traversing through all Datastores and Datacenters.
-
#list_datastores(df) ⇒ Object
This helper will traverse across all datacenters and folders and gather all datastores available on vSphere.
-
#list_virtual_machines(credentials) ⇒ Object
This helper will traverse across all datacenters and datastores and gather all virtual machines available on vSphere.
- #load_serialized_instance(datastore, task_key) ⇒ Object
-
#map_task_to_instance(datastore, task_key, new_instance) ⇒ Object
Map given instance to task.
-
#stored_tasks(datastore, vsphere) ⇒ Object
Yield all tasks if they are included in mapper storage directory.
Instance Method Details
#extract_architecture(text) ⇒ Object
153 154 155 156 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 153 def extract_architecture(text) 'x86_64' if text.include?('64-bit') 'i386' if text.include?('32-bit') end |
#find_datastore(credentials, name) ⇒ Object
This helper will try to find a Datastore object in all Datacenters. Datastore is used to place instance on create to correct place
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 77 def find_datastore(credentials, name) vsphere = new_client(credentials) safely do rootFolder = vsphere.serviceInstance.content.rootFolder rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).collect do |dc| list_datastores(dc.datastoreFolder).each do |d| if d.name == name return d end end end end end |
#find_resource_pool(credentials, name) ⇒ Object
Find a ResourcePool object associated by given Datastore ResourcePool is defined for Datacenter and is used for launching a new instance
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 60 def find_resource_pool(credentials, name) vsphere = new_client(credentials) safely do rootFolder = vsphere.serviceInstance.content.rootFolder dc = rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).select do |dc| dc.datastoreFolder.childEntity.find { |d| d.name == name }.nil? == false end.flatten.compact.first dc = rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).first dc.hostFolder.childEntity.collect.first.resourcePool end end |
#find_vm(credentials, name) ⇒ Object
Find a VirtualMachine traversing through all Datastores and Datacenters
This helper will return a Hash: { :datastore => NAME_OF_DS, :instance => VM } Returning datastore is necesarry for constructing a correct realm for an instance
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 29 def find_vm(credentials, name) vsphere = new_client(credentials) safely do rootFolder = vsphere.serviceInstance.content.rootFolder vm = {} rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).each do |dc| dslist = list_datastores(dc.datastoreFolder) dslist.each do |datastore| vm[:instance] = datastore.vm.find { |x| x.name == name } if vm[:instance] vm[:datastore] = datastore.name break end stored_tasks(datastore, vsphere) do |task| if task.info.entity.class == RbVmomi::VIM::VirtualMachine and ['queued', 'running'].member? task.info.state vm = { :stored_instance => load_serialized_instance(datastore,task.info.key), :datastore => datastore.name } end end end break if vm[:datastore] end vm end end |
#list_datastores(df) ⇒ Object
This helper will traverse across all datacenters and folders and gather all datastores available on vSphere
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 114 def list_datastores(df) datastores = [] df.childEntity.each do |object| if object.class.to_s == 'Folder' datastores += list_datastores(object) else datastores << object end end datastores end |
#list_virtual_machines(credentials) ⇒ Object
This helper will traverse across all datacenters and datastores and gather all virtual machines available on vSphere
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 94 def list_virtual_machines(credentials) vsphere = new_client(credentials) vms = [] rootFolder = vsphere.serviceInstance.content.rootFolder rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).each do |dc| list_datastores(dc.datastoreFolder).each do |datastore| vms += datastore.vm.collect { |vm| { :instance => vm, :datastore => datastore.name } unless vm.nil? } stored_tasks(datastore, vsphere) do |task| if task.info.entity.class == RbVmomi::VIM::VirtualMachine vms << { :stored_instance => load_serialized_instance(datastore, task.info.key), :datastore => datastore.name } end end end end vms.flatten.compact end |
#load_serialized_instance(datastore, task_key) ⇒ Object
133 134 135 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 133 def load_serialized_instance(datastore, task_key) VSphere::FileManager::load_mapping(datastore, task_key) end |
#map_task_to_instance(datastore, task_key, new_instance) ⇒ Object
Map given instance to task. Task name is used as a filename.
128 129 130 131 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 128 def map_task_to_instance(datastore, task_key, new_instance) VSphere::FileManager::store_mapping!(datastore, YAML::dump(new_instance).to_s, task_key) new_instance end |
#stored_tasks(datastore, vsphere) ⇒ Object
Yield all tasks if they are included in mapper storage directory.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/deltacloud/drivers/vsphere/vsphere_client.rb', line 138 def stored_tasks(datastore, vsphere) tasks = VSphere::FileManager::list_mappings(datastore) return [] if tasks.empty? vsphere.serviceInstance.content.taskManager.recentTask.each do |task| if tasks.include?(task.info.key) and ['queued', 'running'].member?(task.info.state) yield task tasks.delete(task.info.key) end end # Delete old left tasks tasks.select { |f| f =~ /task-(\d+)/ }.each do |task| VSphere::FileManager::delete_mapping!(datastore, task) end end |