diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/executor/__init__.py b/xCAT-openbmc-py/lib/python/agent/hwctl/executor/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/task.py b/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_power.py similarity index 99% rename from xCAT-openbmc-py/lib/python/agent/hwctl/task.py rename to xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_power.py index aac1f2422..c400ee13d 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/task.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/executor/openbmc_power.py @@ -10,7 +10,7 @@ import time from common.task import ParallelNodesCommand from common.exceptions import SelfClientException, SelfServerException -from hwctl import openbmc +from hwctl import openbmc_client as openbmc import logging logger = logging.getLogger('xcatagent') diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py similarity index 100% rename from xCAT-openbmc-py/lib/python/agent/hwctl/openbmc.py rename to xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py diff --git a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py index 8b49b88eb..004a392bc 100644 --- a/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py +++ b/xCAT-openbmc-py/lib/python/agent/xcatagent/openbmc.py @@ -13,7 +13,7 @@ import gevent from common import utils from common import exceptions as xcat_exception -from hwctl.task import OpenBMCPowerTask +from hwctl.executor.openbmc_power import OpenBMCPowerTask from hwctl.power import DefaultPowerManager from xcatagent import base @@ -73,51 +73,6 @@ POWER_REBOOT_OPTIONS = ('boot', 'reset') POWER_SET_OPTIONS = ('on', 'off', 'bmcreboot', 'softoff') POWER_GET_OPTIONS = ('bmcstate', 'state', 'stat', 'status') -RPOWER_URLS = { - "on" : { - "url" : PROJECT_URL + "/state/host0/attr/RequestedHostTransition", - "field" : "xyz.openbmc_project.State.Host.Transition.On", - }, - "off" : { - "url" : PROJECT_URL + "/state/chassis0/attr/RequestedPowerTransition", - "field" : "xyz.openbmc_project.State.Chassis.Transition.Off", - }, - "softoff" : { - "url" : PROJECT_URL + "/state/host0/attr/RequestedHostTransition", - "field" : "xyz.openbmc_project.State.Host.Transition.Off", - }, - "bmcreboot" : { - "url" : PROJECT_URL + "/state/bmc0/attr/RequestedBMCTransition", - "field" : "xyz.openbmc_project.State.BMC.Transition.Reboot", - }, - "state" : { - "url" : PROJECT_URL + "/state/enumerate", - }, -} - -RPOWER_STATE = { - "on" : "on", - "off" : "off", - "Off" : "off", - "softoff" : "softoff", - "boot" : "reset", - "reset" : "reset", - "bmcreboot" : "BMC reboot", - "Ready" : "BMC Ready", - "NotReady" : "BMC NotReady", - "chassison" : "on (Chassis)", - "Running" : "on", - "Quiesced" : "quiesced", -} - -POWER_STATE_DB = { - "on" : "powering-on", - "off" : "powering-off", - "softoff" : "powering-off", - "boot" : "powering-on", - "reset" : "powering-on", -} - class OpenBMC(base.BaseDriver): headers = {'Content-Type': 'application/json'} @@ -501,107 +456,6 @@ class OpenBMC(base.BaseDriver): return RESULT_OK - def _set_power_onoff(self, subcommand): - """ Set power on/off/softoff/bmcreboot - :param subcommand: subcommand for rpower - :returns: ok if success - :raise: error message if failed - """ - url = HTTP_PROTOCOL + self.bmcip + RPOWER_URLS[subcommand]['url'] - data = { "data": RPOWER_URLS[subcommand]['field'] } - try: - response = self.client.request('PUT', url, OpenBMC.headers, - data, 'rpower_' + subcommand) - except (xcat_exception.SelfServerException, - xcat_exception.SelfClientException) as e: - if subcommand != 'bmcreboot': - result = e.message - return result - - return RESULT_OK - - - def _get_power_state(self, subcommand): - """ Get power current state - :param subcommand: state/stat/status/bmcstate - :returns: current state if success - :raise: error message if failed - """ - result = '' - bmc_not_ready = 'NotReady' - url = HTTP_PROTOCOL + self.bmcip + RPOWER_URLS['state']['url'] - try: - response = self.client.request('GET', url, OpenBMC.headers, - '', 'rpower_' + subcommand) - except xcat_exception.SelfServerException, e: - if subcommand == 'bmcstate': - result = bmc_not_ready - else: - result = e.message - except xcat_exception.SelfClientException, e: - result = e.message - - if result: - return result - - for key in response['data']: - key_type = key.split('/')[-1] - if key_type == 'bmc0': - bmc_current_state = response['data'][key]['CurrentBMCState'].split('.')[-1] - if key_type == 'chassis0': - chassis_current_state = response['data'][key]['CurrentPowerState'].split('.')[-1] - if key_type == 'host0': - host_current_state = response['data'][key]['CurrentHostState'].split('.')[-1] - - if subcommand == 'bmcstate': - if bmc_current_state == 'Ready': - return bmc_current_state - else: - return bmc_not_ready - - if chassis_current_state == 'Off': - return chassis_current_state - elif chassis_current_state == 'On': - if host_current_state == 'Off': - return 'chassison' - elif host_current_state == 'Quiesced': - return host_current_state - elif host_current_state == 'Running': - return host_current_state - else: - return 'Unexpected chassis state=' + host_current_state - else: - return 'Unexpected chassis state=' + chassis_current_state - - - def _rpower_boot(self): - """Power boot - :returns: 'reset' if success - :raise: error message if failed - """ - result = self._set_power_onoff('off') - if result != RESULT_OK: - return result - self.messager.update_node_attributes('status', self.node, POWER_STATE_DB['off']) - - start_timeStamp = int(time.time()) - for i in range (0,30): - status = self._get_power_state('state') - if status in RPOWER_STATE and RPOWER_STATE[status] == 'off': - break - gevent.sleep( 2 ) - - end_timeStamp = int(time.time()) - - if status not in RPOWER_STATE or RPOWER_STATE[status] != 'off': - wait_time = str(end_timeStamp - start_timeStamp) - result = 'Error: Sent power-off command but state did not change ' \ - 'to off after waiting %s seconds. (State= %s).' % (wait_time, status) - return result - - result = self._set_power_onoff('on') - return result - def rflash(self, args): """handle rflash command :param args: subcommands and parameters for rflash @@ -665,65 +519,49 @@ class OpenBMC(base.BaseDriver): self.rflash_log_handle.close() - def rpower(self, args): - """handle rpower command - :param args: subcommands for rpower - """ - subcommand = args[0] - try: - result = self._login() - except xcat_exception.SelfServerException as e: - if subcommand == 'bmcstate': - result = '%s: %s' % (self.node, RPOWER_STATE['NotReady']) - else: - result = '%s: %s' % (self.node, e.message) - except xcat_exception.SelfClientException as e: - result = '%s: %s' % (self.node, e.message) - - if result != RESULT_OK: - self.messager.info(result) - return - - new_status = '' - if subcommand in POWER_SET_OPTIONS: - result = self._set_power_onoff(subcommand) - if result == RESULT_OK: - result = RPOWER_STATE[subcommand] - new_status = POWER_STATE_DB.get(subcommand, '') - - if subcommand in POWER_GET_OPTIONS: - tmp_result = self._get_power_state(subcommand) - result = RPOWER_STATE.get(tmp_result, tmp_result) - - if subcommand == 'boot': - result = self._rpower_boot() - if result == RESULT_OK: - result = RPOWER_STATE[subcommand] - new_status = POWER_STATE_DB.get(subcommand, '') - - if subcommand == 'reset': - status = self._get_power_state('state') - if status == 'Off' or status == 'chassison': - result = RPOWER_STATE['Off'] - else: - result = self._rpower_boot() - if result == RESULT_OK: - result = RPOWER_STATE[subcommand] - new_status = POWER_STATE_DB.get(subcommand, '') - - message = '%s: %s' % (self.node, result) - self.messager.info(message) - if new_status: - self.messager.update_node_attributes('status', self.node, new_status) - - class OpenBMCManager(base.BaseManager): - def __init__(self, messager, cwd, nodes, envs): + def __init__(self, messager, cwd, nodes=None, envs=None): super(OpenBMCManager, self).__init__(messager, cwd) self.nodes = nodes + self.debugmode = (envs and envs.get('debugmode')) or None + + #TODO, remove the global variable DEBUGMODE global DEBUGMODE DEBUGMODE = envs['debugmode'] + def rpower(self, nodesinfo, args): + + # 1, parse args + parser = argparse.ArgumentParser(description='Handle rpower operations.') + parser.add_argument('--action', + help="rpower subcommand.") + parser.add_argument('-V', '--verbose', action='store_true', + help="rpower verbose mode.") + args.insert(0,'--action') + opts = parser.parse_args(args) + + # 2, validate the args + if opts.action is None: + self.messager.error("Not specify the subcommand for rpower") + return + + if opts.action not in (POWER_GET_OPTIONS + POWER_SET_OPTIONS + POWER_REBOOT_OPTIONS): + self.messager.error("Not supported subcommand for rpower: %s" % opts.action) + return + + # 3, run the subcommands + runner = OpenBMCPowerTask(nodesinfo, callback=self.messager, debugmode=self.debugmode, verbose=opts.verbose) + if opts.action == 'bmcstate': + DefaultPowerManager().get_bmc_state(runner) + elif opts.action == 'bmcreboot': + DefaultPowerManager().reboot_bmc(runner) + elif opts.action in POWER_GET_OPTIONS: + DefaultPowerManager().get_power_state(runner) + elif opts.action in POWER_REBOOT_OPTIONS: + DefaultPowerManager().reboot(runner, optype=opts.action) + else: + DefaultPowerManager().set_power_state(runner, power_state=opts.action) + def _get_full_path(self,file_path): if type(self.cwd) == 'unicode': dir_path = self.cwd @@ -819,56 +657,3 @@ class OpenBMCManager(base.BaseManager): self.nodes, nodeinfo, 'rflash', args) self._summary(nodes_num, 'Firmware update') - def rpower(self, nodeinfo, args): - mgr = OpenBMCManager2(self.messager, self.cwd, nodes=self.nodes) - mgr.debugmode = DEBUGMODE - mgr.rpower(nodeinfo, args) - - def _rpower(self, nodeinfo, args): - super(OpenBMCManager, self).process_nodes_worker('openbmc', 'OpenBMC', - self.nodes, nodeinfo, 'rpower', args) - -class OpenBMCManager2(base.BaseManager): - def __init__(self, messager, cwd, nodes=None, envs=None): - super(OpenBMCManager2, self).__init__(messager, cwd) - self.nodes = nodes - self.debugmode = (envs and envs.get('debugmode')) or None - - def rpower(self, nodesinfo, args): - - # 1, parse args - parser = argparse.ArgumentParser(description='Handle rpower operations.') - parser.add_argument('--action', - help="rpower subcommand.") - parser.add_argument('-V', '--verbose', action='store_true', - help="rpower verbose mode.") - args.insert(0,'--action') - opts = parser.parse_args(args) - - # 2, validate the args - if opts.action is None: - self.messager.error("Not specify the subcommand for rpower") - return - - if opts.action not in (POWER_GET_OPTIONS + POWER_SET_OPTIONS + POWER_REBOOT_OPTIONS): - self.messager.error("Not supported subcommand for rpower: %s" % opts.action) - return - - # 3, run the subcommands - runner = OpenBMCPowerTask(nodesinfo, callback=self.messager, debugmode=self.debugmode, verbose=opts.verbose) - if opts.action == 'bmcstate': - DefaultPowerManager().get_bmc_state(runner) - elif opts.action == 'bmcreboot': - DefaultPowerManager().reboot_bmc(runner) - elif opts.action in POWER_GET_OPTIONS: - DefaultPowerManager().get_power_state(runner) - elif opts.action in POWER_REBOOT_OPTIONS: - DefaultPowerManager().reboot(runner, optype=opts.action) - else: - DefaultPowerManager().set_power_state(runner, power_state=opts.action) - - def rflash(self, nodesinfo, args): - # 1, parse args - # 2, validate the args - # 3, run the subcommands - pass diff --git a/xCAT-openbmc-py/xCAT-openbmc-py.spec b/xCAT-openbmc-py/xCAT-openbmc-py.spec index dcd8d8ee7..8ba431f1a 100644 --- a/xCAT-openbmc-py/xCAT-openbmc-py.spec +++ b/xCAT-openbmc-py/xCAT-openbmc-py.spec @@ -32,10 +32,12 @@ install -d $RPM_BUILD_ROOT/%{prefix}/lib/python/agent install -d $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/xcatagent install -d $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/common install -d $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/hwctl +install -d $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/hwctl/executor install -m755 lib/python/agent/*.py $RPM_BUILD_ROOT/%{prefix}/lib/python/agent install -m644 lib/python/agent/xcatagent/*.py $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/xcatagent install -m644 lib/python/agent/common/*.py $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/common install -m644 lib/python/agent/hwctl/*.py $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/hwctl +install -m644 lib/python/agent/hwctl/executor/*.py $RPM_BUILD_ROOT/%{prefix}/lib/python/agent/hwctl/executor %ifnos linux rm -rf $RPM_BUILD_ROOT/%{prefix}/lib/python/agent