Module: Vixen::Bridge
Constant Summary collapse
- @@all_my_blocks =
[]
Constants included from Constants
Constants::VixApiVersion, Constants::VixCreateSnapshotOptions, Constants::VixError, Constants::VixEventType, Constants::VixFileAttributes, Constants::VixFindItemType, Constants::VixHandle, Constants::VixHandleType, Constants::VixHostOptions, Constants::VixPowerState, Constants::VixPropertyId, Constants::VixPropertyType, Constants::VixServiceProvider, Constants::VixVMOpenOptions, Constants::VixVMPowerOptions
Class Method Summary collapse
- .connect(hostType, hostname, port, username, password, &block) ⇒ Object
- .create_snapshot(vm_handle, name, description, &block) ⇒ Object
- .current_power_state(vm_handle) ⇒ Object
- .current_snapshot(vm_handle) ⇒ Object
- .destroy(handle) ⇒ Object
- .disconnect(handle) ⇒ Object
- .get_int_property(handle, property_id) ⇒ Object
- .get_parent(snapshot_handle) ⇒ Object
- .get_string_property(handle, property_id) ⇒ Object
- .library_location ⇒ Object
- .open_vm(host_handle, vm_path, &block) ⇒ Object
- .pointer_to(type, &block) ⇒ Object
- .pointer_to_bool(&block) ⇒ Object
- .pointer_to_handle(&block) ⇒ Object
- .pointer_to_int(&block) ⇒ Object
- .pointer_to_string(&block) ⇒ Object
- .power_off(vm_handle, &block) ⇒ Object
- .power_off_using_guest(vm_handle, &block) ⇒ Object
- .power_on(vm_handle, &block) ⇒ Object
- .remove_snapshot(vm, snapshot, &block) ⇒ Object
- .reset(vm_handle, &block) ⇒ Object
- .reset_using_guest(vm_handle, &block) ⇒ Object
- .revert_to_snapshot(vm, snapshot, &block) ⇒ Object
- .running_vms(host_handle, &block) ⇒ Object
- .safe_proc_from_block(&block) ⇒ Object
- .spin_until_job_complete(operation, job, initial_sleep_time = 0.1, sleep_increment = 0.02) ⇒ Object
- .suspend(vm_handle, &block) ⇒ Object
- .wait_for_async_handle_creation_job(operation, pointer_to_handle, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) ⇒ Object
- .wait_for_async_job(operation, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) ⇒ Object
Class Method Details
.connect(hostType, hostname, port, username, password, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vixen/bridge.rb', line 67 def self.connect(hostType, hostname, port, username, password, &block) progress_proc = safe_proc_from_block &block hostname = "https://%s%s/sdk" % [hostname, port == 0 ? '' : ":#{port}"] if hostname Vixen.logger.info "connecting to %s with username %s" % [hostname.inspect, username.inspect] job = Vixen::Model::Job.new(VixHost_Connect(VixApiVersion[:api_version], hostType, hostname, port, username, password, 0, VixHandle[:invalid], progress_proc, nil)) spin_until_job_complete("connect to host", job) pointer_to_handle do |host_handle_pointer| Vixen.logger.debug "getting handle from connection job" VixJob_Wait job.handle, VixPropertyId[:job_result_handle], :pointer, host_handle_pointer, :int, VixPropertyId[:none] end end |
.create_snapshot(vm_handle, name, description, &block) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/vixen/bridge.rb', line 217 def self.create_snapshot(vm_handle, name, description, &block) progress_proc = safe_proc_from_block &block snapshot_handle = pointer_to_handle do |snapshot_handle_pointer| wait_for_async_handle_creation_job "create snapshot", snapshot_handle_pointer, 1, 0.2 do Vixen.logger.info "creating %s snapshot" % name VixVM_CreateSnapshot vm_handle, name, description, VixCreateSnapshotOptions[:include_memory], VixHandle[:invalid], progress_proc, nil end end end |
.current_power_state(vm_handle) ⇒ Object
337 338 339 |
# File 'lib/vixen/bridge.rb', line 337 def self.current_power_state(vm_handle) get_int_property vm_handle, VixPropertyId[:vm_power_state] end |
.current_snapshot(vm_handle) ⇒ Object
244 245 246 247 248 249 |
# File 'lib/vixen/bridge.rb', line 244 def self.current_snapshot(vm_handle) pointer_to_handle do |snapshot_handle_pointer| Vixen.logger.debug "retrieving current snapshot" VixVM_GetCurrentSnapshot vm_handle, snapshot_handle_pointer end end |
.destroy(handle) ⇒ Object
200 201 202 203 204 |
# File 'lib/vixen/bridge.rb', line 200 def self.destroy(handle) Vixen.logger.debug "destroying %s handle (%s)" % [Vixen::Constants::VixHandleType[Vix_GetHandleType(handle)], handle] Vix_ReleaseHandle handle end |
.disconnect(handle) ⇒ Object
194 195 196 197 198 |
# File 'lib/vixen/bridge.rb', line 194 def self.disconnect(handle) Vixen.logger.debug "disconnecting from %s handle (%s)" % [Vixen::Constants::VixHandleType[Vix_GetHandleType(handle)], handle] VixHost_Disconnect handle end |
.get_int_property(handle, property_id) ⇒ Object
270 271 272 273 274 275 276 277 |
# File 'lib/vixen/bridge.rb', line 270 def self.get_int_property(handle, property_id) pointer_to_int do |int_pointer| Vixen.logger.debug "getting %s property" % Vixen::Constants::VixPropertyId[property_id] Vix_GetProperties(handle, property_id, :pointer, int_pointer, :int, VixPropertyId[:none]) end end |
.get_parent(snapshot_handle) ⇒ Object
251 252 253 254 255 256 |
# File 'lib/vixen/bridge.rb', line 251 def self.get_parent(snapshot_handle) pointer_to_handle do |snapshot_handle_pointer| Vixen.logger.debug "retrieving snapshot parent" VixSnapshot_GetParent snapshot_handle, snapshot_handle_pointer end end |
.get_string_property(handle, property_id) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/vixen/bridge.rb', line 258 def self.get_string_property(handle, property_id) string = pointer_to_string do |string_pointer| Vixen.logger.debug "getting %s property" % Vixen::Constants::VixPropertyId[property_id] Vix_GetProperties(handle, property_id, :pointer, string_pointer, :int, VixPropertyId[:none]) end value = string.read_string.dup Vix_FreeBuffer(string) return value end |
.library_location ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/vixen/bridge.rb', line 11 def self.library_location kernel = Facter.value(:kernel) || '' arch = Facter.value(:architecture) case kernel.downcase when 'darwin' ext = 'dylib' when 'linux' ext = 'so' when 'windows' ext = 'dll' end File.join(File.dirname(__FILE__), %W[.. .. ext #{kernel} #{arch} libvixAllProducts.#{ext}]) end |
.open_vm(host_handle, vm_path, &block) ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/vixen/bridge.rb', line 206 def self.open_vm(host_handle, vm_path, &block) progress_proc = safe_proc_from_block &block vm_handle = pointer_to_handle do |vm_handle_pointer| wait_for_async_handle_creation_job "open vm", vm_handle_pointer do Vixen.logger.info "opening %s" % vm_path VixHost_OpenVM host_handle, vm_path, VixVMOpenOptions[:normal], VixHandle[:invalid], progress_proc, nil end end end |
.pointer_to(type, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/vixen/bridge.rb', line 90 def self.pointer_to(type, &block) pointer = FFI::MemoryPointer.new type, 1 err = yield pointer unless err == VixError[:ok] raise "problem executing pointer_to_#{type} block. (error: %s, %s)" % [err, Vix_GetErrorText(err, nil)] end pointer.send "read_#{type}".to_sym end |
.pointer_to_bool(&block) ⇒ Object
112 113 114 |
# File 'lib/vixen/bridge.rb', line 112 def self.pointer_to_bool(&block) (pointer_to :int, &block) != 0 end |
.pointer_to_handle(&block) ⇒ Object
100 101 102 |
# File 'lib/vixen/bridge.rb', line 100 def self.pointer_to_handle(&block) pointer_to :int, &block end |
.pointer_to_int(&block) ⇒ Object
108 109 110 |
# File 'lib/vixen/bridge.rb', line 108 def self.pointer_to_int(&block) pointer_to :int, &block end |
.pointer_to_string(&block) ⇒ Object
104 105 106 |
# File 'lib/vixen/bridge.rb', line 104 def self.pointer_to_string(&block) pointer_to :pointer, &block end |
.power_off(vm_handle, &block) ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/vixen/bridge.rb', line 300 def self.power_off(vm_handle, &block) progress_proc = safe_proc_from_block &block wait_for_async_job "power off VM" do Vixen.logger.debug "powering off vm (%s)" % vm_handle VixVM_PowerOff vm_handle, VixVMPowerOptions[:normal], progress_proc, nil end end |
.power_off_using_guest(vm_handle, &block) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/vixen/bridge.rb', line 287 def self.power_off_using_guest(vm_handle, &block) begin progress_proc = safe_proc_from_block &block wait_for_async_job "power off VM using tools" do Vixen.logger.debug "powering off vm (%s)" % vm_handle VixVM_PowerOff vm_handle, VixVMPowerOptions[:from_guest], progress_proc, nil end true rescue false end end |
.power_on(vm_handle, &block) ⇒ Object
279 280 281 282 283 284 285 |
# File 'lib/vixen/bridge.rb', line 279 def self.power_on(vm_handle, &block) progress_proc = safe_proc_from_block &block wait_for_async_job "power on VM" do Vixen.logger.debug "powering on vm (%s)" % vm_handle VixVM_PowerOn vm_handle, VixVMPowerOptions[:normal], VixHandle[:invalid], progress_proc, nil end end |
.remove_snapshot(vm, snapshot, &block) ⇒ Object
237 238 239 240 241 242 |
# File 'lib/vixen/bridge.rb', line 237 def self.remove_snapshot(vm, snapshot, &block) progress_proc = safe_proc_from_block &block wait_for_async_job(("remove %s snapshot" % snapshot.display_name), 1, 0.2) do VixVM_RemoveSnapshot vm.handle, snapshot.handle, 0, progress_proc, nil end end |
.reset(vm_handle, &block) ⇒ Object
321 322 323 324 325 326 327 |
# File 'lib/vixen/bridge.rb', line 321 def self.reset(vm_handle, &block) progress_proc = safe_proc_from_block &block wait_for_async_job "reset VM" do Vixen.logger.debug "resetting vm (%s)" % vm_handle VixVM_Reset vm_handle, VixVMPowerOptions[:normal], progress_proc, nil end end |
.reset_using_guest(vm_handle, &block) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/vixen/bridge.rb', line 308 def self.reset_using_guest(vm_handle, &block) begin progress_proc = safe_proc_from_block &block wait_for_async_job "reset VM using tools" do Vixen.logger.debug "resetting vm (%s)" % vm_handle VixVM_Reset vm_handle, VixVMPowerOptions[:from_guest], progress_proc, nil end true rescue false end end |
.revert_to_snapshot(vm, snapshot, &block) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/vixen/bridge.rb', line 229 def self.revert_to_snapshot(vm, snapshot, &block) progress_proc = safe_proc_from_block &block wait_for_async_job(("revert to %s snapshot" % snapshot.display_name), 1, 0.2) do VixVM_RevertToSnapshot vm.handle, snapshot.handle, VixVMPowerOptions[:normal], VixHandle[:invalid], progress_proc, nil end end |
.running_vms(host_handle, &block) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/vixen/bridge.rb', line 165 def self.running_vms(host_handle, &block) available_vms = [] collect_proc = safe_proc_from_block do |job_handle, event_type, more_event_info, client_data| if event_type == VixEventType[:find_item] path = get_string_property more_event_info, VixPropertyId[:found_item_location] if path Vixen.logger.debug "adding running vms %s" % path available_vms << path end end if block_given? Vixen.logger.debug "preparing to call user-supplied block for running vms progress" block.call job_handle, event_type, more_event_info, client_data end end Vixen.logger.debug "finding running vms" job = Vixen::Model::Job.new(VixHost_FindItems(host_handle, VixFindItemType[:running_vms], VixHandle[:invalid], -1, collect_proc, nil)) spin_until_job_complete "running vms", job available_vms end |
.safe_proc_from_block(&block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vixen/bridge.rb', line 56 def self.safe_proc_from_block(&block) return nil unless block_given? Proc.new do |*args| begin block.call args rescue puts STDERR, $! end end end |
.spin_until_job_complete(operation, job, initial_sleep_time = 0.1, sleep_increment = 0.02) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vixen/bridge.rb', line 147 def self.spin_until_job_complete(operation, job, initial_sleep_time = 0.1, sleep_increment = 0.02) sleep_time = initial_sleep_time while ( not pointer_to_bool do |bool_pointer| Vixen.logger.debug "sleeping waiting for %s job (%s) to complete" % [operation, job.handle] sleep sleep_time sleep_time += sleep_increment Vixen.logger.debug "checking completion of %s job (%s)" % [operation, job.handle] thr = Thread.start { sleep 0.01; VixJob_CheckCompletion(job.handle, bool_pointer) } Vixen.logger.debug "waiting for thread to complete" x = thr.value Vixen.logger.debug "thread completed with #{x}" x end) do end end |
.suspend(vm_handle, &block) ⇒ Object
329 330 331 332 333 334 335 |
# File 'lib/vixen/bridge.rb', line 329 def self.suspend(vm_handle, &block) progress_proc = safe_proc_from_block &block wait_for_async_job "suspend VM" do Vixen.logger.debug "suspending vm (%s)" % vm_handle VixVM_Suspend vm_handle, VixVMPowerOptions[:normal], progress_proc, nil end end |
.wait_for_async_handle_creation_job(operation, pointer_to_handle, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/vixen/bridge.rb', line 130 def self.wait_for_async_handle_creation_job(operation, pointer_to_handle, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) job = Vixen::Model::Job.new(yield) Vixen.logger.debug "Waiting for async %s job (%s) to create a new handle" % [operation, job.handle] spin_until_job_complete(operation, job, initial_sleep_time, sleep_increment) err = VixJob_Wait job.handle, VixPropertyId[:job_result_handle], :pointer, pointer_to_handle, :int, VixPropertyId[:none] unless err == VixError[:ok] Vixen.logger.error "While executing %s VIX API returned error: %s: %s" % [operation, err, Vix_GetErrorText(err, nil)] raise "couldn't %s. (error: %s, %s)" % [operation, err, Vix_GetErrorText(err, nil)] end err end |
.wait_for_async_job(operation, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/vixen/bridge.rb', line 116 def self.wait_for_async_job(operation, initial_sleep_time = 0.1, sleep_increment = 0.02, &block) job = Vixen::Model::Job.new(yield) Vixen.logger.debug "Waiting for async %s job (%s)" % [operation, job.handle] spin_until_job_complete(operation, job, initial_sleep_time, sleep_increment) err = VixJob_Wait job.handle, VixPropertyId[:none] unless err == VixError[:ok] Vixen.logger.error "While executing %s VIX API returned error: %s: %s" % [operation, err, Vix_GetErrorText(err, nil)] raise "couldn't %s. (error: %s, %s)" % [operation, err, Vix_GetErrorText(err, nil)] end end |