Add ShutdownDomain action and use during halt sequence

This adds in a ShutdownDomain action which allows for the
    GracefulHalt action to attempt to shutdown the domain. If
    it does not transition to domain successfully to a shutoff
    state, the ShutdownDomain action is used to "nicely" shutdown
    the domain. Likewise, if that action fails to transition the
    domain, the HaltDomain action will be used to forcibly stop it.
This commit is contained in:
Chris Roberts 2021-06-14 09:04:54 -07:00 committed by Darragh Bailey
parent 44d7c5526d
commit 9acfd16dfc
3 changed files with 39 additions and 1 deletions

View File

@ -145,7 +145,11 @@ module VagrantPlugins
# VM is running, halt it.
b3.use Call, GracefulHalt, :shutoff, :running do |env3, b4|
if !env3[:result]
b4.use HaltDomain
b4.use Call, ShutdownDomain, :shutoff, :running do |env4, b5|
if !env4[:result]
b5.use HaltDomain
end
end
end
end
end
@ -352,6 +356,7 @@ module VagrantPlugins
autoload :ForwardPorts, action_root.join('forward_ports')
autoload :ClearForwardedPorts, action_root.join('forward_ports')
autoload :HaltDomain, action_root.join('halt_domain')
autoload :ShutdownDomain, action_root.join('shutdown_domain')
autoload :HandleBoxImage, action_root.join('handle_box_image')
autoload :HandleStoragePool, action_root.join('handle_storage_pool')
autoload :RemoveLibvirtImage, action_root.join('remove_libvirt_image')

View File

@ -0,0 +1,31 @@
require 'log4r'
module VagrantPlugins
module ProviderLibvirt
module Action
# Shutdown the domain.
class ShutdownDomain
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
def call(env)
timeout = env[:machine].config.vm.graceful_halt_timeout
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
end
end
end
end

View File

@ -29,6 +29,8 @@ en:
Poweroff domain.
destroy_domain: |-
Removing domain...
shutdown_domain: |-
Attempting direct shutdown of domain...
halt_domain: |-
Halting domain...
resuming_domain: |-