Project

General

Profile

« Previous | Next » 

Revision c1452df6

Added by Marek Hulán almost 11 years ago

  • ID c1452df67bf983bc4f6bd5039f5549975a8af1da

Fixes #2821 - Autoload paths, PowerManager children must be in namespace in order to autoload

View differences:

app/models/host/managed.rb
def power
opts = {:host => self}
if compute_resource_id && uuid
VirtPowerManager.new(opts)
PowerManager::Virt.new(opts)
elsif bmc_available?
BMCPowerManager.new(opts)
PowerManager::BMC.new(opts)
else
raise ::Foreman::Exception.new(N_("Unknown power management support - can't continue"))
end
app/services/power_manager.rb
class PowerManager
module PowerManager
SUPPORTED_ACTIONS = [N_('start'), N_('stop'), N_('poweroff'), N_('reboot'), N_('reset'), N_('state')]
def initialize(opts = {})
@host = opts[:host]
end
def self.method_missing(method, *args)
logger.warn "invalid power state request #{action} for host: #{host}"
raise ::Foreman::Exception.new(N_("Invalid power state request: %{action}, supported actions are %{supported}"), { :action => action, :supported => SUPPORTED_ACTIONS })
end
def state
N_("Unknown")
end
def logger
Rails.logger
end
private
attr_reader :host
end
app/services/power_manager/base.rb
module PowerManager
class Base
def initialize(opts = {})
@host = opts[:host]
end
def self.method_missing(method, *args)
logger.warn "invalid power state request #{action} for host: #{host}"
raise ::Foreman::Exception.new(N_("Invalid power state request: %{action}, supported actions are %{supported}"), { :action => action, :supported => SUPPORTED_ACTIONS })
end
def state
N_("Unknown")
end
def logger
Rails.logger
end
private
attr_reader :host
end
end
app/services/power_manager/bmc.rb
module PowerManager
class BMC < Base
def initialize(opts = {})
super(opts)
@proxy = host.bmc_proxy
end
SUPPORTED_ACTIONS.each do |method|
define_method method do # def start
proxy.power(:action => action_map[method.to_sym]) # proxy.power(:action => 'on')
end # end
end
private
attr_reader :proxy
#TODO: consider moving this to the proxy code, so we can just delegate like as with Virt.
def action_map
{
:start => 'on',
:stop => 'off',
:poweroff => 'off',
:reboot => 'soft',
:reset => 'cycle',
:state => 'status'
}
end
end
end
app/services/power_manager/bmc_power_manager.rb
class BMCPowerManager < PowerManager
def initialize(opts = {})
super(opts)
@proxy = host.bmc_proxy
end
SUPPORTED_ACTIONS.each do |method|
define_method method do # def start
proxy.power(:action => action_map[method.to_sym]) # proxy.power(:action => 'on')
end # end
end
private
attr_reader :proxy
#TODO: consider moving this to the proxy code, so we can just delegate like as with Virt.
def action_map
{
:start => 'on',
:stop => 'off',
:poweroff => 'off',
:reboot => 'soft',
:reset => 'cycle',
:state => 'status'
}
end
end
app/services/power_manager/virt.rb
module PowerManager
class Virt < Base
def initialize(opts = {})
super(opts)
begin
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
logger.warn "Error has occurred while communicating to #{host.compute_resource}: #{e}"
logger.debug e.backtrace
raise Foreman::Exception.new(N_("Error has occurred while communicating with %{cr}: %{e}"), { :cr => host.compute_resource, :e => e })
end
end
def state
# make sure we fetch latest vm status
vm.reload
vm.state
end
(SUPPORTED_ACTIONS - ['state']).each do |method|
define_method method do
vm.send(method.to_sym)
end
end
private
attr_reader :vm
end
end
app/services/power_manager/virt_power_manager.rb
class VirtPowerManager < PowerManager
def initialize(opts = {})
super(opts)
begin
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
logger.warn "Error has occurred while communicating to #{host.compute_resource}: #{e}"
logger.debug e.backtrace
raise Foreman::Exception.new(N_("Error has occurred while communicating with %{cr}: %{e}"), { :cr => host.compute_resource, :e => e })
end
end
def state
# make sure we fetch latest vm status
vm.reload
vm.state
end
(SUPPORTED_ACTIONS - ['state']).each do |method|
define_method method do
vm.send(method.to_sym)
end
end
private
attr_reader :vm
end
config/application.rb
config.autoload_paths += Dir[ Rails.root.join('app', 'models', 'power_manager') ]
config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
config.autoload_paths += Dir["#{config.root}/app/services)"]
config.autoload_paths += Dir["#{config.root}/app/observers)"]
config.autoload_paths += Dir["#{config.root}/app/mailers)"]
config.autoload_paths += Dir["#{config.root}/app/services"]
config.autoload_paths += Dir["#{config.root}/app/observers"]
config.autoload_paths += Dir["#{config.root}/app/mailers"]
config.autoload_paths += %W(#{config.root}/app/models/auth_sources)
config.autoload_paths += %W(#{config.root}/app/models/compute_resources)

Also available in: Unified diff