class VagrantPlugins::ProviderLibvirt::Action::ShutdownDomain

Shutdown the domain.

Public Class Methods

new(app, _env, target_state, source_state) click to toggle source
# File lib/vagrant-libvirt/action/shutdown_domain.rb, line 27
def initialize(app, _env, target_state, source_state)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::shutdown_domain')
  @target_state = target_state
  @source_state = source_state
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/vagrant-libvirt/action/shutdown_domain.rb, line 34
def call(env)
  timeout = env[:machine].config.vm.graceful_halt_timeout

  start_time = env[:shutdown_start_time]

  if start_time.nil?
    # this really shouldn't happen
    raise Errors::CallChainError, require_action: StartShutdownTimer.name, current_action: ShutdownDomain.name
  end

  # return if successful, otherwise will ensure result is set to false
  env[:result] = env[:machine].state.id == @target_state

  return @app.call(env) if env[:result]

  current_time = Time.now

  # if we've already exceeded the timeout
  return @app.call(env) if current_time - start_time >= timeout

  # otherwise construct a new timeout.
  timeout = timeout - (current_time - start_time)

  domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
  if env[:machine].state.id == @source_state
    env[:ui].info(I18n.t('vagrant_libvirt.shutdown_domain'))
    domain.shutdown
    domain.wait_for(timeout) { !ready? }
  end

  env[:result] = env[:machine].state.id == @target_state

  @app.call(env)
end