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.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vSphere/util/vm_helpers.rb', line 96

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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vSphere/util/vm_helpers.rb', line 124

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>)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vSphere/util/vm_helpers.rb', line 57

def enumerate_snapshots(vm)
  snapshot_info = vm.snapshot

  snapshot_root = if snapshot_info.nil?
                    []
                  else
                    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



32
33
34
# File 'lib/vSphere/util/vm_helpers.rb', line 32

def get_vm_state(vm)
  vm.runtime.powerState
end

#power_off_vm(vm) ⇒ Object



19
20
21
# File 'lib/vSphere/util/vm_helpers.rb', line 19

def power_off_vm(vm)
  vm.PowerOffVM_Task.wait_for_completion
end

#power_on_vm(vm) ⇒ Object



15
16
17
# File 'lib/vSphere/util/vm_helpers.rb', line 15

def power_on_vm(vm)
  vm.PowerOnVM_Task.wait_for_completion
end

#powered_off?(vm) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/vSphere/util/vm_helpers.rb', line 40

def powered_off?(vm)
  get_vm_state(vm).eql?(VmState::POWERED_OFF)
end

#powered_on?(vm) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/vSphere/util/vm_helpers.rb', line 36

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vSphere/util/vm_helpers.rb', line 153

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



24
25
26
# File 'lib/vSphere/util/vm_helpers.rb', line 24

def resume_vm(vm)
  vm.PowerOnVM_Task.wait_for_completion
end

#suspend_vm(vm) ⇒ Object



28
29
30
# File 'lib/vSphere/util/vm_helpers.rb', line 28

def suspend_vm(vm)
  vm.SuspendVM_Task.wait_for_completion
end

#suspended?(vm) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/vSphere/util/vm_helpers.rb', line 44

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