Module: VagrantPlugins::VSphere::Util::VmHelpers

Instance Method Summary collapse

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.

Parameters:

  • vm (RbVmomi::VIM::VirtualMachine)
  • name (String)

Yields:

  • (Integer)

    Percentage complete as an integer. Called multiple times.



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.

Parameters:

  • vm (RbVmomi::VIM::VirtualMachine)
  • name (String)

Yields:

  • (Integer)

    Percentage complete as an integer. Called multiple times.



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.

Parameters:

  • vm (RbVmomi::VIM::VirtualMachine)

Returns:

  • (Enumerator<RbVmomi::VIM::VirtualMachineSnapshotTree>)


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

Returns:

  • (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

Returns:

  • (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.

Parameters:

  • vm (RbVmomi::VIM::VirtualMachine)
  • name (String)

Yields:

  • (Integer)

    Percentage complete as an integer. Called multiple times.



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

#suspended?(vm) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/vSphere/util/vm_helpers.rb', line 42

def suspended?(vm)
  get_vm_state(vm).eql?(VmState::SUSPENDED)
end