Module: VagrantPlugins::VSphere::Util::VmHelpers
- Included in:
- Action::GetState, Action::PowerOff, Action::PowerOn, Action::SnapshotDelete, Action::SnapshotList, Action::SnapshotRestore, Action::SnapshotSave
- Defined in:
- lib/vSphere/util/vm_helpers.rb
Instance Method Summary collapse
-
#create_snapshot(vm, name) {|Integer| ... } ⇒ void
Create a named snapshot on a given VM.
-
#delete_snapshot(vm, name) {|Integer| ... } ⇒ void
Delete a named snapshot on a given VM.
-
#enumerate_snapshots(vm) ⇒ Enumerator<RbVmomi::VIM::VirtualMachineSnapshotTree>
Enumerate VM snapshot tree.
- #get_vm_state(vm) ⇒ Object
- #power_off_vm(vm) ⇒ Object
- #power_on_vm(vm) ⇒ Object
- #powered_off?(vm) ⇒ Boolean
- #powered_on?(vm) ⇒ Boolean
-
#restore_snapshot(vm, name) {|Integer| ... } ⇒ void
Restore a VM to a named snapshot.
- #suspended?(vm) ⇒ Boolean
Instance Method Details
#create_snapshot(vm, name) {|Integer| ... } ⇒ void
This method returns an undefined value.
Create a named snapshot on a given VM
This method creates a named snapshot on the given VM. This method blocks until the snapshot creation task is complete. An optional block can be passed which is used to report progress.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/vSphere/util/vm_helpers.rb', line 85 def create_snapshot(vm, name) task = vm.CreateSnapshot_Task( name: name, memory: false, quiesce: false) if block_given? task.wait_for_progress do |progress| yield progress unless progress.nil? end else task.wait_for_completion end end |
#delete_snapshot(vm, name) {|Integer| ... } ⇒ void
This method returns an undefined value.
Delete a named snapshot on a given VM
This method deletes a named snapshot on the given VM. This method blocks until the snapshot deletion task is complete. An optional block can be passed which is used to report progress.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vSphere/util/vm_helpers.rb', line 112 def delete_snapshot(vm, name) snapshot = enumerate_snapshots(vm).find { |s| s.name == name } # No snapshot matching "name" return nil if snapshot.nil? task = snapshot.snapshot.RemoveSnapshot_Task(removeChildren: false) if block_given? task.wait_for_progress do |progress| yield progress unless progress.nil? end else task.wait_for_completion end end |
#enumerate_snapshots(vm) ⇒ Enumerator<RbVmomi::VIM::VirtualMachineSnapshotTree>
Enumerate VM snapshot tree
This method returns an enumerator that performs a depth-first walk of the VM snapshot grap and yields each VirtualMachineSnapshotTree node.
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 |
# File 'lib/vSphere/util/vm_helpers.rb', line 46 def enumerate_snapshots(vm) snapshot_info = vm.snapshot if snapshot_info.nil? snapshot_root = [] else snapshot_root = snapshot_info.rootSnapshotList end recursor = lambda do |snapshot_list| Enumerator.new do |yielder| snapshot_list.each do |s| # Yield the current VirtualMachineSnapshotTree object yielder.yield s # Recurse into child VirtualMachineSnapshotTree objects children = recursor.call(s.childSnapshotList) loop do yielder.yield children.next end end end end recursor.call(snapshot_root) end |
#get_vm_state(vm) ⇒ Object
21 22 23 |
# File 'lib/vSphere/util/vm_helpers.rb', line 21 def get_vm_state(vm) vm.runtime.powerState end |
#power_off_vm(vm) ⇒ Object
17 18 19 |
# File 'lib/vSphere/util/vm_helpers.rb', line 17 def power_off_vm(vm) vm.PowerOffVM_Task.wait_for_completion end |
#power_on_vm(vm) ⇒ Object
13 14 15 |
# File 'lib/vSphere/util/vm_helpers.rb', line 13 def power_on_vm(vm) vm.PowerOnVM_Task.wait_for_completion end |
#powered_off?(vm) ⇒ Boolean
29 30 31 |
# File 'lib/vSphere/util/vm_helpers.rb', line 29 def powered_off?(vm) get_vm_state(vm).eql?(VmState::POWERED_OFF) end |
#powered_on?(vm) ⇒ Boolean
25 26 27 |
# File 'lib/vSphere/util/vm_helpers.rb', line 25 def powered_on?(vm) get_vm_state(vm).eql?(VmState::POWERED_ON) end |
#restore_snapshot(vm, name) {|Integer| ... } ⇒ void
This method returns an undefined value.
Restore a VM to a named snapshot
This method restores a VM to the named snapshot state. This method blocks until the restoration task is complete. An optional block can be passed which is used to report progress.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/vSphere/util/vm_helpers.rb', line 141 def restore_snapshot(vm, name) snapshot = enumerate_snapshots(vm).find { |s| s.name == name } # No snapshot matching "name" return nil if snapshot.nil? task = snapshot.snapshot.RevertToSnapshot_Task(suppressPowerOn: true) if block_given? task.wait_for_progress do |progress| yield progress unless progress.nil? end else task.wait_for_completion end end |