Class: VagrantPlugins::ProviderLibvirt::Action::HaltDomain

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/halt_domain.rb

Overview

Halt the domain.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ HaltDomain

Returns a new instance of HaltDomain.



8
9
10
11
# File 'lib/vagrant-libvirt/action/halt_domain.rb', line 8

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

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
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
51
52
53
# File 'lib/vagrant-libvirt/action/halt_domain.rb', line 13

def call(env)
  env[:ui].info(I18n.t('vagrant_libvirt.halt_domain'))

  timeout = env[:machine].config.vm.graceful_halt_timeout
  domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
  raise Errors::NoDomainError if domain.nil?

  if env[:force_halt]
    domain.poweroff
    return @app.call(env)
  end

  begin
    Timeout.timeout(timeout) do
      begin
        env[:machine].guest.capability(:halt)
      rescue Timeout::Error
        raise
      rescue
        @logger.info('Trying Libvirt graceful shutdown.')
        # Read domain object again
        dom = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
        if dom.state.to_s == 'running'
          dom.shutdown
        end
      end

      domain.wait_for(timeout) do
        !ready?
      end
    end
  rescue Timeout::Error
    @logger.info('VM is still running. Calling force poweroff.')
    domain.poweroff
  rescue
    @logger.error('Failed to shutdown cleanly. Calling force poweroff.')
    domain.poweroff
  end

  @app.call(env)
end