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
- #clone_to!(path) ⇒ 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, timeout = 5) ⇒ 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
216 217 218 219 220 221 222 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 216 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
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 224 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
28 29 30 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 28 def annotation config.annotation end |
#annotation=(value) ⇒ Object
32 33 34 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 32 def annotation=(value) ReconfigVM_Task(spec: RbVmomi::VIM.VirtualMachineConfigSpec(annotation: value)).wait_for_completion end |
#clone_to(path, opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 10 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 |
#clone_to!(path) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 21 def clone_to!(path) dest_vm = monkey.vm(path) dest_vm.destroy if dest_vm clone_to(path) end |
#destroy ⇒ Object
5 6 7 8 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 5 def destroy self.PowerOffVM_Task.wait_for_completion unless runtime.powerState == 'poweredOff' self.Destroy_Task.wait_for_completion end |
#find_property(name) ⇒ Object
172 173 174 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 172 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
69 70 71 72 73 74 75 76 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 69 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 51 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
79 80 81 82 83 84 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 79 def move_to!(path) dest_vm = monkey.vm(path) dest_vm.destroy if dest_vm move_to(path) end |
#port_ready?(port, timeout = 5) ⇒ Boolean
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 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 86 def port_ready?(port, timeout = 5) ip = guest_ip or return false ## modified from http://spin.atomicobject.com/2013/09/30/socket-connection-timeout-ruby/ addr = Socket.getaddrinfo(ip, nil) sockaddr = Socket.pack_sockaddr_in(port, addr[0][3]) Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket| socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) begin # Initiate the socket connection in the background. If it doesn't fail # immediately it will raise an IO::WaitWritable (Errno::EINPROGRESS) # indicating the connection is in progress. socket.connect_nonblock(sockaddr) rescue IO::WaitWritable # IO.select will block until the socket is writable or the timeout # is exceeded - whichever comes first. if IO.select(nil, [socket], nil, timeout) begin # Verify there is now a good connection socket.connect_nonblock(sockaddr) rescue Errno::EISCONN # Good news everybody, the socket is connected! return true rescue Errno::ETIMEDOUT, Errno::EPERM, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH return false rescue # An unexpected exception was raised - the connection is no good. raise end else # IO.select returns nil when the socket is not ready before timeout # seconds have elapsed return false end ensure socket.close end return true end end |
#property(*args) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 36 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
47 48 49 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 47 def property!(name) read_property(name) || raise("vApp Property not found. [#{name}]") end |
#read_property(name) ⇒ Object
176 177 178 179 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 176 def read_property(name) p = find_property(name) p.nil? ? nil : p[:value] end |
#ready? ⇒ Boolean
168 169 170 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 168 def ready? ! guest_ip.nil? end |
#set_property(name, value) ⇒ Object
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 214 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 181 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 value = value.join(',') if value.is_a?(Array) 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
160 161 162 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 160 def start PowerOnVM_Task().wait_for_completion unless runtime.powerState == 'poweredOn' end |
#started? ⇒ Boolean
164 165 166 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 164 def started? runtime.powerState == 'poweredOn' end |
#stop ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 152 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 132 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 "Waited #{max} seconds, giving up." end true end |
#wait_for_port(port) ⇒ Object
148 149 150 |
# File 'lib/vmonkey/vim/VirtualMachine.rb', line 148 def wait_for_port(port) wait_for { port_ready?(port) } end |