mirror of
				https://github.com/xcat2/xcat-core.git
				synced 2025-11-04 05:12:30 +00:00 
			
		
		
		
	Merge pull request #4788 from robin2008/openbmc-refactor
using docopt here for arguments parsing
This commit is contained in:
		@@ -1,8 +1,30 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
###############################################################################
 | 
			
		||||
# IBM(c) 2018 EPL license http://www.eclipse.org/legal/epl-v10.html
 | 
			
		||||
###############################################################################
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
#
 | 
			
		||||
import struct
 | 
			
		||||
import sys
 | 
			
		||||
import inspect
 | 
			
		||||
import logging
 | 
			
		||||
from logging.handlers import SysLogHandler
 | 
			
		||||
 | 
			
		||||
XCAT_LOG_FMT = logging.Formatter("%(asctime)s %(levelname)s " +
 | 
			
		||||
                                 "%(name)s %(process)d " +
 | 
			
		||||
                                 "(%(filename)s:%(lineno)d) "+
 | 
			
		||||
                                 "%(message)s")
 | 
			
		||||
XCAT_LOG_FMT.datefmt = '%Y-%m-%d %H:%M:%S'
 | 
			
		||||
 | 
			
		||||
def getxCATLog(name=None):
 | 
			
		||||
    xl = logging.getLogger(name)
 | 
			
		||||
    xl.fmt = XCAT_LOG_FMT
 | 
			
		||||
    return xl
 | 
			
		||||
 | 
			
		||||
def enableSyslog(name='xcat'):
 | 
			
		||||
    h = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_LOCAL4)
 | 
			
		||||
    h.setFormatter(logging.Formatter('%s: ' % name + '%(levelname)s %(message)s'))
 | 
			
		||||
    logging.getLogger('xcatagent').addHandler(h)
 | 
			
		||||
 | 
			
		||||
def int2bytes(num):
 | 
			
		||||
    return struct.pack('i', num)
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,6 @@ class PowerInterface(object):
 | 
			
		||||
        """Return the power state of the task's nodes.
 | 
			
		||||
 | 
			
		||||
        :param task: a Task instance containing the nodes to act on.
 | 
			
		||||
        :raises: MissingParameterValue if a required parameter is missing.
 | 
			
		||||
        :returns: a power state.
 | 
			
		||||
        """
 | 
			
		||||
        return task.run('get_state')
 | 
			
		||||
@@ -23,19 +22,15 @@ class PowerInterface(object):
 | 
			
		||||
        """Set the power state of the task's nodes.
 | 
			
		||||
 | 
			
		||||
        :param task: a Task instance containing the nodes to act on.
 | 
			
		||||
        :param power_state: Any power state from :mod:`ironic.common.states`.
 | 
			
		||||
        :param power_state: Any supported power state.
 | 
			
		||||
        :param timeout: timeout (in seconds) positive integer (> 0) for any
 | 
			
		||||
          power state. ``None`` indicates to use default timeout.
 | 
			
		||||
        :raises: MissingParameterValue if a required parameter is missing.
 | 
			
		||||
        """
 | 
			
		||||
        return task.run('set_state', power_state, timeout=timeout)
 | 
			
		||||
 | 
			
		||||
    def reboot(self, task, optype='boot', timeout=None):
 | 
			
		||||
        """Perform a hard reboot of the task's nodes.
 | 
			
		||||
 | 
			
		||||
        Drivers are expected to properly handle case when node is powered off
 | 
			
		||||
        by powering it on.
 | 
			
		||||
 | 
			
		||||
        :param task: a Task instance containing the node to act on.
 | 
			
		||||
        :param timeout: timeout (in seconds) positive integer (> 0) for any
 | 
			
		||||
          power state. ``None`` indicates to use default timeout.
 | 
			
		||||
@@ -46,16 +41,14 @@ class PowerInterface(object):
 | 
			
		||||
        """Return the bmc state of the task's nodes.
 | 
			
		||||
 | 
			
		||||
        :param task: a Task instance containing the nodes to act on.
 | 
			
		||||
        :raises: MissingParameterValue if a required parameter is missing.
 | 
			
		||||
        :returns: a power state.
 | 
			
		||||
        :returns: a bmc state.
 | 
			
		||||
        """
 | 
			
		||||
        return task.run('get_bmcstate')
 | 
			
		||||
 | 
			
		||||
    def reboot_bmc(self, task, optype='warm'):
 | 
			
		||||
        """Return the bmc state of the task's nodes.
 | 
			
		||||
        """Set the BMC state of the task's nodes.
 | 
			
		||||
 | 
			
		||||
        :param task: a Task instance containing the nodes to act on.
 | 
			
		||||
        :raises: MissingParameterValue if a required parameter is missing.
 | 
			
		||||
        :returns: a power state.
 | 
			
		||||
        """
 | 
			
		||||
        return task.run('reboot_bmc', optype)
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,6 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
import argparse
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
import sys
 | 
			
		||||
@@ -21,6 +20,10 @@ from hwctl.setboot import DefaultBootManager
 | 
			
		||||
 | 
			
		||||
from xcatagent import base
 | 
			
		||||
import openbmc_rest
 | 
			
		||||
import logging
 | 
			
		||||
logger = logging.getLogger('xcatagent')
 | 
			
		||||
if not logger.handlers:
 | 
			
		||||
    utils.enableSyslog('xcat.agent')
 | 
			
		||||
 | 
			
		||||
HTTP_PROTOCOL = "https://"
 | 
			
		||||
PROJECT_URL = "/xyz/openbmc_project"
 | 
			
		||||
@@ -531,43 +534,51 @@ class OpenBMCManager(base.BaseManager):
 | 
			
		||||
        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']
 | 
			
		||||
 | 
			
		||||
        if self.debugmode:
 | 
			
		||||
            logger.setLevel(logging.DEBUG)
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
        rpower_usage = """
 | 
			
		||||
        Usage:
 | 
			
		||||
            rpower [-V|--verbose] [on|off|softoff|reset|boot|bmcreboot|bmcstate|stat|state|status]
 | 
			
		||||
 | 
			
		||||
        # 2, validate the args
 | 
			
		||||
        if opts.action is None:
 | 
			
		||||
            self.messager.error("Not specify the subcommand for rpower")
 | 
			
		||||
        Options:
 | 
			
		||||
            -V --verbose   rpower verbose mode.
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            opts=docopt(rpower_usage, argv=args)
 | 
			
		||||
 | 
			
		||||
            self.verbose=opts.pop('--verbose')
 | 
			
		||||
            action=[k for k,v in opts.items() if v][0]
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            # It will not be here as perl has validation for args
 | 
			
		||||
            self.messager.error("Failed to parse arguments for rpower: %s" % args)
 | 
			
		||||
            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)
 | 
			
		||||
        # 2, validate the args
 | 
			
		||||
        if action not in (POWER_GET_OPTIONS + POWER_SET_OPTIONS + POWER_REBOOT_OPTIONS):
 | 
			
		||||
            self.messager.error("Not supported subcommand for rpower: %s" % action)
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        # 3, run the subcommands
 | 
			
		||||
        runner = OpenBMCPowerTask(nodesinfo, callback=self.messager, debugmode=self.debugmode, verbose=opts.verbose)
 | 
			
		||||
        if opts.action == 'bmcstate':
 | 
			
		||||
        runner = OpenBMCPowerTask(nodesinfo, callback=self.messager, debugmode=self.debugmode, verbose=self.verbose)
 | 
			
		||||
        if action == 'bmcstate':
 | 
			
		||||
            DefaultPowerManager().get_bmc_state(runner)
 | 
			
		||||
        elif opts.action == 'bmcreboot':
 | 
			
		||||
        elif action == 'bmcreboot':
 | 
			
		||||
            DefaultPowerManager().reboot_bmc(runner)
 | 
			
		||||
        elif opts.action in POWER_GET_OPTIONS:
 | 
			
		||||
        elif action in POWER_GET_OPTIONS:
 | 
			
		||||
            DefaultPowerManager().get_power_state(runner)
 | 
			
		||||
        elif opts.action in POWER_REBOOT_OPTIONS:
 | 
			
		||||
            DefaultPowerManager().reboot(runner, optype=opts.action)
 | 
			
		||||
        elif action in POWER_REBOOT_OPTIONS:
 | 
			
		||||
            DefaultPowerManager().reboot(runner, optype=action)
 | 
			
		||||
        else:
 | 
			
		||||
            DefaultPowerManager().set_power_state(runner, power_state=opts.action)
 | 
			
		||||
            DefaultPowerManager().set_power_state(runner, power_state=action)
 | 
			
		||||
 | 
			
		||||
    def rsetboot(self, nodesinfo, args):
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user