Class: Fog::Compute::Vsphere::Real
- Inherits:
-
Object
- Object
- Fog::Compute::Vsphere::Real
show all
- Includes:
- Shared
- Defined in:
- lib/fog/vsphere/requests/compute/find_vm_by_ref.rb,
lib/fog/vsphere/compute.rb,
lib/fog/vsphere/requests/compute/vm_clone.rb,
lib/fog/vsphere/requests/compute/vm_reboot.rb,
lib/fog/vsphere/requests/compute/vm_destroy.rb,
lib/fog/vsphere/requests/compute/vm_power_on.rb,
lib/fog/vsphere/requests/compute/current_time.rb,
lib/fog/vsphere/requests/compute/vm_power_off.rb,
lib/fog/vsphere/requests/compute/list_virtual_machines.rb
Overview
The Real and Mock classes share the same method because list_virtual_machines will be properly mocked for us
Instance Attribute Summary
Attributes included from Shared
#vsphere_is_vcenter, #vsphere_rev, #vsphere_server, #vsphere_username
Instance Method Summary
collapse
Methods included from Shared
#convert_vm_mob_ref_to_attr_hash, #find_vm_by_ref
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/fog/vsphere/compute.rb', line 85
def initialize(options={})
require 'rbvmomi'
@vsphere_username = options[:vsphere_username]
@vsphere_password = options[:vsphere_password]
@vsphere_server = options[:vsphere_server]
@vsphere_port = options[:vsphere_port] || 443
@vsphere_path = options[:vsphere_path] || '/sdk'
@vsphere_ns = options[:vsphere_ns] || 'urn:vim25'
@vsphere_rev = options[:vsphere_rev] || '4.0'
@vsphere_ssl = options[:vsphere_ssl] || true
@vsphere_expected_pubkey_hash = options[:vsphere_expected_pubkey_hash]
@vsphere_must_reauthenticate = false
@connection = nil
bad_cert = false
loop do
begin
@connection = RbVmomi::VIM.new :host => @vsphere_server,
:port => @vsphere_port,
:path => @vsphere_path,
:ns => @vsphere_ns,
:rev => @vsphere_rev,
:ssl => @vsphere_ssl,
:insecure => bad_cert
break
rescue OpenSSL::SSL::SSLError
raise if bad_cert
bad_cert = true
end
end
if bad_cert then
validate_ssl_connection
end
if not options[:vsphere_rev]
rev = @connection.serviceContent.about.apiVersion
@connection.rev = [ rev, ENV['FOG_VSPHERE_REV'] || '4.1' ].min
end
@vsphere_is_vcenter = @connection.serviceContent.about.apiType == "VirtualCenter"
@vsphere_rev = @connection.rev
authenticate
end
|
Instance Method Details
#current_time ⇒ Object
6
7
8
9
|
# File 'lib/fog/vsphere/requests/compute/current_time.rb', line 6
def current_time
current_time = @connection.serviceInstance.CurrentTime
{ 'current_time' => current_time }
end
|
#list_virtual_machines(options = {}) ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/fog/vsphere/requests/compute/list_virtual_machines.rb', line 7
def list_virtual_machines(options = {})
if options['instance_uuid'] then
list_all_virtual_machines_by_instance_uuid(options)
else
list_all_virtual_machines
end
end
|
#vm_clone(options = {}) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/fog/vsphere/requests/compute/vm_clone.rb', line 24
def vm_clone(options = {})
options = vm_clone_check_options(options)
notfound = lambda { raise Fog::Compute::Vsphere::NotFound, "Cloud not find VM template" }
vm_mob_ref = list_all_virtual_machine_mobs.find(notfound) do |vm|
convert_vm_mob_ref_to_attr_hash(vm)['instance_uuid'] == options['instance_uuid']
end
container = vm_mob_ref.parent
until container.kind_of? RbVmomi::VIM::Datacenter
container = container.parent
end
dc = container
resource_pool = dc.hostFolder.children.first.resourcePool
relocation_spec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => resource_pool,
:transform => options['transform'] || 'sparse')
clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => relocation_spec,
:powerOn => options['power_on'] || true,
:template => false)
task = vm_mob_ref.CloneVM_Task(:folder => vm_mob_ref.parent, :name => options['name'], :spec => clone_spec)
tries = 0
new_vm = begin
list_virtual_machines['virtual_machines'].detect(lambda { raise Fog::Vsphere::Errors::NotFound }) do |vm|
vm['name'] == options['name']
end
rescue Fog::Vsphere::Errors::NotFound
tries += 1
if tries <= 10 then
sleep 1
retry
end
nil
end
{
'vm_ref' => new_vm ? new_vm['mo_ref'] : nil,
'task_ref' => task._ref
}
end
|
#vm_destroy(options = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/fog/vsphere/requests/compute/vm_destroy.rb', line 6
def vm_destroy(options = {})
raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
search_filter = { :uuid => options['instance_uuid'], 'vmSearch' => true, 'instanceUuid' => true }
vm_mob_ref = @connection.searchIndex.FindAllByUuid(search_filter).first
unless vm_mob_ref.kind_of? RbVmomi::VIM::VirtualMachine
raise Fog::Vsphere::Errors::NotFound,
"Could not find VirtualMachine with instance uuid #{options['instance_uuid']}"
end
task = vm_mob_ref.Destroy_Task
task.wait_for_completion
{ 'task_state' => task.info.state }
end
|
#vm_power_off(options = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/fog/vsphere/requests/compute/vm_power_off.rb', line 6
def vm_power_off(options = {})
options = { 'force' => false }.merge(options)
raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
search_filter = { :uuid => options['instance_uuid'], 'vmSearch' => true, 'instanceUuid' => true }
vm_mob_ref = @connection.searchIndex.FindAllByUuid(search_filter).first
if options['force'] then
task = vm_mob_ref.PowerOffVM_Task
task.wait_for_completion
{ 'task_state' => task.info.result, 'power_off_type' => 'cut_power' }
else
vm_mob_ref.ShutdownGuest
{
'task_state' => "running",
'power_off_type' => 'shutdown_guest',
}
end
end
|
#vm_power_on(options = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/fog/vsphere/requests/compute/vm_power_on.rb', line 6
def vm_power_on(options = {})
raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
search_filter = { :uuid => options['instance_uuid'], 'vmSearch' => true, 'instanceUuid' => true }
vm_mob_ref = @connection.searchIndex.FindAllByUuid(search_filter).first
task = vm_mob_ref.PowerOnVM_Task
task.wait_for_completion
{ 'task_state' => task.info.state }
end
|
#vm_reboot(options = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/fog/vsphere/requests/compute/vm_reboot.rb', line 6
def vm_reboot(options = {})
options = { 'force' => false }.merge(options)
raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
search_filter = { :uuid => options['instance_uuid'], 'vmSearch' => true, 'instanceUuid' => true }
vm_mob_ref = @connection.searchIndex.FindAllByUuid(search_filter).first
if options['force'] then
task = vm_mob_ref.ResetVM_Task
task.wait_for_completion
{ 'task_state' => task.info.result, 'reboot_type' => 'reset_power' }
else
vm_mob_ref.ShutdownGuest
{ 'task_state' => "running", 'reboot_type' => 'reboot_guest' }
end
end
|