Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v2/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def disassociate

api :PUT, "/hosts/:id/power", N_("Run a power operation on host")
param :id, :identifier_dottable, :required => true
param :power_action, String, :required => true, :desc => N_("power action, valid actions are (on/start), (off/stop), (soft/reboot), (cycle/reset), (state/status)")
param :power_action, String, :required => true, :desc => N_("power action, valid actions are (on/start), (off/stop), reboot, reset, soft, cycle, (state/status)")

def power
unless @host.supports_power?
Expand Down
24 changes: 22 additions & 2 deletions app/services/power_manager/bmc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,39 @@ def ready?

attr_reader :proxy

def power_action_v2
smart_proxy = host.smart_proxies.with_features(:BMC).first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug #10697, the smart_proxies method does not include Proxies that are assigned as BMC only.


return false unless smart_proxy
smart_proxy.has_capability?(:BMC, :power_action_v2)
end

# TODO: consider moving this to the proxy code, so we can just delegate like as with Virt.
def action_map
super.deep_merge({
:start => 'on',
:stop => 'off',
:poweroff => 'off',
:reboot => 'soft',
:reset => 'cycle',
:reboot => power_action_v2 ? 'do_reboot' : 'soft',
:reset => power_action_v2 ? 'do_reset' : 'cycle',
:state => 'status',
:ready? => 'ready?',
})
end

# Avoid infinite loop when action_map maps :reboot to 'reboot'.
# This method was introduced as part of foreman #3073 and smart-proxy #38498
# to explicitly handle reset/reboot actions, ensuring backward compatibility
# with older Smart Proxy implementations that do not support the new capabilities.
def do_reboot
default_action(:reboot)
end

# For the same reason as above, this handles the case where :reset maps to 'reset'.
def do_reset
default_action(:reset)
end

def default_action(action)
proxy.power(:action => action.to_s) # proxy.power(:action => 'on')
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/proxy_api/bmc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def power(args)
full_path_as_hash = bmc_url_for_get('power', args[:action])
response = parse(get(full_path_as_hash[:path], query: full_path_as_hash[:query]))
response.is_a?(Hash) ? response['result'] : response
when "on", "off", "cycle", "soft"
when "on", "off", "cycle", "soft", "reboot", "reset"
res = parse put(with_provider(args), bmc_url_for('power', args[:action]))
res && (res['result'] == true || res['result'] == "#{@target}: ok\n")
else
Expand Down
50 changes: 50 additions & 0 deletions test/unit/power_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,54 @@ def actions_list(host)
end
actions.uniq
end

test "should call reboot and reset directly when power_action_v2 is supported" do
host = FactoryBot.build_stubbed(:host, :managed)
bmc_proxy_mock = mock('bmc_proxy')
nic_mock = mock('nic_bmc')
relation_mock = mock('smart_proxies_relation')
bmc_caps = mock('bmc_caps')
filtered_relation_mock = mock('filtered_relation_mock')

host.stubs(:bmc_proxy).returns(bmc_proxy_mock)
host.stubs(:bmc_nic).returns(nic_mock)
nic_mock.stubs(:credentials_present?).returns(true)
nic_mock.stubs(:provider).returns('IPMI')
host.stubs(:bmc_available?).returns(true)
host.stubs(:smart_proxies).returns(relation_mock)
relation_mock.stubs(:with_features).with(:BMC).returns(filtered_relation_mock)
filtered_relation_mock.stubs(:first).returns(bmc_caps)
bmc_caps.stubs(:has_capability?).with(:BMC, :power_action_v2).returns(true)

bmc_proxy_mock.expects(:power).with(:action => "reboot").returns(true)
bmc_proxy_mock.expects(:power).with(:action => "reset").returns(true)

assert host.power.reboot
assert host.power.reset
end

test "should fallback to soft and cycle when power_action_v2 is not supported" do
host = FactoryBot.build_stubbed(:host, :managed)
bmc_proxy_mock = mock('bmc_proxy')
nic_mock = mock('nic_bmc')
relation_mock = mock('smart_proxies_relation')
bmc_caps = mock('bmc_caps')
filtered_relation_mock = mock('filtered_relation_mock')

host.stubs(:bmc_proxy).returns(bmc_proxy_mock)
host.stubs(:bmc_nic).returns(nic_mock)
nic_mock.stubs(:credentials_present?).returns(true)
nic_mock.stubs(:provider).returns('IPMI')
host.stubs(:bmc_available?).returns(true)
host.stubs(:smart_proxies).returns(relation_mock)
relation_mock.stubs(:with_features).with(:BMC).returns(filtered_relation_mock)
filtered_relation_mock.stubs(:first).returns(bmc_caps)
bmc_caps.stubs(:has_capability?).with(:BMC, :power_action_v2).returns(false)

bmc_proxy_mock.expects(:power).with(:action => "soft").returns(true)
bmc_proxy_mock.expects(:power).with(:action => "cycle").returns(true)

assert host.power.reboot
assert host.power.reset
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { translate as __ } from '../../../../common/I18n';
export const POWER_REQURST_KEY = 'HOST_TOGGLE_POWER';
export const POWER_REQUEST_OPTIONS = { key: POWER_REQURST_KEY, params: { timeout: 30 } };
export const BASE_POWER_STATES = { off: __('Off'), on: __('On') };
export const BMC_POWER_STATES = { soft: __('Reboot'), cycle: __('Reset') };
export const BMC_POWER_STATES = { reboot: __('Reboot'), reset: __('Reset'), soft: __('Soft'), cycle: __('Cycle') };
export const SUPPORTED_POWER_STATES = {
...BASE_POWER_STATES,
...BMC_POWER_STATES,
Expand Down