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: |-