From 9acfd16dfcb8d18bb1825a7f2cdd75d9d3b976c1 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 14 Jun 2021 09:04:54 -0700 Subject: [PATCH] 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. --- lib/vagrant-libvirt/action.rb | 7 ++++- lib/vagrant-libvirt/action/shutdown_domain.rb | 31 +++++++++++++++++++ locales/en.yml | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant-libvirt/action/shutdown_domain.rb diff --git a/lib/vagrant-libvirt/action.rb b/lib/vagrant-libvirt/action.rb index 32be744..dd9db73 100644 --- a/lib/vagrant-libvirt/action.rb +++ b/lib/vagrant-libvirt/action.rb @@ -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') diff --git a/lib/vagrant-libvirt/action/shutdown_domain.rb b/lib/vagrant-libvirt/action/shutdown_domain.rb new file mode 100644 index 0000000..a1f8728 --- /dev/null +++ b/lib/vagrant-libvirt/action/shutdown_domain.rb @@ -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 diff --git a/locales/en.yml b/locales/en.yml index 5daa320..4998755 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -29,6 +29,8 @@ en: Poweroff domain. destroy_domain: |- Removing domain... + shutdown_domain: |- + Attempting direct shutdown of domain... halt_domain: |- Halting domain... resuming_domain: |-