Class: PowerManager::Virt

Inherits:
Base show all
Defined in:
app/services/power_manager/virt.rb

Instance Method Summary collapse

Methods inherited from Base

#logger, method_missing, respond_to_missing?

Constructor Details

#initialize(opts = {}) ⇒ Virt

Returns a new instance of Virt.


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/power_manager/virt.rb', line 5

def initialize(opts = {})
  super(opts)
  begin
    Timeout.timeout(15) do
      @vm = host.compute_resource.find_vm_by_uuid(host.uuid)
    end
  rescue Timeout::Error
    raise Foreman::Exception.new(N_("Timeout has occurred while communicating with %s"), host.compute_resource)
  rescue => e
    Foreman::Logging.exception("Error has occurred while communicating to #{host.compute_resource}", e)
    raise Foreman::Exception.new(N_("Error has occurred while communicating with %{cr}: %{e}"), { :cr => host.compute_resource, :e => e })
  end
end

Instance Method Details

#action_mapObject


36
37
38
39
40
41
42
43
44
45
# File 'app/services/power_manager/virt.rb', line 36

def action_map
  super.deep_merge({
                     :on       => 'start',
                     :off      => 'stop',
                     :soft     => 'reboot',
                     :cycle    => 'reset',
                     :status   => {:action => :virt_state, :output => :state_output},
                     :state    => {:action => :virt_state, :output => :state_output},
                   })
end

#default_action(action) ⇒ Object


32
33
34
# File 'app/services/power_manager/virt.rb', line 32

def default_action(action)
  vm.send(action)
end

#state_output(result) ⇒ Object


25
26
27
28
29
30
# File 'app/services/power_manager/virt.rb', line 25

def state_output(result)
  result = result.to_s
  return 'on' if result =~ /started/i
  return 'off' if result =~ /paused/i
  translate_status(result) # unknown output
end

#virt_stateObject


19
20
21
22
23
# File 'app/services/power_manager/virt.rb', line 19

def virt_state
  # make sure we fetch latest vm status
  vm.reload
  vm.state
end