Class: VagrantPlugins::ESXi::Action::Halt

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vmware-esxi/action/halt.rb

Overview

This action will halt (power off) the VM

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ Halt

Returns a new instance of Halt.



9
10
11
12
# File 'lib/vagrant-vmware-esxi/action/halt.rb', line 9

def initialize(app, _env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::halt')
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
# File 'lib/vagrant-vmware-esxi/action/halt.rb', line 14

def call(env)
  halt(env)
  @app.call(env)
end

#halt(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-vmware-esxi/action/halt.rb', line 19

def halt(env)
  @logger.info('vagrant-vmware-esxi, halt: start...')

  # Get config.
  machine = env[:machine]
  config = env[:machine].provider_config

  if env[:machine_state].to_s == 'powered_off'
    env[:ui].info I18n.t('vagrant_vmware_esxi.already_powered_off')
  elsif env[:machine_state].to_s == 'not_created'
    env[:ui].info I18n.t('vagrant_vmware_esxi.already_destroyed')
  else
    Net::SSH.start(config.esxi_hostname, config.esxi_username,
      password:                   config.esxi_password,
      port:                       config.esxi_hostport,
      keys:                       config.local_private_keys,
      timeout:                    20,
      number_of_password_prompts: 0,
      non_interactive:            true
    ) do |ssh|

      r = ssh.exec!("vim-cmd vmsvc/power.off #{machine.id}")
      config.saved_ipaddress = nil

      if r.exitstatus != 0
        raise Errors::ESXiError,
              message: "Unable to power off the VM:\n    #{r}"
      end
      env[:ui].info I18n.t('vagrant_vmware_esxi.states.powered_off.short')
    end
  end
end