Class: VSphereCloud::Client
- Inherits:
-
Object
- Object
- VSphereCloud::Client
show all
- Includes:
- VimSdk
- Defined in:
- lib/cloud/vsphere/client.rb
Defined Under Namespace
Classes: AlreadyLoggedInException, DuplicateName, FileNotFoundException, NotLoggedInException, TaskException
Constant Summary
Constants included
from VimSdk
VimSdk::BASE_VERSION, VimSdk::DYNAMIC_TYPES, VimSdk::SOAP_BODY_END, VimSdk::SOAP_BODY_START, VimSdk::SOAP_BODY_TAG, VimSdk::SOAP_END, VimSdk::SOAP_ENVELOPE_END, VimSdk::SOAP_ENVELOPE_START, VimSdk::SOAP_ENVELOPE_TAG, VimSdk::SOAP_FAULT_TAG, VimSdk::SOAP_HEADER_END, VimSdk::SOAP_HEADER_START, VimSdk::SOAP_HEADER_TAG, VimSdk::SOAP_NAMESPACE_MAP, VimSdk::SOAP_START, VimSdk::VERSION1, VimSdk::XMLNS_SOAPENC, VimSdk::XMLNS_SOAPENV, VimSdk::XMLNS_VMODL_BASE, VimSdk::XMLNS_XSD, VimSdk::XMLNS_XSI, VimSdk::XML_ENCODING, VimSdk::XML_HEADER
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_persistent_disk_property_to_vm(vm, disk) ⇒ Object
-
#answer_vm(vm, question, answer) ⇒ Object
-
#create_datastore_folder(folder_path, datacenter) ⇒ Object
-
#create_disk(datacenter_mob, datastore, disk_cid, disk_folder, disk_size_in_mb, disk_type) ⇒ Object
-
#create_folder(name) ⇒ Object
-
#delete_disk(datacenter_mob, path) ⇒ Object
-
#delete_folder(folder) ⇒ Object
-
#delete_path(datacenter_mob, path) ⇒ Object
-
#delete_vm(vm) ⇒ Object
-
#find_all_stemcell_replicas(datacenter, stemcell_id) ⇒ Object
-
#find_all_stemcell_replicas_in_datastore(datacenter, stemcell_id, datastore_name) ⇒ Object
-
#find_by_inventory_path(path) ⇒ Object
-
#find_disk(disk_cid, datastore, disk_folder) ⇒ Object
-
#find_disk_size_using_browser(datastore, disk_cid, disk_folder) ⇒ Object
-
#find_parent(obj, parent_type) ⇒ Object
-
#find_vm_by_ip(ip) ⇒ Object
-
#find_vm_by_name(datacenter, vm_name) ⇒ Object
-
#get_cdrom_device(vm) ⇒ Object
-
#get_perf_counters(mobs, names, options = {}) ⇒ Object
-
#initialize(host, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#login(username, password, locale) ⇒ Object
-
#logout ⇒ Object
-
#move_disk(source_datacenter_mob, source_path, dest_datacenter_mob, dest_path) ⇒ Object
-
#power_off_vm(vm_mob) ⇒ Object
-
#power_on_vm(datacenter, vm) ⇒ Object
-
#reconfig_vm(vm, config) ⇒ Object
-
#remove_custom_field_def(name, mob_type) ⇒ Object
-
#set_custom_field(mob, name, value) ⇒ Object
-
#wait_for_task(task) ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ Client
Returns a new instance of Client.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/cloud/vsphere/client.rb', line 13
def initialize(host, options={})
@soap_stub = SoapStub.new(host, options[:soap_log]).create
@service_instance = Vim::ServiceInstance.new('ServiceInstance', @soap_stub)
begin
@service_content = @service_instance.content
rescue HTTPClient::ConnectTimeoutError => e
raise "Please make sure the CPI has proper network access to vSphere. (#{e.class}: #{e.message})"
end
@metrics_cache = {}
@lock = Mutex.new
@logger = options.fetch(:logger) { Bosh::Clouds::Config.logger }
@cloud_searcher = CloudSearcher.new(service_content, @logger)
end
|
Instance Attribute Details
#cloud_searcher ⇒ Object
Returns the value of attribute cloud_searcher.
11
12
13
|
# File 'lib/cloud/vsphere/client.rb', line 11
def cloud_searcher
@cloud_searcher
end
|
#service_content ⇒ Object
Returns the value of attribute service_content.
11
12
13
|
# File 'lib/cloud/vsphere/client.rb', line 11
def service_content
@service_content
end
|
#service_instance ⇒ Object
Returns the value of attribute service_instance.
11
12
13
|
# File 'lib/cloud/vsphere/client.rb', line 11
def service_instance
@service_instance
end
|
#soap_stub ⇒ Object
Returns the value of attribute soap_stub.
11
12
13
|
# File 'lib/cloud/vsphere/client.rb', line 11
def soap_stub
@soap_stub
end
|
Instance Method Details
#add_persistent_disk_property_to_vm(vm, disk) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/cloud/vsphere/client.rb', line 315
def add_persistent_disk_property_to_vm(vm, disk)
vm_config = Vim::Vm::ConfigSpec.new
v_app_config_spec = VimSdk::Vim::VApp::VmConfigSpec.new
v_app_property_spec = VimSdk::Vim::VApp::PropertySpec.new
v_app_property_info = VimSdk::Vim::VApp::PropertyInfo.new
vm_disk = vm.disk_by_cid(disk.cid)
disk_device_key = vm_disk.key
if vm.get_vapp_property_by_key(disk_device_key) != nil
@logger.debug("Disk property already exists '#{disk.cid}' on vm '#{vm.cid}'")
return
end
v_app_property_info.key = disk_device_key
v_app_property_info.id = disk.cid
v_app_property_info.label = disk.cid
v_app_property_info.category = 'BOSH Persistent Disks'
v_app_property_info.type = 'string'
v_app_property_info.value = disk.path
v_app_property_info.description = 'Used by BOSH to track persistent disks. Change at your own risk.'
v_app_property_info.user_configurable = true
v_app_property_spec.info = v_app_property_info
v_app_property_spec.operation = VimSdk::Vim::Option::ArrayUpdateSpec::Operation::ADD
v_app_config_spec.property << v_app_property_spec
vm_config.v_app_config = v_app_config_spec
reconfig_vm(vm.mob, vm_config)
end
|
#answer_vm(vm, question, answer) ⇒ Object
61
62
63
|
# File 'lib/cloud/vsphere/client.rb', line 61
def answer_vm(vm, question, answer)
vm.answer(question, answer)
end
|
#create_datastore_folder(folder_path, datacenter) ⇒ Object
131
132
133
|
# File 'lib/cloud/vsphere/client.rb', line 131
def create_datastore_folder(folder_path, datacenter)
@service_content.file_manager.make_directory(folder_path, datacenter, true)
end
|
#create_disk(datacenter_mob, datastore, disk_cid, disk_folder, disk_size_in_mb, disk_type) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/cloud/vsphere/client.rb', line 266
def create_disk(datacenter_mob, datastore, disk_cid, disk_folder, disk_size_in_mb, disk_type)
if disk_type.nil?
raise 'no disk type specified'
end
disk_path = "[#{datastore.name}] #{disk_folder}/#{disk_cid}.vmdk"
create_parent_folder(datacenter_mob, disk_path)
disk_spec = VimSdk::Vim::VirtualDiskManager::FileBackedVirtualDiskSpec.new
disk_spec.disk_type = disk_type
disk_spec.capacity_kb = disk_size_in_mb * 1024
disk_spec.adapter_type = 'lsiLogic'
task = service_content.virtual_disk_manager.create_virtual_disk(
disk_path,
datacenter_mob,
disk_spec
)
wait_for_task(task)
Resources::PersistentDisk.new(disk_cid, disk_size_in_mb, datastore, disk_folder)
end
|
#create_folder(name) ⇒ Object
135
136
137
|
# File 'lib/cloud/vsphere/client.rb', line 135
def create_folder(name)
@service_content.root_folder.create_folder(name)
end
|
#delete_disk(datacenter_mob, path) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/cloud/vsphere/client.rb', line 100
def delete_disk(datacenter_mob, path)
task = service_content.virtual_disk_manager.delete_virtual_disk(
path,
datacenter_mob
)
begin
wait_for_task(task)
rescue => e
unless e.message =~ /File .* was not found/
raise e
end
end
end
|
#delete_folder(folder) ⇒ Object
139
140
141
142
|
# File 'lib/cloud/vsphere/client.rb', line 139
def delete_folder(folder)
task = folder.destroy
wait_for_task(task)
end
|
#delete_path(datacenter_mob, path) ⇒ Object
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/cloud/vsphere/client.rb', line 89
def delete_path(datacenter_mob, path)
task = @service_content.file_manager.delete_file(path, datacenter_mob)
begin
wait_for_task(task)
rescue => e
unless e.message =~ /File .* was not found/
raise e
end
end
end
|
#delete_vm(vm) ⇒ Object
56
57
58
59
|
# File 'lib/cloud/vsphere/client.rb', line 56
def delete_vm(vm)
task = vm.destroy
wait_for_task(task)
end
|
#find_all_stemcell_replicas(datacenter, stemcell_id) ⇒ Object
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/cloud/vsphere/client.rb', line 163
def find_all_stemcell_replicas(datacenter, stemcell_id)
matches = []
yield_all_resources_by_name(datacenter, 'VirtualMachine') do |vm_mob, name|
if name =~ Regexp.new(stemcell_id)
matches << vm_mob
end
end
matches
end
|
#find_all_stemcell_replicas_in_datastore(datacenter, stemcell_id, datastore_name) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/cloud/vsphere/client.rb', line 174
def find_all_stemcell_replicas_in_datastore(datacenter, stemcell_id, datastore_name)
matches = []
yield_all_resources_by_name(datacenter, 'VirtualMachine') do |vm_mob, name|
if name =~ Regexp.new(stemcell_id)
vm_datastore = @cloud_searcher.get_property(vm_mob,
Vim::VirtualMachine, 'datastore', ensure_all: true)
if vm_datastore.first.name == datastore_name
matches << vm_mob
end
end
end
matches
end
|
#find_by_inventory_path(path) ⇒ Object
144
145
146
147
|
# File 'lib/cloud/vsphere/client.rb', line 144
def find_by_inventory_path(path)
full_path = Array(path).join("/")
@service_content.search_index.find_by_inventory_path(full_path)
end
|
#find_disk(disk_cid, datastore, disk_folder) ⇒ Object
261
262
263
264
|
# File 'lib/cloud/vsphere/client.rb', line 261
def find_disk(disk_cid, datastore, disk_folder)
disk_size_in_mb = find_disk_size_using_browser(datastore, disk_cid, disk_folder)
disk_size_in_mb.nil? ? nil : Resources::PersistentDisk.new(disk_cid, disk_size_in_mb, datastore, disk_folder)
end
|
#find_disk_size_using_browser(datastore, disk_cid, disk_folder) ⇒ Object
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/cloud/vsphere/client.rb', line 290
def find_disk_size_using_browser(datastore, disk_cid, disk_folder)
search_spec_details = VimSdk::Vim::Host::DatastoreBrowser::FileInfo::Details.new
search_spec_details.file_type = true
query_details = VimSdk::Vim::Host::DatastoreBrowser::VmDiskQuery::Details.new
query_details.capacity_kb = true
query = VimSdk::Vim::Host::DatastoreBrowser::VmDiskQuery.new
query.details = query_details
search_spec = VimSdk::Vim::Host::DatastoreBrowser::SearchSpec.new
search_spec.details = search_spec_details
search_spec.match_pattern = ["#{disk_cid}.vmdk"]
search_spec.query = [query]
datastore_path = "[#{datastore.name}] #{disk_folder}"
@logger.debug("Trying to find disk in : #{datastore_path}")
vm_disk_infos = wait_for_task(datastore.mob.browser.search("[#{datastore.name}] #{disk_folder}", search_spec)).file
return nil if vm_disk_infos.empty?
vm_disk_infos.first.capacity_kb / 1024
rescue VimSdk::SoapError, FileNotFoundException
nil
end
|
#find_parent(obj, parent_type) ⇒ Object
44
45
46
47
48
49
|
# File 'lib/cloud/vsphere/client.rb', line 44
def find_parent(obj, parent_type)
while obj && obj.class != parent_type
obj = @cloud_searcher.get_property(obj, obj.class, "parent", :ensure_all => true)
end
obj
end
|
#find_vm_by_ip(ip) ⇒ Object
149
150
151
|
# File 'lib/cloud/vsphere/client.rb', line 149
def find_vm_by_ip(ip)
@service_content.search_index.find_by_ip(nil, ip, true)
end
|
#find_vm_by_name(datacenter, vm_name) ⇒ Object
153
154
155
156
157
158
159
160
161
|
# File 'lib/cloud/vsphere/client.rb', line 153
def find_vm_by_name(datacenter, vm_name)
yield_all_resources_by_name(datacenter, 'VirtualMachine') do |vm_mob, name|
if name == vm_name
return vm_mob
end
end
nil
end
|
#get_cdrom_device(vm) ⇒ Object
84
85
86
87
|
# File 'lib/cloud/vsphere/client.rb', line 84
def get_cdrom_device(vm)
devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
devices.find { |device| device.kind_of?(Vim::Vm::Device::VirtualCdrom) }
end
|
#get_perf_counters(mobs, names, options = {}) ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/cloud/vsphere/client.rb', line 228
def get_perf_counters(mobs, names, options = {})
metrics = find_perf_metric_names(mobs.first, names)
metric_ids = metrics.values
metric_name_by_id = {}
metrics.each { |name, metric| metric_name_by_id[metric.counter_id] = name }
queries = []
mobs.each do |mob|
queries << Vim::PerformanceManager::QuerySpec.new(
:entity => mob,
:metric_id => metric_ids,
:format => Vim::PerformanceManager::Format::CSV,
:interval_id => options[:interval_id] || 20,
:max_sample => options[:max_sample])
end
query_perf_response = @service_content.perf_manager.query_stats(queries)
result = {}
query_perf_response.each do |mob_stats|
mob_entry = {}
counters = mob_stats.value
counters.each do |counter_stats|
counter_id = counter_stats.id.counter_id
values = counter_stats.value
mob_entry[metric_name_by_id[counter_id]] = values
end
result[mob_stats.entity] = mob_entry
end
result
end
|
#login(username, password, locale) ⇒ Object
31
32
33
34
|
# File 'lib/cloud/vsphere/client.rb', line 31
def login(username, password, locale)
raise AlreadyLoggedInException if @session
@session = @service_content.session_manager.login(username, password, locale)
end
|
#logout ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/cloud/vsphere/client.rb', line 36
def logout
raise NotLoggedInException unless @session
@session = nil
@service_content.session_manager.logout
rescue VimSdk::SoapError => e
@logger.info "Failed to logout: #{e.message}"
end
|
#move_disk(source_datacenter_mob, source_path, dest_datacenter_mob, dest_path) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/cloud/vsphere/client.rb', line 115
def move_disk(source_datacenter_mob, source_path, dest_datacenter_mob, dest_path)
create_parent_folder(dest_datacenter_mob, dest_path)
@logger.info("Moving disk: #{source_path} to #{dest_path}")
task = service_content.virtual_disk_manager.move_virtual_disk(
source_path,
source_datacenter_mob,
dest_path,
dest_datacenter_mob,
false,
nil
)
wait_for_task(task)
@logger.info('Moved disk')
end
|
#power_off_vm(vm_mob) ⇒ Object
79
80
81
82
|
# File 'lib/cloud/vsphere/client.rb', line 79
def power_off_vm(vm_mob)
task = vm_mob.power_off
wait_for_task(task)
end
|
#power_on_vm(datacenter, vm) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/cloud/vsphere/client.rb', line 65
def power_on_vm(datacenter, vm)
task = datacenter.power_on_vm([vm], nil)
result = wait_for_task(task)
raise 'Recommendations were detected, you may be running in Manual DRS mode. Aborting.' if result.recommendations.any?
if result.attempted.empty?
raise "Could not power on VM '#{vm}': #{result.not_attempted.map(&:fault).map(&:msg).join(', ')}"
else
task = result.attempted.first.task
wait_for_task(task)
end
end
|
#reconfig_vm(vm, config) ⇒ Object
51
52
53
54
|
# File 'lib/cloud/vsphere/client.rb', line 51
def reconfig_vm(vm, config)
task = vm.reconfigure(config)
wait_for_task(task)
end
|
#remove_custom_field_def(name, mob_type) ⇒ Object
381
382
383
384
385
386
387
388
389
390
391
|
# File 'lib/cloud/vsphere/client.rb', line 381
def remove_custom_field_def(name, mob_type)
@fields_manager ||= @service_content.custom_fields_manager
name = name.to_s
custom_fields = @fields_manager.field
field = custom_fields.find do |field|
field.name == name && field.managed_object_type == mob_type
end
unless field.nil?
@fields_manager.remove_field_definition(field.key)
end
end
|
#set_custom_field(mob, name, value) ⇒ Object
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
# File 'lib/cloud/vsphere/client.rb', line 348
def set_custom_field(mob, name, value)
@fields_manager ||= @service_content.custom_fields_manager
name = name.to_s
value = value.to_s
field = nil
begin
field = @fields_manager.add_field_definition(name, mob.class, nil, nil)
rescue SoapError => e
if e.fault.kind_of?(Vim::Fault::NoPermission)
@logger.warn("Can't create custom field definition due to lack of permission: #{e.message}")
elsif e.fault.kind_of?(Vim::Fault::DuplicateName)
@logger.warn("Custom field definition already exists: #{e.message}")
custom_fields = @fields_manager.field
field = custom_fields.find do |field|
field.name == name && field.managed_object_type == mob.class
end
else
raise e
end
end
unless field.nil?
begin
@fields_manager.set_field(mob, field.key, value)
rescue SoapError => e
if e.fault.kind_of?(Vim::Fault::NoPermission)
@logger.warn("Can't set custom fields due to lack of permission: #{e.message}")
end
end
end
end
|
#wait_for_task(task) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/cloud/vsphere/client.rb', line 189
def wait_for_task(task)
interval = 1.0
started = Time.now
loop do
properties = @cloud_searcher.get_properties(
[task],
Vim::Task,
["info.progress", "info.state", "info.result", "info.error"],
ensure: ["info.state"]
)[task]
duration = Time.now - started
raise "Task exceeded 60 minutes, task properties: #{properties}" if duration > 3600
if properties["info.progress"] && properties["info.progress"] > 0
interval = ((duration * 100 / properties["info.progress"]) - duration) / 5
if interval < 1
interval = 1
elsif interval > 10
interval = 10
elsif interval > duration
interval = duration
end
end
case properties["info.state"]
when Vim::TaskInfo::State::RUNNING
sleep(interval)
when Vim::TaskInfo::State::QUEUED
sleep(interval)
when Vim::TaskInfo::State::SUCCESS
return properties["info.result"]
when Vim::TaskInfo::State::ERROR
raise task_exception_for_vim_fault(properties["info.error"])
end
end
end
|