Class: VagrantPlugins::VSphere::Action::PowerOff

Inherits:
Object
  • Object
show all
Includes:
Util::VimHelpers, Util::VmHelpers
Defined in:
lib/vSphere/action/power_off.rb

Instance Method Summary collapse

Methods included from Util::VmHelpers

#get_vm_state, #power_off_vm, #power_on_vm, #powered_off?, #powered_on?, #suspended?

Methods included from Util::VimHelpers

#find_clustercompute_or_compute_resource, #get_compute_resource, #get_customization_spec_info_by_name, #get_datacenter, #get_datastore, #get_network_by_name, #get_resource_pool, #get_vm_by_uuid

Constructor Details

#initialize(app, _env) ⇒ PowerOff

Returns a new instance of PowerOff.



13
14
15
# File 'lib/vSphere/action/power_off.rb', line 13

def initialize(app, _env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vSphere/action/power_off.rb', line 17

def call(env)
  vm = get_vm_by_uuid env[:vSphere_connection], env[:machine]

  # If the vm is suspended, we need to turn it on so that we can turn it off.
  # This may seem counterintuitive, but the vsphere API documentation states
  # that the Power Off task for a VM will fail if the state is not poweredOn
  # see: https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#powerOff
  if suspended?(vm)
    env[:ui].info I18n.t('vsphere.power_on_vm')
    power_on_vm(vm)
  end

  # Powering off is a no-op if we can't find the VM or if it is already off
  unless vm.nil? || powered_off?(vm)
    env[:ui].info I18n.t('vsphere.power_off_vm')
    power_off_vm(vm)
  end

  @app.call env
end