Class: VSphereCloud::Client
- Inherits:
-
Object
- Object
- VSphereCloud::Client
show all
- Includes:
- VimSdk
- Defined in:
- lib/cloud/vsphere/client.rb
Defined Under Namespace
Classes: AlreadyLoggedInException, NotLoggedInException
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
-
#answer_vm(vm, question, answer) ⇒ Object
-
#copy_disk(source_datacenter, source_path, dest_datacenter, dest_path) ⇒ Object
-
#create_datastore_folder(folder_path, datacenter) ⇒ Object
-
#create_folder(name) ⇒ Object
-
#delete_disk(datacenter, path) ⇒ Object
-
#delete_folder(folder) ⇒ Object
-
#delete_path(datacenter, path) ⇒ Object
-
#delete_vm(vm) ⇒ Object
-
#find_by_inventory_path(path) ⇒ Object
-
#find_parent(obj, parent_type) ⇒ Object
-
#get_cdrom_device(vm) ⇒ Object
-
#get_perf_counters(mobs, names, options = {}) ⇒ Object
-
#has_disk?(disk_path, disk_datacenter) ⇒ Boolean
-
#initialize(host, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#login(username, password, locale) ⇒ Object
-
#logout ⇒ Object
-
#move_disk(source_datacenter, source_path, dest_datacenter, dest_path) ⇒ Object
-
#move_into_folder(folder, objects) ⇒ Object
-
#move_into_root_folder(objects) ⇒ Object
-
#power_off_vm(vm) ⇒ Object
-
#power_on_vm(datacenter, vm) ⇒ Object
-
#reconfig_vm(vm, config) ⇒ Object
-
#wait_for_task(task) ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ Client
Returns a new instance of Client.
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/cloud/vsphere/client.rb', line 14
def initialize(host, options={})
@soap_stub = SoapStub.new(host, options[:soap_log]).create
@service_instance =Vim::ServiceInstance.new('ServiceInstance', @soap_stub)
@service_content = @service_instance.content
@metrics_cache = {}
@lock = Mutex.new
@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.
12
13
14
|
# File 'lib/cloud/vsphere/client.rb', line 12
def cloud_searcher
@cloud_searcher
end
|
#service_content ⇒ Object
Returns the value of attribute service_content.
12
13
14
|
# File 'lib/cloud/vsphere/client.rb', line 12
def service_content
@service_content
end
|
#service_instance ⇒ Object
Returns the value of attribute service_instance.
12
13
14
|
# File 'lib/cloud/vsphere/client.rb', line 12
def service_instance
@service_instance
end
|
#soap_stub ⇒ Object
Returns the value of attribute soap_stub.
12
13
14
|
# File 'lib/cloud/vsphere/client.rb', line 12
def soap_stub
@soap_stub
end
|
Instance Method Details
#answer_vm(vm, question, answer) ⇒ Object
55
56
57
|
# File 'lib/cloud/vsphere/client.rb', line 55
def answer_vm(vm, question, answer)
vm.answer(question, answer)
end
|
#copy_disk(source_datacenter, source_path, dest_datacenter, dest_path) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/cloud/vsphere/client.rb', line 122
def copy_disk(source_datacenter, source_path, dest_datacenter, dest_path)
tasks = []
[".vmdk", "-flat.vmdk"].each do |extension|
tasks << @service_content.file_manager.copy_file("#{source_path}#{extension}", source_datacenter,
"#{dest_path}#{extension}", dest_datacenter, false)
end
tasks.each { |task| wait_for_task(task) }
end
|
#create_datastore_folder(folder_path, datacenter) ⇒ Object
132
133
134
|
# File 'lib/cloud/vsphere/client.rb', line 132
def create_datastore_folder(folder_path, datacenter)
@service_content.file_manager.make_directory(folder_path, datacenter, true)
end
|
#create_folder(name) ⇒ Object
136
137
138
|
# File 'lib/cloud/vsphere/client.rb', line 136
def create_folder(name)
@service_content.root_folder.create_folder(name)
end
|
#delete_disk(datacenter, path) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/cloud/vsphere/client.rb', line 94
def delete_disk(datacenter, path)
tasks = []
[".vmdk", "-flat.vmdk"].each do |extension|
tasks << @service_content.file_manager.delete_file("#{path}#{extension}", datacenter)
end
tasks.each do |task|
begin
wait_for_task(task)
rescue => e
unless e.message =~ /File .* was not found/
raise e
end
end
end
end
|
#delete_folder(folder) ⇒ Object
150
151
152
153
|
# File 'lib/cloud/vsphere/client.rb', line 150
def delete_folder(folder)
task = folder.destroy
wait_for_task(task)
end
|
#delete_path(datacenter, path) ⇒ Object
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/cloud/vsphere/client.rb', line 83
def delete_path(datacenter, path)
task = @service_content.file_manager.delete_file(path, datacenter)
begin
wait_for_task(task)
rescue => e
unless e.message =~ /File .* was not found/
raise e
end
end
end
|
#delete_vm(vm) ⇒ Object
50
51
52
53
|
# File 'lib/cloud/vsphere/client.rb', line 50
def delete_vm(vm)
task = vm.destroy
wait_for_task(task)
end
|
#find_by_inventory_path(path) ⇒ Object
171
172
173
174
|
# File 'lib/cloud/vsphere/client.rb', line 171
def find_by_inventory_path(path)
full_path = Array(path).join("/")
@service_content.search_index.find_by_inventory_path(full_path)
end
|
#find_parent(obj, parent_type) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/cloud/vsphere/client.rb', line 38
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
|
#get_cdrom_device(vm) ⇒ Object
78
79
80
81
|
# File 'lib/cloud/vsphere/client.rb', line 78
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/cloud/vsphere/client.rb', line 215
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
|
#has_disk?(disk_path, disk_datacenter) ⇒ Boolean
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/cloud/vsphere/client.rb', line 155
def has_disk?(disk_path, disk_datacenter)
datacenter = find_by_inventory_path(disk_datacenter)
[".vmdk", "-flat.vmdk"].each do |extension|
begin
uuid = @service_content.virtual_disk_manager.query_virtual_disk_uuid(
"#{disk_path}#{extension}", datacenter
)
return true if uuid
rescue VimSdk::SoapError
end
end
false
end
|
#login(username, password, locale) ⇒ Object
27
28
29
30
|
# File 'lib/cloud/vsphere/client.rb', line 27
def login(username, password, locale)
raise AlreadyLoggedInException if @session
@session = @service_content.session_manager.login(username, password, locale)
end
|
#logout ⇒ Object
32
33
34
35
36
|
# File 'lib/cloud/vsphere/client.rb', line 32
def logout
raise NotLoggedInException unless @session
@session = nil
@service_content.session_manager.logout
end
|
#move_disk(source_datacenter, source_path, dest_datacenter, dest_path) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/cloud/vsphere/client.rb', line 110
def move_disk(source_datacenter, source_path, dest_datacenter, dest_path)
tasks = []
[".vmdk", "-flat.vmdk"].each do |extension|
tasks << @service_content.file_manager.move_file(
"#{source_path}#{extension}", source_datacenter,
"#{dest_path}#{extension}", dest_datacenter, false
)
end
tasks.each { |task| wait_for_task(task) }
end
|
#move_into_folder(folder, objects) ⇒ Object
140
141
142
143
|
# File 'lib/cloud/vsphere/client.rb', line 140
def move_into_folder(folder, objects)
task = folder.move_into(objects)
wait_for_task(task)
end
|
#move_into_root_folder(objects) ⇒ Object
145
146
147
148
|
# File 'lib/cloud/vsphere/client.rb', line 145
def move_into_root_folder(objects)
task = @service_content.root_folder.move_into(objects)
wait_for_task(task)
end
|
#power_off_vm(vm) ⇒ Object
73
74
75
76
|
# File 'lib/cloud/vsphere/client.rb', line 73
def power_off_vm(vm)
task = vm.power_off
wait_for_task(task)
end
|
#power_on_vm(datacenter, vm) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/cloud/vsphere/client.rb', line 59
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: #{result.not_attempted.map(&:msg).join(', ')}"
else
task = result.attempted.first.task
wait_for_task(task)
end
end
|
#reconfig_vm(vm, config) ⇒ Object
45
46
47
48
|
# File 'lib/cloud/vsphere/client.rb', line 45
def reconfig_vm(vm, config)
task = vm.reconfigure(config)
wait_for_task(task)
end
|
#wait_for_task(task) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
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
|
# File 'lib/cloud/vsphere/client.rb', line 176
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 taking too long" 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 properties["info.error"].msg
end
end
end
|