Class: RbVmomi::VIM::VirtualMachine
- Inherits:
-
Object
- Object
- RbVmomi::VIM::VirtualMachine
- Defined in:
- lib/vmonkey/vim/VirtualMachine.rb
Instance Method Summary collapse
- #_clone_params(vm_name, dest, opts) ⇒ Object
- #_clone_spec(dest, opts) ⇒ Object
- #annotation ⇒ Object
- #annotation=(value) ⇒ Object
- #clone_to(path, opts = {}) ⇒ Object
- #destroy ⇒ Object
- #find_property(name) ⇒ Object
-
#guest_ip ⇒ Object
backported from rbvmomi 1.8 for rbvmomi 1.5 support.
- #move_to(path) ⇒ Object
- #move_to!(path) ⇒ Object
- #port_ready?(port) ⇒ Boolean
- #property(*args) ⇒ Object
- #property!(name) ⇒ Object
- #read_property(name) ⇒ Object
- #ready? ⇒ Boolean
- #set_property(name, value) ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #wait_for(max = 300, interval = 2, &block) ⇒ Object
- #wait_for_port(port) ⇒ Object
Instance Method Details
#_clone_params(vm_name, dest, opts) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 174 def _clone_params(vm_name, dest, opts) { name: vm_name, folder: dest.vm_folder, spec: _clone_spec(dest, opts) } end |
#_clone_spec(dest, opts) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 182 def _clone_spec(dest, opts) opts[:config] ||= {} clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec( location: RbVmomi::VIM.VirtualMachineRelocateSpec(pool: dest.vm_pool), powerOn: false, template: false ) clone_spec.config = RbVmomi::VIM.VirtualMachineConfigSpec(deviceChange: Array.new) clone_spec.customization = monkey.customization_spec(opts[:customization_spec]) clone_spec.config.annotation = opts[:config][:annotation] clone_spec.config.numCPUs = opts[:config][:num_cpus] clone_spec.config.memoryMB = opts[:config][:memory_mb] clone_spec end |
#annotation ⇒ Object
19 20 21 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 19 def annotation config.annotation end |
#annotation=(value) ⇒ Object
23 24 25 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 23 def annotation=(value) ReconfigVM_Task(spec: RbVmomi::VIM.VirtualMachineConfigSpec(annotation: value)).wait_for_completion end |
#clone_to(path, opts = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 8 def clone_to(path, opts = {}) dest = monkey.get(path.parent) unless dest.is_a? RbVmomi::VIM::Folder or dest.is_a? RbVmomi::VIM::VirtualApp raise "Cannot clone_to [#{path.parent}] - destination must specify a Folder or VirtualApp" end params = _clone_params(path.basename, dest, opts) self.CloneVM_Task(params).wait_for_completion end |
#destroy ⇒ Object
3 4 5 6 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 3 def destroy self.PowerOffVM_Task.wait_for_completion unless runtime.powerState == 'poweredOff' self.Destroy_Task.wait_for_completion end |
#find_property(name) ⇒ Object
132 133 134 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 132 def find_property(name) config.vAppConfig.property.find { |p| p.props[:id] == name.to_s } end |
#guest_ip ⇒ Object
backported from rbvmomi 1.8 for rbvmomi 1.5 support
60 61 62 63 64 65 66 67 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 60 def guest_ip g = self.guest if g.ipAddress && (g.toolsStatus == "toolsOk" || g.toolsStatus == "toolsOld") g.ipAddress else nil end end |
#move_to(path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 42 def move_to(path) monkey.vm(path) && raise("VirtualMachine already exists. [#{path}]") rename = name != path.basename to_folder = monkey.folder! path.parent reparent = parent != to_folder if reparent Rename_Task(newName: "#{path.basename}-tmp").wait_for_completion if rename to_folder.MoveIntoFolder_Task(list: [self]).wait_for_completion Rename_Task(newName: path.basename).wait_for_completion if rename else Rename_Task(newName: path.basename).wait_for_completion end end |
#move_to!(path) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 70 def move_to!(path) dest_vm = monkey.vm(path) dest_vm.destroy if dest_vm move_to(path) end |
#port_ready?(port) ⇒ Boolean
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 77 def port_ready?(port) ip = guest_ip or return false tcp_socket = TCPSocket.new(ip, port) readable = IO.select([tcp_socket], nil, nil, 5) if readable true else false end rescue Errno::ETIMEDOUT, Errno::EPERM, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH false ensure tcp_socket && tcp_socket.close end |
#property(*args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 27 def property(*args) case args.size when 1 read_property(*args) when 2 set_property(*args) else raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 or 2)") end end |
#property!(name) ⇒ Object
38 39 40 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 38 def property!(name) read_property(name) || raise("vApp Property not found. [#{name}]") end |
#read_property(name) ⇒ Object
136 137 138 139 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 136 def read_property(name) p = find_property(name) p.nil? ? nil : p[:value] end |
#ready? ⇒ Boolean
128 129 130 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 128 def ready? ! guest_ip.nil? end |
#set_property(name, value) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 141 def set_property(name, value) if config.vAppConfig && config.vAppConfig.property existing_property = find_property(name) end if existing_property operation = 'edit' property_key = existing_property.props[:key] else operation = 'add' property_key = name.object_id end vm_config_spec = RbVmomi::VIM.VirtualMachineConfigSpec( vAppConfig: RbVmomi::VIM.VmConfigSpec( property: [ RbVmomi::VIM.VAppPropertySpec( operation: operation, info: { key: property_key, id: name.to_s, type: 'string', userConfigurable: true, value: value })])) if config.vAppConfig.nil? || config.vAppConfig.ovfEnvironmentTransport.empty? vm_config_spec[:vAppConfig][:ovfEnvironmentTransport] = ['com.vmware.guestInfo'] end ReconfigVM_Task( spec: vm_config_spec ).wait_for_completion end |
#start ⇒ Object
120 121 122 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 120 def start PowerOnVM_Task().wait_for_completion unless runtime.powerState == 'poweredOn' end |
#started? ⇒ Boolean
124 125 126 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 124 def started? runtime.powerState == 'poweredOn' end |
#stop ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 112 def stop return if runtime.powerState == 'poweredOff' ShutdownGuest() sleep 2 until runtime.powerState == 'poweredOff' rescue PowerOffVM_Task().wait_for_completion unless runtime.powerState == 'poweredOff' end |
#wait_for(max = 300, interval = 2, &block) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 92 def wait_for(max=300, interval=2, &block) elapsed = 0 start = Time.now until (result = yield) || (elapsed > max) sleep interval elapsed = Time.now - start end unless result raise Errors::TimeoutError.new "Waited #{max} seconds, giving up" end true end |
#wait_for_port(port) ⇒ Object
108 109 110 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 108 def wait_for_port(port) wait_for { port_ready?(port) } end |