lib/vagrant-libvirt/action/halt_domain.rb in vagrant-libvirt-0.4.1 vs lib/vagrant-libvirt/action/halt_domain.rb in vagrant-libvirt-0.5.0

- old
+ new

@@ -11,26 +11,42 @@ end 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? - begin - env[:machine].guest.capability(:halt) - rescue - @logger.info('Trying Libvirt graceful shutdown.') - domain.shutdown + if env[:force_halt] + domain.poweroff + return @app.call(env) end - begin - domain.wait_for(30) do - !ready? + 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 Fog::Errors::TimeoutError + 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