Sha256: 0f548ace0f40e4948bb61da8c1cb930ac285d9089f423fde34ac0399cb671bc4

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

require 'log4r'

module VagrantPlugins
  module ProviderLibvirt
    module Action
      # Halt the domain.
      class HaltDomain
        def initialize(app, _env)
          @logger = Log4r::Logger.new('vagrant_libvirt::action::halt_domain')
          @app = app
        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?

          if env[:force_halt]
            domain.poweroff
            return @app.call(env)
          end

          begin
            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 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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vagrant-libvirt-0.5.3 lib/vagrant-libvirt/action/halt_domain.rb
vagrant-libvirt-0.5.2 lib/vagrant-libvirt/action/halt_domain.rb
vagrant-libvirt-0.5.1 lib/vagrant-libvirt/action/halt_domain.rb
vagrant-libvirt-0.5.0 lib/vagrant-libvirt/action/halt_domain.rb