Module: VagrantPlugins::VSphere::Util::VmHelpers
- Included in:
- Action::GetState, Action::PowerOff, Action::PowerOn, Action::Resume, Action::SnapshotDelete, Action::SnapshotList, Action::SnapshotRestore, Action::SnapshotSave, Action::Suspend
- 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.
- #resume_vm(vm) ⇒ Object
- #suspend_vm(vm) ⇒ Object
- #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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vSphere/util/vm_helpers.rb', line 94 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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/vSphere/util/vm_helpers.rb', line 121 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vSphere/util/vm_helpers.rb', line 55 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
30 31 32 |
# File 'lib/vSphere/util/vm_helpers.rb', line 30 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
38 39 40 |
# File 'lib/vSphere/util/vm_helpers.rb', line 38 def powered_off?(vm) get_vm_state(vm).eql?(VmState::POWERED_OFF) end |
#powered_on?(vm) ⇒ Boolean
34 35 36 |
# File 'lib/vSphere/util/vm_helpers.rb', line 34 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.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vSphere/util/vm_helpers.rb', line 150 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 |
#resume_vm(vm) ⇒ Object
22 23 24 |
# File 'lib/vSphere/util/vm_helpers.rb', line 22 def resume_vm(vm) vm.PowerOnVM_Task.wait_for_completion end |
#suspend_vm(vm) ⇒ Object
26 27 28 |
# File 'lib/vSphere/util/vm_helpers.rb', line 26 def suspend_vm(vm) vm.SuspendVM_Task.wait_for_completion end |