Class: VagrantPlugins::ProviderLibvirt::Action::IsSuspended

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/is_suspended.rb

Overview

This can be used with “Call” built-in to check if the machine is suspended and branch in the middleware.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ IsSuspended

Returns a new instance of IsSuspended.



9
10
11
# File 'lib/vagrant-libvirt/action/is_suspended.rb', line 9

def initialize(app, _env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vagrant-libvirt/action/is_suspended.rb', line 13

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] = [:paused, :pmsuspended].include?(env[:machine].state.id)
      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] = [:paused, :pmsuspended].include?(env[:machine].state.id)
    end
  end

  @app.call(env)
end