Sha256: 147bc7247a82ed0aa636f30733465617a5f7126e9a8616db2bf1f3cbf5ddf29e

Contents?: true

Size: 1.52 KB

Versions: 19

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module VagrantPlugins
  module ProviderLibvirt
    module Action
      # This can be used with "Call" built-in to check if the machine
      # is suspended and branch in the middleware.
      class IsSuspended
        def initialize(app, _env)
          @app = app
        end

        def call(env)
          domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
          raise Errors::NoDomainError if domain.nil?

          config = env[:machine].provider_config
          libvirt_domain = env[:machine].provider.driver.connection.client.lookup_domain_by_uuid(env[:machine].id)
          if config.suspend_mode == 'managedsave'
            if libvirt_domain.has_managed_save?
              env[:result] = env[:machine].state.id == :shutoff
            else
              env[:result] = env[:machine].state.id == :paused
              if env[:result]
                env[:ui].warn('One time switching to pause suspend mode, found a paused VM.')
                config.suspend_mode = 'pause'
              end
            end
          else
            if libvirt_domain.has_managed_save?
              env[:ui].warn('One time switching to managedsave suspend mode, state found.')
              env[:result] = [:shutoff, :paused].include?(env[:machine].state.id)
              config.suspend_mode = 'managedsave'
            else
              env[:result] = env[:machine].state.id == :paused
            end
          end

          @app.call(env)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
vagrant-libvirt-0.11.1 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.8 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.7 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.6 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.5 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.4 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.3 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.2 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.1 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.10.0 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.9.0 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.8.2 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.8.1 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.8.0 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.7.0 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.6.3 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.6.2 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.6.1 lib/vagrant-libvirt/action/is_suspended.rb
vagrant-libvirt-0.6.0 lib/vagrant-libvirt/action/is_suspended.rb