def __init__(self,
                 ipmitool=None,
                 logfile=sys.stdout,
                 prompt=None,
                 block_setup_term=None,
                 delaybeforesend=None):
        self.logfile = logfile
        self.ipmitool = ipmitool
        self.state = IPMIConsoleState.DISCONNECTED
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.delaybeforesend = delaybeforesend
        self.block_setup_term = block_setup_term  # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0  # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0  # flags the object to abandon setup_term operations, like when system off

        # FUTURE - System Console currently tracked in System Object
        # state tracking, reset on boot and state changes
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
示例#2
0
 def __init__(self, ip=None, username=None, password=None):
     self.hostname = ip
     self.username = username
     self.password = password
     self.curl = CurlTool(ip=ip, username=username, password=password)
     self.util = OpTestUtil()
     self.login()
示例#3
0
 def __init__(self,
              ip=None,
              username=None,
              password=None,
              logfile=sys.stdout,
              ipmi=None,
              rest=None,
              web=None,
              check_ssh_keys=False,
              known_hosts_file=None):
     self.cv_bmcIP = ip
     self.cv_bmcUser = username
     self.cv_bmcPasswd = password
     self.cv_IPMI = ipmi
     self.rest = rest
     self.cv_WEB = web
     self.logfile = logfile
     self.check_ssh_keys = check_ssh_keys
     self.known_hosts_file = known_hosts_file
     self.ssh = OpTestSSH(ip,
                          username,
                          password,
                          logfile,
                          prompt=None,
                          block_setup_term=0,
                          check_ssh_keys=check_ssh_keys,
                          known_hosts_file=known_hosts_file)
     # OpTestUtil instance is NOT conf's
     self.util = OpTestUtil()
示例#4
0
 def __init__(self,
              ip=None,
              username=None,
              password=None,
              logfile=sys.stdout,
              ipmi=None,
              rest=None,
              web=None,
              check_ssh_keys=False,
              known_hosts_file=None):
     self.cv_bmcIP = ip
     self.cv_bmcUser = username
     self.cv_bmcPasswd = password
     self.cv_IPMI = ipmi
     self.rest = rest
     self.cv_WEB = web
     self.logfile = logfile
     self.check_ssh_keys = check_ssh_keys
     self.known_hosts_file = known_hosts_file
     self.ssh = OpTestSSH(ip,
                          username,
                          password,
                          logfile,
                          prompt='\[PEXPECT\]#',
                          check_ssh_keys=check_ssh_keys,
                          known_hosts_file=known_hosts_file)
     self.util = OpTestUtil()
示例#5
0
    def __init__(self, host, username, password, logfile=sys.stdout, port=22,
            prompt=None, check_ssh_keys=False, known_hosts_file=None,
            block_setup_term=None, delaybeforesend=None, use_parent_logger=True):
        self.state = ConsoleState.DISCONNECTED
        self.host = host
        self.username = username
        self.password = password
        self.port = port
        self.logfile = logfile
        self.check_ssh_keys=check_ssh_keys
        self.known_hosts_file=known_hosts_file
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # ssh state tracking, reset on boot and state changes
        # ssh port 2200 console tracking not done on SSH object, its done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
        self.use_parent_logger = use_parent_logger
 def __init__(self,
              i_bmcIP,
              i_bmcUser,
              i_bmcPwd,
              logfile=sys.stdout,
              host=None,
              delaybeforesend=None):
     self.cv_bmcIP = i_bmcIP
     self.cv_bmcUser = i_bmcUser
     self.cv_bmcPwd = i_bmcPwd
     self.logfile = logfile
     self.ipmitool = IPMITool(method='lanplus',
                              ip=i_bmcIP,
                              username=i_bmcUser,
                              password=i_bmcPwd,
                              logfile=logfile)
     self.pUpdate = pUpdate(method='lan',
                            ip=i_bmcIP,
                            username=i_bmcUser,
                            password=i_bmcPwd)
     self.console = IPMIConsole(ipmitool=self.ipmitool,
                                logfile=self.logfile,
                                delaybeforesend=delaybeforesend)
     # OpTestUtil instance is NOT conf's
     self.util = OpTestUtil()
     self.host = host
示例#7
0
    def __init__(self, qemu_binary=None, pnor=None, skiboot=None,
            prompt=None, kernel=None, initramfs=None,
            block_setup_term=None, delaybeforesend=None,
            logfile=sys.stdout, hda=None, cdrom=None):
        self.qemu_binary = qemu_binary
        self.pnor = pnor
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.hda = hda
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.cdrom = cdrom
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
示例#8
0
    def __init__(self,
                 i_ffdcDir=None,
                 bmc=None,
                 host=None,
                 state=OpSystemState.UNKNOWN):
        self.bmc = self.cv_BMC = bmc
        self.cv_HOST = host
        self.cv_IPMI = bmc.get_ipmi()
        self.rest = self.bmc.get_rest_api()
        self.console = self.bmc.get_host_console()
        self.util = OpTestUtil()

        # We have a state machine for going in between states of the system
        # initially, everything in UNKNOWN, so we reset things.
        # But, we allow setting an initial state if you, say, need to
        # run against an already IPLed system
        self.state = state
        self.stateHandlers = {}
        self.stateHandlers[OpSystemState.UNKNOWN] = self.run_UNKNOWN
        self.stateHandlers[OpSystemState.OFF] = self.run_OFF
        self.stateHandlers[OpSystemState.IPLing] = self.run_IPLing
        self.stateHandlers[OpSystemState.PETITBOOT] = self.run_PETITBOOT
        self.stateHandlers[
            OpSystemState.PETITBOOT_SHELL] = self.run_PETITBOOT_SHELL
        self.stateHandlers[OpSystemState.BOOTING] = self.run_BOOTING
        self.stateHandlers[OpSystemState.OS] = self.run_OS
        self.stateHandlers[OpSystemState.POWERING_OFF] = self.run_POWERING_OFF

        # We track the state of loaded IPMI modules here, that way
        # we only need to try the modprobe once per IPL.
        # We reset as soon as we transition away from OpSystemState.OS
        # a TODO is to support doing this in petitboot shell as well.
        self.ipmiDriversLoaded = False
示例#9
0
 def __init__(self,
              i_hostip,
              i_hostuser,
              i_hostpasswd,
              i_bmcip,
              i_results_dir,
              scratch_disk="",
              boot_os="",
              proxy="",
              logfile=sys.stdout,
              check_ssh_keys=False,
              known_hosts_file=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     parent_dir = os.path.dirname(os.path.abspath(__file__))
     self.results_dir = i_results_dir
     self.logfile = logfile
     self.ssh = OpTestSSH(i_hostip,
                          i_hostuser,
                          i_hostpasswd,
                          logfile=self.logfile,
                          check_ssh_keys=check_ssh_keys,
                          known_hosts_file=known_hosts_file)
     self.scratch_disk = scratch_disk
     self.boot_os = boot_os
     self.proxy = proxy
     self.scratch_disk_size = None
     self.conf = OpTestConfiguration.conf
示例#10
0
 def __init__(self, i_bmcIP, i_bmcUser, i_bmcPasswd,
              i_bmcUserIpmi,i_bmcPasswdIpmi,i_ffdcDir=None, i_lparip=None,
              i_lparuser=None, i_lparPasswd=None):
     self.cv_BMC = OpTestBMC(i_bmcIP,i_bmcUser,i_bmcPasswd,i_ffdcDir)
     self.cv_IPMI = OpTestIPMI(i_bmcIP,i_bmcUserIpmi,i_bmcPasswdIpmi,
                               i_ffdcDir)
     self.cv_LPAR = OpTestLpar(i_lparip, i_lparuser, i_lparPasswd)
     self.util = OpTestUtil()
示例#11
0
 def __init__(self, ip=None, username=None, password=None):
     self.hostname = ip
     self.username = username
     self.password = password
     self.curl = CurlTool(ip=ip, username=username, password=password)
     self.util = OpTestUtil()
     self.util.PingFunc(self.hostname, BMC_CONST.PING_RETRY_FOR_STABILITY)
     self.login()
     self.wait_for_bmc_runtime()
示例#12
0
 def __init__(self,
              i_hostip,
              i_hostuser,
              i_hostpasswd,
              i_bmcip,
              i_ffdcDir=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     self.cv_ffdcDir = i_ffdcDir
示例#13
0
 def __init__(self,
              i_hostip,
              i_hostuser,
              i_hostpasswd,
              i_bmcip,
              i_ffdcDir=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     self.cv_ffdcDir = i_ffdcDir
     parent_dir = os.path.dirname(os.path.abspath(__file__))
     self.results_dir = self.cv_ffdcDir
     self.ssh = SSHConnection(i_hostip, i_hostuser, i_hostpasswd)
    def __init__(self, host, username, password, logfile=sys.stdout, port=22,
            prompt=None, check_ssh_keys=False, known_hosts_file=None,
            block_setup_term=None, delaybeforesend=None, use_parent_logger=True):
        self.state = ConsoleState.DISCONNECTED
        self.host = host
        self.username = username
        self.password = password
        self.port = port
        self.logfile = logfile
        self.check_ssh_keys=check_ssh_keys
        self.known_hosts_file=known_hosts_file
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # ssh state tracking, reset on boot and state changes
        # ssh port 2200 console tracking not done on SSH object, its done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
        self.use_parent_logger = use_parent_logger
示例#15
0
 def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_ffdcDir=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     self.cv_ffdcDir = i_ffdcDir
    def __init__(self, mambo_binary=None,
            mambo_initial_run_script=None,
            mambo_autorun=None,
            skiboot=None,
            prompt=None,
            kernel=None,
            initramfs=None,
            block_setup_term=None,
            delaybeforesend=None,
            logfile=sys.stdout):
        self.mambo_binary = mambo_binary
        self.mambo_initial_run_script = mambo_initial_run_script
        self.mambo_autorun = mambo_autorun
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
 def __init__(self,
              i_bmcIP,
              i_bmcUser,
              i_bmcPasswd,
              i_bmcUserIpmi,
              i_bmcPasswdIpmi,
              i_ffdcDir=None,
              i_hostip=None,
              i_hostuser=None,
              i_hostPasswd=None):
     self.cv_BMC = OpTestBMC(i_bmcIP, i_bmcUser, i_bmcPasswd, i_ffdcDir)
     self.cv_IPMI = OpTestIPMI(i_bmcIP, i_bmcUserIpmi, i_bmcPasswdIpmi,
                               i_ffdcDir)
     self.cv_HOST = OpTestHost(i_hostip, i_hostuser, i_hostPasswd, i_bmcIP)
     self.cv_WEB = OpTestWeb(i_bmcIP, i_bmcUserIpmi, i_bmcPasswdIpmi)
     self.util = OpTestUtil()
示例#18
0
 def __init__(self, ip=None, username=None, password=None):
     self.hostname = ip
     self.username = username
     self.password = password
     self.curl = CurlTool(ip=ip,
                          username=username,
                          password=password)
     self.util = OpTestUtil()
     self.login()
示例#19
0
    def __init__(self, i_bmcIP, i_bmcUser, i_bmcPwd, i_ffdcDir):

        self.cv_bmcIP = i_bmcIP
        self.cv_bmcUser = i_bmcUser
        self.cv_bmcPwd = i_bmcPwd
        self.cv_ffdcDir = i_ffdcDir
        self.cv_cmd = 'ipmitool -H %s -I lanplus -U %s -P %s ' \
                      % (self.cv_bmcIP, self.cv_bmcUser, self.cv_bmcPwd)
        self.util = OpTestUtil()
示例#20
0
 def __init__(self,
              i_bmcIP,
              i_bmcUser,
              i_bmcPwd,
              i_ffdcDir,
              host=None,
              delaybeforesend=None):
     self.cv_bmcIP = i_bmcIP
     self.cv_bmcUser = i_bmcUser
     self.cv_bmcPwd = i_bmcPwd
     self.cv_ffdcDir = i_ffdcDir
     self.ipmitool = IPMITool(method='lanplus',
                              ip=i_bmcIP,
                              username=i_bmcUser,
                              password=i_bmcPwd)
     self.console = IPMIConsole(ipmitool=self.ipmitool,
                                logdir=i_ffdcDir,
                                delaybeforesend=delaybeforesend)
     self.util = OpTestUtil()
     self.host = host
示例#21
0
    def __init__(self,
                 i_bmcIP,
                 i_bmcUser,
                 i_bmcPwd,
                 i_ffdcDir,
                 i_hostip=None,
                 i_hostuser=None,
                 i_hostPasswd=None):

        self.cv_bmcIP = i_bmcIP
        self.cv_bmcUser = i_bmcUser
        self.cv_bmcPwd = i_bmcPwd
        self.cv_ffdcDir = i_ffdcDir
        self.cv_baseIpmiCmd = 'ipmitool -H %s -I lanplus' % (self.cv_bmcIP)
        if self.cv_bmcUser:
            self.cv_baseIpmiCmd += ' -U %s' % (self.cv_bmcUser)
        if self.cv_bmcPwd:
            self.cv_baseIpmiCmd += ' -P %s' % (self.cv_bmcPwd)
        self.cv_baseIpmiCmd += ' '
        self.util = OpTestUtil()
        self.host_ip = i_hostip
        self.host_user = i_hostuser
        self.host_passwd = i_hostPasswd
示例#22
0
    def __init__(self,
                 mambo_binary=None,
                 mambo_initial_run_script=None,
                 mambo_autorun=None,
                 skiboot=None,
                 prompt=None,
                 kernel=None,
                 initramfs=None,
                 block_setup_term=None,
                 delaybeforesend=None,
                 timeout_factor=1,
                 logfile=sys.stdout):
        self.mambo_binary = mambo_binary
        self.mambo_initial_run_script = mambo_initial_run_script
        self.mambo_autorun = mambo_autorun
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term  # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0  # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0  # flags the object to abandon setup_term operations, like when system off
        self.timeout_factor = timeout_factor  # functional simulators are notoriously slow, so multiply all default timeouts by this factor

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
示例#23
0
 def __init__(self,
              i_hostip,
              i_hostuser,
              i_hostpasswd,
              i_bmcip,
              i_results_dir,
              scratch_disk="",
              proxy="",
              logfile=sys.stdout):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     parent_dir = os.path.dirname(os.path.abspath(__file__))
     self.results_dir = i_results_dir
     self.logfile = logfile
     self.ssh = SSHConnection(i_hostip,
                              i_hostuser,
                              i_hostpasswd,
                              logfile=self.logfile)
     self.scratch_disk = scratch_disk
     self.proxy = proxy
     self.scratch_disk_size = None
示例#24
0
 def __init__(self, ip=None, username=None, password=None,
              logfile=sys.stdout, ipmi=None, rest=None,
              web=None, check_ssh_keys=False, known_hosts_file=None):
     self.cv_bmcIP = ip
     self.cv_bmcUser = username
     self.cv_bmcPasswd = password
     self.cv_IPMI = ipmi
     self.rest = rest
     self.cv_WEB = web
     self.logfile = logfile
     self.check_ssh_keys = check_ssh_keys
     self.known_hosts_file = known_hosts_file
     self.ssh = OpTestSSH(ip, username, password, logfile, prompt=None,
             block_setup_term=0, check_ssh_keys=check_ssh_keys, known_hosts_file=known_hosts_file)
     # OpTestUtil instance is NOT conf's
     self.util = OpTestUtil()
示例#25
0
 def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_results_dir,
              scratch_disk="", proxy="", logfile=sys.stdout,
              check_ssh_keys=False, known_hosts_file=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     parent_dir = os.path.dirname(os.path.abspath(__file__))
     self.results_dir = i_results_dir
     self.logfile = logfile
     self.ssh = OpTestSSH(i_hostip, i_hostuser, i_hostpasswd,
             logfile=self.logfile, check_ssh_keys=check_ssh_keys,
             known_hosts_file=known_hosts_file, use_default_bash=True)
     self.scratch_disk = scratch_disk
     self.proxy = proxy
     self.scratch_disk_size = None
 def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_results_dir,
              scratch_disk="", proxy="", logfile=sys.stdout,
              check_ssh_keys=False, known_hosts_file=None):
     self.ip = i_hostip
     self.user = i_hostuser
     self.passwd = i_hostpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
     self.results_dir = i_results_dir
     self.logfile = logfile
     self.ssh = OpTestSSH(i_hostip, i_hostuser, i_hostpasswd,
             logfile=self.logfile, check_ssh_keys=check_ssh_keys,
             known_hosts_file=known_hosts_file)
     self.scratch_disk = scratch_disk
     self.proxy = proxy
     self.scratch_disk_size = None
     self.conf = OpTestConfiguration.conf
     self.check_ssh_keys = check_ssh_keys
     self.known_hosts_file = known_hosts_file
    def __init__(self, ipmitool=None, logfile=sys.stdout, prompt=None,
            block_setup_term=None, delaybeforesend=None):
        self.ipmitool = ipmitool
        self.state = IPMIConsoleState.DISCONNECTED
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.delaybeforesend = delaybeforesend
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # FUTURE - System Console currently tracked in System Object
        # state tracking, reset on boot and state changes
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
示例#28
0
 def __init__(self, i_lparip, i_lparuser, i_lparpasswd, i_bmcip):
     self.ip = i_lparip
     self.user = i_lparuser
     self.passwd = i_lparpasswd
     self.util = OpTestUtil()
     self.bmcip = i_bmcip
示例#29
0
class QemuConsole():
    """
    A 'connection' to the Qemu Console involves *launching* qemu.
    Closing a connection will *terminate* the qemu process.
    """
    def __init__(self, qemu_binary=None, pnor=None, skiboot=None,
            prompt=None, kernel=None, initramfs=None,
            block_setup_term=None, delaybeforesend=None,
            logfile=sys.stdout, hda=None, cdrom=None):
        self.qemu_binary = qemu_binary
        self.pnor = pnor
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.hda = hda
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.cdrom = cdrom
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        try:
            rc_child = self.pty.close()
            exitCode = signalstatus = None
            if self.pty.status != -1: # leaving for debug
              if os.WIFEXITED(self.pty.status):
                exitCode = os.WEXITSTATUS(self.pty.status)
              else:
                signalstatus = os.WTERMSIG(self.pty.status)
            self.state = ConsoleState.DISCONNECTED
        except pexpect.ExceptionPexpect as e:
            self.state = ConsoleState.DISCONNECTED
            raise "Qemu Console: failed to close console"
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            pass
        log.debug("Qemu close -> TERMINATE")

    def connect(self):
        if self.state == ConsoleState.CONNECTED:
            return self.pty
        else:
            self.util.clear_state(self) # clear when coming in DISCONNECTED

        log.debug("#Qemu Console CONNECT")

        cmd = ("%s" % (self.qemu_binary)
               + " -machine powernv -m 4G"
               + " -nographic -nodefaults"
           )
        if self.pnor:
            cmd = cmd + " -drive file={},format=raw,if=mtd".format(self.pnor)
        if self.skiboot:
            cmd = cmd + " -bios %s" % (self.skiboot)
        if self.kernel:
            cmd = cmd + " -kernel %s" % (self.kernel)
            if self.initramfs is not None:
                cmd = cmd + " -initrd %s" % (self.initramfs)

        if self.hda is not None:
            # Put the disk on the first PHB
            cmd = (cmd
                    + " -drive file={},id=disk01,if=none".format(self.hda)
                    + " -device virtio-blk-pci,drive=disk01,id=virtio01,bus=pcie.0,addr=0"
                )
        if self.cdrom is not None:
            # Put the CDROM on the second PHB
            cmd = (cmd
                    + " -drive file={},id=cdrom01,if=none,media=cdrom".format(self.cdrom)
                    + " -device virtio-blk-pci,drive=cdrom01,id=virtio02,bus=pcie.1,addr=0"
                )
        # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15
        # use skiroot as the source, no sshd in skiroot
        fru_path = os.path.join(OpTestConfiguration.conf.basedir, "test_binaries", "qemu_fru")
        cmd = cmd + " -nic user,model=virtio-net-pci"
        cmd = cmd + " -device ipmi-bmc-sim,id=bmc0,frudatafile=" + fru_path + " -device isa-ipmi-bt,bmc=bmc0,irq=10"
        cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off"
        print(cmd)
        try:
          self.pty = OPexpect.spawn(cmd,logfile=self.logfile)
        except Exception as e:
          self.state = ConsoleState.DISCONNECTED
          raise CommandFailed('OPexpect.spawn',
                  'OPexpect.spawn encountered a problem: ' + str(e), -1)

        self.state = ConsoleState.CONNECTED
        self.pty.setwinsize(1000,1000)
        if self.delaybeforesend:
          self.pty.delaybeforesend = self.delaybeforesend

        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
          self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        # Wait a moment for isalive() to read a correct value and then check
        # if the command has already exited. If it has then QEMU has most
        # likely encountered an error and there's no point proceeding.
        time.sleep(0.2)
        if not self.pty.isalive():
            raise CommandFailed(cmd, self.pty.read(), self.pty.status)
        return self.pty

    def get_console(self):
        if self.state == ConsoleState.DISCONNECTED:
            self.util.clear_state(self)
            self.connect()
        else:
            if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
                self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)
示例#30
0
class QemuConsole():
    """
    A 'connection' to the Qemu Console involves *launching* qemu.
    Closing a connection will *terminate* the qemu process.
    """
    def __init__(self, qemu_binary=None, pnor=None, skiboot=None,
            prompt=None, kernel=None, initramfs=None,
            block_setup_term=None, delaybeforesend=None,
            logfile=sys.stdout, hda=None, cdrom=None):
        self.qemu_binary = qemu_binary
        self.pnor = pnor
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.hda = hda
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.cdrom = cdrom
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        try:
            rc_child = self.pty.close()
            exitCode = signalstatus = None
            if self.pty.status != -1: # leaving for debug
              if os.WIFEXITED(self.pty.status):
                exitCode = os.WEXITSTATUS(self.pty.status)
              else:
                signalstatus = os.WTERMSIG(self.pty.status)
            self.state = ConsoleState.DISCONNECTED
        except pexpect.ExceptionPexpect as e:
            self.state = ConsoleState.DISCONNECTED
            raise "Qemu Console: failed to close console"
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            pass
        log.debug("Qemu close -> TERMINATE")

    def connect(self):
        if self.state == ConsoleState.CONNECTED:
            return self.pty
        else:
            self.util.clear_state(self) # clear when coming in DISCONNECTED

        log.debug("#Qemu Console CONNECT")

        cmd = ("%s" % (self.qemu_binary)
               + " -machine powernv -m 4G"
               + " -nographic -nodefaults"
           )
        if self.pnor:
            cmd = cmd + " -drive file={},format=raw,if=mtd".format(self.pnor)
        if self.skiboot:
            cmd = cmd + " -bios %s" % (self.skiboot)
        if self.kernel:
            cmd = cmd + " -kernel %s" % (self.kernel)
            if self.initramfs is not None:
                cmd = cmd + " -initrd %s" % (self.initramfs)

        if self.hda is not None:
            # Put the disk on the first PHB
            cmd = (cmd
                    + " -drive file={},id=disk01,if=none".format(self.hda)
                    + " -device virtio-blk-pci,drive=disk01,id=virtio01,bus=pcie.0,addr=0"
                )
        if self.cdrom is not None:
            # Put the CDROM on the second PHB
            cmd = (cmd
                    + " -drive file={},id=cdrom01,if=none,media=cdrom".format(self.cdrom)
                    + " -device virtio-blk-pci,drive=cdrom01,id=virtio02,bus=pcie.1,addr=0"
                )
        # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15
        # use skiroot as the source, no sshd in skiroot
        cmd = cmd + " -nic user,model=virtio-net-pci"
        cmd = cmd + " -device ipmi-bmc-sim,id=bmc0 -device isa-ipmi-bt,bmc=bmc0,irq=10"
        cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off"
        print(cmd)
        try:
          self.pty = OPexpect.spawn(cmd,logfile=self.logfile)
        except Exception as e:
          self.state = ConsoleState.DISCONNECTED
          raise CommandFailed('OPexpect.spawn',
                  'OPexpect.spawn encountered a problem: ' + str(e), -1)

        self.state = ConsoleState.CONNECTED
        self.pty.setwinsize(1000,1000)
        if self.delaybeforesend:
          self.pty.delaybeforesend = self.delaybeforesend

        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
          self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        # Wait a moment for isalive() to read a correct value and then check
        # if the command has already exited. If it has then QEMU has most
        # likely encountered an error and there's no point proceeding.
        time.sleep(0.2)
        if not self.pty.isalive():
            raise CommandFailed(cmd, self.pty.read(), self.pty.status)
        return self.pty

    def get_console(self):
        if self.state == ConsoleState.DISCONNECTED:
            self.util.clear_state(self)
            self.connect()
        else:
            if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
                self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)
示例#31
0
class OpTestLpar():

    ##
    # @brief Initialize this object
    #
    # @param i_lparip @type string: IP Address of the lpar
    # @param i_lparuser @type string: Userid to log into the lpar
    # @param i_lparpasswd @type string: Password of the userid to log into the lpar
    #
    def __init__(self, i_lparip, i_lparuser, i_lparpasswd):
        self.ip = i_lparip
        self.user = i_lparuser
        self.passwd = i_lparpasswd
        self.util = OpTestUtil()

    ##
    #   @brief This method executes the command(i_cmd) on the host using a ssh session
    #
    #   @param i_cmd: @type string: Command to be executed on host through a ssh session
    #   @return command output if command execution is successful else raises OpTestError
    #
    def _ssh_execute(self, i_cmd):

        l_host = self.ip
        l_user = self.user
        l_pwd = self.passwd

        l_output = ''
        ssh_ver = '-2'

        self.util.PingFunc(l_host, BMC_CONST.PING_RETRY_FOR_STABILITY)

        # Flush everything out prior to forking
        sys.stdout.flush()

        # Connect the child controlling terminal to a pseudo-terminal
        try:
            pid, fd = pty.fork()
        except OSError as e:
                # Explicit chain of errors
            l_msg = "Got OSError attempting to fork a pty session for ssh."
            raise OpTestError(l_msg)

        if pid == 0:
            # In child process.  Issue attempt ssh connection to remote host

            arglist = ('/usr/bin/ssh -o StrictHostKeyChecking=no',
                       l_host, ssh_ver, '-k', '-l', l_user, i_cmd)

            try:
                os.execv('/usr/bin/ssh', arglist)
            except Exception as e:
                # Explicit chain of errors
                l_msg = "Can not spawn os.execv for ssh."
                print l_msg
                raise OpTestError(l_msg)

        else:
            # In parent process
            # Polling child process for output
            poll = select.poll()
            poll.register(fd, select.POLLIN)

            start_time = time.time()
            # time.sleep(1)
            while True:
                try:
                    evt = poll.poll()
                    x = os.read(fd, 1024)
                    #print "ssh x= " + x
                    end_time = time.time()
                    if(end_time - start_time > 1500):
                        if(i_cmd.__contains__('updlic') or i_cmd.__contains__('update_flash')):
                            continue
                        else:
                            l_msg = "Timeout occured/SSH request " \
                                    "un-responded even after 25 minutes"
                            print l_msg
                            raise OpTestError(l_msg)

                    if(x.__contains__('(yes/no)')):
                        l_res = "yes\r\n"
                        os.write(fd, l_res)
                    if(x.__contains__('s password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if(x.__contains__('Password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if(x.__contains__('password')):
                        response = l_pwd + "\r\n"
                        os.write(fd, response)
                    if(x.__contains__('yes')):
                        response = '1' + "\r\n"
                        os.write(fd, response)
                    if(x.__contains__('Connection refused')):
                        print x
                        raise OpTestError(x)
                    if(x.__contains__('Received disconnect from')):
                        self.ssh_ver = '-1'
                    if(x.__contains__('Connection closed by')):
                        print (x)
                        raise OpTestError(x)
                    if(x.__contains__("WARNING: POSSIBLE DNS SPOOFING DETECTED")):
                        print (x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if(x.__contains__("WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED")):
                        print (x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if(x.__contains__("Permission denied")):
                        l_msg = "Wrong Login or Password(" + l_user + "/" + l_pwd + ") :" + x
                        print (l_msg)
                        raise OpTestError(l_msg)
                    if(x.__contains__("Rebooting") or \
                       (x.__contains__("rebooting the system"))):
                        l_output = l_output + x
                        raise OpTestError(l_output)
                    if(x.__contains__("Connection timed out")):
                        l_msg = "Connection timed out/" + \
                            l_host + " is not pingable"
                        print (x)
                        raise OpTestError(l_msg)
                    if(x.__contains__("could not connect to CLI daemon")):
                        print(x)
                        raise OpTestError("Director server is not up/running("
                                          "Do smstop then smstart to restart)")
                    if((x.__contains__("Error:")) and (i_cmd.__contains__('rmsys'))):
                        print(x)
                        raise OpTestError("Error removing:" + l_host)
                    if((x.__contains__("Bad owner or permissions on /root/.ssh/config"))):
                        print(x)
                        raise OpTestError("Bad owner or permissions on /root/.ssh/config,"
                                          "Try 'chmod -R 600 /root/.ssh' & retry operation")

                    l_output = l_output + x
                    # time.sleep(1)
                except OSError:
                    break
        if l_output.__contains__("Name or service not known"):
            reason = 'SSH Failed for :' + l_host + \
                "\n Please provide a valid Hostname"
            print reason
            raise OpTestError(reason)

        # Gather child process status to freeup zombie and
        # Close child file descriptor before return
        if (fd):
            os.waitpid(pid, 0)
            os.close(fd)
        return l_output

    ##
    # @brief Get and Record Ubunto OS level
    #
    # @return l_oslevel @type string: OS level of the partition provided
    #         or raise OpTestError
    #
    def lpar_get_OS_Level(self):

        l_oslevel = self._ssh_execute(BMC_CONST.BMC_GET_OS_RELEASE)
        print l_oslevel
        return l_oslevel


    ##
    # @brief Executes a command on the os of the bmc to protect network setting
    #
    # @return OpTestError if failed
    #
    def lpar_protect_network_setting(self):
        try:
            l_rc = self._ssh_execute(BMC_CONST.OS_PRESERVE_NETWORK)
        except:
            l_errmsg = "Can't preserve network setting"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief Performs a cold reset onto the lpar
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def lpar_cold_reset(self):
        # TODO: cold reset command to os is not too stable
        l_cmd = BMC_CONST.LPAR_COLD_RESET
        print ("Applying Cold reset. Wait for "
                            + str(BMC_CONST.BMC_COLD_RESET_DELAY) + "sec")
        l_rc = self._ssh_execute(l_cmd)
        if BMC_CONST.BMC_PASS_COLD_RESET in l_rc:
            print l_rc
            time.sleep(BMC_CONST.BMC_COLD_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Cold reset Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def lpar_code_update(self, i_image, imagecomponent):

        # Copy the hpm file to the tmp folder in the partition
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying hpm file to lpar failed"
            print l_msg
            raise OpTestError(l_msg)

        #self.lpar_protect_network_setting() #writing to partition is not stable
        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + imagecomponent
        print l_cmd
        try:
            l_rc = self._ssh_execute(l_cmd)
            print l_rc
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if(l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)
class OpTestSSH():
    def __init__(self, host, username, password, logfile=sys.stdout, port=22,
            prompt=None, check_ssh_keys=False, known_hosts_file=None,
            block_setup_term=None, delaybeforesend=None, use_parent_logger=True):
        self.state = ConsoleState.DISCONNECTED
        self.host = host
        self.username = username
        self.password = password
        self.port = port
        self.logfile = logfile
        self.check_ssh_keys=check_ssh_keys
        self.known_hosts_file=known_hosts_file
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # ssh state tracking, reset on boot and state changes
        # ssh port 2200 console tracking not done on SSH object, its done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1
        self.use_parent_logger = use_parent_logger

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        if self.state == ConsoleState.DISCONNECTED:
            return
        try:
            self.pty.send("\r")
            self.pty.send('~.')
            close_rc = self.pty.expect([pexpect.TIMEOUT, pexpect.EOF], timeout=10)
            rc_child = self.pty.close()
            exitCode = signalstatus = None
            if self.pty.status != -1: # leaving here for debug
              if os.WIFEXITED(self.pty.status):
                exitCode = os.WEXITSTATUS(self.pty.status)
              else:
                signalstatus = os.WTERMSIG(self.pty.status)
            self.state = ConsoleState.DISCONNECTED
        except pexpect.ExceptionPexpect as e:
            self.state = ConsoleState.DISCONNECTED
            raise "SSH Console: failed to close ssh console"
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            pass

    def connect(self):
        if self.state == ConsoleState.CONNECTED:
            rc_child = self.close()
            self.state = ConsoleState.DISCONNECTED
        else:
            self.util.clear_state(self) # clear when coming in DISCONNECTED

        cmd = ("sshpass -p %s " % (self.password)
               + " ssh"
               + " -p %s" % str(self.port)
               + " -l %s %s" % (self.username, self.host)
               + " -o PubkeyAuthentication=no -o afstokenpassing=no"
               )

        if not self.check_ssh_keys:
            cmd = (cmd
                    + " -q"
                    + " -o 'UserKnownHostsFile=/dev/null' "
                    + " -o 'StrictHostKeyChecking=no'"
                    )
        elif self.known_hosts_file:
            cmd = (cmd + " -o UserKnownHostsFile=" + self.known_hosts_file)

        # For multi threades SSH sessions use individual logger and file handlers per session.
        if self.use_parent_logger:
            self.log = log
        else:
            self.log = OpTestLogger.optest_logger_glob.get_custom_logger(__name__)

        self.log.debug(cmd)

        try:
          self.pty = OPexpect.spawn(cmd,
                                    logfile=self.logfile,
                failure_callback=set_system_to_UNKNOWN_BAD,
                failure_callback_data=self.system)
        except Exception as e:
          self.state = ConsoleState.DISCONNECTED
          raise CommandFailed('OPexepct.spawn encountered a problem', -1)

        self.state = ConsoleState.CONNECTED
        # set for bash, otherwise it takes the 24x80 default
        self.pty.setwinsize(1000,1000)
        if self.delaybeforesend:
          self.pty.delaybeforesend = self.delaybeforesend
        self.pty.logfile_read = OpTestLogger.FileLikeLogger(log)
        time.sleep(2) # delay here in case messages like afstokenpassing unsupported show up which mess up setup_term
        self.check_set_term()
        return self.pty

    def check_set_term(self):
        if self.block_setup_term is not None:
          setup_term_flag = self.block_setup_term # caller control
        else:
          setup_term_flag = self.system.block_setup_term # system defined control
        if self.port == 2200:
          if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set !=1:
            self.util.setup_term(self.system, self.pty, None, setup_term_flag)
        else:
          if self.SUDO_set != 1 or self.LOGIN_set != 1 or self.PS1_set !=1:
            self.util.setup_term(self.system, self.pty, self, setup_term_flag)

    def get_console(self):
        if self.state == ConsoleState.DISCONNECTED:
          self.connect()

        count = 0
        while (not self.pty.isalive()):
            self.log.info('# Reconnecting')
            if (count > 0):
                time.sleep(1)
            self.connect()
            count += 1
            if count > 120:
                raise "SSH: not able to get console"

        self.check_set_term()

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)
示例#33
0
class HostManagement():
    def __init__(self, ip=None, username=None, password=None):
        self.hostname = ip
        self.username = username
        self.password = password
        self.curl = CurlTool(ip=ip, username=username, password=password)
        self.util = OpTestUtil()
        self.login()

    '''
    curl -c cjar -k -X POST -H "Content-Type: application/json" \
    -d '{"data": [ "root", "0penBmc" ] }' https://bmc/login
    '''

    def login(self):
        data = '\'{"data": [ "root", "0penBmc" ] }\''
        self.curl.feed_data(dbus_object="/login",
                            operation='w',
                            command="POST",
                            data=data)
        self.curl.run()

    '''
    Logout:
    curl -c cjar -b cjar -k -X POST -H "Content-Type: application/json" \
    -d '{"data": [ ] }' \
    https://bmc/logout
    '''

    def logout(self):
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object="/logout",
                            operation='rw',
                            command="POST",
                            data=data)
        self.curl.run()

    '''
    Inventory enumerate:
    /xyz/openbmc_project/inventory
    curl -b cjar -k https://bmc/xyz/openbmc_project/inventory/enumerate
    '''

    def get_inventory(self):
        self.curl.feed_data(
            dbus_object="/xyz/openbmc_project/inventory/enumerate",
            operation="r")
        self.curl.run()

    def sensors(self):
        self.curl.feed_data(
            dbus_object="/xyz/openbmc_project/sensors/enumerate",
            operation="r")
        self.curl.run()

    '''
    Get Chassis Power State:
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X GET -d '{"data":
    []}' https://bmc/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState
    '''

    def get_power_state(self):
        data = '\'{"data" : []}\''
        obj = '/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState'
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="GET",
                            data=data)
        self.curl.run()

    '''
    Get Host State:
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X GET -d '{"data":
    []}' https://bmc/xyz/openbmc_project/state/host0/attr/CurrentHostState
    '''

    def get_host_state(self):
        data = '\'{"data" : []}\''
        obj = '/xyz/openbmc_project/state/host0/attr/CurrentHostState'
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="GET",
                            data=data)
        self.curl.run()

    '''
    Reboot server gracefully:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.Reboot"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''

    def soft_reboot(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    Reboot server Immediately:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.Reboot"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''

    def hard_reboot(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    power soft server: Not yet implemented (TODO)
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X POST -d '{"data":
    []}' https://bmc/org/openbmc/control/chassis0/action/softPowerOff
    '''

    def power_soft(self):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/control/chassis0/action/softPowerOff"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="POST",
                            data=data)
        self.curl.run()

    '''
    power off server:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.Off"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''

    def power_off(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Off\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    power on server:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.On"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''

    def power_on(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.On\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    List SEL
    curl -b cjar -k -H "Content-Type: application/json" -X GET \
    -d '{"data" : []}' \
    https://bmc/xyz/openbmc_project/logging/enumerate
    '''

    def list_sel(self):
        print "List of SEL entries"
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/logging/enumerate"
        self.curl.feed_data(dbus_object=obj,
                            operation='r',
                            command="GET",
                            data=data)
        return self.curl.run()

    def get_sel_ids(self):
        list = []
        data = self.list_sel()
        list = re.findall(r"/xyz/openbmc_project/logging/entry/(\d{1,})",
                          str(data))
        if list:
            print "SEL entries list by ID: %s" % list
        return list

    def clear_sel_by_id(self):
        print "Clearing SEL entries by id"
        list = self.get_sel_ids()
        for id in list:
            data = '\'{"data" : []}\''
            obj = "/xyz/openbmc_project/logging/entry/%s/action/Delete" % id
            self.curl.feed_data(dbus_object=obj,
                                operation='rw',
                                command="POST",
                                data=data)
            self.curl.run()

    '''
    clear SEL : Clearing complete SEL is not yet implemented (TODO)
    curl -b cjar -k -H "Content-Type: application/json" -X POST \
    -d '{"data" : []}' \
    https://bmc/org/openbmc/records/events/action/clear
    '''

    def clear_sel(self):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/records/events/action/clear"
        try:
            self.curl.feed_data(dbus_object=obj,
                                operation='r',
                                command="POST",
                                data=data)
            self.curl.run()
        except FailedCurlInvocation as f:
            print "# Ignoring failure clearing SELs, not all OpenBMC builds support this yet"
            pass

    '''
    set boot device to setup
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -d "{\"data\": \"Setup\"}" -X PUT
    https://bmc/org/openbmc/settings/host0/attr/boot_flags
    '''

    def set_bootdev_to_setup(self):
        data = '\'{"data": \"Setup\"}\''
        obj = "/org/openbmc/settings/host0/attr/boot_flags"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    set boot device to default
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -d "{\"data\": \"Default\"}" -X PUT
    https://bmc/org/openbmc/settings/host0/attr/boot_flags
    '''

    def set_bootdev_to_none(self):
        data = '\'{\"data\": \"Default\"}\''
        obj = "/org/openbmc/settings/host0/attr/boot_flags"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()

    '''
    Boot progress
    curl   -b cjar   -k  -H  'Content-Type: application/json'   -d '{"data": [ ] }'
    -X GET https://bmc//org/openbmc/sensors/host/BootProgress
    '''

    def wait_for_runtime(self, timeout=10):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/sensors/host/BootProgress"
        self.curl.feed_data(dbus_object=obj,
                            operation='r',
                            command="GET",
                            data=data)
        timeout = time.time() + 60 * timeout
        while True:
            output = self.curl.run()
            obj = re.search('"value": "(.*?)"', output)
            if obj:
                print "System state: %s" % obj.group(1)
            if "FW Progress, Starting OS" in output:
                print "System FW booted to runtime: IPL finished"
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True

    def wait_for_standby(self, timeout=10):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/sensors/host/BootProgress"
        self.curl.feed_data(dbus_object=obj,
                            operation='r',
                            command="GET",
                            data=data)
        timeout = time.time() + 60 * timeout
        while True:
            output = self.curl.run()
            obj = re.search('"value": "(.*?)"', output)
            if obj:
                print "System state: %s" % obj.group(1)
            if '"value": "Off"' in output:
                print "System reached standby state"
                break
            if time.time() > timeout:
                l_msg = "Standby timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True

    '''
    BMC reset
    curl -c cjar -b cjar -k -H "Content-Type: application/json"
    -d "{\"data\" : \"xyz.openbmc_project.State.BMC.Transition.Reboot\"}" -X PUT
    https://bmc/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition
    '''

    def bmc_reset(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.BMC.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition"
        self.curl.feed_data(dbus_object=obj,
                            operation='rw',
                            command="PUT",
                            data=data)
        self.curl.run()
        time.sleep(10)
        self.util.PingFunc(self.hostname, BMC_CONST.PING_RETRY_FOR_STABILITY)
        time.sleep(5)  # Need some stablity here
        self.login()
        self.wait_for_bmc_runtime()

    def get_bmc_state(self):
        obj = "/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        return self.curl.run()

    def wait_for_bmc_runtime(self, timeout=10):
        timeout = time.time() + 60 * timeout
        output = ""
        while True:
            if '"description": "Login required"' in output:
                self.login()
            try:
                output = self.get_bmc_state()
            except:
                pass
            if '"data": "xyz.openbmc_project.State.BMC.BMCState.Ready"' in output:
                print "BMC is UP & Ready"
                break
            if time.time() > timeout:
                l_msg = "BMC Ready timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True
示例#34
0
class OpTestBMC():
    '''
    The main object for communicating with a BMC and taking actions with it.
    '''
    def __init__(self,
                 ip=None,
                 username=None,
                 password=None,
                 logfile=sys.stdout,
                 ipmi=None,
                 rest=None,
                 web=None,
                 check_ssh_keys=False,
                 known_hosts_file=None):
        self.cv_bmcIP = ip
        self.cv_bmcUser = username
        self.cv_bmcPasswd = password
        self.cv_IPMI = ipmi
        self.rest = rest
        self.cv_WEB = web
        self.logfile = logfile
        self.check_ssh_keys = check_ssh_keys
        self.known_hosts_file = known_hosts_file
        self.ssh = OpTestSSH(ip,
                             username,
                             password,
                             logfile,
                             prompt=None,
                             block_setup_term=0,
                             check_ssh_keys=check_ssh_keys,
                             known_hosts_file=known_hosts_file)
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()

    def set_system(self, system):
        self.ssh.set_system(system)

    def bmc_host(self):
        return self.cv_bmcIP

    def get_ipmi(self):
        '''
        Get an object that can be used to do things over IPMI
        '''
        return self.cv_IPMI

    def get_rest_api(self):
        '''
        OpenBMC specific REST API.
        '''
        return self.rest

    def get_host_console(self):
        return self.cv_IPMI.get_host_console()

    def run_command(self, command, timeout=60, retry=0):
        '''
        Run a command on the BMC itself.
        '''
        return self.ssh.run_command(command, timeout, retry)

    ##
    # @brief
    #
    # @return BMC_CONST.FW_SUCCESS on success and
    #         raise OpTestError on failure
    #
    def reboot(self):
        '''
        This function issues the reboot command on the BMC console.  It then
        pings the BMC until it responds, which presumably means that it is done
        rebooting.

        :raises: :class:`common.OpTestError`
        '''
        retries = 0
        try:
            self.ssh.run_command('reboot')
        except SSHSessionDisconnected as e:
            pass
        except CommandFailed as e:
            pass
        self.ssh.close()
        log.info('Sent reboot command now waiting for reboot to complete...')
        # Wait for BMC to go down.
        self.util.ping_fail_check(self.cv_bmcIP)
        # Wait for BMC to ping back.
        self.util.PingFunc(self.cv_bmcIP,
                           totalSleepTime=BMC_CONST.PING_RETRY_POWERCYCLE)
        # Ping the system until it reboots
        while True:
            try:
                subprocess.check_call(["ping", self.cv_bmcIP, "-c1"])
                break
            except subprocess.CalledProcessError as e:
                log.debug("Ping return code: ", e.returncode, "retrying...")
                retries += 1
                time.sleep(10)

            if retries > 10:
                l_msg = "Error. BMC is not responding to pings"
                log.error(l_msg)
                raise OpTestError(l_msg)

            log.info('BMC reboot complete.')

        return BMC_CONST.FW_SUCCESS

    def image_transfer(self, i_imageName, copy_as=None):
        '''
        This function copies the given image to the BMC /tmp dir.

        :param i_imageName: Local file to copy across
        :param copy_as: file name to copy to (in /tmp)
        :returns: Exit code of scp or rsync process
        '''
        img_path = i_imageName
        ssh_opts = ' -o PubkeyAuthentication=no '
        if not self.check_ssh_keys:
            ssh_opts = ssh_opts + ' -o StrictHostKeyChecking=no'
        elif self.known_hosts_file:
            ssh_opts = ssh_opts + ' -o UserKnownHostsFile=' + self.known_hosts_file

        rsync_cmd = 'rsync -P -v -e "ssh -k' + ssh_opts + '" %s %s@%s:/tmp' % (
            img_path, self.cv_bmcUser, self.cv_bmcIP)
        if copy_as:
            rsync_cmd = rsync_cmd + '/' + copy_as

        log.debug(rsync_cmd)
        rsync = pexpect.spawn(rsync_cmd)
        rsync.logfile = OpTestLogger.FileLikeLogger(log)
        rsync.expect('assword: ')
        rsync.sendline(self.cv_bmcPasswd)
        r = rsync.expect(['total size is', 'error while loading shared lib'],
                         timeout=1800)
        if r == 1:
            # On AMI BMCs that are missing libacl.so.1 for rsync,
            # we have to fall back to "scp"...
            # which is actually SSH+dd because there's no scp
            # This is notable for Palmetto
            log.debug("Falling back to SCP")
            if copy_as is None:
                copy_as = os.path.basename(img_path)
            scp_cmd = "bash -c \"sshpass -p {} ssh".format(
                self.cv_bmcPasswd) + ssh_opts + ' -o LogLevel=quiet'
            scp_cmd = scp_cmd + " {}@{} dd of=/tmp/{} < {}\"".format(
                self.cv_bmcUser, self.cv_bmcIP, copy_as, img_path)
            log.debug(scp_cmd)
            scp = pexpect.spawn(scp_cmd, timeout=120)
            scp.expect(pexpect.EOF)
            scp.wait()
            scp.close()
            chmod_cmd = "sshpass -p {} ssh {} {}@{} chmod +x /tmp/{}".format(
                self.cv_bmcPasswd, ssh_opts, self.cv_bmcUser, self.cv_bmcIP,
                copy_as)
            log.debug(chmod_cmd)
            chmod = pexpect.spawn(chmod_cmd)
            chmod.expect(pexpect.EOF)
            chmod.wait()
            chmod.close()
            return scp.exitstatus
        else:
            rsync.expect(pexpect.EOF)
            rsync.close()
            return rsync.exitstatus

    def pnor_img_flash_ami(self, i_pflash_dir, i_imageName):
        '''
        This function flashes the PNOR image using pflash tool,
        And this function will work based on the assumption that pflash
        tool available in i_pflash_dir. Depending on the BMC type (and even
        *version* of BMC firmware) we may have had to copy over pflash
        ourselves.

        :param i_pflash_dir: directory where pflash tool is present.
        :type i_pflash_dir: str
        :param i_imageName: Name of the image file.
                            e.g. `firestone.pnor` or `firestone_update.pnor`
        :type i_imageName: str

        :returns: pflash command return code
        '''
        cmd = i_pflash_dir + '/pflash -e -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def pnor_img_flash_openbmc(self, i_imageName):
        '''
        on openbmc systems pflash tool available
        '''
        cmd = 'pflash -E -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_ami(self, i_pflash_dir, i_imageName, i_partName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P %s' % (i_imageName,
                                                                 i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_openbmc(self, i_imageName, i_partName):
        cmd = 'pflash -p /tmp/%s -e -f -P %s' % (i_imageName, i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def validate_pflash_tool(self, i_dir=""):
        '''
        This function validates presence of pflash tool, which will be
        used for pnor image flash

        :param i_dir: directory where pflash tool should be present.
        :returns: True/False
        '''
        i_dir = os.path.join(i_dir, "pflash")
        try:
            l_res = self.ssh.run_command("which %s" % i_dir)
        except CommandFailed:
            l_msg = "# pflash tool is not available on BMC"
            log.error(l_msg)
            return False
        return True

    def has_inband_bootdev(self):
        return True

    def has_os_boot_sensor(self):
        return True

    def has_host_status_sensor(self):
        return True

    def has_occ_active_sensor(self):
        return True

    def supports_ipmi_dcmi(self):
        return True

    def has_ipmi_sel(self):
        return True
示例#35
0
class HostManagement():
    def __init__(self, ip=None, username=None, password=None):
        self.hostname = ip
        self.username = username
        self.password = password
        self.curl = CurlTool(ip=ip,
                             username=username,
                             password=password)
        self.util = OpTestUtil()
        self.login()

    '''
    curl -c cjar -k -X POST -H "Content-Type: application/json" \
    -d '{"data": [ "root", "0penBmc" ] }' https://bmc/login
    '''
    def login(self):
        data = '\'{"data": [ "root", "0penBmc" ] }\''
        self.curl.feed_data(dbus_object="/login", operation='w', command="POST", data=data)
        self.curl.run()

    '''
    Logout:
    curl -c cjar -b cjar -k -X POST -H "Content-Type: application/json" \
    -d '{"data": [ ] }' \
    https://bmc/logout
    '''
    def logout(self):
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object="/logout", operation='rw', command="POST", data=data)
        self.curl.run()

    '''
    Inventory enumerate:
    /xyz/openbmc_project/inventory
    curl -b cjar -k https://bmc/xyz/openbmc_project/inventory/enumerate
    '''
    def get_inventory(self):
        self.curl.feed_data(dbus_object="/xyz/openbmc_project/inventory/enumerate", operation="r")
        self.curl.run()

    def sensors(self):
        self.curl.feed_data(dbus_object="/xyz/openbmc_project/sensors/enumerate", operation="r")
        self.curl.run()

    '''
    Get Current Host Power State:
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X GET -d '{"data":
    []}' https://bmc/xyz/openbmc_project/state/host0/attr/CurrentHost
    '''
    def get_power_state(self):
        data = '\'{"data" : []}\''
        obj = '/xyz/openbmc_project/state/host0/attr/CurrentHostState'
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET", data=data)
        self.curl.run()

    '''
    Get Host State:
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X GET -d '{"data":
    []}' https://bmc/xyz/openbmc_project/state/host0/attr/CurrentHostState
    '''
    def get_host_state(self):
        data = '\'{"data" : []}\''
        obj = '/xyz/openbmc_project/state/host0/attr/CurrentHostState'
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET", data=data)
        self.curl.run()

    '''
    Reboot server gracefully:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.Reboot"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''
    def soft_reboot(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    Reboot server Immediately:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.Reboot"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''
    def hard_reboot(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    power soft server:
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Chassis.Transition.Off"}'
    https://bmc/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition
    '''
    def power_soft(self):
        data = '\'{"data" : \"xyz.openbmc_project.State.Chassis.Transition.Off\"}\''
        obj = "xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    power off server:
    https://bmc/xyz/openbmc_project/state/chassis0/attr/RequestedHostTransition
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''
    def power_off(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Chassis.Transition.Off\"}\''
        obj = "/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition"
        try:
            self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
            self.curl.run()
        except FailedCurlInvocation as f:
            print "# Ignoring failure powering off chassis, trying powering off host"
            pass
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.Off\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    power on server:
    curl -c cjar b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data": "xyz.openbmc_project.State.Host.Transition.On"}'
    https://bmc/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
    '''
    def power_on(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.Host.Transition.On\"}\''
        obj = "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    List SEL
    curl -b cjar -k -H "Content-Type: application/json" -X GET \
    -d '{"data" : []}' \
    https://bmc/xyz/openbmc_project/logging/enumerate
    '''
    def list_sel(self):
        print "List of SEL entries"
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/logging/enumerate"
        self.curl.feed_data(dbus_object=obj, operation='r', command="GET", data=data)
        return self.curl.run()

    def get_sel_ids(self):
        sels = []
        data = self.list_sel()
        data = json.loads(data)
        for k in data['data']:
            print repr(k)
            m = re.match(r"/xyz/openbmc_project/logging/entry/(\d{1,})$", k)
            if m:
                sels.append(m.group(1))
        print repr(sels)
        return sels

    def clear_sel_by_id(self):
        print "Clearing SEL entries by id"
        list = self.get_sel_ids()
        for id in list:
            data = '\'{"data" : []}\''
            obj = "/xyz/openbmc_project/logging/entry/%s/action/Delete" % id
            self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
            self.curl.run()

    def verify_clear_sel(self):
        print "Check if SEL has really zero entries or not"
        list = []
        list = self.get_sel_ids()
        if not list:
            return True
        return False

    '''
    clear SEL : Clearing complete SEL
    curl -b cjar -k -H "Content-Type: application/json" -X POST \
    -d '{"data" : []}' \
    https://bmc/xyz/openbmc_project/logging/action/DeleteAll
    '''
    def clear_sel(self):
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/logging/action/DeleteAll"
        try:
            self.curl.feed_data(dbus_object=obj, operation='r', command="POST", data=data)
            self.curl.run()
        except FailedCurlInvocation as f:
            print "# Ignoring failure clearing SELs, not all OpenBMC builds support this yet"
            pass

    '''
    get current boot device info
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -d '{"data" : []}' -X GET
    https://bmc/xyz/openbmc_project/control/host0/boot/attr/bootmode
    '''
    def get_current_bootdev(self):
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/control/host0/boot/attr/bootmode"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET", data=data)
        output = self.curl.run()
        result = json.loads(output)
        data = result.get('data')
        bootmode = ""
        if "Setup" in data:
            bootmode = "Setup"
        elif "Regular" in data:
            bootmode = "Regular"
        return bootmode

    '''
    set boot device to setup
    curl -c cjar -b cjar -k -H "Content-Type: application/json"
    -d "{"data": \"xyz.openbmc_project.Control.Boot.Mode.Modes.Setup\"}" -X PUT
    https://bmc/xyz/openbmc_project/control/host0/boot/attr/bootmode
    '''
    def set_bootdev_to_setup(self):
        data = '\'{"data": \"xyz.openbmc_project.Control.Boot.Mode.Modes.Setup\"}\''
        obj = "/xyz/openbmc_project/control/host0/boot/attr/bootmode"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    set boot device to regular/default
    curl -c cjar -b cjar -k -H "Content-Type: application/json"
    -d "{"data": \"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular\"}" -X PUT
    https://bmc/xyz/openbmc_project/control/host0/boot/attr/bootmode
    '''
    def set_bootdev_to_none(self):
        data = '\'{"data": \"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular\"}\''
        obj = "/xyz/openbmc_project/control/host0/boot/attr/bootmode"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    '''
    get boot progress info
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -d
    '{"data": [ ] }' -X GET
    https://bmc//xyz/openbmc_project/state/host0/attr/BootProgress
    '''
    def get_boot_progress(self):
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/state/host0/attr/BootProgress"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET", data=data)
        self.curl.run()

    '''
    Wait for OpenBMC Host state
    This is only on more modern OpenBMC builds.
    If unsupported, return None and fall back to old method.
    We can't just continue to use the old method until it disappears as
    it is actively broken (always returns Off).
    NOTE: The whole BMC:CHassis:Host mapping is completely undocumented and
    undiscoverable. At some point, this may change from 0,0,0 and one of each and
    everything is going to be a steaming pile of fail.
    '''
    def wait_for_host_state(self, target_state, host=0, timeout=10):
        data = '\'{"data" : []}\''
        obj = "/xyz/openbmc_project/state/host%d" % host
        self.curl.feed_data(dbus_object=obj, operation='r', command="GET", data=data)
        timeout = time.time() + 60*timeout
        target_state = "xyz.openbmc_project.State.Host.HostState.%s" % target_state
        while True:
            output = self.curl.run()
            result = json.loads(output)
            print repr(result)
            if result.get('data') is None or result.get('data').get('CurrentHostState') is None:
                return None
            state = result['data']['CurrentHostState']
            print "System state: %s (target %s)" % (state, target_state)
            if state == target_state:
                break
            if time.time() > timeout:
                raise OpTestError("Timeout waiting for host state to become %s" % target_state)
            time.sleep(5)
        return True

    def wait_for_standby(self, timeout=10):
        r = self.wait_for_host_state("Off", timeout=timeout)
        if r is None:
            print "Falling back to old BootProgress"
            return old_wait_for_standby(timeout)

    def wait_for_runtime(self, timeout=10):
        r = self.wait_for_host_state("Running", timeout=timeout)
        if r is None:
            print "Falling back to old BootProgress"
            return old_wait_for_standby(timeout)

    '''
    Boot progress
    curl   -b cjar   -k  -H  'Content-Type: application/json'   -d '{"data": [ ] }'
    -X GET https://bmc//xyz/openbmc_project/state/host0/attr/BootProgress
    '''
    def old_wait_for_runtime(self, timeout=10):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/sensors/host/BootProgress"
        self.curl.feed_data(dbus_object=obj, operation='r', command="GET", data=data)
        timeout = time.time() + 60*timeout
        while True:
            output = self.curl.run()
            result = json.loads(output)
            print repr(result)
            state = result['data']['value']
            print "System state: %s" % state
            if state == 'FW Progress, Starting OS':
                print "System FW booted to runtime: IPL finished"
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True

    def old_wait_for_standby(self, timeout=10):
        data = '\'{"data" : []}\''
        obj = "/org/openbmc/sensors/host/BootProgress"
        self.curl.feed_data(dbus_object=obj, operation='r', command="GET", data=data)
        timeout = time.time() + 60*timeout
        while True:
            output = self.curl.run()
            result = json.loads(output)
            print repr(result)
            state = result['data']['value']
            print "System state: %s" % state
            if state == 'Off':
                print "System reached standby state"
                break
            if time.time() > timeout:
                l_msg = "Standby timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True

    '''
    BMC reset
    curl -c cjar -b cjar -k -H "Content-Type: application/json"
    -d "{\"data\" : \"xyz.openbmc_project.State.BMC.Transition.Reboot\"}" -X PUT
    https://bmc/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition
    '''
    def bmc_reset(self):
        data = '\'{\"data\" : \"xyz.openbmc_project.State.BMC.Transition.Reboot\"}\''
        obj = "/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()
        # Wait for BMC to go down.
        self.util.ping_fail_check(self.hostname)
        # Wait for BMC to ping back.
        self.util.PingFunc(self.hostname, BMC_CONST.PING_RETRY_FOR_STABILITY)
        # Wait for BMC ready state.
        self.wait_for_bmc_runtime()

    def get_bmc_state(self):
        obj = "/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        return self.curl.run()

    def wait_for_bmc_runtime(self, timeout=10):
        timeout = time.time() + 60*timeout
        output = ""
        while True:
            if '"description": "Login required"' in output:
                self.login()
            try:
                output = self.get_bmc_state()
            except FailedCurlInvocation as cf:
                output = cf.output
            if '"data": "xyz.openbmc_project.State.BMC.BMCState.Ready"' in output:
                print "BMC is UP & Ready"
                break
            if time.time() > timeout:
                l_msg = "BMC Ready timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
        return True

    def get_list_of_image_ids(self):
        obj = "/xyz/openbmc_project/software/"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        output = self.curl.run()
        r = json.loads(output)
        print repr(r)
        ids = []
        for k in r['data']:
            m = re.match(r'/xyz/openbmc_project/software/(.*)', k)
            # According to the OpenBMC docs, Image ID can be
            # Implementation defined, and thus, the word 'active'
            # would be a valid ID.
            # Except that it isn't. The documentation is lies.
            # It seems that 'active' is special.
            # There is no documentation as to what other
            # strings are special, or could become special.
            # So, HACK HACK HACK around it. :(
            # https://github.com/openbmc/phosphor-dbus-interfaces/tree/55d03ca/xyz/openbmc_project/Software#image-identifier
            # This is getting insane, it's not just 'active'
            # It's 'functional' as well. Or some other things.
            # So, we assume that if we don't have 'Purpose' it's something special
            # like the 'active' or (new) 'functional'.
            # Adriana has promised me that this is safe into the future.
            if m:
                i = self.image_data(m.group(1))
                if i['data'].get('Purpose') is not None:
                    ids.append(m.group(1))

        print "List of images id's: %s" % ids
        return ids

    def image_data(self, id):
        obj = "/xyz/openbmc_project/software/%s" % id
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        return json.loads(self.curl.run())

    """
    Upload a image
    curl   -b cjar  -c cjar   -k  -H  'Content-Type: application/octet-stream'   -T witherspoon.pnor.squashfs.tar
    -X POST https://bmc//upload/image
    """
    def upload_image(self, image):
        header = " \'Content-Type: application/octet-stream\' "
        obj = "/upload/image"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", header=header, upload_file=image)
        self.curl.run()

    # priority 0 -primary (Boot side of the image)
    def get_image_priority(self, id):
        output = self.image_data(id)
        print repr(output)
        return output['data']['Priority']

    """
    Set the image priority
    curl -b cjar -k -H "Content-Type: application/json" -X PUT -d '{"data":0}'
    https://$BMC_IP/xyz/openbmc_project/software/061c4bdb/attr/Priority
    """
    def set_image_priority(self, id, level):
        obj = "/xyz/openbmc_project/software/%s/attr/Priority" % id
        data =  '\'{\"data\":%s}\'' % level
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()


    def image_ready_for_activation(self, id, timeout=10):
        timeout = time.time() + 60*timeout
        while True:
            output = self.image_data(id)
            print repr(output)
            if output['data']['Activation'] == "xyz.openbmc_project.Software.Activation.Activations.Ready":
                print "Image upload is successful & Ready for activation"
                break
            if time.time() > timeout:
                raise OpTestError("Image is not ready for activation/Timeout happened")
            time.sleep(5)
        return True

    """
    Activate a image
    curl -b cjar -k -H "Content-Type: application/json" -X PUT
    -d '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}'
    https://bmc/xyz/openbmc_project/software/<image id>/attr/RequestedActivation
    """
    def activate_image(self, id):
        obj = "/xyz/openbmc_project/software/%s/attr/RequestedActivation" % id
        data =  '\'{\"data\":\"xyz.openbmc_project.Software.Activation.RequestedActivations.Active\"}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        self.curl.run()

    """
    Delete a image
    curl -b cjar -k -H "Content-Type: application/json" -X DELETE

    https://bmc/xyz/openbmc_project/software/<image id>/attr/RequestedActivation
    """
    def delete_image(self, id):
        try:
            # First, we try the "new" method, as of at least ibm-v2.0-0-r26.1-0-gfb7714a
            obj = "/xyz/openbmc_project/software/%s/action/delete" % id
            data = '\'{"data" : []}\''
            self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
            self.curl.run()
        except FailedCurlInvocation as f:
            # Try falling back to the old method (everything prior? who knows)
            obj = "/xyz/openbmc_project/software/%s" % id
            self.curl.feed_data(dbus_object=obj, operation='rw', command="DELETE")
            self.curl.run()



    def wait_for_image_active_complete(self, id, timeout=10):
        timeout = time.time() + 60*timeout
        while True:
            output = self.image_data(id)
            if output['data']['Activation'] == 'xyz.openbmc_project.Software.Activation.Activations.Activating':
                print "Image activation is in progress"
            if output['data']['Activation'] == 'xyz.openbmc_project.Software.Activation.Activations.Active':
                print "Image activated successfully, Good to go for power on...."
                break
            if output['data']['Activation'] == 'xyz.openbmc_project.Software.Activation.Activations.Failed':
                print "Image activation failed. Good luck."
                return False
            if time.time() > timeout:
                raise OpTestError("Image is failed to activate/Timeout happened")
            time.sleep(5)
        return True

    def host_image_ids(self):
        l = self.get_list_of_image_ids()
        for id in l[:]:
            i = self.image_data(id)
            # Here, we assume that if we don't have 'Purpose' it's something special
            # like the 'active' or (new) 'functional'.
            # Adriana has promised me that this is safe.
            print repr(i)
            if i['data'].get('Purpose') != 'xyz.openbmc_project.Software.Version.VersionPurpose.Host':
                l.remove(id)
        print "Host Image IDS: %s" % repr(l)
        return l

    def bmc_image_ids(self):
        l = self.get_list_of_image_ids()
        for id in l[:]:
            i = self.image_data(id)
            # Here, we assume that if we don't have 'Purpose' it's something special
            # like the 'active' or (new) 'functional'.
            # Adriana has promised me that this is safe.
            print repr(i)
            if i['data'].get('Purpose') != 'xyz.openbmc_project.Software.Version.VersionPurpose.BMC':
                l.remove(id)
        print "BMC Image IDS: %s" % repr(l)
        return l

    """
    Listing available Dumps:
    $ curl -c cjar -b cjar -k https://$BMC_IP/xyz/openbmc_project/dump/list
    """
    def list_available_dumps(self):
        obj = "/xyz/openbmc_project/dump/list"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        return self.curl.run()


    def get_dump_ids(self):
        dump_ids = []
        output = self.list_available_dumps()
        data = json.loads(output)
        print repr(data)
        for k in data['data']:
            print repr(k)
            m = re.match(r"/xyz/openbmc_project/dump/entry/(\d{1,})$", k)
            if m:
                dump_ids.append(m.group(1))
        print repr(dump_ids)
        return dump_ids

    """
    Down Load Dump:
    $ curl -O -J -c cjar -b cjar -k -X GET https://$BMC_IP/download/dump/$ID
    """
    def download_dump(self, dump_id):
        obj = "/download/dump/%s" % dump_id
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET", remote_name=True)
        self.curl.run()

    """
    Delete dump.
    $ curl -c cjar -b cjar -k -H "Content-Type: application/json" -d "{\"data\": []}"
      -X POST  https://$BMC_IP/xyz/openbmc_project/dump/entry/3/action/Delete
    """
    def delete_dump(self, dump_id):
        obj = "/xyz/openbmc_project/dump/entry/%s/action/Delete" % dump_id
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        self.curl.run()

    def delete_all_dumps(self):
        ids = self.get_dump_ids()
        for id in ids:
            self.delete_dump(id)

    """
    Create new Dump:
    $ curl -c cjar -b cjar -k -H "Content-Type: application/json" -d "{\"data\": []}"
       -X POST  https://$BMC_IP/xyz/openbmc_project/dump/action/CreateDump
    """
    def create_new_dump(self):
        obj = "/xyz/openbmc_project/dump/action/CreateDump"
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        dump_capture = False
        try:
            output = self.curl.run()
            dump_capture = True
        except FailedCurlInvocation as cf:
            output = cf.output
        data = json.loads(output)
        if dump_capture:
            print "OpenBMC Dump capture was successful"
            return data['data']
        print repr(data['data'].get('exception'))
        if data['data']['exception'] == "DBusException('Dump not captured due to a cap.',)":
            print "Dumps are exceeded in the system, trying to delete existing ones"
            self.delete_all_dumps()
            output = self.curl.run()
            data = json.loads(output)
            return data['data']

    def wait_for_dump_finish(self, dump_id):
        for i in range(20):
            ids = self.get_dump_ids()
            if str(dump_id) in ids:
                print "Dump %s is ready to download/offload" % str(dump_id)
                return True
            time.sleep(5)
        else:
            return False

    def software_enumerate(self):
        obj = "/xyz/openbmc_project/software"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        return json.loads(self.curl.run())

    # Returns True  - if field mode enabled.
    #         False - if it is disabled.
    def has_field_mode_set(self):
        data = self.software_enumerate()
        print repr(data)
        val = data['data'].get('FieldModeEnabled')
        if int(val) == 1:
            return True
        return False

    """
    Set field mode : 1 - enables it
    curl -b cjar -k -H 'Content-Type: application/json' -X PUT -d '{"data":0}'
    https://bmcip/xyz/openbmc_project/software/attr/FieldModeEnabled
    """
    def set_field_mode(self, mode):
        obj = "/xyz/openbmc_project/software/attr/FieldModeEnabled"
        data = '\'{"data" : %s}\'' % int(mode)
        self.curl.feed_data(dbus_object=obj, operation='rw', command="PUT", data=data)
        return json.loads(self.curl.run())

    """
    1. functional boot side validation for both BMC and PNOR.
    $ curl -c cjar -b cjar -k -H "Content-Type: application/json" https://$BMC_IP/xyz/openbmc_project/software/functional
    {
    "data": {
        "endpoints": [
        "/xyz/openbmc_project/software/061c4bdb",
        "/xyz/openbmc_project/software/608e9ebe"
        ]
    }
    """
    def validate_functional_bootside(self, id):
        obj = "/xyz/openbmc_project/software/functional"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        output = self.curl.run()
        print output
        if id in output:
            return True
        return False

    def is_image_already_active(self, id):
        output = self.image_data(id)
        if output['data']['Activation'] == 'xyz.openbmc_project.Software.Activation.Activations.Active':
            return True
        return False

    def get_occ_ids(self):
        obj = "/org/open_power/control/enumerate"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        output = self.curl.run()
        data = json.loads(output)
        occ_ids = []
        for k in data['data']:
            print repr(k)
            m = re.match(r"/org/open_power/control/occ(\d{1,})$", k)
            if m:
                occ_ids.append(m.group(1))
        print repr(occ_ids)
        return occ_ids

    """
    Get state of OCC's
    curl -c cjar -b cjar -k -H "Content-Type: application/json" -X  GET
    https://$BMC_IP/org/open_power/control/occ0
    """
    def is_occ_active(self, id):
        obj = "/org/open_power/control/occ%s" % str(id)
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        output = self.curl.run()
        data = json.loads(output)
        if data['data']['OccActive'] == 1:
            print "# OCC%s is active" % str(id)
            return True
        print "# OCC%s is not active" % str(id)
        return False

    """
    curl -b cjar -k -H 'Content-Type: application/json' -X PUT -d '{"data":0}'
    https://$BMC_IP/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable
    0 - Disable by default, 1 - Enable
    """
    def enable_power_cap(self, enable):
        obj = "/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable"
        data = '\'{"data" : %s}\'' % int(enable)
        self.curl.feed_data(dbus_object=obj, operation='rw', data=data, command="PUT")
        self.curl.run()

    # Enables the power cap.
    def power_cap_enable(self):
        self.enable_power_cap("1")

    # Disables the power cap.
    def power_cap_disable(self):
        self.enable_power_cap("0")

    """
    /xyz/openbmc_project/control/host0/power_cap
    """
    def get_power_cap_settings(self):
        obj = "/xyz/openbmc_project/control/host0/power_cap"
        self.curl.feed_data(dbus_object=obj, operation='rw', command="GET")
        output = self.curl.run()
        data = json.loads(output)
        PowerCapEnable = data['data']['PowerCapEnable']
        PowerCap = data['data']['PowerCap']
        return PowerCapEnable, PowerCap

    """
    curl -b cjar -k -H 'Content-Type: application/json' -X POST -d '{"data":[]}'
    https://${bmc}/org/open_power/control/gard/action/Reset
    """
    def clear_gard_records(self):
        obj = "/org/open_power/control/gard/action/Reset"
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        self.curl.run()

    """
    $ curl -c cjar -b cjar -k -H 'Content-Type: application/json' -X POST
    -d '{"data":[]}' https://${bmc}/xyz/openbmc_project/software/action/Reset
    """
    def factory_reset_software(self):
        obj = "/xyz/openbmc_project/software/action/Reset"
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        self.curl.run()

    """
    $ curl -c cjar -b cjar -k -H 'Content-Type: application/json' -X POST
    -d '{"data":[]}' https://${bmc}/xyz/openbmc_project/network/action/Reset
    """
    def factory_reset_network(self):
        obj = "/xyz/openbmc_project/network/action/Reset"
        data = '\'{"data" : []}\''
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        self.curl.run()

    """
    $ curl -c cjar -b cjar -k -H "Content-Type: application/json" -d "{\"data\": [\"abc123\"] }"
    -X POST  https://${bmc}/xyz/openbmc_project/user/root/action/SetPassword
    """
    def update_root_password(self, password):
        obj = "/xyz/openbmc_project/user/root/action/SetPassword"
        data = '\'{"data" : ["%s"]}\'' % str(password)
        self.curl.feed_data(dbus_object=obj, operation='rw', command="POST", data=data)
        self.curl.run()
示例#36
0
class OpTestIPMI():
    def __init__(self,
                 i_bmcIP,
                 i_bmcUser,
                 i_bmcPwd,
                 i_ffdcDir,
                 host=None,
                 delaybeforesend=None):
        self.cv_bmcIP = i_bmcIP
        self.cv_bmcUser = i_bmcUser
        self.cv_bmcPwd = i_bmcPwd
        self.cv_ffdcDir = i_ffdcDir
        self.ipmitool = IPMITool(method='lanplus',
                                 ip=i_bmcIP,
                                 username=i_bmcUser,
                                 password=i_bmcPwd)
        self.console = IPMIConsole(ipmitool=self.ipmitool,
                                   logdir=i_ffdcDir,
                                   delaybeforesend=delaybeforesend)
        self.util = OpTestUtil()
        self.host = host

    # Get the IPMIConsole object, to run commands on the host etc.
    def get_host_console(self):
        return self.console

    ##
    # @brief This function clears the sensor data
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_sdr_clear(self):
        output = self.ipmitool.run('sel clear')
        if 'Clearing SEL' in output:
            # FIXME: This code should instead check for 'erasure completed'
            #        and the status of the erasure, rather than this crude loop
            retries = 20
            while (retries > 0):
                output = self.ipmitool.run('sel elist')
                if 'no entries' in output:
                    return BMC_CONST.FW_SUCCESS
                else:
                    l_msg = "Sensor data still has entries!"
                    print l_msg
                    retries -= 1
                    if (retries == 0):
                        raise OpTestError(l_msg)
                    time.sleep(1)
        else:
            l_msg = "Clearing the sensor data Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function sends the chassis power off ipmitool command
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_power_off(self):
        output = self.ipmitool.run('chassis power off')
        if 'Down/Off' in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power OFF Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function sends the chassis power on ipmitool command
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_power_on(self):
        output = self.ipmitool.run('chassis power on')
        if 'Up/On' in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power ON Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function sends the chassis power soft ipmitool command
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_power_soft(self):
        output = self.ipmitool.run('chassis power soft')
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        if "Chassis Power Control: Soft" in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power Soft Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function sends the chassis power cycle ipmitool command
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_power_cycle(self):
        output = self.ipmitool.run('chassis power cycle')
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        if "Chassis Power Control: Cycle" in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power Cycle Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function sends the chassis power reset ipmitool command
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_power_reset(self):
        r = self.ipmitool.run('chassis power reset')
        if not BMC_CONST.CHASSIS_POWER_RESET in r:
            raise Exception("IPMI 'chassis power reset' failed: %s " % r)

    ##
    # @brief This function sends the chassis power diag ipmitool command
    #
    def ipmi_power_diag(self):
        r = self.ipmitool.run('chassis power diag')
        if not "Chassis Power Control: Diag" in r:
            raise Exception("IPMI 'chassis power diag' failed: %s " % r)

    ##
    # @brief This function starts the sol capture and waits for the IPL to end. The
    #        marker for IPL completion is the Host Status sensor which reflects the ACPI
    #        power state of the system.  When it reads S0/G0: working it means that the
    #        petitboot is has began loading.  The overall timeout for the IPL is defined
    #        in the test configuration options.'''
    #
    # @param timeout @type int: The number of minutes to wait for IPL to complete,
    #       i.e. How long to poll the ACPI sensor for working state before giving up.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipl_wait_for_working_state(self, timeout=10):
        sol = self.console.get_console()
        timeout = time.time() + 60 * timeout
        cmd = 'sdr elist |grep \'Host Status\''
        output = self.ipmitool.run(cmd)
        if not "Host Status" in output:
            return BMC_CONST.FW_PARAMETER
        while True:
            output = self.ipmitool.run(cmd)
            if 'S0/G0: working' in output:
                print "Host Status is S0/G0: working, IPL finished"
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)

        try:
            self.ipmitool.run('sol deactivate')
            self.console.terminate()
        except subprocess.CalledProcessError:
            l_msg = 'SOL already deactivated'
            print l_msg
            self.console.terminate()
            raise OpTestError(l_msg)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function waits for the IPL to end. The marker for IPL completion is the
    #        Host Status sensor which reflects the ACPI power state of the system.  When it
    #        reads S0/G0: working it means that the petitboot is has began loading.
    #
    # @param timeout @type int: The number of minutes to wait for IPL to complete,
    #       i.e. How long to poll the ACPI sensor for working state before giving up.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_ipl_wait_for_working_state_v1(self, timeout=10):
        timeout = time.time() + 60 * timeout
        cmd = 'sdr elist |grep \'Host Status\''
        output = self.ipmitool.run(cmd)
        if not "Host Status" in output:
            return BMC_CONST.FW_PARAMETER

        while True:
            if 'S0/G0: working' in output:
                print "Host Status is S0/G0: working, IPL finished"
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(5)
            output = self.ipmitool.run(cmd)
        return BMC_CONST.FW_SUCCESS

    def ipmi_ipl_wait_for_login(self, l_con, timeout=10):
        l_rc = l_con.expect_exact(BMC_CONST.IPMI_SOL_CONSOLE_ACTIVATE_OUTPUT,
                                  timeout=120)
        if l_rc == 0:
            print "IPMI: sol console activated"
        else:
            l_msg = "Error: not able to get IPMI console"
            raise OpTestError(l_msg)
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        l_con.send("\r")
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        l_rc = l_con.expect_exact(BMC_CONST.IPMI_CONSOLE_EXPECT_ENTER_OUTPUT,
                                  timeout=120)
        if l_rc == BMC_CONST.IPMI_CONSOLE_EXPECT_LOGIN:
            return BMC_CONST.FW_SUCCESS
        elif l_rc in BMC_CONST.IPMI_CONSOLE_EXPECT_PETITBOOT:
            return BMC_CONST.FW_SUCCESS
        elif l_rc in BMC_CONST.IPMI_CONSOLE_EXPECT_RANDOM_STATE:
            l_msg = "Error: system is in random state"
            raise OpTestError(l_msg)
        else:
            l_con.expect(pexpect.TIMEOUT, timeout=30)
            print l_con.before
            raise OpTestError("Timeout waiting for IPL")
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function waits for system to reach standby state or soft off. The
    #        marker for standby state is the Host Status sensor which reflects the ACPI
    #        power state of the system.  When it reads S5/G2: soft-off it means that the
    #        system reached standby or soft-off state. The overall timeout for the standby is defined
    #        in the test configuration options.'''
    #
    # @param i_timeout @type int: The number of seconds to wait for system to reach standby,
    #       i.e. How long to poll the ACPI sensor for soft-off state before giving up.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_wait_for_standby_state(self, i_timeout=120):
        l_timeout = time.time() + i_timeout
        l_cmd = 'sdr elist |grep \'Host Status\''
        output = self.ipmitool.run(l_cmd)
        if not "Host Status" in output:
            return BMC_CONST.FW_PARAMETER
        while True:
            l_output = self.ipmitool.run(l_cmd)
            if BMC_CONST.CHASSIS_SOFT_OFF in l_output:
                print "Host Status is S5/G2: soft-off, system reached standby"
                break
            if time.time() > l_timeout:
                l_msg = "Standby timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_STANDBY_DELAY)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function waits for the Host OS Boot(IPL) to end. The
    #        marker for IPL completion is the OS Boot sensor which reflects status
    #        of host OS Boot. When it reads boot completed it means that the
    #        Host OS Booted successfully.  The overall timeout for the IPL is defined
    #        in the test configuration options.'''
    #
    # @param i_timeout @type int: The number of minutes to wait for IPL to complete or Boot time,
    #       i.e. How long to poll the OS Boot sensor for working state before giving up.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_wait_for_os_boot_complete(self, i_timeout=10):
        l_timeout = time.time() + 60 * i_timeout
        l_cmd = 'sdr elist |grep \'OS Boot\''
        output = self.ipmitool.run(l_cmd)
        if not "OS Boot" in output:
            return BMC_CONST.FW_PARAMETER
        while True:
            l_output = self.ipmitool.run(l_cmd)
            if BMC_CONST.OS_BOOT_COMPLETE in l_output:
                print "Host OS is booted"
                break
            if time.time() > l_timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function waits for the Host OS Boot(IPL) to end. The
    #        marker for IPL completion is the OS Boot sensor which reflects status
    #        of host OS Boot. When it reads boot completed it means that the
    #        Host OS Booted successfully.
    #
    # @param i_timeout @type int: The number of minutes to wait for IPL to complete or Boot time,
    #       i.e. How long to poll the OS Boot sensor for working state before giving up.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_wait_for_os_boot_complete_v1(self, i_timeout=10):
        l_timeout = time.time() + 60 * i_timeout
        l_cmd = 'sdr elist |grep \'OS Boot\''
        l_output = self.ipmitool.run(l_cmd)
        if not "OS Boot" in l_output:
            return BMC_CONST.FW_PARAMETER

        while True:
            if BMC_CONST.OS_BOOT_COMPLETE in l_output:
                print "Host OS is booted"
                break
            if time.time() > l_timeout:
                l_msg = "IPL timeout"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)
            l_output = self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function dumps the sel log and looks for specific hostboot error
    #        log string
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_sel_check(self, i_string="Transition to Non-recoverable"):
        logFile = self.cv_ffdcDir + '/' + 'host_sel_elist.log'
        output = self.ipmitool.run('sel elist')

        with open('%s' % logFile, 'w') as f:
            for line in output:
                f.write(line)

        if i_string in output:
            l_msg = 'Error log(s) detected during IPL. Please see %s' % logFile
            print l_msg
            raise OpTestError(l_msg)
        else:
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief Determines the power status of the bmc
    #
    # @return l_output @type string: Power status of bmc
    #         "Chassis Power is on" or "Chassis Power is off"
    #
    def ipmi_power_status(self):
        l_output = self.ipmitool.run('chassis power status')
        if ('on' in l_output):
            return BMC_CONST.CHASSIS_POWER_ON
        elif ('off' in l_output):
            return BMC_CONST.CHASSIS_POWER_OFF
        else:
            raise OpTestError("Can't recognize chassis power status: " +
                              str(l_output))

    ##
    # @brief Performs a cold reset onto the bmc
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_cold_reset(self):

        l_initstatus = self.ipmi_power_status()
        print("Applying Cold reset.")
        rc = self.ipmitool.run(BMC_CONST.BMC_COLD_RESET)
        if BMC_CONST.BMC_PASS_COLD_RESET in rc:
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)
            self.util.PingFunc(self.cv_bmcIP,
                               BMC_CONST.PING_RETRY_FOR_STABILITY)
            l_finalstatus = self.ipmi_power_status()
            if (l_initstatus != l_finalstatus):
                print('initial status ' + str(l_initstatus))
                print('final status ' + str(l_finalstatus))
                print('Power status changed during cold reset')
                raise OpTestError('Power status changed')
            return BMC_CONST.FW_SUCCESS
        else:
            print "Cold reset failed"
            print rc
            raise OpTestError(rc)

    ##
    # @brief Performs a warm reset onto the bmc
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_warm_reset(self):

        l_cmd = BMC_CONST.BMC_WARM_RESET
        print("Applying Warm reset. Wait for " +
              str(BMC_CONST.BMC_WARM_RESET_DELAY) + "sec")
        rc = self.ipmitool.run(l_cmd)
        if BMC_CONST.BMC_PASS_WARM_RESET in rc:
            print rc
            time.sleep(BMC_CONST.BMC_WARM_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Warm reset Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief Preserves the network setting
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_preserve_network_setting(self):

        print("Protecting BMC network setting")
        l_cmd = BMC_CONST.BMC_PRESRV_LAN
        rc = self.ipmitool.run(l_cmd)

        if BMC_CONST.BMC_ERROR_LAN in rc:
            l_msg = "Can't protect setting! Please preserve setting manually"
            print l_msg
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_code_update(self, i_image, i_imagecomponent):

        self.ipmi_cold_reset()
        l_cmd = BMC_CONST.BMC_HPM_UPDATE + i_image + " " + i_imagecomponent
        self.ipmi_preserve_network_setting()
        try:
            # FIXME: This is currently broken with the ipmitool rework
            # We're likely to timeout waiting for the user to hit 'y'
            rc = self.ipmitool.run(l_cmd,
                                   background=False,
                                   cmdprefix="echo y |")
            print rc
            self.ipmi_cold_reset()

        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if (rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief Get information on active sides for both BMC and PNOR
    # example 0x0080 indicates primary side is activated
    #         0x0180 indicates golden side is activated
    #
    # @return returns the active sides for BMC and PNOR chip (that are either primary of golden)
    #         l_bmc_side, l_pnor_side or raise OpTestError
    #
    def ipmi_get_side_activated(self):

        l_result = self.ipmitool.run(
            BMC_CONST.BMC_ACTIVE_SIDE).strip().split('\n')

        for i in range(len(l_result)):
            if ('BIOS' in l_result[i]):
                if (l_result[i].__contains__(BMC_CONST.PRIMARY_SIDE)):
                    print("Primary side of PNOR is active")
                    l_pnor_side = BMC_CONST.PRIMARY_SIDE
                elif (BMC_CONST.GOLDEN_SIDE in l_result[i]):
                    print("Golden side of PNOR is active")
                    l_pnor_side = BMC_CONST.GOLDEN_SIDE
                else:
                    l_msg = "Error determining active side: " + l_result
                    print l_msg
                    raise OpTestError(l_msg)
            elif ('BMC' in l_result[i]):
                if (l_result[i].__contains__(BMC_CONST.PRIMARY_SIDE)):
                    print("Primary side of BMC is active")
                    l_bmc_side = BMC_CONST.PRIMARY_SIDE
                elif (BMC_CONST.GOLDEN_SIDE in l_result[i]):
                    print("Golden side of BMC is active")
                    l_bmc_side = BMC_CONST.GOLDEN_SIDE
                else:
                    l_msg = "Error determining active side: " + l_result
                    print l_msg
                    raise OpTestError(l_msg)
            else:
                l_msg = "Error determining active side: " + +l_result
                print l_msg
                raise OpTestError(l_msg)

        return l_bmc_side, l_pnor_side

    ##
    # @brief Get PNOR level
    #
    # @return pnor level of the bmc
    #         or raise OpTestError
    #
    def ipmi_get_PNOR_level(self):
        l_rc = self.ipmitool.run(BMC_CONST.BMC_MCHBLD)
        return l_rc

    ##
    # @brief This function gets the BMC golden side firmware version.
    #        Below are the fields to decode the version of firmware
    #        1      Completion code
    #                   0x00 – success
    #                   0x81 – Not a valid image in golden SPI.
    #                   0xff – Reading Golden side SPI failed.
    #       2       Device ID (0x02)
    #       3       IPMI Dev version (0x01)
    #       4       Firmware Revision 1 (Major )
    #       5       Firmware Revision 2 (Minor)
    #       6       IPMI Version
    #       7       Additional Device support refer IPMI spec.
    #       8-10    Manufacture ID
    #       11-12   Product ID
    #       13-16   Auxiliary Firmware Revision
    #
    # @return l_rc @type str: BMC golden side firmware version
    #         or raise OpTestError
    #           ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x1a
    #           20 01 02 16 02 bf 00 00 00 bb aa 4d 4c 01 00
    #
    def ipmi_get_bmc_golden_side_version(self):
        print "IPMI: Getting the BMC Golden side version"
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_BMC_GOLDEN_SIDE_VERSION)
        return l_rc

    ##
    # @brief This function gets the size of pnor partition
    #
    # @param i_part @type str: partition name to retrieve the size.
    #                          partition can be NVRAM, GUARD and BOOTKERNEL
    #                          TODO: Add support for remaining partitions.
    #
    # @return l_rc @type str: size of partition raise OpTestError when fails
    #         Ex:ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x0c
    #            0x42 0x4f 0x4f 0x54 0x4b 0x45 0x52 0x4e 0x45 0x4c 0x00 0x00 0x00 0x00 0x0 0x0 0x0
    #           00 00 f0 00
    #           This function will return "00 00 f0 00"
    #
    def ipmi_get_pnor_partition_size(self, i_part):
        print "IPMI: Getting the size of %s PNOR Partition" % i_part
        if i_part == BMC_CONST.PNOR_NVRAM_PART:
            l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_NVRAM_PARTITION_SIZE)
        elif i_part == BMC_CONST.PNOR_GUARD_PART:
            l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_GUARD_PARTITION_SIZE)
        elif i_part == BMC_CONST.PNOR_BOOTKERNEL_PART:
            l_rc = self.ipmitool.run(
                BMC_CONST.IPMI_GET_BOOTKERNEL_PARTITION_SIZE)
        else:
            l_msg = "please provide valid partition eye catcher name"
            print l_rc
            raise OpTestError(l_msg)
        return l_rc

    ##
    # @brief This function is used to check whether BMC Completed Booting.
    #        BMC Booting Status:
    #           00h = Booting Completed.
    #           C0h = Still Booting.
    #
    # @return l_res @type str: output of command or raise OpTestError
    #          ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x0a
    #          00
    #        It returns 00 if BMC completed booting else it gives C0
    #
    def ipmi_get_bmc_boot_completion_status(self):
        print "IPMI: Getting the BMC Boot completion status"
        l_res = self.ipmitool.run(BMC_CONST.IPMI_HAS_BMC_BOOT_COMPLETED)
        return l_res

    ##
    # @brief This command is used to get the State of Fault RollUP LED.
    #        Fault RollUP LED      0x00
    #
    # @return l_res @type str: output of command or raise OpTestError
    #          ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x00
    #          00
    #        LED State Table: Below are the states it can get
    #               0x0  LED OFF
    #               0x1  LED ON
    #               0x2  LED Standby Blink Rate
    #               0x3  LED Slow Blink rate.
    #
    def ipmi_get_fault_led_state(self):
        print "IPMI: Getting the fault rollup LED state"
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_FAULT_ROLLUP)
        return l_res

    ##
    # @brief This command is used to get the State of Power ON LED.
    #        Power ON LED      0x01
    #
    # @return l_res @type str: output of command or raise OpTestError
    #           ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x01
    #           01
    #
    #        LED State Table: Below are the states it can get
    #               0x0  LED OFF
    #               0x1  LED ON
    #               0x2  LED Standby Blink Rate
    #               0x3  LED Slow Blink rate.
    #
    def ipmi_get_power_on_led_state(self):
        print "IPMI: Getting the Power ON LED state"
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_POWER_ON)
        return l_res

    ##
    # @brief This command is used to get the State of Host Status LED.
    #        Host Status LED       0x02
    #
    # @return l_res @type str: output of command or raise OpTestError
    #       ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x02
    #       00
    #
    #        LED State Table: Below are the states it can get
    #               0x0  LED OFF
    #               0x1  LED ON
    #               0x2  LED Standby Blink Rate
    #               0x3  LED Slow Blink rate.
    #
    def ipmi_get_host_status_led_state(self):
        print "IPMI: Getting the Host status LED state"
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_HOST_STATUS)
        return l_res

    ##
    # @brief This command is used to get the State of Chassis Identify LED.
    #        Chassis Identify LED  0x03
    #
    # @return l_res @type str: output of command or raise OpTestError
    #       ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x03
    #       00
    #
    #        LED State Table: Below are the states it can get
    #               0x0  LED OFF
    #               0x1  LED ON
    #               0x2  LED Standby Blink Rate
    #               0x3  LED Slow Blink rate.
    #
    def ipmi_get_chassis_identify_led_state(self):
        print "IPMI: Getting the Chassis Identify LED state"
        l_res = self.ipmitool.run(
            BMC_CONST.IPMI_GET_LED_STATE_CHASSIS_IDENTIFY)
        return l_res

    ##
    # @brief This function is used to set the state of a given LED.
    #
    # @param i_led @type str: led number to set the state.
    #        EX: LED Number Table to use.
    #            Fault RollUP LED      0x00
    #            Power ON LED          0x01
    #            Host Status LED       0x02
    #            Chassis Identify LED  0x03
    #
    # @param i_state @type str: state of led to set.
    #           LED State to be set.
    #               0x0  LED OFF
    #               0x1  LED ON
    #               0x2  LED Standby Blink Rate
    #               0x3  LED Slow Blink rate.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_set_led_state(self, i_led, i_state):
        l_led = i_led
        l_state = i_state
        print "IPMI: Setting the %s LED with %s state" % (l_led, l_state)
        l_cmd = "raw 0x3a 0x03 %s %s" % (l_led, l_state)
        l_res = self.ipmitool.run(l_cmd)
        if "00" in l_res:
            print "Set LED state got success"
            return BMC_CONST.FW_SUCCESS
        elif "0x90" in l_res:
            print "Invalid LED number"
        else:
            l_msg = "IPMI: Set LED state failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function is used to enable Fan control Task thread running on BMC
    #        Ex: ipmitool raw 0x3a 0x12 0x01
    #
    # @return l_rc @type str: return code of command or raise OpTestError when fails
    #           Completion Code: 00h = success
    #                            cch = Invalid Request Data
    #                            83 h = File not created in start case.
    #                            80 h – Invalid Operation Mode
    #
    def ipmi_enable_fan_control_task_command(self):
        print "IPMI: Enabling the Fan control task thread state"
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_ENABLE_FAN_CONTROL_TASK_THREAD)
        return l_rc

    ##
    # @brief This function is used to disable Fan control Task thread running on BMC
    #        Ex: ipmitool raw 0x3a 0x12 0x00
    #
    # @return l_rc @type str: return code of command or raise OpTestError when fails
    #           Completion Code: 00h = success
    #                            cch = Invalid Request Data
    #                            83 h = File not created in start case.
    #                            80 h – Invalid Operation Mode
    #
    def ipmi_disable_fan_control_task_command(self):
        print "IPMI: Disabling the Fan control task thread state"
        l_rc = self.ipmitool.run(
            BMC_CONST.IPMI_DISABLE_FAN_CONTROL_TASK_THREAD)
        return l_rc

    ##
    # @brief This function is used to get the state of fan control task thread.
    #        Ex: ipmitool -I lanplus -U admin -P admin -H <BMC IP> raw 0x3a 0x13
    #            00
    #            ipmitool -I lanplus -U admin -P admin -H <BMC IP> raw 0x3a 0x13
    #            01
    #
    # @return l_state @type str: raise OpTestError when fails
    #           IPMI_FAN_CONTROL_THREAD_RUNNING = "01"
    #           IPMI_FAN_CONTROL_THREAD_NOT_RUNNING = "00"
    #           If fan control loop is running it will return "01"
    #           else it will return "00"
    #
    def ipmi_get_fan_control_task_state_command(self):
        print "IPMI: Getting the state of fan control task thread"
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_FAN_CONTROL_TASK_THREAD_STATE)
        if BMC_CONST.IPMI_FAN_CONTROL_THREAD_NOT_RUNNING in l_rc:
            print "IPMI: Fan control task thread state is not running"
            l_state = BMC_CONST.IPMI_FAN_CONTROL_THREAD_NOT_RUNNING
        elif BMC_CONST.IPMI_FAN_CONTROL_THREAD_RUNNING in l_rc:
            print "IPMI: Fan control task thread state is running"
            l_state = BMC_CONST.IPMI_FAN_CONTROL_THREAD_RUNNING
        else:
            l_msg = "IPMI: Invalid response from fan control thread state command"
            raise OpTestError(l_msg)
        return l_state

    ##
    # @brief set power limit of bmc
    #
    # @param i_powerlimit @type int: power limit to be set at BMC
    #
    # @return raise OpTestError when fails
    #
    def ipmi_set_power_limit(self, i_powerlimit):

        l_rc = self.ipmitool.run(BMC_CONST.SET_POWER_LIMIT + i_powerlimit)
        if (i_powerlimit not in l_rc):
            raise OpTestError(l_rc)

    ##
    # @brief Determines the power limit on the bmc
    #
    # @return l_powerlimit @type int: current power limit on bmc
    #         or raise OpTestError
    #
    def ipmi_get_power_limit(self):

        l_powerlimit = self.ipmitool.run(BMC_CONST.GET_POWER_LIMIT)
        return l_powerlimit

    ##
    # @brief Activates the power limit of the target bmc
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_activate_power_limit(self):

        l_rc = self.ipmitool.run(BMC_CONST.DCMI_POWER_ACTIVATE)
        if (BMC_CONST.POWER_ACTIVATE_SUCCESS in l_rc):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power limit activation failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief Deactivates the power limit of the target bmc
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_deactivate_power_limit(self):

        l_rc = self.ipmitool.run(BMC_CONST.DCMI_POWER_DEACTIVATE)
        if (BMC_CONST.POWER_DEACTIVATE_SUCCESS in l_rc):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power limit deactivation failed. " \
                    "Make sure a power limit is set before activating it"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief Enable OCC Sensor
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_enable_all_occ_sensor(self):

        l_status = self.ipmi_get_occ_status()
        # example ssample: OCC Active | 08h | ok  | 210.0 |)

        # Get sensor ids to enable all OCCs
        l_status = l_status.rsplit("\n", 1)[0].split("\n")
        for i in range(len(l_status)):
            l_sensor_id = l_status[i].split("|")[1].strip()[:2]
            self.ipmitool.run(BMC_CONST.BMC_OCC_SENSOR + l_sensor_id +
                              BMC_CONST.BMC_ENABLE_OCC)

        # Wait for OCC to stabilize
        time.sleep(BMC_CONST.OCC_ENABLE_WAIT)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Disable OCC Sensor
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_disable_all_occ_sensor(self):

        l_status = self.ipmi_get_occ_status()

        # Get sensor ids to disable all OCCs
        l_status = l_status.rsplit("\n", 1)[0].split("\n")
        for i in range(len(l_status)):
            l_sensor_id = l_status[i].split("|")[1].strip()[:2]
            self.ipmitool.run(BMC_CONST.BMC_OCC_SENSOR + l_sensor_id +
                              BMC_CONST.BMC_DISABLE_OCC)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Get OCC status
    #  (example:
    #   OCC 1 Active     | 08h | ok  | 210.0 | Device Enabled
    #   OCC 2 Active     | 09h | ok  | 210.1 | Device Enabled)
    #
    # @return OCC sensor status or raise OpTestError
    #
    def ipmi_get_occ_status(self):
        l_result = self.ipmitool.run(BMC_CONST.OP_CHECK_OCC)
        if ("Device" not in l_result):
            l_msg = "Can't recognize output"
            print(l_msg + ": " + l_result)
            raise OpTestError(l_msg)

        return l_result

    ##
    # @brief This function gets the sel log
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_get_sel_list(self):
        return self.ipmitool.run(BMC_CONST.BMC_SEL_LIST)

    ##
    # @brief This function gets the sdr list
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipmi_get_sdr_list(self):
        return self.ipmitool.run(BMC_CONST.BMC_SDR_ELIST)

    ##
    # @brief Sets BIOS sensor and BOOT count to boot pnor from the primary side
    #
    # @param i_bios_sensor @type string: Id for BIOS Golden Sensor (example habanero=0x5c)
    # @param i_boot_sensor @type string: Id for BOOT Count Sensor (example habanero=80)
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError if failed
    #
    def ipmi_set_pnor_primary_side(self, i_bios_sensor, i_boot_sensor):

        print '\nSetting PNOR to boot into Primary Side'

        #Set the Boot Count sensor to 2
        l_cmd = BMC_CONST.BMC_BOOT_COUNT_2.replace('xx', i_boot_sensor)
        self.ipmitool.run(l_cmd)

        #Set the BIOS Golden Side sensor to 0
        l_cmd = BMC_CONST.BMC_BIOS_GOLDEN_SENSOR_TO_PRIMARY.replace(
            'xx', i_bios_sensor)
        self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Sets BIOS sensor and BOOT count to boot pnor from the golden side
    #
    # @param i_bios_sensor @type string: Id for BIOS Golden Sensor (example habanero=0x5c)
    # @param i_boot_sensor @type string: Id for BOOT Count Sensor (example habanero=80)
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError if failed
    #
    def ipmi_set_pnor_golden_side(self, i_bios_sensor, i_boot_sensor):

        print '\nSetting PNOR to boot into Golden Side'

        #Set the Boot Count sensor to 2
        l_cmd = BMC_CONST.BMC_BOOT_COUNT_2.replace('xx', i_boot_sensor)
        self.ipmitool.run(l_cmd)

        #Set the BIOS Golden Side sensor to 1
        l_cmd = BMC_CONST.BMC_BIOS_GOLDEN_SENSOR_TO_GOLDEN.replace(
            'xx', i_bios_sensor)
        self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Sets auto-reboot policy with given policy(i_policy)
    #
    # @param i_policy @type string: type of policy to be set(chassis policy <i_policy>)
    #                               always-off
    #                               always-on
    #                               previous
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError if failed
    #
    def ipmi_set_power_policy(self, i_policy):
        print "IPMI: Setting the power policy to %s" % i_policy
        l_cmd = "chassis policy %s" % i_policy
        l_res = self.ipmitool.run(l_cmd)
        print l_res

    ##
    # @brief Set boot device to be boot to BIOS (i.e. petitboot)
    # @return 0 for success or throws exception
    #
    def ipmi_set_boot_to_petitboot(self):
        l_output = self.ipmitool.run('chassis bootdev bios')
        if ('Set Boot Device to bios' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to bios: " +
                              str(l_output))

    ##
    # @brief Set boot device to be boot to disk (i.e. OS)
    # @return 0 for success or throws exception
    #
    def ipmi_set_boot_to_disk(self):
        l_output = self.ipmitool.run('chassis bootdev disk')
        if ('Set Boot Device to disk' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to disk: " +
                              str(l_output))

    ##
    # @brief Set no boot override so that local config will be effective
    # @return 0 for success or throws exception
    #
    def ipmi_set_no_override(self):
        l_output = self.ipmitool.run('chassis bootdev none')
        if ('Set Boot Device to none' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to none: " +
                              str(l_output))

    def enter_ipmi_lockdown_mode(self):
        self.ipmitool.run('raw 0x32 0xf3 0x4c 0x4f 0x43 0x4b 0x00')

    def exit_ipmi_lockdown_mode(self):
        self.ipmitool.run('raw 0x32 0xF4 0x55 0x4e 0x4c 0x4f 0x43 0x4b 0x00')

    def last_sel(self):
        return self.ipmitool.run("sel list last 1")

    def sdr_get_watchdog(self):
        return self.ipmitool.run("sdr get \'Watchdog\'")

    def mc_get_watchdog(self):
        return self.ipmitool.run("mc watchdog get")
示例#37
0
class OpTestBMC():
    def __init__(self, ip=None, username=None, password=None,
                 logfile=sys.stdout, ipmi=None, rest=None,
                 web=None, check_ssh_keys=False, known_hosts_file=None):
        self.cv_bmcIP = ip
        self.cv_bmcUser = username
        self.cv_bmcPasswd = password
        self.cv_IPMI = ipmi
        self.rest = rest
        self.cv_WEB = web
        self.logfile = logfile
        self.check_ssh_keys = check_ssh_keys
        self.known_hosts_file = known_hosts_file
        self.ssh = OpTestSSH(ip, username, password, logfile, prompt=None,
                block_setup_term=0, check_ssh_keys=check_ssh_keys, known_hosts_file=known_hosts_file)
        self.util = OpTestUtil()

    def set_system(self, system):
        self.ssh.set_system(system)

    def bmc_host(self):
        return self.cv_bmcIP

    def get_ipmi(self):
        return self.cv_IPMI

    def get_rest_api(self):
        return self.rest

    def get_host_console(self):
        return self.cv_IPMI.get_host_console()

    def run_command(self, command, timeout=60, retry=0):
        return self.ssh.run_command(command, timeout, retry)

    ##
    # @brief This function issues the reboot command on the BMC console.  It then
    #    pings the BMC until it responds, which presumably means that it is done
    #    rebooting.  It returns the number of failed pings.  The caller should make
    #    returned value is greater than 1
    #
    # @return BMC_CONST.FW_SUCCESS on success and
    #         raise OpTestError on failure
    #
    def reboot(self):

        retries = 0
        try:
            self.ssh.run_command('reboot')
        except SSHSessionDisconnected as e:
            pass
        except CommandFailed as e:
            pass
        self.ssh.close()
        log.info('Sent reboot command now waiting for reboot to complete...')
        # Wait for BMC to go down.
        self.util.ping_fail_check(self.cv_bmcIP)
        # Wait for BMC to ping back.
        self.util.PingFunc(self.cv_bmcIP, BMC_CONST.PING_RETRY_FOR_STABILITY)
        '''  Ping the system until it reboots  '''
        while True:
            try:
                subprocess.check_call(["ping", self.cv_bmcIP, "-c1"])
                break
            except subprocess.CalledProcessError as e:
                log.debug("Ping return code: ", e.returncode, "retrying...")
                retries += 1
                time.sleep(10)

            if retries > 10:
                l_msg = "Error. BMC is not responding to pings"
                log.error(l_msg)
                raise OpTestError(l_msg)

            log.info('BMC reboot complete.')

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function copies the given image to the BMC /tmp dir
    #
    # @return the rsync command return code
    #
    def image_transfer(self,i_imageName, copy_as=None):

        img_path = i_imageName
        ssh_opts = ' -o PubkeyAuthentication=no '
        if not self.check_ssh_keys:
            ssh_opts = ssh_opts + ' -o StrictHostKeyChecking=no'
        elif self.known_hosts_file:
            ssh_opts = ssh_opts + ' -o UserKnownHostsFile=' + self.known_hosts_file

        rsync_cmd = 'rsync -P -v -e "ssh -k' + ssh_opts + '" %s %s@%s:/tmp' % (img_path, self.cv_bmcUser, self.cv_bmcIP)
        if copy_as:
            rsync_cmd = rsync_cmd + '/' + copy_as

        log.debug(rsync_cmd)
        rsync = pexpect.spawn(rsync_cmd)
        rsync.logfile = OpTestLogger.FileLikeLogger(log)
        rsync.expect('assword: ')
        rsync.sendline(self.cv_bmcPasswd)
        r = rsync.expect(['total size is', 'error while loading shared lib'], timeout=1800)
        if r == 1:
            # On AMI BMCs that are missing libacl.so.1 for rsync,
            # we have to fall back to "scp"...
            # which is actually SSH+dd because there's no scp
            # This is notable for Palmetto
            log.debug("Falling back to SCP")
            if copy_as is None:
                copy_as = os.path.basename(img_path)
            scp_cmd = "bash -c \"sshpass -p {} ssh".format(self.cv_bmcPasswd) + ssh_opts + ' -o LogLevel=quiet'
            scp_cmd = scp_cmd + " {}@{} dd of=/tmp/{} < {}\"".format(self.cv_bmcUser, self.cv_bmcIP,copy_as,img_path)
            log.debug(scp_cmd)
            scp = pexpect.spawn(scp_cmd, timeout=120)
            scp.expect(pexpect.EOF)
            scp.wait()
            scp.close()
            chmod_cmd = "sshpass -p {} ssh {} {}@{} chmod +x /tmp/{}".format(self.cv_bmcPasswd, ssh_opts, self.cv_bmcUser, self.cv_bmcIP, copy_as)
            log.debug(chmod_cmd)
            chmod = pexpect.spawn(chmod_cmd)
            chmod.expect(pexpect.EOF)
            chmod.wait()
            chmod.close()
            return scp.exitstatus
        else:
            rsync.expect(pexpect.EOF)
            rsync.close()
            return rsync.exitstatus


    ##
    # @brief This function flashes the PNOR image using pflash tool,
    #        And this function will work based on the assumption that pflash
    #        tool available in i_pflash_dir.(user need to mount pflash tool
    #        as pflash tool removed from BMC)
    #
    # @param i_pflash_dir @type string: directory where pflash tool is present.
    # @param i_imageName @type string: Name of the image file
    #                         Ex: firestone.pnor or firestone_update.pnor
    #        Ex:/tmp/pflash -e -f -p /tmp/firestone_update.pnor
    #           /tmp/pflash -e -f -p /tmp/firestone.pnor
    #
    #       Note: -E removed, it will erase entire pnor chip irrespective of
    #             type of image( *.pnor or *_update.pnor).
    #             -e will erase flash area only based on the type of image.
    #
    # @return pflash command return code
    #
    def pnor_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -e -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    # on openbmc systems pflash tool available
    def pnor_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -E -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_ami(self, i_pflash_dir, i_imageName, i_partName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P %s' % (i_imageName, i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_openbmc(self, i_imageName, i_partName):
        cmd = 'pflash -p /tmp/%s -e -f -P %s' % (i_imageName, i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    ##
    # @brief This function validates presence of pflash tool, which will be
    #        used for pnor image flash
    #
    # @param i_dir @type string: directory where pflash tool should be present.
    #
    # @return BMC_CONST.FW_SUCCESS if pflash tool is available or raise OpTestError
    #
    def validate_pflash_tool(self, i_dir=""):
        i_dir = os.path.join(i_dir, "pflash")
        try:
            l_res = self.ssh.run_command("which %s" % i_dir)
        except CommandFailed:
            l_msg = "# pflash tool is not available on BMC"
            log.error(l_msg)
            return False
        return True

    def has_inband_bootdev(self):
        return True

    def has_os_boot_sensor(self):
        return True

    def has_host_status_sensor(self):
        return True

    def has_occ_active_sensor(self):
        return True

    def supports_ipmi_dcmi(self):
        return True

    def has_ipmi_sel(self):
        return True
示例#38
0
class OpTestHost():

    ##
    # @brief Initialize this object
    #
    # @param i_hostip @type string: IP Address of the host
    # @param i_hostuser @type string: Userid to log into the host
    # @param i_hostpasswd @type string: Password of the userid to log into the host
    # @param i_bmcip @type string: IP Address of the bmc
    #
    def __init__(self,
                 i_hostip,
                 i_hostuser,
                 i_hostpasswd,
                 i_bmcip,
                 i_results_dir,
                 scratch_disk="",
                 proxy="",
                 logfile=sys.stdout):
        self.ip = i_hostip
        self.user = i_hostuser
        self.passwd = i_hostpasswd
        self.util = OpTestUtil()
        self.bmcip = i_bmcip
        parent_dir = os.path.dirname(os.path.abspath(__file__))
        self.results_dir = i_results_dir
        self.logfile = logfile
        self.ssh = SSHConnection(i_hostip,
                                 i_hostuser,
                                 i_hostpasswd,
                                 logfile=self.logfile)
        self.scratch_disk = scratch_disk
        self.proxy = proxy
        self.scratch_disk_size = None

    def hostname(self):
        return self.ip

    def username(self):
        return self.user

    def password(self):
        return self.passwd

    def get_scratch_disk(self):
        return self.scratch_disk

    def get_scratch_disk_size(self, console=None):
        if self.scratch_disk_size is not None:
            return self.scratch_disk_size
        if console is None:
            raise Exception(
                "You need to call get_scratch_disk_size() with a console first"
            )
        console.run_command("stty cols 300")
        dev_sdX = console.run_command("readlink -f %s" %
                                      self.get_scratch_disk())
        dev_sdX = dev_sdX[0].replace("/dev/", "")
        scratch_disk_size = console.run_command("cat /sys/block/%s/size" %
                                                dev_sdX)
        # Use the (undocumented) /size sysfs property of nr 512byte sectors
        self.scratch_disk_size = int(scratch_disk_size[0]) * 512

    def get_proxy(self):
        return self.proxy

    def get_ssh_connection(self):
        return self.ssh

    ##
    # @brief Get and Record Ubunto OS level
    #
    # @return l_oslevel @type string: OS level of the host provided
    #         or raise OpTestError
    #
    def host_get_OS_Level(self):
        l_oslevel = self.ssh.run_command("cat /etc/os-release", timeout=60)
        return '\n'.join(l_oslevel)

    ##
    # @brief Executes a command on the os of the bmc to protect network setting
    #
    # @return OpTestError if failed
    #
    def host_protect_network_setting(self):
        try:
            l_rc = self.ssh.run_command(BMC_CONST.OS_PRESERVE_NETWORK,
                                        timeout=60)
        except:
            l_errmsg = "Can't preserve network setting"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief Performs a cold reset onto the host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_cold_reset(self):

        print("Applying Cold reset on host.")
        l_rc = self.ssh.run_command(BMC_CONST.HOST_COLD_RESET, timeout=60)

        # TODO: enable once defect SW331585 is fixed
        '''if BMC_CONST.BMC_PASS_COLD_RESET in l_rc:
            print l_rc
            time.sleep(BMC_CONST.BMC_COLD_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Cold reset Failed"
            print l_msg
            raise OpTestError(l_msg)'''

        self.util.PingFunc(self.bmcip, BMC_CONST.PING_RETRY_FOR_STABILITY)

    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_code_update(self, i_image, imagecomponent):

        # Copy the hpm file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user, self.ip, "/tmp/",
                                      self.passwd)
        except:
            l_msg = "Copying hpm file to host failed"
            print l_msg
            raise OpTestError(l_msg)

        #self.host_protect_network_setting() #writing to host is not stable
        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + " " + imagecomponent
        print l_cmd
        try:
            l_rc = self.ssh.run_command(l_cmd, timeout=1500)
            print l_rc
            self.ssh.run_command("rm -rf /tmp/" + i_image.rsplit("/", 1)[1],
                                 timeout=120)
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if (l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

    def host_run_command(self, i_cmd, timeout=1500):
        return self.ssh.run_command(i_cmd, timeout)

    ##
    # @brief It will gather OPAL Message logs and store the copy in a logfile
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_opal_msg_log(self):
        try:
            l_data = '\n'.join(self.host_run_command(BMC_CONST.OPAL_MSG_LOG))
        except OpTestError:
            l_msg = "Failed to gather OPAL message logs"
            raise OpTestError(l_msg)

        if not self.results_dir:
            print l_data
            return

        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Opal_msglog_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        print fn
        with open(fn, 'w') as f:
            f.write(l_data)

    ##
    # @brief Check if one or more binaries are present on host
    #
    # @param i_cmd @type string: binaries to check for
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_check_command(self, *i_cmd):
        l_cmd = 'which ' + ' '.join(i_cmd)
        print l_cmd
        try:
            l_res = self.host_run_command(l_cmd)
        except CommandFailed as c:
            l_msg = "host_check_command: (%s) not present on host. output of '%s': %s" % (
                ','.join(i_cmd), l_cmd, '\n'.join(c.output))
            print l_msg
            raise OpTestError(l_msg)

        return True

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self.ssh.run_command("uname -a | awk {'print $3'}",
                                        timeout=60)
        l_kernel = ''.join(l_kernel)
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        try:
            l_res = self.ssh.run_command("test -e %s" % l_file, timeout=60)
        except CommandFailed as c:
            raise NoKernelConfig(i_kernel, l_file)

        print "Config file is available"
        l_cmd = "cat %s" % (l_file)
        l_res = self.ssh.run_command(l_cmd, timeout=60)
        config_opts = {}
        for o in l_res:
            m = re.match('# (.*) is not set', o)
            if m:
                config_opts[m.group(0)] = 'n'
            else:
                if '=' in o:
                    opt, val = o.split("=")
                    config_opts[opt] = val

        if config_opts.get(i_config) not in ["y", "m"]:
            raise KernelConfigNotSet(i_config)

        return config_opts[i_config]

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            return ''.join(
                self.ssh.run_command("dpkg -S `which %s`" % i_cmd, timeout=60))
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            return ''.join(self.ssh.run_command(l_cmd, timeout=60))

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            o = self.ssh.run_command("modprobe ibmpowernv", timeout=60)
            cmd = "lsmod | grep -i ibmpowernv"
            response = self.ssh.run_command(cmd, timeout=60)
            if "ibmpowernv" not in ''.join(response):
                l_msg = "ibmpowernv module is not loaded, exiting"
                raise OpTestError(l_msg)
            else:
                print "ibmpowernv module is loaded"
            print cmd
            print response
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone --depth=1 %s %s" % (l_msg, i_dir)
        self.ssh.run_command("rm -rf %s" % i_dir, timeout=300)
        self.ssh.run_command("mkdir %s" % i_dir, timeout=60)
        try:
            print l_cmd
            res = self.ssh.run_command(l_cmd, timeout=1500)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief reads getscom data
    # example:
    # Chip ID  | Rev   | Chip type
    # ---------|-------|--------
    # 80000005 | DD2.0 | Centaur memory buffer
    # 80000004 | DD2.0 | Centaur memory buffer
    # 00000000 | DD2.0 | P8 (Venice) processor
    #
    # @param i_xscom_dir @type string: directory where getscom is installed
    #
    # @return @type string: getscom data
    #        else raise OpTestError
    def host_read_getscom_data(self, i_xscom_dir):
        try:
            l_rc = self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_xscom_dir +
                                        BMC_CONST.OS_GETSCOM_LIST,
                                        timeout=60)
        except OpTestError as e:
            l_errmsg = "Can't get getscom data"
            print l_errmsg
            raise OpTestError(l_errmsg)

        if ("command not found" in l_rc):
            l_errmsg = "Failed to locate getscom. Make sure it is installed in dir: " + i_xscom_dir
            print l_errmsg
            raise OpTestError(l_errmsg)

        return l_rc

    ##
    # @brief injects error using getscom
    #
    # @param i_xscom_dir @type string: directory where putscom is installed
    # param i_error @type string: error to be injected including the location
    #
    # @return output generated after executing putscom command or else raise OpTestError
    #
    def host_putscom(self, i_xscom_dir, i_error):

        print('Injecting Error.')
        l_rc = self._execute_no_return(BMC_CONST.SUDO_COMMAND + i_xscom_dir +
                                       BMC_CONST.OS_PUTSCOM_ERROR + i_error)

    ##
    # @brief Clears the gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def host_clear_gard_records(self, i_gard_dir):

        l_rc = self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_gard_dir +
                                    BMC_CONST.CLEAR_GARD_CMD,
                                    timeout=60)

        if (BMC_CONST.GARD_CLEAR_SUCCESSFUL not in l_rc):
            l_msg = l_rc + '. Failed to clear gard'
            print l_msg
            raise OpTestError(l_msg)

        # Check to make sure no gard records were left
        l_result = self.host_list_gard_records(i_gard_dir)
        if (BMC_CONST.NO_GARD_RECORDS not in l_result):
            l_msg = l_rc + '. Gard records still found after clearing them'
            print l_msg
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Lists all gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return gard records or else raise OpTestError
    #
    def host_list_gard_records(self, i_gard_dir):
        try:
            return self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_gard_dir +
                                        BMC_CONST.LIST_GARD_CMD,
                                        timeout=60)
        except:
            l_errmsg = "Can't clear gard records"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief  Execute a command on the targeted host but don't expect
    #             any return data.
    #
    # @param     i_cmd: @type str: command to be executed
    # @param     i_timeout: @type int:
    #
    # @return   BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def _execute_no_return(self, i_cmd, i_timeout=60):

        print('Executing command: ' + i_cmd)
        try:
            p = pxssh.pxssh()
            p.login(self.ip, self.user, self.passwd)
            p.sendline()
            p.prompt()
            p.sendline(i_cmd)
            p.prompt(i_timeout)
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Failed to execute command: " + i_cmd
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief enable/disable cpu states
    #
    # @param i_cpu_state @type string: BMC_CONST.CPU_ENABLE_STATE/
    #                                  BMC_CONST.CPU_DISABLE_STATE
    #
    # @return BMC_CONST.FW_SUCCESS or OpTestError
    #
    def host_disable_enable_cpu_states(self, i_cpu_state):
        try:
            self.ssh.run_command(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE1 + "; do echo " + \
                              i_cpu_state  + " > $i; done'", timeout=60)
            self.ssh.run_command(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE2 + "; do echo " + \
                              i_cpu_state  + " > $i; done'", timeout=60)
            return BMC_CONST.FW_SUCCESS
        except:
            l_errmsg = "Could not enable/disable cpu idle states"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief It will load the module using modprobe and verify whether it is loaded or not
    #
    # @param i_module @type string: module name, which we want to load on host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module(self, i_module):
        try:
            l_res = self.host_run_command("modprobe %s" % i_module)
        except CommandFailed as c:
            l_msg = "Error in loading the module %s, modprobe failed: %s" % (
                i_module, str(c))
            raise OpTestError(l_msg)
        l_res = self.host_run_command("lsmod | grep -i --color=never %s" %
                                      i_module)
        if re.search(i_module, ''.join(l_res)):
            print "%s module is loaded" % i_module
            return BMC_CONST.FW_SUCCESS
        else:
            raise KernelModuleNotLoaded(i_module)

    ##
    # @brief This function will read real time clock(RTC) time using hwclock utility
    #
    # @return l_res @type string: return hwclock value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_hwclock(self):
        print "Reading the hwclock"
        self.host_run_command("hwclock -r;echo $?")

    ##
    # @brief This function will read system time using date utility (This will be mantained by kernel)
    #
    # @return l_res @type string: return system time value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_systime(self):
        print "Reading system time using date utility"
        l_res = self.host_run_command("date")
        return l_res

    ##
    # @brief This function will set hwclock time using the --date option
    #        format should be "2015-01-01 12:12:12"
    #
    # @param i_time @type string: this is the time for setting the hwclock
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_set_hwclock_time(self, i_time):
        print "Setting the hwclock time to %s" % i_time
        self.host_run_command("hwclock --set --date \'%s\'" % i_time)

    ##
    # @brief This function will load driver module on host based on the config option
    #        if config value m: built as a module
    #                        y: driver built into kernel itself
    #        else   raises OpTestError
    #
    # @param i_kernel @type string: kernel version to get config file
    #        i_config @type string: config option to check in config file
    #        i_module @type string: driver module to load on host based on config value
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module_based_on_config(self, i_kernel, i_config, i_module):
        l_val = self.host_check_config(i_kernel, i_config)
        if l_val == 'm':
            self.host_load_module(i_module)
        elif l_val == 'y':
            print "Driver built into kernel itself"
        elif l_val == 'n':
            raise KernelConfigNotSet(i_config)

    ##
    # @brief It will clone latest skiboot git repository in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_skiboot_source(self, i_dir):
        l_msg = 'https://github.com/open-power/skiboot.git/'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning skiboot git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function enable only a single core
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_single_core(self):
        self.host_run_command("ppc64_cpu --cores-on=1")

    ##
    # @brief This function is used to get list of PCI PHB domains.
    #
    # @return self.pci_domains list of PHB domains @type list or raise OpTestError
    #
    def host_get_list_of_pci_domains(self):
        self.pci_domains = []
        self.host_run_command("lspci -mm")
        res = self.host_run_command('lspci -mm | cut -d":" -f1 | sort | uniq')
        for domain in res:
            if not domain:
                continue
            if len(domain) != 4:
                domain = ''.join(("00", domain))
            domain = 'PCI' + domain
            if not self.pci_domains.__contains__(domain):
                self.pci_domains.append(domain)
        print self.pci_domains
        return self.pci_domains

    ##
    # @brief This function is used to get the PHB domain of root port where
    #        the filesystem is mounted(We need to skip this in EEH tests as recovery
    #        will fail on this domain is expected)
    #
    # @return boot_domain root PHB @type string or raise OpTestError
    #
    def host_get_root_phb(self):
        cmd = "df -h /boot | awk 'END {print $1}'"
        res = self.host_run_command(cmd)
        boot_disk = ''.join(res).split("/dev/")[1]
        boot_disk = boot_disk.replace("\r\n", "")
        cmd = "ls -l /dev/disk/by-path/ | grep %s | awk '{print $(NF-2)}'" % boot_disk
        res = self.host_run_command(cmd)
        matchObj = re.search(r"\d{4}(?!\d)", '\n'.join(res), re.S)
        if not matchObj:
            raise OpTestError("Not able to find out root phb domain")
        boot_domain = 'PCI' + matchObj.group(0)
        return boot_domain

    ##
    # @brief It will gather kernel dmesg logs and store the copy in a logfile
    #        which will be stored in results dir.
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_kernel_log(self):
        try:
            l_data = '\n'.join(self.host_run_command("dmesg"))
        except OpTestError:
            l_msg = "Failed to gather kernel dmesg log"
            raise OpTestError(l_msg)
        if not self.results_dir:
            print l_data
            return
        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Kernel_dmesg_log_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        print fn
        with open(fn, 'w') as f:
            f.write(l_data)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function starts opal_errd daemon
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_opal_errd_daemon(self):
        self.host_run_command("systemctl start opal_errd")

    ##
    # @brief This function stops opal_errd daemon
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_stop_opal_errd_daemon(self):
        self.host_run_command("systemctl stop opal_errd")

    ##
    # @brief This function gets the status of opal_errd daemon
    #
    # @return True/False: if opal_errd run/not run or throws exception
    #                     if not able to get status
    #
    def host_get_status_of_opal_errd_daemon(self):
        res = self.host_run_command(
            "ps -ef | grep -v grep | grep opal_errd | wc -l")
        print res
        if res[0].strip() == "0":
            print "Opal_errd daemon is not running"
            return False
        elif res[0].strip() == "1":
            print "Opal_errd daemon is running"
            return True
        else:
            raise OpTestError("Not able to get status of opal errd daemon")

    ##
    # @brief This function lists all error logs in host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_all_errorlogs(self):
        self.host_run_command("opal-elog-parse -l")

    ##
    # @brief This function lists all service action logs in host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_all_service_action_logs(self):
        self.host_run_command("opal-elog-parse -s")

    ##
    # @brief This function gets the number of error logs
    #
    # @return res or raise OpTestError
    #
    def host_get_number_of_errorlogs(self):
        res = self.host_run_command("ls %s | wc -l" % BMC_CONST.OPAL_ELOG_DIR)
        print res
        return res

    ##
    # @brief This function clears/acknowledges all error logs in host
    #
    # @return True on success or raise OpTestError
    #
    def host_clear_error_logs(self):
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_ELOG_DIR)
        res = self.host_run_command("ls %s -1 --color=never" %
                                    BMC_CONST.OPAL_ELOG_SYSFS_DIR)
        print '\n'.join(res)
        for entry in res:
            entry = entry.strip()
            if entry == '':
                continue
            self.host_run_command("echo 1 > %s/%s/acknowledge" %
                                  (BMC_CONST.OPAL_ELOG_SYSFS_DIR, entry))
        return True

    ##
    # @brief This function clears/acknowledges all dumps in host
    #
    # @return True on success or raise OpTestError
    #
    def host_clear_all_dumps(self):
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_DUMP_DIR)
        res = self.host_run_command("ls %s -1 --color=never" %
                                    BMC_CONST.OPAL_DUMP_SYSFS_DIR)
        for entry in res:
            entry = entry.strip()
            if (entry == "initiate_dump") or (entry == ''):
                continue
            else:
                self.host_run_command("echo 1 > %s/%s/acknowledge" %
                                      (BMC_CONST.OPAL_DUMP_SYSFS_DIR, entry))
        return True

    # @brief This function disables kdump service
    #
    # @param os_level: string output of /etc/os-release
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_disable_kdump_service(self, os_level):
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service")
            try:
                self.host_run_command("systemctl status kdump-tools.service")
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    print str(cf)
                    raise OpTestError("kdump-tools service is failed to stop")
        else:
            self.host_run_command("systemctl stop kdump.service")
            try:
                self.host_run_command("systemctl status kdump.service")
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    print str(cf)
                    raise OpTestError("kdump service is failed to stop")

    ##
    # @brief This function disables kdump service
    #
    # @param os_level: string output of /etc/os-release
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_kdump_service(self, os_level):
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service")
            self.host_run_command("systemctl start kdump-tools.service")
            self.host_run_command("systemctl status kdump-tools.service")
        else:
            self.host_run_command("systemctl stop kdump.service")
            self.host_run_command("systemctl start kdump.service")
            self.host_run_command("service status kdump.service")

    def host_check_sysfs_path_availability(self, path):
        res = self.host_run_command("ls %s" % path)
        if "No such file or directory" in res:
            return False
        return True

    def host_check_dt_node_exist(self, node_path):
        path = "/proc/device-tree/" + node_path
        res = self.host_run_command("ls %s" % path)
        if "No such file or directory" in res:
            return False
        return True

    def host_get_list_of_chips(self):
        res = self.host_run_command("PATH=/usr/local/sbin:$PATH getscom -l")
        chips = []
        for line in res:
            matchObj = re.search("(\d{8}).*processor", line)
            if matchObj:
                chips.append(matchObj.group(1))
        if not chips:
            raise Exception("Getscom failed to list processor chip ids")
        chips.sort()
        print chips  # ['00000000', '00000001', '00000010']
        return chips

    def host_get_cores(self):
        proc_gen = self.host_get_proc_gen()
        core_ids = {}
        cpu_files = self.host_run_command(
            "find /sys/devices/system/cpu/ -name 'cpu[0-9]*'")
        for cpu_file in cpu_files:
            cpu_id = self.host_run_command("basename %s | tr -dc '0-9'" %
                                           cpu_file)[-1]
            try:
                pir = self.host_run_command("cat %s/pir" % cpu_file)[-1]
            except CommandFailed:
                continue
            if proc_gen in ["POWER8", "POWER8E"]:
                core_id = hex((int("0x%s" % pir, 16) >> 3) & 0xf)
                chip_id = hex((int("0x%s" % pir, 16) >> 7) & 0x3f)
            elif proc_gen in ["POWER9"]:
                core_id = hex((int("0x%s" % pir, 16) >> 2) & 0x3f)
                chip_id = hex((int("0x%s" % pir, 16) >> 8) & 0x7f)
            else:
                raise OpTestError("Unknown or new processor type")
            core_id = core_id.split('x')[1]
            chip_id = chip_id.split('x')[1]

            if chip_id in core_ids:
                core_ids[chip_id].append(core_id)
            else:
                core_ids[chip_id] = [core_id]

        for i in core_ids:
            core_ids[i] = list(set(core_ids[i]))
        core_ids = sorted(core_ids.iteritems())
        print core_ids
        return core_ids

    # Supported on OpenPower and P9 FSP system
    def host_prd_supported(self, bmc_type):
        if not "FSP" in bmc_type:
            return True

        proc_gen = self.host_get_proc_gen()
        if proc_gen in ["POWER8", "POWER8E"]:
            return False

        return True

    def host_get_proc_gen(self):
        try:
            if self.proc_gen:
                pass
        except AttributeError:
            self.proc_gen = ''.join(
                self.host_run_command(
                    "grep '^cpu' /proc/cpuinfo |uniq|sed -e 's/^.*: //;s/ .*//;'"
                ))
        return self.proc_gen

    def host_get_smt(self):
        self.cpu = self.host_get_proc_gen()
        if self.cpu in ["POWER8", "POWER8E"]:
            return 8
        elif self.cpu in ["POWER9"]:
            return 4
        else:
            return 1

    def host_get_core_count(self):
        res = self.host_run_command("lscpu --all -e| wc -l")
        return int(res[0]) / (self.host_get_smt())

    def host_gather_debug_logs(self):
        self.ssh.run_command_ignore_fail(
            "grep ',[0-4]\]' /sys/firmware/opal/msglog")
        self.ssh.run_command_ignore_fail(
            "dmesg -T --level=alert,crit,err,warn")

    def host_copy_fake_gard(self):
        path = os.path.abspath(os.path.join("common", os.pardir))
        i_image = path + "/test_binaries/fake.gard"
        # Copy the fake.gard file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user, self.ip, "/tmp/",
                                      self.passwd)
        except:
            l_msg = "Copying fake.gard file to host failed"
            print l_msg
            raise OpTestError(l_msg)

    def host_pflash_get_partition(self, partition):
        d = self.host_run_command("pflash --info")
        for line in d:
            s = re.search(partition, line)
            if s:
                m = re.match(
                    r'ID=\d+\s+\S+\s+((0[xX])?[0-9a-fA-F]+)..(0[xX])?[0-9a-fA-F]+\s+\(actual=((0[xX])?[0-9a-fA-F]+)\).*',
                    line)
                offset = int(m.group(1), 16)
                length = int(m.group(4), 16)
                ret = {'offset': offset, 'length': length}
        return ret
示例#39
0
class IPMIConsole():
    def __init__(self,
                 ipmitool=None,
                 logfile=sys.stdout,
                 prompt=None,
                 block_setup_term=None,
                 delaybeforesend=None):
        self.logfile = logfile
        self.ipmitool = ipmitool
        self.state = IPMIConsoleState.DISCONNECTED
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.delaybeforesend = delaybeforesend
        self.block_setup_term = block_setup_term  # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0  # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0  # flags the object to abandon setup_term operations, like when system off

        # FUTURE - System Console currently tracked in System Object
        # state tracking, reset on boot and state changes
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        if self.state == IPMIConsoleState.DISCONNECTED:
            return
        try:
            self.pty.send("\r")
            self.pty.send('~.')
            close_rc = self.pty.expect(
                ['[terminated ipmitool]', pexpect.TIMEOUT, pexpect.EOF],
                timeout=10)
            log.debug("CLOSE Expect Buffer ID={}".format(hex(id(self.pty))))
            rc_child = self.pty.close()
            self.state = IPMIConsoleState.DISCONNECTED
            exitCode = signalstatus = None
            if self.pty.status != -1:  # leaving for future debug
                if os.WIFEXITED(self.pty.status):
                    exitCode = os.WEXITSTATUS(self.pty.status)
                else:
                    signalstatus = os.WTERMSIG(self.pty.status)
        except pexpect.ExceptionPexpect:
            self.state = IPMIConsoleState.DISCONNECTED
            raise OpTestError("IPMI: failed to close ipmi console")
        except Exception as e:
            self.state = IPMIConsoleState.DISCONNECTED
            pass

    def connect(self, logger=None):
        if self.state == IPMIConsoleState.CONNECTED:
            rc_child = self.close()
        else:
            self.util.clear_state(self)

        try:
            self.ipmitool.run('sol deactivate')
        except OpTestError:
            log.info('SOL already deactivated')

        cmd = self.ipmitool.binary_name() + self.ipmitool.arguments(
        ) + ' sol activate'
        try:
            self.pty = OPexpect.spawn(
                cmd,
                logfile=self.logfile,
                failure_callback=set_system_to_UNKNOWN_BAD,
                failure_callback_data=self.system)
        except Exception as e:
            self.state = IPMIConsoleState.DISCONNECTED
            raise CommandFailed(
                'OPexpect.spawn',
                "OPexpect.spawn encountered a problem, command was '{}'".
                format(cmd), -1)

        log.debug("#IPMI SOL CONNECT")
        self.state = IPMIConsoleState.CONNECTED
        self.pty.setwinsize(1000, 1000)

        if logger:
            self.pty.logfile_read = OpTestLogger.FileLikeLogger(logger)
        else:
            self.pty.logfile_read = OpTestLogger.FileLikeLogger(log)

        if self.delaybeforesend:
            self.pty.delaybeforesend = self.delaybeforesend
        rc = self.pty.expect_exact([
            '[SOL Session operational.  Use ~? for help]', pexpect.TIMEOUT,
            pexpect.EOF
        ],
                                   timeout=10)
        log.debug("rc={}".format(rc))
        if rc == 0:
            if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
                self.util.setup_term(self.system, self.pty, None,
                                     self.system.block_setup_term)
            time.sleep(0.2)
            log.debug("CONNECT starts Expect Buffer ID={}".format(
                hex(id(self.pty))))
            return self.pty
        if rc == 1:
            self.pty.close()
            time.sleep(60)  # give things a minute to clear
            raise CommandFailed(
                'sol activate',
                "IPMI: pexpect.TIMEOUT while trying to establish"
                " connection, command was '{}'".format(cmd), -1)
        if rc == 2:
            self.pty.close()
            time.sleep(60)  # give things a minute to clear
            raise CommandFailed(
                'sol activate',
                "IPMI: insufficient resources for session, unable"
                " to establish IPMI v2 / RMCP+ session, command was '{}'".
                format(cmd), -1)

    def get_console(self, logger=None):
        if self.state == IPMIConsoleState.DISCONNECTED:
            self.connect(logger)

        count = 0
        while (not self.pty.isalive()):
            log.warning('# Reconnecting')
            if (count > 0):
                time.sleep(BMC_CONST.IPMI_SOL_ACTIVATE_TIME)
            self.connect()
            count += 1
            if count > 120:
                raise "IPMI: not able to get sol console"
        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
            self.util.setup_term(self.system, self.pty, None,
                                 self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)
示例#40
0
class OpTestHost():

    ##
    # @brief Initialize this object
    #
    # @param i_hostip @type string: IP Address of the host
    # @param i_hostuser @type string: Userid to log into the host
    # @param i_hostpasswd @type string: Password of the userid to log into the host
    # @param i_bmcip @type string: IP Address of the bmc
    #
    def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_results_dir,
                 scratch_disk="", proxy="", logfile=sys.stdout,
                 check_ssh_keys=False, known_hosts_file=None):
        self.ip = i_hostip
        self.user = i_hostuser
        self.passwd = i_hostpasswd
        self.util = OpTestUtil()
        self.bmcip = i_bmcip
        parent_dir = os.path.dirname(os.path.abspath(__file__))
        self.results_dir = i_results_dir
        self.logfile = logfile
        self.ssh = OpTestSSH(i_hostip, i_hostuser, i_hostpasswd,
                logfile=self.logfile, check_ssh_keys=check_ssh_keys,
                known_hosts_file=known_hosts_file, use_default_bash=True)
        self.scratch_disk = scratch_disk
        self.proxy = proxy
        self.scratch_disk_size = None

    def hostname(self):
        return self.ip

    def username(self):
        return self.user

    def password(self):
        return self.passwd


    def get_scratch_disk(self):
        return self.scratch_disk

    def get_scratch_disk_size(self, console=None):
        if self.scratch_disk_size is not None:
            return self.scratch_disk_size
        if console is None:
            raise Exception("You need to call get_scratch_disk_size() with a console first")
        console.run_command("stty cols 300")
        dev_sdX = console.run_command("readlink -f %s" % self.get_scratch_disk())
        dev_sdX = dev_sdX[0].replace("/dev/","")
        scratch_disk_size = console.run_command("cat /sys/block/%s/size" % dev_sdX)
        # Use the (undocumented) /size sysfs property of nr 512byte sectors
        self.scratch_disk_size = int(scratch_disk_size[0])*512


    def get_proxy(self):
        return self.proxy

    def get_ssh_connection(self):
        return self.ssh

    ##
    # @brief Get and Record Ubunto OS level
    #
    # @return l_oslevel @type string: OS level of the host provided
    #         or raise OpTestError
    #
    def host_get_OS_Level(self):
        l_oslevel = self.ssh.run_command("cat /etc/os-release", timeout=60)
        return '\n'.join(l_oslevel)


    ##
    # @brief Executes a command on the os of the bmc to protect network setting
    #
    # @return OpTestError if failed
    #
    def host_protect_network_setting(self):
        try:
            l_rc = self.ssh.run_command(BMC_CONST.OS_PRESERVE_NETWORK, timeout=60)
        except:
            l_errmsg = "Can't preserve network setting"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief Performs a cold reset onto the host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_cold_reset(self):

        print ("Applying Cold reset on host.")
        l_rc = self.ssh.run_command(BMC_CONST.HOST_COLD_RESET, timeout=60)

        # TODO: enable once defect SW331585 is fixed
        '''if BMC_CONST.BMC_PASS_COLD_RESET in l_rc:
            print l_rc
            time.sleep(BMC_CONST.BMC_COLD_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Cold reset Failed"
            print l_msg
            raise OpTestError(l_msg)'''

        self.util.PingFunc(self.bmcip, BMC_CONST.PING_RETRY_FOR_STABILITY)


    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_code_update(self, i_image, imagecomponent):

        # Copy the hpm file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying hpm file to host failed"
            print l_msg
            raise OpTestError(l_msg)

        #self.host_protect_network_setting() #writing to host is not stable
        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + " " + imagecomponent
        print l_cmd
        try:
            l_rc = self.ssh.run_command(l_cmd, timeout=1500)
            print l_rc
            self.ssh.run_command("rm -rf /tmp/" + i_image.rsplit("/", 1)[1],timeout=120)
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if(l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

    def host_run_command(self, i_cmd, timeout=1500):
        return self.ssh.run_command(i_cmd, timeout)

    ##
    # @brief It will gather OPAL Message logs and store the copy in a logfile
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_opal_msg_log(self):
        try:
            l_data = '\n'.join(self.host_run_command(BMC_CONST.OPAL_MSG_LOG))
        except OpTestError:
            l_msg = "Failed to gather OPAL message logs"
            raise OpTestError(l_msg)

        if not self.results_dir:
            print l_data
            return

        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Opal_msglog_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        print fn
        with open(fn, 'w') as f:
            f.write(l_data)


    ##
    # @brief Check if one or more binaries are present on host
    #
    # @param i_cmd @type string: binaries to check for
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_check_command(self, *i_cmd):
        l_cmd = 'which ' + ' '.join(i_cmd)
        print l_cmd
        try:
            l_res = self.host_run_command(l_cmd)
        except CommandFailed as c:
            l_msg = "host_check_command: (%s) not present on host. output of '%s': %s" % (','.join(i_cmd), l_cmd, '\n'.join(c.output))
            print l_msg
            raise OpTestError(l_msg)

        return True

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self.ssh.run_command("uname -a | awk {'print $3'}", timeout=60)
        l_kernel = ''.join(l_kernel)
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        try:
            l_res = self.ssh.run_command("test -e %s" % l_file, timeout=60)
        except CommandFailed as c:
            raise NoKernelConfig(i_kernel, l_file)

        print "Config file is available"
        l_cmd = "cat %s" % (l_file)
        l_res = self.ssh.run_command(l_cmd, timeout=60)
        config_opts = {}
        for o in l_res:
            m = re.match('# (.*) is not set', o)
            if m:
                config_opts[m.group(0)]='n'
            else:
                if '=' in o:
                    opt, val = o.split("=")
                    config_opts[opt] = val

        if config_opts.get(i_config) not in ["y","m"]:
                raise KernelConfigNotSet(i_config)

        return config_opts[i_config]

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            return ''.join(self.ssh.run_command("dpkg -S `which %s`" % i_cmd, timeout=60))
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            return ''.join(self.ssh.run_command(l_cmd, timeout=60))

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            o = self.ssh.run_command("modprobe ibmpowernv",timeout=60)
            cmd = "lsmod | grep -i ibmpowernv"
            response = self.ssh.run_command(cmd, timeout=60)
            if "ibmpowernv" not in ''.join(response):
                l_msg = "ibmpowernv module is not loaded, exiting"
                raise OpTestError(l_msg)
            else:
                print "ibmpowernv module is loaded"
            print cmd
            print response
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone --depth=1 %s %s" % (l_msg, i_dir)
        self.ssh.run_command("rm -rf %s" % i_dir, timeout=300)
        self.ssh.run_command("mkdir %s" % i_dir, timeout=60)
        try:
            print l_cmd
            res = self.ssh.run_command(l_cmd, timeout=1500)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief reads getscom data
    # example:
    # Chip ID  | Rev   | Chip type
    # ---------|-------|--------
    # 80000005 | DD2.0 | Centaur memory buffer
    # 80000004 | DD2.0 | Centaur memory buffer
    # 00000000 | DD2.0 | P8 (Venice) processor
    #
    # @param i_xscom_dir @type string: directory where getscom is installed
    #
    # @return @type string: getscom data
    #        else raise OpTestError
    def host_read_getscom_data(self, i_xscom_dir):
        try:
            l_rc = self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_xscom_dir + BMC_CONST.OS_GETSCOM_LIST, timeout=60)
        except OpTestError as e:
            l_errmsg = "Can't get getscom data"
            print l_errmsg
            raise OpTestError(l_errmsg)

        if("command not found" in l_rc):
            l_errmsg = "Failed to locate getscom. Make sure it is installed in dir: " + i_xscom_dir
            print l_errmsg
            raise OpTestError(l_errmsg)

        return l_rc


    ##
    # @brief injects error using getscom
    #
    # @param i_xscom_dir @type string: directory where putscom is installed
    # param i_error @type string: error to be injected including the location
    #
    # @return output generated after executing putscom command or else raise OpTestError
    #
    def host_putscom(self, i_xscom_dir, i_error):

        print('Injecting Error.')
        l_rc = self.ssh.run_command_ignore_fail(BMC_CONST.SUDO_COMMAND + i_xscom_dir + BMC_CONST.OS_PUTSCOM_ERROR + i_error)

    ##
    # @brief Clears the gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def host_clear_gard_records(self, i_gard_dir):

        l_rc = self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_gard_dir + BMC_CONST.CLEAR_GARD_CMD, timeout=60)

        if(BMC_CONST.GARD_CLEAR_SUCCESSFUL not in l_rc):
            l_msg = l_rc + '. Failed to clear gard'
            print l_msg
            raise OpTestError(l_msg)

        # Check to make sure no gard records were left
        l_result = self.host_list_gard_records(i_gard_dir)
        if(BMC_CONST.NO_GARD_RECORDS not in l_result):
            l_msg = l_rc + '. Gard records still found after clearing them'
            print l_msg
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS


    ##
    # @brief Lists all gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return gard records or else raise OpTestError
    #
    def host_list_gard_records(self, i_gard_dir):
        try:
            return self.ssh.run_command(BMC_CONST.SUDO_COMMAND + i_gard_dir + BMC_CONST.LIST_GARD_CMD, timeout=60)
        except:
            l_errmsg = "Can't clear gard records"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief enable/disable cpu states
    #
    # @param i_cpu_state @type string: BMC_CONST.CPU_ENABLE_STATE/
    #                                  BMC_CONST.CPU_DISABLE_STATE
    #
    # @return BMC_CONST.FW_SUCCESS or OpTestError
    #
    def host_disable_enable_cpu_states(self, i_cpu_state):
        try:
            self.ssh.run_command(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE1 + "; do echo " + \
                              i_cpu_state  + " > $i; done'", timeout=60)
            self.ssh.run_command(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE2 + "; do echo " + \
                              i_cpu_state  + " > $i; done'", timeout=60)
            return BMC_CONST.FW_SUCCESS
        except:
            l_errmsg = "Could not enable/disable cpu idle states"
            print l_errmsg
            raise OpTestError(l_errmsg)


    ##
    # @brief It will load the module using modprobe and verify whether it is loaded or not
    #
    # @param i_module @type string: module name, which we want to load on host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module(self, i_module):
        try:
            l_res = self.host_run_command("modprobe %s" % i_module)
        except CommandFailed as c:
            l_msg = "Error in loading the module %s, modprobe failed: %s" % (i_module,str(c))
            raise OpTestError(l_msg)
        l_res = self.host_run_command("lsmod | grep -i --color=never %s" % i_module)
        if re.search(i_module, ''.join(l_res)):
            print "%s module is loaded" % i_module
            return BMC_CONST.FW_SUCCESS
        else:
            raise KernelModuleNotLoaded(i_module)

    ##
    # @brief This function will read real time clock(RTC) time using hwclock utility
    #
    # @return l_res @type string: return hwclock value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_hwclock(self):
        print "Reading the hwclock"
        self.host_run_command("hwclock -r;echo $?")

    ##
    # @brief This function will read system time using date utility (This will be mantained by kernel)
    #
    # @return l_res @type string: return system time value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_systime(self):
        print "Reading system time using date utility"
        l_res = self.host_run_command("date")
        return l_res

    ##
    # @brief This function will set hwclock time using the --date option
    #        format should be "2015-01-01 12:12:12"
    #
    # @param i_time @type string: this is the time for setting the hwclock
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_set_hwclock_time(self, i_time):
        print "Setting the hwclock time to %s" % i_time
        self.host_run_command("hwclock --set --date \'%s\'" % i_time)

    ##
    # @brief This function will load driver module on host based on the config option
    #        if config value m: built as a module
    #                        y: driver built into kernel itself
    #        else   raises OpTestError
    #
    # @param i_kernel @type string: kernel version to get config file
    #        i_config @type string: config option to check in config file
    #        i_module @type string: driver module to load on host based on config value
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module_based_on_config(self, i_kernel, i_config, i_module):
        l_val = self.host_check_config(i_kernel, i_config)
        if l_val == 'm':
            self.host_load_module(i_module)
        elif l_val == 'y':
            print "Driver built into kernel itself"
        elif l_val == 'n':
            raise KernelConfigNotSet(i_config)


    ##
    # @brief It will clone latest skiboot git repository in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_skiboot_source(self, i_dir):
        l_msg = 'https://github.com/open-power/skiboot.git/'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning skiboot git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function enable only a single core
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_single_core(self):
        self.host_run_command("ppc64_cpu --cores-on=1")

    ##
    # @brief This function is used to get list of PCI PHB domains.
    #
    # @return self.pci_domains list of PHB domains @type list or raise OpTestError
    #
    def host_get_list_of_pci_domains(self):
        self.pci_domains = []
        self.host_run_command("lspci -mm")
        res = self.host_run_command('lspci -mm | cut -d":" -f1 | sort | uniq')
        for domain in res:
            if not domain:
                continue
            if len(domain) != 4:
                domain = ''.join(("00", domain))
            domain = 'PCI' + domain
            if not self.pci_domains.__contains__(domain):
                self.pci_domains.append(domain)
        print self.pci_domains
        return self.pci_domains

    ##
    # @brief This function is used to get the PHB domain of root port where
    #        the filesystem is mounted(We need to skip this in EEH tests as recovery
    #        will fail on this domain is expected)
    #
    # @return boot_domain root PHB @type string or raise OpTestError
    #
    def host_get_root_phb(self):
        cmd = "df -h /boot | awk 'END {print $1}'"
        res = self.host_run_command(cmd)
        boot_disk = ''.join(res).split("/dev/")[1]
        boot_disk = boot_disk.replace("\r\n", "")
        cmd  = "ls -l /dev/disk/by-path/ | grep %s | awk '{print $(NF-2)}'" % boot_disk
        res = self.host_run_command(cmd)
        matchObj = re.search(r"\d{4}(?!\d)", '\n'.join(res), re.S)
        if not matchObj:
            raise OpTestError("Not able to find out root phb domain")
        boot_domain = 'PCI' + matchObj.group(0)
        return  boot_domain

    ##
    # @brief It will gather kernel dmesg logs and store the copy in a logfile
    #        which will be stored in results dir.
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_kernel_log(self):
        try:
            l_data = '\n'.join(self.host_run_command("dmesg"))
        except OpTestError:
            l_msg = "Failed to gather kernel dmesg log"
            raise OpTestError(l_msg)
        if not self.results_dir:
            print l_data
            return
        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Kernel_dmesg_log_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        print fn
        with open(fn, 'w') as f:
            f.write(l_data)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function starts opal_errd daemon
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_opal_errd_daemon(self):
        self.host_run_command("systemctl start opal_errd")

    ##
    # @brief This function stops opal_errd daemon
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_stop_opal_errd_daemon(self):
        self.host_run_command("systemctl stop opal_errd")

    ##
    # @brief This function gets the status of opal_errd daemon
    #
    # @return True/False: if opal_errd run/not run or throws exception
    #                     if not able to get status
    #
    def host_get_status_of_opal_errd_daemon(self):
        res = self.host_run_command("ps -ef | grep -v grep | grep opal_errd | wc -l")
        print res
        if res[0].strip() == "0":
            print "Opal_errd daemon is not running"
            return False
        elif res[0].strip() == "1":
            print "Opal_errd daemon is running"
            return True
        else:
            raise OpTestError("Not able to get status of opal errd daemon")

    ##
    # @brief This function lists all error logs in host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_all_errorlogs(self):
        self.host_run_command("opal-elog-parse -l")


    ##
    # @brief This function lists all service action logs in host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_all_service_action_logs(self):
        self.host_run_command("opal-elog-parse -s")

    ##
    # @brief This function gets the number of error logs
    #
    # @return res or raise OpTestError
    #
    def host_get_number_of_errorlogs(self):
        res = self.host_run_command("ls %s | wc -l" % BMC_CONST.OPAL_ELOG_DIR)
        print res
        return res

    ##
    # @brief This function clears/acknowledges all error logs in host
    #
    # @return True on success or raise OpTestError
    #
    def host_clear_error_logs(self):
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_ELOG_DIR)
        res = self.host_run_command("ls %s -1 --color=never" % BMC_CONST.OPAL_ELOG_SYSFS_DIR)
        print '\n'.join(res)
        for entry in res:
            entry = entry.strip()
            if entry == '':
                continue
            self.host_run_command("echo 1 > %s/%s/acknowledge" % (BMC_CONST.OPAL_ELOG_SYSFS_DIR, entry))
        return True
    ##
    # @brief This function clears/acknowledges all dumps in host
    #
    # @return True on success or raise OpTestError
    #
    def host_clear_all_dumps(self):
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_DUMP_DIR)
        res = self.host_run_command("ls %s -1 --color=never" % BMC_CONST.OPAL_DUMP_SYSFS_DIR)
        for entry in res:
            entry = entry.strip()
            if (entry == "initiate_dump") or (entry == ''):
                continue
            else:
                self.host_run_command("echo 1 > %s/%s/acknowledge" % (BMC_CONST.OPAL_DUMP_SYSFS_DIR, entry))
        return True

    # @brief This function disables kdump service
    #
    # @param os_level: string output of /etc/os-release
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_disable_kdump_service(self, os_level):
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service")
            try:
                self.host_run_command("systemctl status kdump-tools.service")
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    print str(cf)
                    raise OpTestError("kdump-tools service is failed to stop")
        else:
            self.host_run_command("systemctl stop kdump.service")
            try:
                self.host_run_command("systemctl status kdump.service")
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    print str(cf)
                    raise OpTestError("kdump service is failed to stop")

    ##
    # @brief This function disables kdump service
    #
    # @param os_level: string output of /etc/os-release
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_kdump_service(self, os_level):
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service")
            self.host_run_command("systemctl start kdump-tools.service")
            self.host_run_command("systemctl status kdump-tools.service")
        else:
            self.host_run_command("systemctl stop kdump.service")
            self.host_run_command("systemctl start kdump.service")
            self.host_run_command("service status kdump.service")

    def host_check_sysfs_path_availability(self, path):
        res = self.host_run_command("ls %s" % path)
        if "No such file or directory" in res:
            return False
        return True

    def host_check_dt_node_exist(self, node_path):
        path = "/proc/device-tree/" + node_path
        res = self.host_run_command("ls %s" % path)
        if "No such file or directory" in res:
            return False
        return True

    def host_get_list_of_chips(self):
        res = self.host_run_command("PATH=/usr/local/sbin:$PATH getscom -l")
        chips = []
        for line in res:
            matchObj = re.search("(\d{8}).*processor", line)
            if matchObj:
                chips.append(matchObj.group(1))
        if not chips:
            raise Exception("Getscom failed to list processor chip ids")
        chips.sort()
        print chips # ['00000000', '00000001', '00000010']
        return chips

    def host_get_cores(self):
        proc_gen = self.host_get_proc_gen()
        core_ids = {}
        cpu_files = self.host_run_command("find /sys/devices/system/cpu/ -name 'cpu[0-9]*'")
        for cpu_file in cpu_files:
            cpu_id = self.host_run_command("basename %s | tr -dc '0-9'" % cpu_file)[-1]
            try:
                pir = self.host_run_command("cat %s/pir" % cpu_file)[-1]
            except CommandFailed:
                continue
            if proc_gen in ["POWER8", "POWER8E"]:
                core_id = hex((int("0x%s" % pir, 16) >> 3 ) & 0xf)
                chip_id = hex((int("0x%s" % pir, 16) >> 7 ) & 0x3f)
            elif proc_gen in ["POWER9"]:
                core_id =hex((int("0x%s" % pir, 16) >> 2 ) & 0x3f)
                chip_id = hex((int("0x%s" % pir, 16) >> 8 ) & 0x7f)
            else:
                raise OpTestError("Unknown or new processor type")
            core_id = core_id.split('x')[1]
            chip_id = chip_id.split('x')[1]

            if chip_id in core_ids:
                core_ids[chip_id].append(core_id)
            else:
                core_ids[chip_id] = [core_id]

        for i in core_ids:
            core_ids[i] = list(set(core_ids[i]))
        core_ids = sorted(core_ids.iteritems())
        print core_ids
        return core_ids

    # Supported on OpenPower and P9 FSP system
    def host_prd_supported(self, bmc_type):
        if not "FSP" in bmc_type:
            return True

        proc_gen = self.host_get_proc_gen()
        if proc_gen in ["POWER8", "POWER8E"]:
            return False

        return True

    def host_get_proc_gen(self):
        try:
            if self.proc_gen:
                pass
        except AttributeError:
            self.proc_gen = ''.join(self.host_run_command("grep '^cpu' /proc/cpuinfo |uniq|sed -e 's/^.*: //;s/[,]* .*//;'"))
        return self.proc_gen

    def host_get_smt(self):
        self.cpu = self.host_get_proc_gen()
        if self.cpu in ["POWER8", "POWER8E"]:
            return 8
        elif self.cpu in ["POWER9"]:
            return 4
        else:
            return 1

    def host_get_core_count(self):
        res = self.host_run_command("lscpu --all -e| wc -l")
        return int(res[0])/(self.host_get_smt())

    def host_gather_debug_logs(self):
        self.ssh.run_command_ignore_fail("grep ',[0-4]\]' /sys/firmware/opal/msglog")
        self.ssh.run_command_ignore_fail("dmesg -T --level=alert,crit,err,warn")

    def host_copy_fake_gard(self):
        path = os.path.abspath(os.path.join("common", os.pardir))
        i_image = path + "/test_binaries/fake.gard"
        # Copy the fake.gard file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying fake.gard file to host failed"
            print l_msg
            raise OpTestError(l_msg)

    def host_pflash_get_partition(self, partition):
        d = self.host_run_command("pflash --info")
        for line in d:
            s = re.search(partition, line)
            if s:
                m = re.match(r'ID=\d+\s+\S+\s+((0[xX])?[0-9a-fA-F]+)..(0[xX])?[0-9a-fA-F]+\s+\(actual=((0[xX])?[0-9a-fA-F]+)\).*', line)
                offset = int(m.group(1), 16)
                length = int(m.group(4), 16)
                ret = {'offset': offset, 'length': length}
        return ret

    ##
    # @brief Check that host has a CAPI FPGA card
    #
    # @return True or False
    #
    def host_has_capi_fpga_card(self):
        l_cmd = "lspci -d \"1014:0477\""
        l_res = self.host_run_command(l_cmd)
        l_res = " ".join(l_res)
        if (l_res.__contains__('IBM Device 0477')):
            l_msg = "Host has a CAPI FPGA card"
            print l_msg
            return True
        else:
            l_msg = "Host has no CAPI FPGA card; skipping test"
            print l_msg
            return False

    ##
    # @brief Clone latest cxl-tests git repository in i_dir directory
    #
    # @param i_dir @type string: directory where cxl-tests will be cloned
    #
    # @return True or False
    #
    def host_clone_cxl_tests(self, i_dir):
        l_msg = "https://github.com/ibm-capi/cxl-tests.git"
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            l_res = self.host_run_command(l_cmd)
            return True
        except:
            l_msg = "Cloning cxl-tests git repository is failed"
            return False

    def host_build_cxl_tests(self, i_dir):
        l_cmd = "cd %s; make" % i_dir
        self.host_run_command(l_cmd)
        l_cmd = "test -x %s/libcxl/libcxl.so" % i_dir
        self.host_run_command(l_cmd)
        l_cmd = "test -x %s/libcxl_tests; echo $?" % i_dir
        self.host_run_command(l_cmd)
        l_cmd = "test -x %s/memcpy_afu_ctx; echo $?" % i_dir
        self.host_run_command(l_cmd)

    def host_check_binary(self, i_dir, i_file):
        l_cmd = "test -x %s/%s;" % (i_dir, i_file)
        try:
            self.host_run_command(l_cmd)
            l_msg = "Executable file %s/%s is available" % (i_dir, i_file)
            print l_msg
            return True
        except CommandFailed:
            l_msg = "Executable file %s/%s is not present" % (i_dir, i_file)
            print l_msg
            return False
class IPMIConsole():
    def __init__(self, ipmitool=None, logfile=sys.stdout, prompt=None,
            block_setup_term=None, delaybeforesend=None):
        self.ipmitool = ipmitool
        self.state = IPMIConsoleState.DISCONNECTED
        self.delaybeforesend = delaybeforesend
        self.system = None
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.delaybeforesend = delaybeforesend
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # FUTURE - System Console currently tracked in System Object
        # state tracking, reset on boot and state changes
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        if self.state == IPMIConsoleState.DISCONNECTED:
            return
        try:
            self.pty.send("\r")
            self.pty.send('~.')
            close_rc = self.pty.expect(['[terminated ipmitool]', pexpect.TIMEOUT, pexpect.EOF], timeout=10)
            rc_child = self.pty.close()
            self.state = IPMIConsoleState.DISCONNECTED
            exitCode = signalstatus = None
            if self.pty.status != -1: # leaving for future debug
              if os.WIFEXITED(self.pty.status):
                exitCode = os.WEXITSTATUS(self.pty.status)
              else:
                signalstatus = os.WTERMSIG(self.pty.status)
        except pexpect.ExceptionPexpect:
            self.state = IPMIConsoleState.DISCONNECTED
            raise OpTestError("IPMI: failed to close ipmi console")
        except Exception as e:
            self.state = IPMIConsoleState.DISCONNECTED
            pass

    def connect(self):
        if self.state == IPMIConsoleState.CONNECTED:
          rc_child = self.close()
        else:
          self.util.clear_state(self)

        try:
            self.ipmitool.run('sol deactivate')
        except OpTestError:
            log.info('SOL already deactivated')

        cmd = self.ipmitool.binary_name() + self.ipmitool.arguments() + ' sol activate'
        try:
          self.pty = OPexpect.spawn(cmd,
                                  failure_callback=set_system_to_UNKNOWN_BAD,
                                  failure_callback_data=self.system)
        except Exception as e:
          self.state = IPMIConsoleState.DISCONNECTED
          raise CommandFailed('OPexpect.spawn', "OPexpect.spawn encountered a problem, command was '{}'".format(cmd), -1)

        log.debug("#IPMI SOL CONNECT")
        self.state = IPMIConsoleState.CONNECTED
        self.pty.setwinsize(1000,1000)
        self.pty.logfile_read = OpTestLogger.FileLikeLogger(log)
        if self.delaybeforesend:
	    self.pty.delaybeforesend = self.delaybeforesend
        rc = self.pty.expect_exact(['[SOL Session operational.  Use ~? for help]', pexpect.TIMEOUT, pexpect.EOF], timeout=10)
        if rc == 0:
          if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set !=1:
            self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)
          time.sleep(0.2)
          return self.pty
        if rc == 1:
          self.pty.close()
          time.sleep(60) # give things a minute to clear
          raise CommandFailed('sol activate',
            "IPMI: pexpect.TIMEOUT while trying to establish"
            " connection, command was '{}'"
            .format(cmd), -1)
        if rc == 2:
          self.pty.close()
          time.sleep(60) # give things a minute to clear
          raise CommandFailed('sol activate',
            "IPMI: insufficient resources for session, unable"
            " to establish IPMI v2 / RMCP+ session, command was '{}'"
            .format(cmd), -1)

    def get_console(self):
        if self.state == IPMIConsoleState.DISCONNECTED:
            self.connect()

        count = 0
        while (not self.pty.isalive()):
            log.warning('# Reconnecting')
            if (count > 0):
                time.sleep(BMC_CONST.IPMI_SOL_ACTIVATE_TIME)
            self.connect()
            count += 1
            if count > 120:
                raise "IPMI: not able to get sol console"
        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set !=1:
          self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)
示例#42
0
class MamboConsole():
    '''
    A 'connection' to the Mambo Console involves *launching* Mambo.
    Closing a connection will *terminate* the Mambo process.
    '''
    def __init__(self,
                 mambo_binary=None,
                 mambo_initial_run_script=None,
                 mambo_autorun=None,
                 skiboot=None,
                 prompt=None,
                 kernel=None,
                 initramfs=None,
                 block_setup_term=None,
                 delaybeforesend=None,
                 timeout_factor=1,
                 logfile=sys.stdout):
        self.mambo_binary = mambo_binary
        self.mambo_initial_run_script = mambo_initial_run_script
        self.mambo_autorun = mambo_autorun
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term  # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0  # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0  # flags the object to abandon setup_term operations, like when system off
        self.timeout_factor = timeout_factor  # functional simulators are notoriously slow, so multiply all default timeouts by this factor

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        try:
            rc_child = self.pty.close()
            exitCode = signalstatus = None
            if self.pty.status != -1:  # leaving for debug
                if os.WIFEXITED(self.pty.status):
                    exitCode = os.WEXITSTATUS(self.pty.status)
                else:
                    signalstatus = os.WTERMSIG(self.pty.status)
            self.state = ConsoleState.DISCONNECTED
        except pexpect.ExceptionPexpect as e:
            self.state = ConsoleState.DISCONNECTED
            raise "Mambo Console: failed to close console"
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            pass
        log.debug("Mambo close -> TERMINATE")

    def connect(self):
        if self.state == ConsoleState.CONNECTED:
            return self.pty
        else:
            self.util.clear_state(self)  # clear when coming in DISCONNECTED

        log.debug("#Mambo Console CONNECT")

        if not os.access(self.mambo_initial_run_script, os.R_OK | os.W_OK):
            raise ParameterCheck(
                message="Check that the file exists with"
                " R/W permissions mambo-initial-run-script={}".format(
                    self.mambo_initial_run_script))

        cmd = ("%s" % (self.mambo_binary) + " -e" +
               " -f {}".format(self.mambo_initial_run_script))

        spawn_env = os.environ
        if self.skiboot:
            spawn_env['SKIBOOT'] = self.skiboot
        if self.kernel:
            spawn_env['SKIBOOT_ZIMAGE'] = self.kernel
        if self.initramfs:
            if not os.access(self.initramfs, os.R_OK | os.W_OK):
                raise ParameterCheck(message="Check that the file exists with"
                                     " R/W permissions flash-initramfs={}".
                                     format(self.initramfs))
            spawn_env['SKIBOOT_INITRD'] = self.initramfs
        if self.mambo_autorun:
            spawn_env['SKIBOOT_AUTORUN'] = str(self.mambo_autorun)
        log.debug("OpTestMambo cmd={} mambo spawn_env={}".format(
            cmd, spawn_env))
        try:
            self.pty = OPexpect.spawn(cmd, logfile=self.logfile, env=spawn_env)
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            raise CommandFailed(
                'OPexpect.spawn',
                'OPexpect.spawn encountered a problem: ' + str(e), -1)

        self.state = ConsoleState.CONNECTED
        self.pty.setwinsize(1000, 1000)
        if self.delaybeforesend:
            self.pty.delaybeforesend = self.delaybeforesend

        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
            self.util.setup_term(self.system, self.pty, None,
                                 self.system.block_setup_term)

        time.sleep(0.2)
        if not self.pty.isalive():
            raise CommandFailed(cmd, self.pty.read(), self.pty.status)
        return self.pty

    def get_console(self):
        if self.state == ConsoleState.DISCONNECTED:
            self.util.clear_state(self)
            self.connect()
        else:
            if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
                self.util.setup_term(self.system, self.pty, None,
                                     self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command,
                                     timeout * self.timeout_factor, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command,
                                                 timeout * self.timeout_factor,
                                                 retry)

    def mambo_run_command(self, command, timeout=60, retry=0):
        return self.util.mambo_run_command(self, command,
                                           timeout * self.timeout_factor,
                                           retry)

    def mambo_exit(self):
        return self.util.mambo_exit(self)

    def mambo_enter(self):
        return self.util.mambo_enter(self)
示例#43
0
class OpTestIPMI():
    def __init__(self,
                 i_bmcIP,
                 i_bmcUser,
                 i_bmcPwd,
                 logfile=sys.stdout,
                 host=None,
                 delaybeforesend=None):
        self.cv_bmcIP = i_bmcIP
        self.cv_bmcUser = i_bmcUser
        self.cv_bmcPwd = i_bmcPwd
        self.logfile = logfile
        self.ipmitool = IPMITool(method='lanplus',
                                 ip=i_bmcIP,
                                 username=i_bmcUser,
                                 password=i_bmcPwd,
                                 logfile=logfile)
        self.pUpdate = pUpdate(method='lan',
                               ip=i_bmcIP,
                               username=i_bmcUser,
                               password=i_bmcPwd)
        self.console = IPMIConsole(ipmitool=self.ipmitool,
                                   logfile=self.logfile,
                                   delaybeforesend=delaybeforesend)
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.host = host

    def set_system(self, system):
        self.console.set_system(system)

    def get_host_console(self):
        '''
        Get the IPMIConsole object, to run commands on the host etc.
        '''
        return self.console

    def ipmi_sdr_clear(self):
        '''
        This function clears the sensor data.
        '''
        output = self.ipmitool.run('sel clear')
        if 'Clearing SEL' in output:
            # FIXME: This code should instead check for 'erasure completed'
            #        and the status of the erasure, rather than this crude loop
            retries = 20
            while (retries > 0):
                output = self.ipmitool.run('sel elist')
                if 'no entries' in output:
                    return BMC_CONST.FW_SUCCESS
                else:
                    l_msg = "Sensor data still has entries!"
                    log.error(l_msg)
                    retries -= 1
                    if (retries == 0):
                        raise OpTestError(l_msg)
                    time.sleep(1)
        else:
            l_msg = "Clearing the sensor data Failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_power_off(self):
        '''
        This function sends the chassis power off ipmitool command.
        '''
        output = self.ipmitool.run('chassis power off')
        if 'Down/Off' in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power OFF Failed: {}".format(output)
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_power_on(self):
        '''
        This function sends the chassis power on ipmitool command
        '''
        output = self.ipmitool.run('chassis power on')
        if 'Up/On' in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power ON Failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_power_soft(self):
        '''
        This function sends the chassis power soft ipmitool command
        '''
        output = self.ipmitool.run('chassis power soft')
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        if "Chassis Power Control: Soft" in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power Soft Failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_power_cycle(self):
        '''
        This function sends the chassis power cycle ipmitool command
        '''
        output = self.ipmitool.run('chassis power cycle')
        time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        if "Chassis Power Control: Cycle" in output:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power Cycle Failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_power_reset(self):
        '''
        This function sends the chassis power reset ipmitool command.
        '''
        r = self.ipmitool.run('chassis power reset')
        self.console.close()
        if not BMC_CONST.CHASSIS_POWER_RESET in r:
            raise Exception("IPMI 'chassis power reset' failed: %s " % r)

    def ipmi_power_diag(self):
        '''
        This function sends the chassis power diag ipmitool command.
        '''
        r = self.ipmitool.run('chassis power diag')
        if not "Chassis Power Control: Diag" in r:
            raise Exception("IPMI 'chassis power diag' failed: %s " % r)

    ##
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def ipl_wait_for_working_state(self, timeout=10):
        '''
        This function starts the sol capture and waits for the IPL to end. The
        marker for IPL completion is the Host Status sensor which reflects the ACPI
        power state of the system.  When it reads S0/G0: working it means that the
        petitboot is has began loading.  The overall timeout for the IPL is defined
        in the test configuration options.

        :param timeout: The number of minutes to wait for IPL to complete,
                        i.e. How long to poll the ACPI sensor for working
                        state before giving up.
        :type timeout: int
        '''
        timeout = time.time() + 60 * timeout
        cmd = 'sdr elist |grep \'Host Status\''
        output = self.ipmitool.run(cmd)
        if not "Host Status" in output:
            return BMC_CONST.FW_PARAMETER
        while True:
            output = self.ipmitool.run(cmd)
            if 'S0/G0: working' in output:
                log.debug("Host Status is S0/G0: working, IPL finished")
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(5)

        try:
            self.ipmitool.run('sol deactivate')
            self.console.close()
        except subprocess.CalledProcessError:
            l_msg = 'SOL already deactivated'
            log.error(l_msg)
            self.console.close()
            raise OpTestError(l_msg)
        return BMC_CONST.FW_SUCCESS

    def ipmi_ipl_wait_for_working_state_v1(self, timeout=10):
        '''
        This function waits for the IPL to end. The marker for IPL completion is the
        Host Status sensor which reflects the ACPI power state of the system.  When it
        reads S0/G0: working it means that the petitboot is has began loading.

        :param timeout: The number of minutes to wait for IPL to complete,
                        i.e. How long to poll the ACPI sensor for working
                        state before giving up.
        :type timeout: int
        '''
        timeout = time.time() + 60 * timeout
        cmd = 'sdr elist |grep \'Host Status\''
        output = self.ipmitool.run(cmd)
        if not "Host Status" in output:
            return BMC_CONST.FW_PARAMETER

        while True:
            if 'S0/G0: working' in output:
                log.debug("Host Status is S0/G0: working, IPL finished")
                break
            if time.time() > timeout:
                l_msg = "IPL timeout"
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(5)
            output = self.ipmitool.run(cmd)
        return BMC_CONST.FW_SUCCESS

    def ipmi_wait_for_standby_state(self, i_timeout=120):
        '''
        This function waits for system to reach standby state or soft off. The
        marker for standby state is the Host Status sensor which reflects the ACPI
        power state of the system.  When it reads S5/G2: soft-off it means that the
        system reached standby or soft-off state. The overall timeout for the standby is defined
        in the test configuration options.

        :param i_timeout: The number of seconds to wait for system to reach standby,
                          i.e. How long to poll the ACPI sensor for soft-off
                          state before giving up.
        :type i_timeout: int
        '''
        l_timeout = time.time() + i_timeout
        l_cmd = ' power status '
        wait_for = 'Chassis Power is off'
        while True:
            l_output = self.ipmitool.run(l_cmd)
            if wait_for in l_output:
                log.debug("Host power is off, system reached standby")
                break
            if time.time() > l_timeout:
                l_msg = "Standby timeout"
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_STANDBY_DELAY)

        return BMC_CONST.FW_SUCCESS

    def ipmi_wait_for_os_boot_complete(self, i_timeout=10):
        '''
        This function waits for the Host OS Boot(IPL) to end. The
        marker for IPL completion is the OS Boot sensor which reflects status
        of host OS Boot. When it reads boot completed it means that the
        Host OS Booted successfully.  The overall timeout for the IPL is defined
        in the test configuration options.

        :param i_timeout: The number of minutes to wait for IPL to complete or Boot time,
                          i.e. How long to poll the OS Boot sensor for working
                          state before giving up.
        :type i_timeout: int
        '''
        l_timeout = time.time() + 60 * i_timeout
        l_cmd = 'sdr elist |grep \'OS Boot\''
        output = self.ipmitool.run(l_cmd)
        if not "OS Boot" in output:
            return BMC_CONST.FW_PARAMETER
        while True:
            l_output = self.ipmitool.run(l_cmd)
            if BMC_CONST.OS_BOOT_COMPLETE in l_output:
                log.debug("Host OS is booted")
                break
            if time.time() > l_timeout:
                l_msg = "IPL timeout"
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)

        return BMC_CONST.FW_SUCCESS

    def ipmi_wait_for_os_boot_complete_v1(self, i_timeout=10):
        '''
        This function waits for the Host OS Boot(IPL) to end. The
        marker for IPL completion is the OS Boot sensor which reflects status
        of host OS Boot. When it reads boot completed it means that the
        Host OS Booted successfully.

        :param i_timeout: The number of minutes to wait for IPL to complete or Boot time,
              i.e. How long to poll the OS Boot sensor for working state before giving up.
        :type i_timeout: int
        '''
        l_timeout = time.time() + 60 * i_timeout
        l_cmd = 'sdr elist |grep \'OS Boot\''
        l_output = self.ipmitool.run(l_cmd)
        if not "OS Boot" in l_output:
            return BMC_CONST.FW_PARAMETER

        while True:
            if BMC_CONST.OS_BOOT_COMPLETE in l_output:
                log.debug("Host OS is booted")
                break
            if time.time() > l_timeout:
                l_msg = "IPL timeout"
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)
            l_output = self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    def ipmi_sel_check(self, i_string="Transition to Non-recoverable"):
        '''
        This function dumps the sel log and looks for specific hostboot error
        log string
        '''
        output = self.ipmitool.run('sel elist')

        if self.logfile:
            for line in output:
                self.logfile.write(line)

        if i_string in output:
            raise OpTestError('Error log(s) detected during IPL: %s' % output)
        else:
            return BMC_CONST.FW_SUCCESS

    def ipmi_sel_elist(self, dump=False):
        '''
        This function dumps the sel elist
        '''
        output = self.ipmitool.run('sel elist')

        if dump:
            print "\n----------------------------------------------------------------------"
            print "SELs"
            print "----------------------------------------------------------------------"
            print "{}".format(output)
            print "----------------------------------------------------------------------"

        return output

    def ipmi_power_status(self):
        '''
        Determines the power status of the bmc

        :returns: string: Power status of bmc
                          "Chassis Power is on" or "Chassis Power is off"
        '''
        l_output = self.ipmitool.run('chassis power status')
        if ('on' in l_output):
            return BMC_CONST.CHASSIS_POWER_ON
        elif ('off' in l_output):
            return BMC_CONST.CHASSIS_POWER_OFF
        else:
            raise OpTestError("Can't recognize chassis power status: " +
                              str(l_output))

    def ipmi_cold_reset(self):
        '''
        Performs a cold reset onto the bmc
        '''
        l_initstatus = self.ipmi_power_status()
        log.debug("Applying Cold reset.")
        rc = self.ipmitool.run(BMC_CONST.BMC_COLD_RESET)
        if BMC_CONST.BMC_PASS_COLD_RESET in rc:
            self.console.close()
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)
            self.util.PingFunc(
                self.cv_bmcIP,
                totalSleepTime=BMC_CONST.PING_RETRY_FOR_STABILITY)
            self.ipmi_wait_for_bmc_runtime()
            l_finalstatus = self.ipmi_power_status()
            if (l_initstatus != l_finalstatus):
                log.debug('initial status ' + str(l_initstatus))
                log.debug('final status ' + str(l_finalstatus))
                log.debug('Power status changed during cold reset')
                raise OpTestError('Power status changed')
            return BMC_CONST.FW_SUCCESS
        else:
            log.error("Cold reset failed, rc={}".format(rc))
            raise OpTestError(rc)

    def ipmi_wait_for_bmc_runtime(self, i_timeout=10):
        '''
        This function waits until BMC Boot finishes after a BMC Cold reset

        0x00
          Boot Complete
        0xC0
          Not Complete

        Here AMI systems returns 00 and SMC Systems return NULL for success
        '''
        l_timeout = time.time() + 60 * i_timeout
        l_cmd = ' raw 0x3a 0x0a '
        while True:
            try:
                l_output = self.ipmitool.run(l_cmd)
                log.debug(l_output)
            except:
                continue
            if "0xc0" in l_output:
                log.info("BMC Still booting...")
            elif "00" in l_output:  # AMI BMC returns 00 as output
                log.info("BMC Completed booting...")
                break
            else:  # SMC BMC returns empty as output
                log.info("BMC Completed booting...")
                break
            if time.time() > l_timeout:
                l_msg = "BMC Boot timeout..."
                log.error(l_msg)
                raise OpTestError(l_msg)
            time.sleep(BMC_CONST.SHORT_WAIT_IPL)
        return BMC_CONST.FW_SUCCESS

    def ipmi_warm_reset(self):
        '''
        Performs a warm reset onto the bmc
        '''
        l_initstatus = self.ipmi_power_status()
        l_cmd = BMC_CONST.BMC_WARM_RESET
        log.info("Applying Warm reset. Wait for " +
                 str(BMC_CONST.BMC_WARM_RESET_DELAY) + "sec")
        rc = self.ipmitool.run(l_cmd)
        if BMC_CONST.BMC_PASS_WARM_RESET in rc:
            log.info("Warm reset result: {}".format(rc))
            self.console.close()
            time.sleep(BMC_CONST.BMC_WARM_RESET_DELAY)
            self.util.PingFunc(
                self.cv_bmcIP,
                totalSleepTime=BMC_CONST.PING_RETRY_FOR_STABILITY)
            l_finalstatus = self.ipmi_power_status()
            if (l_initstatus != l_finalstatus):
                log.debug('initial status ' + str(l_initstatus))
                log.debug('final status ' + str(l_finalstatus))
                log.debug('Power status changed during cold reset')
                raise OpTestError('Power status changed')
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Warm reset Failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_preserve_network_setting(self):
        '''
        Preserves the network setting
        '''
        log.info("Protecting BMC network setting")
        l_cmd = BMC_CONST.BMC_PRESRV_LAN
        rc = self.ipmitool.run(l_cmd)

        if BMC_CONST.BMC_ERROR_LAN in rc:
            l_msg = "Can't protect setting! Please preserve setting manually"
            log.error(l_msg)
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS

    def ipmi_code_update(self, i_image, i_imagecomponent):
        '''
        Flashes image using ipmitool

        :param i_image: hpm file including location
        :param i_imagecomponent: component to be updated from the hpm file
                                 BMC_CONST.BMC_FW_IMAGE_UPDATE
                                 or BMC_CONST.BMC_PNOR_IMAGE
        '''
        self.ipmi_cold_reset()
        time.sleep(5)
        l_cmd = BMC_CONST.BMC_HPM_UPDATE + i_image + " " + i_imagecomponent
        # We need to do a re-try of hpm code update if it fails for the first time
        # As AMI systems are some times failed to flash for the first time.
        count = 0
        while (count < 2):
            self.ipmi_preserve_network_setting()
            time.sleep(5)
            rc = self.ipmitool.run(l_cmd,
                                   background=False,
                                   cmdprefix="echo y |")
            log.info("IPMI code update result: {}".format(rc))
            if (rc.__contains__("Firmware upgrade procedure successful")):
                return BMC_CONST.FW_SUCCESS
            elif count == 1:
                l_msg = "Code Update Failed"
                log.error(l_msg)
                raise OpTestError(l_msg)
            else:
                count = count + 1
                continue

    def ipmi_get_side_activated(self):
        '''
        Get information on active sides for both BMC and PNOR

        - 0x0080 indicates primary side is activated
        - 0x0180 indicates golden side is activated

        :returns: the active sides for BMC and PNOR chip (that are either primary of golden)
                  l_bmc_side, l_pnor_side
        '''

        l_result = self.ipmitool.run(
            BMC_CONST.BMC_ACTIVE_SIDE).strip().split('\n')

        for i in range(len(l_result)):
            if ('BIOS' in l_result[i]):
                if (l_result[i].__contains__(BMC_CONST.PRIMARY_SIDE)):
                    log.info("Primary side of PNOR is active")
                    l_pnor_side = BMC_CONST.PRIMARY_SIDE
                elif (BMC_CONST.GOLDEN_SIDE in l_result[i]):
                    log.info("Golden side of PNOR is active")
                    l_pnor_side = BMC_CONST.GOLDEN_SIDE
                else:
                    l_msg = "Error determining active side: " + l_result
                    log.error(l_msg)
                    raise OpTestError(l_msg)
            elif ('BMC' in l_result[i]):
                if (l_result[i].__contains__(BMC_CONST.PRIMARY_SIDE)):
                    log.info("Primary side of BMC is active")
                    l_bmc_side = BMC_CONST.PRIMARY_SIDE
                elif (BMC_CONST.GOLDEN_SIDE in l_result[i]):
                    log.info("Golden side of BMC is active")
                    l_bmc_side = BMC_CONST.GOLDEN_SIDE
                else:
                    l_msg = "Error determining active side: " + l_result
                    log.error(l_msg)
                    raise OpTestError(l_msg)
            else:
                l_msg = "Error determining active side: " + +l_result
                log.error(l_msg)
                raise OpTestError(l_msg)

        return l_bmc_side, l_pnor_side

    def ipmi_get_PNOR_level(self):
        '''
        Get PNOR level
        '''
        l_rc = self.ipmitool.run(BMC_CONST.BMC_MCHBLD)
        return l_rc

    def ipmi_get_bmc_golden_side_version(self):
        '''
        This function gets the BMC golden side firmware version.
        Below are the fields to decode the version of firmware

        1. Completion code

           - 0x00 – success
           - 0x81 – Not a valid image in golden SPI.
           - 0xff – Reading Golden side SPI failed.

        2. Device ID (0x02)
        3. IPMI Dev version (0x01)
        4. Firmware Revision 1 (Major )
        5. Firmware Revision 2 (Minor)
        6. IPMI Version
        7. Additional Device support refer IPMI spec.
        8. Manufacture ID
        9. Manufacture ID
        10. Manufacture ID
        11. Product ID
        12. Product ID
        13. Auxiliary Firmware Revision
        14. Auxiliary Firmware Revision
        15. Auxiliary Firmware Revision
        16. Auxiliary Firmware Revision

        :returns: BMC golden side firmware version

        ::

         ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x1a
                   20 01 02 16 02 bf 00 00 00 bb aa 4d 4c 01 00
        '''
        log.debug("IPMI: Getting the BMC Golden side version")
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_BMC_GOLDEN_SIDE_VERSION)
        return l_rc

    def ipmi_get_pnor_partition_size(self, i_part):
        '''
        This function gets the size of pnor partition

        :param i_part: partition name to retrieve the size.
                       partition can be NVRAM, GUARD and BOOTKERNEL

        TODO: Add support for remaining partitions.

        :returns: size of partition raise OpTestError when fails

        ::
          ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x0c
          0x42 0x4f 0x4f 0x54 0x4b 0x45 0x52 0x4e 0x45 0x4c 0x00 0x00 0x00 0x00 0x0 0x0 0x0
          00 00 f0 00

        This function will return "00 00 f0 00"
        '''
        log.debug("IPMI: Getting the size of %s PNOR Partition" % i_part)
        if i_part == BMC_CONST.PNOR_NVRAM_PART:
            l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_NVRAM_PARTITION_SIZE)
        elif i_part == BMC_CONST.PNOR_GUARD_PART:
            l_rc = self.ipmitool.run(BMC_CONST.IPMI_GET_GUARD_PARTITION_SIZE)
        elif i_part == BMC_CONST.PNOR_BOOTKERNEL_PART:
            l_rc = self.ipmitool.run(
                BMC_CONST.IPMI_GET_BOOTKERNEL_PARTITION_SIZE)
        else:
            l_msg = "please provide valid partition eye catcher name ({} is invalid)".format(
                i_part)
            log.error(l_msg)
            raise OpTestError(l_msg)
        return l_rc

    def ipmi_get_bmc_boot_completion_status(self):
        '''
        This function is used to check whether BMC Completed Booting.

        BMC Booting Status:

        00h
          Booting Completed.
        C0h
          Still Booting.

        :returns: output of command or raise OpTestError

        ::

            ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x0a
              00

        It returns 00 if BMC completed booting else it gives C0
        '''
        log.debug("IPMI: Getting the BMC Boot completion status")
        l_res = self.ipmitool.run(BMC_CONST.IPMI_HAS_BMC_BOOT_COMPLETED)
        return l_res

    def ipmi_get_fault_led_state(self):
        '''
        This command is used to get the State of Fault RollUP LED. ::

                Fault RollUP LED      0x00

        :returns: output of command or raise OpTestError

        ::
            ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x00
                00

            LED State Table: Below are the states it can get
                    0x0  LED OFF
                    0x1  LED ON
                    0x2  LED Standby Blink Rate
                    0x3  LED Slow Blink rate.
        '''
        log.debug("IPMI: Getting the fault rollup LED state")
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_FAULT_ROLLUP)
        return l_res

    def ipmi_get_power_on_led_state(self):
        '''
        This command is used to get the State of Power ON LED. ::

          Power ON LED      0x01

        :returns: output of command or raise OpTestError

        ::

           ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x01
             01

           LED State Table: Below are the states it can get
                    0x0  LED OFF
                    0x1  LED ON
                    0x2  LED Standby Blink Rate
                    0x3  LED Slow Blink rate.
        '''
        log.debug("IPMI: Getting the Power ON LED state")
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_POWER_ON)
        return l_res

    def ipmi_get_host_status_led_state(self):
        '''
        This command is used to get the State of Host Status LED. ::

                Host Status LED       0x02

        :returns: output of command or raise OpTestError

        ::

          ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x02
            00

          LED State Table: Below are the states it can get
                   0x0  LED OFF
                   0x1  LED ON
                   0x2  LED Standby Blink Rate
                   0x3  LED Slow Blink rate.
        '''
        log.debug("IPMI: Getting the Host status LED state")
        l_res = self.ipmitool.run(BMC_CONST.IPMI_GET_LED_STATE_HOST_STATUS)
        return l_res

    def ipmi_get_chassis_identify_led_state(self):
        '''
        This command is used to get the State of Chassis Identify LED. ::

           Chassis Identify LED  0x03

        :returns: output of command or raise OpTestError

        ::

          ipmitool -I lanplus -H <BMC-IP> -U ADMIN -P admin raw 0x3a 0x02 0x03
            00

          LED State Table: Below are the states it can get
                   0x0  LED OFF
                   0x1  LED ON
                   0x2  LED Standby Blink Rate
                   0x3  LED Slow Blink rate.
        '''
        log.debug("IPMI: Getting the Chassis Identify LED state")
        l_res = self.ipmitool.run(
            BMC_CONST.IPMI_GET_LED_STATE_CHASSIS_IDENTIFY)
        return l_res

    def ipmi_set_led_state(self, i_led, i_state):
        '''
        This function is used to set the state of a given LED.

        :param i_led: led number to set the state.
                      e.g.: LED Number Table to use.

                      ===================== ====
                      ===================== ====
                      Fault RollUP LED      0x00
                      Power ON LED          0x01
                      Host Status LED       0x02
                      Chassis Identify LED  0x03
                      ===================== ====

        :param i_state: state of led to set.
                        LED State to be set.

                        === =======================
                        === =======================
                        0x0 LED OFF
                        0x1 LED ON
                        0x2 LED Standby Blink Rate
                        0x3 LED Slow Blink rate.
                        === =======================
        '''
        l_led = i_led
        l_state = i_state
        log.debug("IPMI: Setting the %s LED with %s state" % (l_led, l_state))
        l_cmd = "raw 0x3a 0x03 %s %s" % (l_led, l_state)
        l_res = self.ipmitool.run(l_cmd)
        if "00" in l_res:
            log.debug("Set LED state got success")
            return BMC_CONST.FW_SUCCESS
        elif "0x90" in l_res:
            log.error("Invalid LED number (i_led={},i_state={}) res:{}".format(
                i_led, i_state, l_les))
        else:
            l_msg = "IPMI: Set LED state failed (i_led={},i_state={}) res:{}".format(
                i_led, i_state, l_les)
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_enable_fan_control_task_command(self):
        '''
        This function is used to enable Fan control Task thread running on BMC
        Ex: ``ipmitool raw 0x3a 0x12 0x01``

        :returns: return code of command or raise OpTestError when fails

        Completion Code:

        00h
          success
        cch
          Invalid Request Data
        83h
          File not created in start case.
        80h
          Invalid Operation Mode
        '''
        log.debug("IPMI: Enabling the Fan control task thread state")
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_ENABLE_FAN_CONTROL_TASK_THREAD)
        return l_rc

    def ipmi_disable_fan_control_task_command(self):
        '''
        This function is used to disable Fan control Task thread running on BMC
        Ex: ``ipmitool raw 0x3a 0x12 0x00``

        :returns: return code of command or raise OpTestError when fails

        Completion Code:

        00h
          success
        cch
          Invalid Request Data
        83h
          File not created in start case.
        80h
          Invalid Operation Mode
        '''
        log.debug("IPMI: Disabling the Fan control task thread state")
        l_rc = self.ipmitool.run(
            BMC_CONST.IPMI_DISABLE_FAN_CONTROL_TASK_THREAD)
        return l_rc

    def ipmi_get_fan_control_task_state_command(self):
        '''
        This function is used to get the state of fan control task thread.
        Ex: ``ipmitool -I lanplus -U admin -P admin -H <BMC IP> raw 0x3a 0x13``
            returns 00 or 01, depending on state.

        :returns: IPMI_FAN_CONTROL_THREAD_RUNNING = "01",
                  IPMI_FAN_CONTROL_THREAD_NOT_RUNNING = "00"
                  If fan control loop is running it will return "01"
                  else it will return "00"
        '''
        log.debug("IPMI: Getting the state of fan control task thread")
        l_rc = self.ipmitool.run(BMC_CONST.IPMI_FAN_CONTROL_TASK_THREAD_STATE)
        if BMC_CONST.IPMI_FAN_CONTROL_THREAD_NOT_RUNNING in l_rc:
            log.info("IPMI: Fan control task thread state is not running")
            l_state = BMC_CONST.IPMI_FAN_CONTROL_THREAD_NOT_RUNNING
        elif BMC_CONST.IPMI_FAN_CONTROL_THREAD_RUNNING in l_rc:
            log.info("IPMI: Fan control task thread state is running")
            l_state = BMC_CONST.IPMI_FAN_CONTROL_THREAD_RUNNING
        else:
            l_msg = "IPMI: Invalid response from fan control thread state command"
            raise OpTestError(l_msg)
        return l_state

    def ipmi_set_power_limit(self, i_powerlimit):
        '''
        set power limit of bmc

        :param i_powerlimit: power limit to be set at BMC
        :type i_powerlimit: int
        '''
        l_rc = self.ipmitool.run(BMC_CONST.SET_POWER_LIMIT + i_powerlimit)
        if (i_powerlimit not in l_rc):
            raise OpTestError(l_rc)

    def ipmi_get_power_limit(self):
        '''
        Determines the power limit on the bmc

        :returns: current power limit on bmc
        '''
        l_powerlimit = self.ipmitool.run(BMC_CONST.GET_POWER_LIMIT)
        return l_powerlimit

    def ipmi_activate_power_limit(self):
        '''
        Activates the power limit of the target bmc
        '''
        l_rc = self.ipmitool.run(BMC_CONST.DCMI_POWER_ACTIVATE)
        if (BMC_CONST.POWER_ACTIVATE_SUCCESS in l_rc):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power limit activation failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_deactivate_power_limit(self):
        '''
        Deactivates the power limit of the target bmc
        '''
        l_rc = self.ipmitool.run(BMC_CONST.DCMI_POWER_DEACTIVATE)
        if (BMC_CONST.POWER_DEACTIVATE_SUCCESS in l_rc):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Power limit deactivation failed. " \
                    "Make sure a power limit is set before activating it"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def ipmi_enable_all_occ_sensor(self):
        '''
        Enable OCC Sensor
        '''
        l_status = self.ipmi_get_occ_status()
        # example ssample: OCC Active | 08h | ok  | 210.0 |)

        # Get sensor ids to enable all OCCs
        l_status = l_status.rsplit("\n", 1)[0].split("\n")
        for i in range(len(l_status)):
            l_sensor_id = l_status[i].split("|")[1].strip()[:2]
            self.ipmitool.run(BMC_CONST.BMC_OCC_SENSOR + l_sensor_id +
                              BMC_CONST.BMC_ENABLE_OCC)

        # Wait for OCC to stabilize
        time.sleep(BMC_CONST.OCC_ENABLE_WAIT)
        return BMC_CONST.FW_SUCCESS

    def ipmi_disable_all_occ_sensor(self):
        '''
        Disable OCC Sensor
        '''
        l_status = self.ipmi_get_occ_status()

        # Get sensor ids to disable all OCCs
        l_status = l_status.rsplit("\n", 1)[0].split("\n")
        for i in range(len(l_status)):
            l_sensor_id = l_status[i].split("|")[1].strip()[:2]
            self.ipmitool.run(BMC_CONST.BMC_OCC_SENSOR + l_sensor_id +
                              BMC_CONST.BMC_DISABLE_OCC)

        return BMC_CONST.FW_SUCCESS

    def ipmi_get_occ_status(self):
        '''
        Get OCC status

        example: ::

           OCC 1 Active     | 08h | ok  | 210.0 | Device Enabled
           OCC 2 Active     | 09h | ok  | 210.1 | Device Enabled

        :returns: OCC sensor status or raise OpTestError
        '''
        l_result = self.ipmitool.run(BMC_CONST.OP_CHECK_OCC)
        if ("Device" not in l_result):
            l_msg = "Can't recognize output"
            log.error(l_msg + ": " + l_result)
            raise OpTestError(l_msg)

        return l_result

    def ipmi_get_sel_list(self):
        '''
        This function gets the sel log
        '''
        return self.ipmitool.run(BMC_CONST.BMC_SEL_LIST)

    def ipmi_get_sdr_list(self):
        '''
        This function gets the sdr list
        '''
        return self.ipmitool.run(BMC_CONST.BMC_SDR_ELIST)

    def ipmi_set_pnor_primary_side(self, i_bios_sensor, i_boot_sensor):
        '''
        Sets BIOS sensor and BOOT count to boot pnor from the primary side

        :param i_bios_sensor: Id for BIOS Golden Sensor (example habanero=0x5c)
        :param i_boot_sensor: Id for BOOT Count Sensor (example habanero=80)
        '''
        log.info('\nSetting PNOR to boot into Primary Side')

        #Set the Boot Count sensor to 2
        l_cmd = BMC_CONST.BMC_BOOT_COUNT_2.replace('xx', i_boot_sensor)
        self.ipmitool.run(l_cmd)

        #Set the BIOS Golden Side sensor to 0
        l_cmd = BMC_CONST.BMC_BIOS_GOLDEN_SENSOR_TO_PRIMARY.replace(
            'xx', i_bios_sensor)
        self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    def ipmi_set_pnor_golden_side(self, i_bios_sensor, i_boot_sensor):
        '''
        Sets BIOS sensor and BOOT count to boot pnor from the golden side

        :param i_bios_sensor: Id for BIOS Golden Sensor (example habanero=0x5c)
        :param i_boot_sensor: Id for BOOT Count Sensor (example habanero=80)
        '''
        log.info('\nSetting PNOR to boot into Golden Side')

        #Set the Boot Count sensor to 2
        l_cmd = BMC_CONST.BMC_BOOT_COUNT_2.replace('xx', i_boot_sensor)
        self.ipmitool.run(l_cmd)

        #Set the BIOS Golden Side sensor to 1
        l_cmd = BMC_CONST.BMC_BIOS_GOLDEN_SENSOR_TO_GOLDEN.replace(
            'xx', i_bios_sensor)
        self.ipmitool.run(l_cmd)

        return BMC_CONST.FW_SUCCESS

    def ipmi_set_power_policy(self, i_policy):
        '''
        Sets auto-reboot policy with given policy(i_policy)

        :param i_policy: type of policy to be set(chassis policy <i_policy>)

                         - always-off
                         - always-on
                         - previous
        '''
        log.debug("IPMI: Setting the power policy to %s" % i_policy)
        l_cmd = "chassis policy %s" % i_policy
        l_res = self.ipmitool.run(l_cmd)
        log.debug(l_res)

    def ipmi_set_boot_to_petitboot(self):
        '''
        Set boot device to be boot to BIOS (i.e. petitboot)
        '''
        l_output = self.ipmitool.run('chassis bootdev bios')
        if ('Set Boot Device to bios' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to bios: " +
                              str(l_output))

    def ipmi_set_boot_to_disk(self):
        '''
        Set boot device to be boot to disk (i.e. OS)
        '''
        l_output = self.ipmitool.run('chassis bootdev disk')
        if ('Set Boot Device to disk' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to disk: " +
                              str(l_output))

    def ipmi_set_no_override(self):
        '''
        Set no boot override so that local config will be effective
        '''
        l_output = self.ipmitool.run('chassis bootdev none')
        if ('Set Boot Device to none' in l_output):
            return 0
        else:
            raise OpTestError("Failure setting bootdev to none: " +
                              str(l_output))

    def enter_ipmi_lockdown_mode(self):
        self.ipmitool.run('raw 0x32 0xf3 0x4c 0x4f 0x43 0x4b 0x00')

    def exit_ipmi_lockdown_mode(self):
        self.ipmitool.run('raw 0x32 0xF4 0x55 0x4e 0x4c 0x4f 0x43 0x4b 0x00')

    def last_sel(self):
        return self.ipmitool.run("sel list last 1")

    def sdr_get_watchdog(self):
        return self.ipmitool.run("sdr get \'Watchdog\'")

    def mc_get_watchdog(self):
        return self.ipmitool.run("mc watchdog get")

    def set_tpm_required(self):
        pass

    def clear_tpm_required(self):
        pass

    def is_tpm_enabled(self):
        pass

    def ipmi_get_golden_side_sensor_id(self):
        cmd = "sdr elist -v | grep -i 'BIOS Golden'"
        output = self.ipmitool.run(cmd)
        matchObj = re.search("BIOS Golden Side \((.*)\)", output)
        id = None
        if matchObj:
            id = matchObj.group(1)
        return id

    def ipmi_get_boot_count_sensor_id(self):
        cmd = "sdr elist -v | grep -i 'Boot Count'"
        output = self.ipmitool.run(cmd)
        matchObj = re.search("Boot Count \((.*)\)", output)
        id = None
        if matchObj:
            id = matchObj.group(1)
        return id
示例#44
0
class OpTestBMC():
    '''
    The main object for communicating with a BMC and taking actions with it.
    '''
    def __init__(self, ip=None, username=None, password=None,
                 logfile=sys.stdout, ipmi=None, rest=None,
                 web=None, check_ssh_keys=False, known_hosts_file=None):
        self.cv_bmcIP = ip
        self.cv_bmcUser = username
        self.cv_bmcPasswd = password
        self.cv_IPMI = ipmi
        self.rest = rest
        self.cv_WEB = web
        self.logfile = logfile
        self.check_ssh_keys = check_ssh_keys
        self.known_hosts_file = known_hosts_file
        self.ssh = OpTestSSH(ip, username, password, logfile, prompt=None,
                block_setup_term=0, check_ssh_keys=check_ssh_keys, known_hosts_file=known_hosts_file)
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()

    def set_system(self, system):
        self.ssh.set_system(system)

    def bmc_host(self):
        return self.cv_bmcIP

    def get_ipmi(self):
        '''
        Get an object that can be used to do things over IPMI
        '''
        return self.cv_IPMI

    def get_rest_api(self):
        '''
        OpenBMC specific REST API.
        '''
        return self.rest

    def get_host_console(self):
        return self.cv_IPMI.get_host_console()

    def run_command(self, command, timeout=60, retry=0):
        '''
        Run a command on the BMC itself.
        '''
        return self.ssh.run_command(command, timeout, retry)

    ##
    # @brief 
    #
    # @return BMC_CONST.FW_SUCCESS on success and
    #         raise OpTestError on failure
    #
    def reboot(self):
        '''
        This function issues the reboot command on the BMC console.  It then
        pings the BMC until it responds, which presumably means that it is done
        rebooting.

        :raises: :class:`common.OpTestError`
        '''
        retries = 0
        try:
            self.ssh.run_command('reboot')
        except SSHSessionDisconnected as e:
            pass
        except CommandFailed as e:
            pass
        self.ssh.close()
        log.info('Sent reboot command now waiting for reboot to complete...')
        # Wait for BMC to go down.
        self.util.ping_fail_check(self.cv_bmcIP)
        # Wait for BMC to ping back.
        self.util.PingFunc(self.cv_bmcIP, totalSleepTime=BMC_CONST.PING_RETRY_POWERCYCLE)
        # Ping the system until it reboots
        while True:
            try:
                subprocess.check_call(["ping", self.cv_bmcIP, "-c1"])
                break
            except subprocess.CalledProcessError as e:
                log.debug("Ping return code: ", e.returncode, "retrying...")
                retries += 1
                time.sleep(10)

            if retries > 10:
                l_msg = "Error. BMC is not responding to pings"
                log.error(l_msg)
                raise OpTestError(l_msg)

            log.info('BMC reboot complete.')

        return BMC_CONST.FW_SUCCESS

    def image_transfer(self, i_imageName, copy_as=None):
        '''
        This function copies the given image to the BMC /tmp dir.

        :param i_imageName: Local file to copy across
        :param copy_as: file name to copy to (in /tmp)
        :returns: Exit code of scp or rsync process
        '''
        img_path = i_imageName
        ssh_opts = ' -o PubkeyAuthentication=no '
        if not self.check_ssh_keys:
            ssh_opts = ssh_opts + ' -o StrictHostKeyChecking=no'
        elif self.known_hosts_file:
            ssh_opts = ssh_opts + ' -o UserKnownHostsFile=' + self.known_hosts_file

        rsync_cmd = 'rsync -P -v -e "ssh -k' + ssh_opts + '" %s %s@%s:/tmp' % (img_path, self.cv_bmcUser, self.cv_bmcIP)
        if copy_as:
            rsync_cmd = rsync_cmd + '/' + copy_as

        log.debug(rsync_cmd)
        rsync = pexpect.spawn(rsync_cmd)
        rsync.logfile = OpTestLogger.FileLikeLogger(log)
        rsync.expect('assword: ')
        rsync.sendline(self.cv_bmcPasswd)
        r = rsync.expect(['total size is', 'error while loading shared lib'], timeout=1800)
        if r == 1:
            # On AMI BMCs that are missing libacl.so.1 for rsync,
            # we have to fall back to "scp"...
            # which is actually SSH+dd because there's no scp
            # This is notable for Palmetto
            log.debug("Falling back to SCP")
            if copy_as is None:
                copy_as = os.path.basename(img_path)
            scp_cmd = "bash -c \"sshpass -p {} ssh".format(self.cv_bmcPasswd) + ssh_opts + ' -o LogLevel=quiet'
            scp_cmd = scp_cmd + " {}@{} dd of=/tmp/{} < {}\"".format(self.cv_bmcUser, self.cv_bmcIP,copy_as,img_path)
            log.debug(scp_cmd)
            scp = pexpect.spawn(scp_cmd, timeout=120)
            scp.expect(pexpect.EOF)
            scp.wait()
            scp.close()
            chmod_cmd = "sshpass -p {} ssh {} {}@{} chmod +x /tmp/{}".format(self.cv_bmcPasswd, ssh_opts, self.cv_bmcUser, self.cv_bmcIP, copy_as)
            log.debug(chmod_cmd)
            chmod = pexpect.spawn(chmod_cmd)
            chmod.expect(pexpect.EOF)
            chmod.wait()
            chmod.close()
            return scp.exitstatus
        else:
            rsync.expect(pexpect.EOF)
            rsync.close()
            return rsync.exitstatus


    def pnor_img_flash_ami(self, i_pflash_dir, i_imageName):
        '''
        This function flashes the PNOR image using pflash tool,
        And this function will work based on the assumption that pflash
        tool available in i_pflash_dir. Depending on the BMC type (and even
        *version* of BMC firmware) we may have had to copy over pflash
        ourselves.

        :param i_pflash_dir: directory where pflash tool is present.
        :type i_pflash_dir: str
        :param i_imageName: Name of the image file.
                            e.g. `firestone.pnor` or `firestone_update.pnor`
        :type i_imageName: str

        :returns: pflash command return code
        '''
        cmd = i_pflash_dir + '/pflash -e -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def pnor_img_flash_openbmc(self, i_imageName):
        '''
        on openbmc systems pflash tool available
        '''
        cmd = 'pflash -E -f -p /tmp/%s' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_ami(self, i_pflash_dir, i_imageName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_ami(self, i_pflash_dir, i_imageName, i_partName):
        cmd = i_pflash_dir + '/pflash -p /tmp/%s -e -f -P %s' % (i_imageName, i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiboot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P PAYLOAD' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def skiroot_img_flash_openbmc(self, i_imageName):
        cmd = 'pflash -p /tmp/%s -e -f -P BOOTKERNEL' % i_imageName
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def flash_part_openbmc(self, i_imageName, i_partName):
        cmd = 'pflash -p /tmp/%s -e -f -P %s' % (i_imageName, i_partName)
        rc = self.ssh.run_command(cmd, timeout=1800)
        return rc

    def validate_pflash_tool(self, i_dir=""):
        '''
        This function validates presence of pflash tool, which will be
        used for pnor image flash

        :param i_dir: directory where pflash tool should be present.
        :returns: True/False
        '''
        i_dir = os.path.join(i_dir, "pflash")
        try:
            l_res = self.ssh.run_command("which %s" % i_dir)
        except CommandFailed:
            l_msg = "# pflash tool is not available on BMC"
            log.error(l_msg)
            return False
        return True

    def has_inband_bootdev(self):
        return True

    def has_os_boot_sensor(self):
        return True

    def has_host_status_sensor(self):
        return True

    def has_occ_active_sensor(self):
        return True

    def supports_ipmi_dcmi(self):
        return True

    def has_ipmi_sel(self):
        return True
class MamboConsole():
    '''
    A 'connection' to the Mambo Console involves *launching* Mambo.
    Closing a connection will *terminate* the Mambo process.
    '''
    def __init__(self, mambo_binary=None,
            mambo_initial_run_script=None,
            mambo_autorun=None,
            skiboot=None,
            prompt=None,
            kernel=None,
            initramfs=None,
            block_setup_term=None,
            delaybeforesend=None,
            logfile=sys.stdout):
        self.mambo_binary = mambo_binary
        self.mambo_initial_run_script = mambo_initial_run_script
        self.mambo_autorun = mambo_autorun
        self.skiboot = skiboot
        self.kernel = kernel
        self.initramfs = initramfs
        self.state = ConsoleState.DISCONNECTED
        self.logfile = logfile
        self.delaybeforesend = delaybeforesend
        self.system = None
        # OpTestUtil instance is NOT conf's
        self.util = OpTestUtil()
        self.prompt = prompt
        self.expect_prompt = self.util.build_prompt(prompt) + "$"
        self.pty = None
        self.block_setup_term = block_setup_term # allows caller specific control of when to block setup_term
        self.setup_term_quiet = 0 # tells setup_term to not throw exceptions, like when system off
        self.setup_term_disable = 0 # flags the object to abandon setup_term operations, like when system off

        # state tracking, reset on boot and state changes
        # console tracking done on System object for the system console
        self.PS1_set = -1
        self.LOGIN_set = -1
        self.SUDO_set = -1

    def set_system(self, system):
        self.system = system

    def set_system_setup_term(self, flag):
        self.system.block_setup_term = flag

    def get_system_setup_term(self):
        return self.system.block_setup_term

    def set_block_setup_term(self, flag):
        self.block_setup_term = flag

    def get_block_setup_term(self):
        return self.block_setup_term

    def enable_setup_term_quiet(self):
        self.setup_term_quiet = 1
        self.setup_term_disable = 0

    def disable_setup_term_quiet(self):
        self.setup_term_quiet = 0
        self.setup_term_disable = 0

    def close(self):
        self.util.clear_state(self)
        try:
            rc_child = self.pty.close()
            exitCode = signalstatus = None
            if self.pty.status != -1: # leaving for debug
              if os.WIFEXITED(self.pty.status):
                exitCode = os.WEXITSTATUS(self.pty.status)
              else:
                signalstatus = os.WTERMSIG(self.pty.status)
            self.state = ConsoleState.DISCONNECTED
        except pexpect.ExceptionPexpect as e:
            self.state = ConsoleState.DISCONNECTED
            raise "Mambo Console: failed to close console"
        except Exception as e:
            self.state = ConsoleState.DISCONNECTED
            pass
        log.debug("Mambo close -> TERMINATE")

    def connect(self):
        if self.state == ConsoleState.CONNECTED:
            return self.pty
        else:
            self.util.clear_state(self) # clear when coming in DISCONNECTED

        log.debug("#Mambo Console CONNECT")

        if not os.access(self.mambo_initial_run_script, os.R_OK|os.W_OK):
            raise ParameterCheck(message="Check that the file exists with"
                " R/W permissions mambo-initial-run-script={}"
                .format(self.mambo_initial_run_script))

        cmd = ("%s" % (self.mambo_binary)
               + " -e"
               + " -f {}".format(self.mambo_initial_run_script)
           )

        spawn_env = {}
        if self.skiboot:
            spawn_env['SKIBOOT'] = self.skiboot
        if self.kernel:
            spawn_env['SKIBOOT_ZIMAGE'] = self.kernel
        if self.initramfs:
            if not os.access(self.initramfs, os.R_OK|os.W_OK):
                raise ParameterCheck(message="Check that the file exists with"
                    " R/W permissions flash-initramfs={}"
                    .format(self.initramfs))
            spawn_env['SKIBOOT_INITRD'] = self.initramfs
        if self.mambo_autorun:
            spawn_env['SKIBOOT_AUTORUN'] = str(self.mambo_autorun)
        log.debug("OpTestMambo cmd={} mambo spawn_env={}".format(cmd, spawn_env))
        try:
          self.pty = OPexpect.spawn(cmd,
              logfile=self.logfile,
              env=spawn_env)
        except Exception as e:
          self.state = ConsoleState.DISCONNECTED
          raise CommandFailed('OPexpect.spawn',
                  'OPexpect.spawn encountered a problem: ' + str(e), -1)

        self.state = ConsoleState.CONNECTED
        self.pty.setwinsize(1000,1000)
        if self.delaybeforesend:
          self.pty.delaybeforesend = self.delaybeforesend

        if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
          self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        time.sleep(0.2)
        if not self.pty.isalive():
            raise CommandFailed(cmd, self.pty.read(), self.pty.status)
        return self.pty

    def get_console(self):
        if self.state == ConsoleState.DISCONNECTED:
            self.util.clear_state(self)
            self.connect()
        else:
            if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1:
                self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term)

        return self.pty

    def run_command(self, command, timeout=60, retry=0):
        return self.util.run_command(self, command, timeout, retry)

    def run_command_ignore_fail(self, command, timeout=60, retry=0):
        return self.util.run_command_ignore_fail(self, command, timeout, retry)

    def mambo_run_command(self, command, timeout=60, retry=0):
        return self.util.mambo_run_command(self, command, timeout, retry)

    def mambo_exit(self):
        return self.util.mambo_exit(self)

    def mambo_enter(self):
        return self.util.mambo_enter(self)
示例#46
0
class OpTestSystem(object):

    ## Initialize this object
    #  @param i_bmcIP The IP address of the BMC
    #  @param i_bmcUser The userid to log into the BMC with
    #  @param i_bmcPasswd The password of the userid to log into the BMC with
    #  @param i_bmcUserIpmi The userid to issue the BMC IPMI commands with
    #  @param i_bmcPasswdIpmi The password of BMC IPMI userid
    #  @param i_ffdcDir Optional param to indicate where to write FFDC
    #
    # "Only required for inband tests" else Default = None
    # @param i_hostIP The IP address of the Host
    # @param i_hostuser The userid to log into the Host
    # @param i_hostPasswd The password of the userid to log into the host with
    #
    def __init__(self,
                 i_ffdcDir=None,
                 bmc=None,
                 host=None,
                 state=OpSystemState.UNKNOWN):
        self.bmc = self.cv_BMC = bmc
        self.cv_HOST = host
        self.cv_IPMI = bmc.get_ipmi()
        self.rest = self.bmc.get_rest_api()
        self.console = self.bmc.get_host_console()
        self.util = OpTestUtil()

        # We have a state machine for going in between states of the system
        # initially, everything in UNKNOWN, so we reset things.
        # But, we allow setting an initial state if you, say, need to
        # run against an already IPLed system
        self.state = state
        self.stateHandlers = {}
        self.stateHandlers[OpSystemState.UNKNOWN] = self.run_UNKNOWN
        self.stateHandlers[OpSystemState.OFF] = self.run_OFF
        self.stateHandlers[OpSystemState.IPLing] = self.run_IPLing
        self.stateHandlers[OpSystemState.PETITBOOT] = self.run_PETITBOOT
        self.stateHandlers[
            OpSystemState.PETITBOOT_SHELL] = self.run_PETITBOOT_SHELL
        self.stateHandlers[OpSystemState.BOOTING] = self.run_BOOTING
        self.stateHandlers[OpSystemState.OS] = self.run_OS
        self.stateHandlers[OpSystemState.POWERING_OFF] = self.run_POWERING_OFF

        # We track the state of loaded IPMI modules here, that way
        # we only need to try the modprobe once per IPL.
        # We reset as soon as we transition away from OpSystemState.OS
        # a TODO is to support doing this in petitboot shell as well.
        self.ipmiDriversLoaded = False

    def skiboot_log_on_console(self):
        return True

    def has_host_accessible_eeprom(self):
        return True

    def has_host_led_support(self):
        return False

    def has_centaurs_in_dt(self):
        return True

    def has_mtd_pnor_access(self):
        return True

    def host(self):
        return self.cv_HOST

    def bmc(self):
        return self.cv_BMC

    def rest(self):
        return self.rest

    def ipmi(self):
        return self.cv_IPMI

    def get_state(self):
        return self.state

    def set_state(self, state):
        self.state = state

    def goto_state(self, state):
        print "OpTestSystem START STATE: %s (target %s)" % (self.state, state)
        while 1:
            self.state = self.stateHandlers[self.state](state)
            print "OpTestSystem TRANSITIONED TO: %s" % (self.state)
            if self.state == state:
                break

    def run_UNKNOWN(self, state):
        self.sys_power_off()
        return OpSystemState.POWERING_OFF

    def run_OFF(self, state):
        if state == OpSystemState.OFF:
            return OpSystemState.OFF
        if state == OpSystemState.UNKNOWN:
            raise "Can't trasition to UNKNOWN state"

        # We clear any possible errors at this stage
        self.sys_sdr_clear()

        if state == OpSystemState.OS:
            # By default auto-boot will be enabled, set no override
            # otherwise system endup booting in default disk.
            self.sys_set_bootdev_no_override()
            #self.cv_IPMI.ipmi_set_boot_to_disk()
        if state == OpSystemState.PETITBOOT or state == OpSystemState.PETITBOOT_SHELL:
            self.sys_set_bootdev_setup()

        r = self.sys_power_on()
        # Only retry once
        if r == BMC_CONST.FW_FAILED:
            r = self.sys_power_on()
            if r == BMC_CONST.FW_FAILED:
                raise 'Failed powering on system'
        return OpSystemState.IPLing

    def run_IPLing(self, state):
        if state == OpSystemState.OFF:
            self.sys_power_off()
            return OpSystemState.POWERING_OFF

        try:
            self.wait_for_petitboot()
        except pexpect.TIMEOUT:
            self.sys_sel_check()
            return OpSystemState.UNKNOWN

        # Once reached to petitboot check for any SEL events
        self.sys_sel_check()
        return OpSystemState.PETITBOOT

    def run_PETITBOOT(self, state):
        if state == OpSystemState.PETITBOOT:
            return OpSystemState.PETITBOOT
        if state == OpSystemState.PETITBOOT_SHELL:
            self.petitboot_exit_to_shell()
            return OpSystemState.PETITBOOT_SHELL

        if state == OpSystemState.OFF:
            self.sys_power_off()
            return OpSystemState.POWERING_OFF

        if state == OpSystemState.OS:
            self.wait_for_kexec()
            return OpSystemState.BOOTING

        raise 'Invalid state transition'

    def run_PETITBOOT_SHELL(self, state):
        if state == OpSystemState.PETITBOOT_SHELL:
            console = self.console.get_console()
            console.sendcontrol('l')
            return OpSystemState.PETITBOOT_SHELL

        if state == OpSystemState.PETITBOOT:
            self.exit_petitboot_shell()
            return OpSystemState.PETITBOOT_SHELL

        self.sys_power_off()
        return OpSystemState.POWERING_OFF

    def run_BOOTING(self, state):
        rc = self.wait_for_login()
        if rc != BMC_CONST.FW_FAILED:
            # Wait for ip to ping as we run host commands immediately
            self.util.PingFunc(self.cv_HOST.ip,
                               BMC_CONST.PING_RETRY_POWERCYCLE)
            return OpSystemState.OS
        raise 'Failed to boot'

    def run_OS(self, state):
        if state == OpSystemState.OS:
            return OpSystemState.OS
        self.ipmiDriversLoaded = False
        self.sys_power_off()
        return OpSystemState.POWERING_OFF

    def run_POWERING_OFF(self, state):
        rc = int(
            self.sys_wait_for_standby_state(
                BMC_CONST.SYSTEM_STANDBY_STATE_DELAY))
        if rc == BMC_CONST.FW_SUCCESS:
            msg = "System is in standby/Soft-off state"
        elif rc == BMC_CONST.FW_PARAMETER:
            msg = "Host Status sensor is not available/Skipping stand-by state check"
        else:
            l_msg = "System failed to reach standby/Soft-off state"
            raise OpTestError(l_msg)
        print msg
        self.cv_HOST.ssh.state = SSHConnectionState.DISCONNECTED
        return OpSystemState.OFF

    def load_ipmi_drivers(self, force=False):
        if self.ipmiDriversLoaded and not force:
            return

        # Get OS level
        l_oslevel = self.cv_HOST.host_get_OS_Level()

        # Get kernel version
        l_kernel = self.cv_HOST.host_get_kernel_version()

        # Checking for ipmitool command and package
        self.cv_HOST.host_check_command("ipmitool")

        l_pkg = self.cv_HOST.host_check_pkg_for_utility(l_oslevel, "ipmitool")
        print "Installed package: %s" % l_pkg

        # loading below ipmi modules based on config option
        # ipmi_devintf, ipmi_powernv and ipmi_masghandler
        self.cv_HOST.host_load_module_based_on_config(
            l_kernel, BMC_CONST.CONFIG_IPMI_DEVICE_INTERFACE,
            BMC_CONST.IPMI_DEV_INTF)
        self.cv_HOST.host_load_module_based_on_config(
            l_kernel, BMC_CONST.CONFIG_IPMI_POWERNV, BMC_CONST.IPMI_POWERNV)
        self.cv_HOST.host_load_module_based_on_config(
            l_kernel, BMC_CONST.CONFIG_IPMI_HANDLER,
            BMC_CONST.IPMI_MSG_HANDLER)
        self.ipmiDriversLoaded = True
        print "IPMI drivers loaded"
        return

    # Login to the host on the console
    # This will behave correctly even if already logged in
    def host_console_login(self):
        # we act on the raw pexpect console
        l_con = self.bmc.get_host_console().get_console()
        l_user = self.cv_HOST.username()
        l_pwd = self.cv_HOST.password()

        l_con.send("\r")
        l_rc = l_con.expect_exact(BMC_CONST.IPMI_CONSOLE_EXPECT_ENTER_OUTPUT,
                                  timeout=120)
        if l_rc == BMC_CONST.IPMI_CONSOLE_EXPECT_LOGIN:
            l_con.sendline(l_user)
            l_rc = l_con.expect(
                [r"[Pp]assword:", pexpect.TIMEOUT, pexpect.EOF], timeout=120)
            time.sleep(0.5)
            if l_rc == BMC_CONST.IPMI_CONSOLE_EXPECT_PASSWORD:
                l_con.sendline(l_pwd)
            else:
                l_msg = "Error: host login failed"
                raise OpTestError(l_msg)
        elif l_rc in BMC_CONST.IPMI_CONSOLE_EXPECT_PETITBOOT:
            l_msg = "Error: system is at petitboot"
            raise OpTestError(l_msg)
        elif l_rc in BMC_CONST.IPMI_CONSOLE_EXPECT_RANDOM_STATE:
            l_msg = "Error: system is in random state"
            raise OpTestError(l_msg)
        elif l_rc in ["#"]:
            # already at root prompt, success!
            return
        elif l_rc in [pexpect.TIMEOUT, pexpect.EOF]:
            print l_con.before
            raise "Timeout/EOF waiting for SOL response"
        elif l_rc in ["$"]:
            pass  # fallthrough and sudo into a root shell
        l_con.send("\r")
        l_rc = l_con.expect_exact(['$', '#'])
        if l_rc == 0:
            l_con.sendline('sudo -s')
            l_con.expect("password for")
            l_con.sendline(l_pwd)
            self.host_console_unique_prompt()
        elif l_rc != 1:
            raise Exception(
                "Invalid response to newline. expected $ or # prompt, got: %s"
                % (l_con.before))

        return BMC_CONST.FW_SUCCESS

    def host_console_unique_prompt(self):
        # We do things to the raw pexpect here
        # Must be logged in or at petitboot shell
        p = self.bmc.get_host_console().get_console()
        p.sendcontrol('l')
        p.expect(r'.+#')
        p.sendline('PS1=[console-pexpect]\#')
        p.expect("\n")  # from us, because echo
        l_rc = p.expect("\[console-pexpect\]#$")
        if l_rc == 0:
            print "Shell prompt changed"
        else:
            raise Exception("Failed during change of shell prompt")

    ############################################################################
    # System Interfaces
    ############################################################################

    ##
    # @brief Clear all SDR's in the System
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_sdr_clear(self):
        try:
            rc = self.cv_IPMI.ipmi_sdr_clear()
        except OpTestError as e:
            time.sleep(BMC_CONST.LONG_WAIT_IPL)
            print("Retry clearing SDR")
            try:
                rc = self.cv_IPMI.ipmi_sdr_clear()
            except OpTestError as e:
                return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Power on the system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_power_on(self):
        try:
            rc = self.cv_IPMI.ipmi_power_on()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Power cycle the system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_power_cycle(self):
        try:
            return self.cv_IPMI.ipmi_power_cycle()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Power soft the system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_power_soft(self):
        try:
            rc = self.cv_IPMI.ipmi_power_soft()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Power off the system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_power_off(self):
        try:
            rc = self.cv_IPMI.ipmi_power_off()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    def sys_set_bootdev_setup(self):
        self.cv_IPMI.ipmi_set_boot_to_petitboot()

    def sys_set_bootdev_no_override(self):
        self.cv_IPMI.ipmi_set_no_override()

    def sys_power_reset(self):
        self.cv_IPMI.ipmi_power_reset()

    ##
    # @brief Warm reset on the bmc system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_warm_reset(self):
        try:
            rc = self.cv_IPMI.ipmi_warm_reset()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Cold reset on the bmc system
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_cold_reset_bmc(self):
        try:
            rc = self.cv_IPMI.ipmi_cold_reset()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Cold reset on the Host
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_host_cold_reset(self):
        try:
            l_rc = self.sys_bmc_power_on_validate_host()
            if (l_rc != BMC_CONST.FW_SUCCESS):
                return BMC_CONST.FW_FAILED
            self.cv_HOST.host_cold_reset()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Wait for boot to end based on serial over lan output data
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_ipl_wait_for_working_state(self, i_timeout=10):
        try:
            rc = self.cv_IPMI.ipl_wait_for_working_state(i_timeout)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    def sys_ipmi_ipl_wait_for_login(self, i_timeout=10):
        l_con = self.console.get_console()
        try:
            rc = self.cv_IPMI.ipmi_ipl_wait_for_login(l_con, i_timeout)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Wait for system to reach standby or[S5/G2: soft-off]
    #
    # @param i_timeout @type int: The number of seconds to wait for system to reach standby,
    #       i.e. How long to poll the ACPI sensor for soft-off state before giving up.
    #
    # @return l_rc @type constant BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_wait_for_standby_state(self, i_timeout=120):
        try:
            l_rc = self.cv_IPMI.ipmi_wait_for_standby_state(i_timeout)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return l_rc

    ##
    # @brief Wait for system boot to host OS, It uses OS Boot sensor
    #
    # @param i_timeout @type int: The number of minutes to wait for IPL to complete or Boot time,
    #       i.e. How long to poll the OS Boot sensor for working state before giving up.
    #
    # @return l_rc @type constant BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_wait_for_os_boot_complete(self, i_timeout=10):
        try:
            l_rc = self.cv_IPMI.ipmi_wait_for_os_boot_complete(i_timeout)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return l_rc

    ##
    # @brief Check for error during IPL that would result in test case failure
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_sel_check(self, i_string="Transition to Non-recoverable"):
        try:
            rc = self.cv_IPMI.ipmi_sel_check(i_string)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return rc

    ##
    # @brief Reboot the BMC
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_reboot(self):
        try:
            rc = self.cv_BMC.reboot()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Validates the partition and waits for partition to connect
    #        important to perform before all inband communications
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_power_on_validate_host(self):
        # Check to see if host credentials are present
        if (self.cv_HOST.ip == None):
            l_msg = "Partition credentials not provided"
            print l_msg
            return BMC_CONST.FW_FAILED

        # Check if partition is active
        try:
            self.util.PingFunc(self.cv_HOST.ip, totalSleepTime=2)
            self.cv_HOST.host_get_OS_Level()
        except OpTestError as e:
            print("Trying to recover partition after error: %s" % (e))
            try:
                self.cv_IPMI.ipmi_power_off()
                self.sys_cold_reset_bmc()
                self.cv_IPMI.ipmi_power_on()
                self.sys_check_host_status()
                self.util.PingFunc(self.cv_HOST.ip,
                                   BMC_CONST.PING_RETRY_POWERCYCLE)
            except OpTestError as e:
                return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function reboots the system(Power off/on) and
    #        check for system status and wait for
    #        FW and Host OS Boot progress to complete.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def sys_hard_reboot(self):
        print "Performing a IPMI Power OFF Operation"
        self.cv_IPMI.ipmi_power_off()
        rc = int(
            self.sys_wait_for_standby_state(
                BMC_CONST.SYSTEM_STANDBY_STATE_DELAY))
        if rc == BMC_CONST.FW_SUCCESS:
            print "System is in standby/Soft-off state"
        elif rc == BMC_CONST.FW_PARAMETER:
            print "Host Status sensor is not available"
            print "Skipping stand-by state check"
        else:
            l_msg = "System failed to reach standby/Soft-off state"
            raise OpTestError(l_msg)
        print "Performing a IPMI Power ON Operation"
        self.cv_IPMI.ipmi_power_on()
        self.sys_check_host_status()
        self.util.PingFunc(self.cv_HOST.ip, BMC_CONST.PING_RETRY_POWERCYCLE)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function will check for system status and wait for
    #        FW and Host OS Boot progress to complete.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def sys_check_host_status(self):
        rc = int(self.sys_ipl_wait_for_working_state())
        if rc == BMC_CONST.FW_SUCCESS:
            print "System booted to working state"
        elif rc == BMC_CONST.FW_PARAMETER:
            print "Host Status sensor is not available"
            print "Skip wait for IPL runtime check"
        else:
            l_msg = "System failed to boot"
            raise OpTestError(l_msg)
        rc = int(self.sys_wait_for_os_boot_complete())
        if rc == BMC_CONST.FW_SUCCESS:
            print "System booted to Host OS"
        elif rc == BMC_CONST.FW_PARAMETER:
            print "OS Boot sensor is not available"
            print "Skip wait for wait for OS boot complete check"
        else:
            l_msg = "System failed to boot Host OS"
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function will check for system status and wait for
    #        FW and Host OS Boot progress to complete.
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def sys_check_host_status_v1(self):
        if int(self.cv_IPMI.ipmi_ipl_wait_for_working_state_v1()
               ) == BMC_CONST.FW_SUCCESS:
            print "System booted to working state"
        else:
            print "There is no Host Status sensor...."
        if int(self.cv_IPMI.ipmi_wait_for_os_boot_complete_v1()
               ) == BMC_CONST.FW_SUCCESS:
            print "System booted to Host OS"
        else:
            print "There is no OS Boot sensor..."

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Issue IPMI PNOR Reprovision request command
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_issue_ipmi_pnor_reprovision_request(self):
        try:
            self.cv_HOST.host_run_command(
                BMC_CONST.HOST_IPMI_REPROVISION_REQUEST)
        except OpTestError as e:
            print "Failed to issue ipmi pnor reprovision request"
            return BMC_CONST.FW_FAILED
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Wait for IPMI PNOR Reprovision to complete(response to 00)
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_wait_for_ipmi_pnor_reprovision_to_complete(self, timeout=10):
        l_res = ""
        timeout = time.time() + 60 * timeout
        while True:
            l_res = self.cv_HOST.host_run_command(
                BMC_CONST.HOST_IPMI_REPROVISION_PROGRESS)
            if "00" in l_res:
                print "IPMI: Reprovision completed"
                break
            if time.time() > timeout:
                l_msg = "Reprovision timeout, progress not reaching to 00"
                print l_msg
                raise OpTestError(l_msg)
            time.sleep(10)
        return BMC_CONST.FW_SUCCESS

    ###########################################################################
    # CODE-UPDATE INTERFACES
    ###########################################################################

    ##
    # @brief Update the BMC fw image using hpm file
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_outofband_fw_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_IPMI.ipmi_code_update(i_image,
                                          BMC_CONST.BMC_FW_IMAGE_UPDATE)
            return self.sys_bmc_power_on_validate_host()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Update the BMC pnor image using hpm file
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_outofband_pnor_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_IPMI.ipmi_code_update(i_image,
                                          BMC_CONST.BMC_PNOR_IMAGE_UPDATE)
            return self.sys_bmc_power_on_validate_host()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Update the BMC fw and pnor image using hpm file
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_outofband_fwandpnor_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_IPMI.ipmi_code_update(i_image,
                                          BMC_CONST.BMC_FWANDPNOR_IMAGE_UPDATE)
            return self.sys_bmc_power_on_validate_host()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Update the BMC fw using hpm file using HOST
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_inband_fw_update_hpm(self, i_image):

        try:
            #TODO: remove power_off() once DEFECT:SW325477 is fixed
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_cold_reset()
            l_rc = self.sys_bmc_power_on_validate_host()
            if (l_rc != BMC_CONST.FW_SUCCESS):
                return BMC_CONST.FW_FAILED
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_HOST.host_code_update(i_image,
                                          BMC_CONST.BMC_FW_IMAGE_UPDATE)
            self.cv_IPMI.ipmi_cold_reset()
        except OpTestError as e:
            self.sys_cold_reset_bmc()
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Update the BMC pnor using hpm file using HOST
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_inband_pnor_update_hpm(self, i_image):

        try:
            #TODO: remove power_off() once DEFECT:SW325477 is fixed
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_cold_reset()
            l_rc = self.sys_bmc_power_on_validate_host()
            if (l_rc != BMC_CONST.FW_SUCCESS):
                return BMC_CONST.FW_FAILED
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_HOST.host_code_update(i_image,
                                          BMC_CONST.BMC_PNOR_IMAGE_UPDATE)
            self.cv_HOST.host_cold_reset()
        except OpTestError as e:
            self.sys_cold_reset_bmc()
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Update the BMC fw and pnor using hpm file using Host
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_bmc_inband_fwandpnor_update_hpm(self, i_image):

        try:
            #TODO: remove power_off() once DEFECT:SW325477 is fixed
            self.cv_IPMI.ipmi_power_off()
            self.cv_IPMI.ipmi_cold_reset()
            l_rc = self.sys_bmc_power_on_validate_host()
            if (l_rc != BMC_CONST.FW_SUCCESS):
                return BMC_CONST.FW_FAILED
            self.cv_IPMI.ipmi_preserve_network_setting()
            self.cv_HOST.host_code_update(i_image,
                                          BMC_CONST.BMC_FWANDPNOR_IMAGE_UPDATE)
            self.cv_HOST.host_cold_reset()
        except OpTestError as e:
            self.sys_cold_reset_bmc()
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Update the PNOR image using hpm file through web GUI
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED or BMC_CONST.FW_INVALID
    #
    def sys_bmc_web_pnor_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.bmc.cv_WEB.web_update_hpm(i_image, BMC_CONST.UPDATE_PNOR)
        except OpTestError as e:
            if (e.args[0] == BMC_CONST.ERROR_SELENIUM_HEADLESS):
                return BMC_CONST.FW_INVALID
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Update the BMC image using hpm file through web GUI
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED or BMC_CONST.FW_INVALID
    #
    def sys_bmc_web_bmc_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.bmc.cv_WEB.web_update_hpm(i_image, BMC_CONST.UPDATE_BMC)
        except OpTestError as e:
            if (e.args[0] == BMC_CONST.ERROR_SELENIUM_HEADLESS):
                return BMC_CONST.FW_INVALID
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Update the BMC and PNOR image using hpm file through web GUI
    #
    # @param i_image HPM file image including location
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED or BMC_CONST.FW_INVALID
    #
    def sys_bmc_web_bmcandpnor_update_hpm(self, i_image):

        try:
            self.cv_IPMI.ipmi_power_off()
            self.bmc.cv_WEB.web_update_hpm(i_image,
                                           BMC_CONST.UPDATE_BMCANDPNOR)
        except OpTestError as e:
            if (e.args[0] == BMC_CONST.ERROR_SELENIUM_HEADLESS):
                return BMC_CONST.FW_INVALID
            return BMC_CONST.FW_FAILED

        return BMC_CONST.FW_SUCCESS

    ###########################################################################
    # Energy Scale
    ###########################################################################

    ##
    # @brief Sets power limit of BMC
    #
    # @param i_powerlimit @type int: power limit to be set at BMC
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_set_power_limit(self, i_powerlimit):
        try:
            self.cv_IPMI.ipmi_power_status()
            self.cv_IPMI.ipmi_set_power_limit(i_powerlimit)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Gets the Power Limit
    #
    # @return l_powerlimit @type int: current power limit on bmc
    #         or BMC_CONST.FW_FAILED
    #
    def sys_get_power_limit(self):
        try:
            return self.cv_IPMI.ipmi_get_power_limit()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Activates the power limit of the target bmc
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_activate_power_limit(self):
        try:
            return self.cv_IPMI.ipmi_activate_power_limit()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Dectivates the power limit of the target bmc
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_deactivate_power_limit(self):
        try:
            return self.cv_IPMI.ipmi_deactivate_power_limit()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Enable OCC Sensor
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    def sys_enable_all_occ_sensor(self):
        try:
            return self.cv_IPMI.ipmi_enable_all_occ_sensor()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Disable OCC Sensor
    #
    # @return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    def sys_disable_all_occ_sensor(self):
        try:
            return self.cv_IPMI.ipmi_disenable_all_occ_sensor()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc CPU sensors
    # example (CPU Core Func 1|0x0|discrete|0x8080|na|na|na|na|na|na)
    #         (CPU Core Temp 1|42.000|degrees C|ok|0.000|0.000|0.000|255.000|255.000|255.000)
    #
    # @return CPU sensor reading if successful or
    #         return BMC_CONST.FW_FAILED
    def read_cpu_occ_sensor(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_CHECK_CPU)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc Processor sensors
    # example (Mem Proc0 Pwr|0.000|Watts|ok|0.000|0.000|0.000|255.000|255.000|255.000)
    #
    # @return Processor sensor reading if successful or
    #         return BMC_CONST.FW_FAILED
    def read_processor_occ_sensor(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_CHECK_PROCESSOR)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc Fan sensors
    # example(Fan 1 |9000.000|RPM|ok|0.000|0.000|0.000|16500.000|16500.000|16500.000)
    #
    # @return Fan sensor reading if successful or
    #         return BMC_CONST.FW_FAILED
    def read_fan_occ_sensor(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_CHECK_FAN)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc DIMM sensors
    # example(DIMM Func 0|0x0|discrete|0x4080|na|na|na|na|na|na)
    #        (DIMM Temp 0|27.000|degrees C|ok|0.000|0.000|0.000|255.000|255.000|255.000)
    #
    # @return Fan sensor reading if successful or
    #         return BMC_CONST.FW_FAILED
    def read_dimm_occ_sensor(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_CHECK_DIMM)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read All BMC sensors
    #
    # @return BMC sensor readings if successful or
    #         return BMC_CONST.FW_FAILED
    def read_sensor_list(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(
                self.cv_IPMI.cv_baseIpmiCmd + BMC_CONST.OP_CHECK_SENSOR_LIST)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc power levels
    # example (Instantaneous power reading:                   297 Watts
    #          Minimum during sampling period:                284 Watts
    #          Maximum during sampling period:                317 Watts
    #          Average power reading over sample period:      297 Watts
    #          IPMI timestamp:                           Tue Nov 24 09:46:03 2015
    #          Sampling period:                          00010000 Milliseconds
    #          Power reading state is:                   activated)
    #
    # @return Power reading if successful or
    #         return BMC_CONST.FW_FAILED
    def read_power_level(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_GET_POWER)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read Bmc temperature levels of CPU and baseboard
    # example (Entity ID Entity Instance              Temp. Readings
    #         (CPU temperature sensors(41h)             0       +28 C)
    #         (Baseboard temperature sensors(42h)       0       +22 C)
    #
    # @return Operating temperature readings if successful or
    #         return BMC_CONST.FW_FAILED
    def read_temperature_level(self):
        try:
            return self.cv_IPMI._ipmitool_cmd_run(self.cv_IPMI.cv_baseIpmiCmd +
                                                  BMC_CONST.OP_GET_TEMP)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Read OCC status
    #
    # @return OCC status if successful or
    #         return BMC_CONST.FW_FAILED
    def read_occ_status(self):
        try:
            return self.cv_IPMI.ipmi_get_occ_status()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief clears the gard records using the gard tool from OS
    #
    # @param i_gard_dir @type string: directory where gard is installed
    #
    # return BMC.CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_clear_gard_records(self, i_gard_dir):
        try:
            self.sys_bmc_power_on_validate_host()
            self.cv_HOST.host_clear_gard_records(i_gard_dir)
            return BMC_CONST.FW_SUCCESS
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Lists all the gard records using the gard tool from OS
    #
    # @param i_gard_dir @type string: directory where gard is installed
    #
    # return gard records or BMC_CONST.FW_FAILED
    #
    def sys_list_gard_records(self, i_gard_dir):
        try:
            self.sys_bmc_power_on_validate_host()
            return self.cv_HOST.host_list_gard_records(i_gard_dir)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief reads getscom data
    #
    # @param i_xscom_dir @type string: directory where getscom is installed
    #
    # return getscom data or BMC_CONST.FW_FAILED
    #
    def sys_read_getscom_data(self, i_xscom_dir):
        try:
            self.sys_bmc_power_on_validate_host()
            return self.cv_HOST.host_read_getscom_data(i_xscom_dir)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief injects error using putscom
    #
    # @param i_xscom_dir @type string: directory where putscom is installed
    # @param i_error @type string: error to be injected including the location
    #
    # @return output generated after executing putscom command or BMC_CONST.FW_FAILED
    #
    def sys_putscom(self, i_xscom_dir, i_error):
        try:
            self.sys_bmc_power_on_validate_host()
            return self.cv_HOST.host_putscom(i_xscom_dir, i_error)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief This function gets the sel log
    #
    # @return sel list or BMC_CONST.FW_FAILED
    #
    def sys_get_sel_list(self):
        try:
            return self.cv_IPMI.ipmi_get_sel_list()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief This function gets the sdr list
    #
    # @return @type string: sdr list or BMC_CONST.FW_FAILED
    #
    def sys_get_sdr_list(self):
        try:
            return self.cv_IPMI.ipmi_get_sdr_list()
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Set BMC to boot pnor from primary side
    #
    # @param i_bios_sensor @type string: Id for BIOS Golden Sensor (example habanero=0x5c)
    # @param i_boot_sensor @type string: Id for BOOT Count Sensor (example habanero=80)
    #
    # return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_set_pnor_boot_primary(self, i_bios_sensor, i_boot_sensor):

        try:
            self.cv_IPMI.ipmi_set_pnor_primary_side(i_bios_sensor,
                                                    i_boot_sensor)
            return BMC_CONST.FW_SUCCESS
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Set BMC to boot pnor from golden side
    #
    # @param i_bios_sensor @type string: Id for BIOS Golden Sensor (example habanero=0x5c)
    # @param i_boot_sensor @type string: Id for BOOT Count Sensor (example habanero=80)
    #
    # return BMC_CONST.FW_SUCCESS or BMC_CONST.FW_FAILED
    #
    def sys_set_pnor_boot_golden(self, i_bios_sensor, i_boot_sensor):

        try:
            self.cv_IPMI.ipmi_set_pnor_golden_side(i_bios_sensor,
                                                   i_boot_sensor)
            return BMC_CONST.FW_SUCCESS
        except OpTestError as e:
            return BMC_CONST.FW_FAILED

    ##
    # @brief Determines which side of BMC and PNOR chip is active
    #
    # @return l_bmc_active and l_pnor_active @type string or return FWConstants.FW_FAILED
    #
    def sys_active_chip_side(self):
        try:
            l_bmc_active, l_pnor_active = self.cv_IPMI.ipmi_get_side_activated(
            )
            return l_bmc_active, l_pnor_active
        except OpTestError as e:
            return BMC_CONST.FW_FAILED, BMC_CONST.FW_FAILED

    ###########################################################################
    # IPMI Console interfaces
    ###########################################################################

    ##
    # @brief It will get the ipmi sol console
    #
    # @return l_con @type Object: it is a object of pexpect.spawn class or raise OpTestError
    #
    def sys_get_ipmi_console(self):
        self.l_con = self.bmc.get_host_console()
        return self.l_con

    ##
    # @brief This function is used to close ipmi console
    #
    # @param i_con @type Object: it is a object of pexpect.spawn class
    #                            this is the active ipmi sol console object
    #
    # @return BMC_CONST.FW_SUCCESS or return BMC_CONST.FW_FAILED
    #
    def sys_ipmi_close_console(self, i_con):
        try:
            l_con = i_con
            self.cv_IPMI.ipmi_close_console(l_con)
        except OpTestError as e:
            return BMC_CONST.FW_FAILED
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function is used to boot the system up to petitboot
    #        So any petitboot related test cases can use this function
    #        to setupt the petitboot
    #
    # @param i_con @type Object: it is a object of pexpect.spawn class
    #                            this is the active ipmi sol console object
    #
    # @return BMC_CONST.FW_SUCCESS or return BMC_CONST.FW_FAILED
    #
    def sys_ipmi_boot_system_to_petitboot(self):
        # Perform a IPMI Power OFF Operation(Immediate Shutdown)
        self.cv_IPMI.ipmi_power_off()
        if int(
                self.sys_wait_for_standby_state(
                    BMC_CONST.SYSTEM_STANDBY_STATE_DELAY)) == 0:
            print "System is in standby/Soft-off state"
        else:
            l_msg = "System failed to reach standby/Soft-off state"
            raise OpTestError(l_msg)

        self.cv_IPMI.ipmi_set_boot_to_petitboot()
        self.cv_IPMI.ipmi_power_on()

        self.wait_for_petitboot()
        self.petitboot_exit_to_shell()

    def wait_for_petitboot(self):
        console = self.console.get_console()
        try:
            # Wait for petitboot (for a *LOOONNNG* time due to verbose IPLs)
            seen = 0
            r = 1
            t = 40
            while seen < 2 and t:
                # TODO check for forward progress
                r = console.expect(['x=exit', 'Petitboot', pexpect.TIMEOUT],
                                   timeout=10)
                if r == 2 and t == 0:
                    raise pexpect.TIMEOUT
                if r in [0, 1]:
                    seen = seen + 1

            # there will be extra things in the pexpect buffer here
        except pexpect.TIMEOUT as e:
            print "Timeout waiting for Petitboot!"
            print str(e)
            raise e

    def wait_for_kexec(self):
        console = self.console.get_console()
        # Wait for kexec to start
        console.expect(['Performing kexec', 'kexec_core: Starting new kernel'],
                       timeout=60)

    def petitboot_exit_to_shell(self):
        console = self.console.get_console()
        # Exiting to petitboot shell
        console.sendcontrol('l')
        console.send('x')
        console.expect('Exiting petitboot')
        console.expect('#')
        # we should have consumed everything in the buffer now.
        print console

    def exit_petitboot_shell(self):
        console = self.console.get_console()
        console.sendcontrol('l')
        console.sendline('exit')
        self.wait_for_petitboot()

    def wait_for_login(self, timeout=600):
        console = self.console.get_console()
        console.sendline('')
        console.expect('login: ', timeout)
示例#47
0
class OpTestHost():

    ##
    # @brief Initialize this object
    #
    # @param i_hostip @type string: IP Address of the host
    # @param i_hostuser @type string: Userid to log into the host
    # @param i_hostpasswd @type string: Password of the userid to log into the host
    # @param i_bmcip @type string: IP Address of the bmc
    # @param i_ffdcDir @type string:specifies the directory under which to save all FFDC data
    #
    def __init__(self,
                 i_hostip,
                 i_hostuser,
                 i_hostpasswd,
                 i_bmcip,
                 i_ffdcDir=None):
        self.ip = i_hostip
        self.user = i_hostuser
        self.passwd = i_hostpasswd
        self.util = OpTestUtil()
        self.bmcip = i_bmcip
        self.cv_ffdcDir = i_ffdcDir

    ##
    #   @brief This method executes the command(i_cmd) on the host using a ssh session
    #
    #   @param i_cmd: @type string: Command to be executed on host through a ssh session
    #   @return command output if command execution is successful else raises OpTestError
    #
    def _ssh_execute(self, i_cmd):

        l_host = self.ip
        l_user = self.user
        l_pwd = self.passwd

        l_output = ''
        ssh_ver = '-2'

        # Flush everything out prior to forking
        sys.stdout.flush()

        # Connect the child controlling terminal to a pseudo-terminal
        try:
            pid, fd = pty.fork()
        except OSError as e:
            # Explicit chain of errors
            l_msg = "Got OSError attempting to fork a pty session for ssh."
            raise OpTestError(l_msg)

        if pid == 0:
            # In child process.  Issue attempt ssh connection to remote host

            arglist = ('/usr/bin/ssh -o StrictHostKeyChecking=no', l_host,
                       ssh_ver, '-k', '-l', l_user, i_cmd)

            try:
                os.execv('/usr/bin/ssh', arglist)
            except Exception as e:
                # Explicit chain of errors
                l_msg = "Can not spawn os.execv for ssh."
                print l_msg
                raise OpTestError(l_msg)

        else:
            # In parent process
            # Polling child process for output
            poll = select.poll()
            poll.register(fd, select.POLLIN)

            start_time = time.time()
            # time.sleep(1)
            while True:
                try:
                    evt = poll.poll()
                    x = os.read(fd, 1024)
                    #print "ssh x= " + x
                    end_time = time.time()
                    if (end_time - start_time > 1500):
                        if (i_cmd.__contains__('updlic')
                                or i_cmd.__contains__('update_flash')):
                            continue
                        else:
                            l_msg = "Timeout occured/SSH request " \
                                    "un-responded even after 25 minutes"
                            print l_msg
                            raise OpTestError(l_msg)

                    if (x.__contains__('(yes/no)')):
                        l_res = "yes\r\n"
                        os.write(fd, l_res)
                    if (x.__contains__('s password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if (x.__contains__('Password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if (x.__contains__('password')):
                        response = l_pwd + "\r\n"
                        os.write(fd, response)
                    if (x.__contains__('yes')):
                        response = '1' + "\r\n"
                        os.write(fd, response)
                    if (x.__contains__('Connection refused')):
                        print x
                        raise OpTestError(x)
                    if (x.__contains__('Received disconnect from')):
                        self.ssh_ver = '-1'
                    if (x.__contains__('Connection closed by')):
                        print(x)
                        raise OpTestError(x)
                    if (x.__contains__(
                            "WARNING: POSSIBLE DNS SPOOFING DETECTED")):
                        print(x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if (x.__contains__(
                            "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED")
                        ):
                        print(x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if (x.__contains__("Permission denied")):
                        l_msg = "Wrong Login or Password(" + l_user + "/" + l_pwd + ") :" + x
                        print(l_msg)
                        raise OpTestError(l_msg)
                    if(x.__contains__("Rebooting") or \
                       (x.__contains__("rebooting the system"))):
                        l_output = l_output + x
                        raise OpTestError(l_output)
                    if (x.__contains__("Connection timed out")):
                        l_msg = "Connection timed out/" + \
                            l_host + " is not pingable"
                        print(x)
                        raise OpTestError(l_msg)
                    if (x.__contains__("could not connect to CLI daemon")):
                        print(x)
                        raise OpTestError("Director server is not up/running("
                                          "Do smstop then smstart to restart)")
                    if ((x.__contains__("Error:"))
                            and (i_cmd.__contains__('rmsys'))):
                        print(x)
                        raise OpTestError("Error removing:" + l_host)
                    if ((x.__contains__(
                            "Bad owner or permissions on /root/.ssh/config"))):
                        print(x)
                        raise OpTestError(
                            "Bad owner or permissions on /root/.ssh/config,"
                            "Try 'chmod -R 600 /root/.ssh' & retry operation")

                    l_output = l_output + x
                    # time.sleep(1)
                except OSError:
                    break
        if l_output.__contains__("Name or service not known"):
            reason = 'SSH Failed for :' + l_host + \
                "\n Please provide a valid Hostname"
            print reason
            raise OpTestError(reason)

        # Gather child process status to freeup zombie and
        # Close child file descriptor before return
        if (fd):
            os.waitpid(pid, 0)
            os.close(fd)
        return l_output

    ##
    # @brief Get and Record Ubunto OS level
    #
    # @return l_oslevel @type string: OS level of the host provided
    #         or raise OpTestError
    #
    def host_get_OS_Level(self):

        l_oslevel = self._ssh_execute(BMC_CONST.BMC_GET_OS_RELEASE)
        print l_oslevel
        return l_oslevel

    ##
    # @brief Executes a command on the os of the bmc to protect network setting
    #
    # @return OpTestError if failed
    #
    def host_protect_network_setting(self):
        try:
            l_rc = self._ssh_execute(BMC_CONST.OS_PRESERVE_NETWORK)
        except:
            l_errmsg = "Can't preserve network setting"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief Performs a cold reset onto the host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_cold_reset(self):

        print("Applying Cold reset on host.")
        l_rc = self._ssh_execute(BMC_CONST.HOST_COLD_RESET)

        # TODO: enable once defect SW331585 is fixed
        '''if BMC_CONST.BMC_PASS_COLD_RESET in l_rc:
            print l_rc
            time.sleep(BMC_CONST.BMC_COLD_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Cold reset Failed"
            print l_msg
            raise OpTestError(l_msg)'''

        self.util.PingFunc(self.bmcip, BMC_CONST.PING_RETRY_FOR_STABILITY)

    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_code_update(self, i_image, imagecomponent):

        # Copy the hpm file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user, self.ip, "/tmp/",
                                      self.passwd)
        except:
            l_msg = "Copying hpm file to host failed"
            print l_msg
            raise OpTestError(l_msg)

        #self.host_protect_network_setting() #writing to host is not stable
        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + " " + imagecomponent
        print l_cmd
        try:
            l_rc = self._ssh_execute(l_cmd)
            print l_rc
            self._ssh_execute("rm -rf /tmp/" + i_image.rsplit("/", 1)[1])
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if (l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will run linux command(i_cmd) on host using private interface _ssh_execute()
    #        making this interface is public
    #
    # @param i_cmd @type string: linux command
    #
    # @return command output if command execution is successful else raises OpTestError
    #
    def host_run_command(self, i_cmd):
        try:
            l_res = self._ssh_execute(i_cmd)
        except:
            l_msg = "Command execution on host failed"
            print l_msg
            print sys.exc_info()
            raise OpTestError(l_msg)
        print l_res
        return l_res

    # @brief It will gather OPAL Message logs and store the copy in a logfile
    #        which will be stored in FFDC dir.
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_opal_msg_log(self):
        try:
            l_data = self.host_run_command(BMC_CONST.OPAL_MSG_LOG)
        except OpTestError:
            l_msg = "Failed to gather OPAL message logs"
            raise OpTestError(l_msg)

        l_res = commands.getstatusoutput("date +%Y%m%d_%H%M")
        l_logFile = "Opal_msglog_%s.log" % l_res[1]
        fn = self.cv_ffdcDir + "/" + l_logFile
        with open(fn, 'w') as f:
            f.write(l_data)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Check if one or more binaries are present on host
    #
    # @param i_cmd @type string: binaries to check for
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_check_command(self, *i_cmd):
        l_cmd = 'which ' + ' '.join(i_cmd) + '; echo $?'
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.splitlines()

        if (int(l_res[-1]) == 0):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "host_check_command: (%s) not present on host. output of '%s': %s" % (
                ','.join(i_cmd), l_cmd, '\n'.join(l_res))
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self._ssh_execute("uname -a | awk {'print $3'}")
        l_kernel = l_kernel.replace("\r\n", "")
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        l_res = self._ssh_execute("test -e %s; echo $?" % l_file)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Config file is available"
        else:
            l_msg = "Config file %s is not available on host" % l_file
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "cat %s | grep -i --color=never %s" % (l_file, i_config)
        print l_cmd
        l_res = self._ssh_execute(l_cmd)
        print l_res
        try:
            l_val = ((l_res.split("=")[1]).replace("\r\n", ""))
        except:
            print l_val
            l_msg = "config option is not set,exiting..."
            print l_msg
            raise OpTestError(l_msg)
        return l_val

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            l_res = self._ssh_execute("dpkg -S `which %s`" % i_cmd)
            return l_res
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            l_res = self._ssh_execute(l_cmd)
            l_pkg = l_res.replace("\r\n", "")
            print l_pkg
            return l_pkg

    ##
    # @brief It will check whether a package is installed in a host OS
    #
    # @param i_oslevel @type string: OS level
    # @param i_package @type string: package name
    #
    # @return BMC_CONST.FW_SUCCESS if package is available
    #         raise OpTestError if package is not available
    #
    def host_check_pkg_availability(self, i_oslevel, i_package):
        if 'Ubuntu' in i_oslevel:
            l_res = self.host_run_command("dpkg -l %s;echo $?" % i_package)
        else:
            l_cmd = "rpm -qa | grep -i %s" % i_package
            l_res = self.host_run_command(l_cmd)
        l_res = l_res.splitlines()
        if (int(l_res[-1]) == 0):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Package %s is not there in host OS" % i_package
            raise OpTestError(l_msg)

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            l_rc = self._ssh_execute("modprobe ibmpowernv; echo $?")
            l_rc = l_rc.replace("\r\n", "")
            if int(l_rc) == 0:
                cmd = "lsmod | grep -i ibmpowernv"
                response = self._ssh_execute(cmd)
                if "ibmpowernv" not in response:
                    l_msg = "ibmpowernv module is not loaded, exiting"
                    raise OpTestError(l_msg)
                else:
                    print "ibmpowernv module is loaded"
                print cmd
                print response
                return BMC_CONST.FW_SUCCESS
            else:
                l_msg = "modprobe failed while loading ibmpowernv,exiting..."
                print l_msg
                raise OpTestError(l_msg)
        else:
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self._ssh_execute("rm -rf %s" % i_dir)
        self._ssh_execute("mkdir %s" % i_dir)
        try:
            print l_cmd
            res = self._ssh_execute(l_cmd)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief reads msglog for getting Chip and Core information
    #
    # @return @type string: Chip and Core information
    #        else raise OpTestError
    def host_read_msglog_core(self):
        try:
            return self._ssh_execute(BMC_CONST.OS_READ_MSGLOG_CORE)
        except:
            l_errmsg = "Can't get msglog data"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief reads getscom data
    # example:
    # Chip ID  | Rev   | Chip type
    # ---------|-------|--------
    # 80000005 | DD2.0 | Centaur memory buffer
    # 80000004 | DD2.0 | Centaur memory buffer
    # 00000000 | DD2.0 | P8 (Venice) processor
    #
    # @param i_xscom_dir @type string: directory where getscom is installed
    #
    # @return @type string: getscom data
    #        else raise OpTestError
    def host_read_getscom_data(self, i_xscom_dir):
        try:
            l_rc = self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_xscom_dir +
                                     BMC_CONST.OS_GETSCOM_LIST)
        except OpTestError as e:
            l_errmsg = "Can't get getscom data"
            print l_errmsg
            raise OpTestError(l_errmsg)

        if ("command not found" in l_rc):
            l_errmsg = "Failed to locate getscom. Make sure it is installed in dir: " + i_xscom_dir
            print l_errmsg
            raise OpTestError(l_errmsg)

        return l_rc

    ##
    # @brief injects error using getscom
    #
    # @param i_xscom_dir @type string: directory where putscom is installed
    # param i_error @type string: error to be injected including the location
    #
    # @return output generated after executing putscom command or else raise OpTestError
    #
    def host_putscom(self, i_xscom_dir, i_error):

        print('Injecting Error.')
        l_rc = self._execute_no_return(BMC_CONST.SUDO_COMMAND + i_xscom_dir +
                                       BMC_CONST.OS_PUTSCOM_ERROR + i_error)

    ##
    # @brief Clears the gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def host_clear_gard_records(self, i_gard_dir):

        l_rc = self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_gard_dir +
                                 BMC_CONST.CLEAR_GARD_CMD)

        if (BMC_CONST.GARD_CLEAR_SUCCESSFUL not in l_rc):
            l_msg = l_rc + '. Failed to clear gard'
            print l_msg
            raise OpTestError(l_msg)

        # Check to make sure no gard records were left
        l_result = self.host_list_gard_records(i_gard_dir)
        if (BMC_CONST.NO_GARD_RECORDS not in l_result):
            l_msg = l_rc + '. Gard records still found after clearing them'
            print l_msg
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Lists all gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return gard records or else raise OpTestError
    #
    def host_list_gard_records(self, i_gard_dir):
        try:
            return self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_gard_dir +
                                     BMC_CONST.LIST_GARD_CMD)
        except:
            l_errmsg = "Can't clear gard records"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief  Execute a command on the targeted host but don't expect
    #             any return data.
    #
    # @param     i_cmd: @type str: command to be executed
    # @param     i_timeout: @type int:
    #
    # @return   BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def _execute_no_return(self, i_cmd, i_timeout=60):

        print('Executing command: ' + i_cmd)
        try:
            p = pxssh.pxssh()
            p.login(self.ip, self.user, self.passwd)
            p.sendline()
            p.prompt()
            p.sendline(i_cmd)
            p.prompt(i_timeout)
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Failed to execute command: " + i_cmd
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief enable/disable cpu states
    #
    # @param i_cpu_state @type string: BMC_CONST.CPU_ENABLE_STATE/
    #                                  BMC_CONST.CPU_DISABLE_STATE
    #
    # @return BMC_CONST.FW_SUCCESS or OpTestError
    #
    def host_disable_enable_cpu_states(self, i_cpu_state):
        try:
            self._ssh_execute(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE1 + "; do echo " + \
                              i_cpu_state  + " > $i; done'")
            self._ssh_execute(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE2 + "; do echo " + \
                              i_cpu_state  + " > $i; done'")
            return BMC_CONST.FW_SUCCESS
        except:
            l_errmsg = "Could not enable/disable cpu idle states"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self._ssh_execute("uname -a | awk {'print $3'}")
        l_kernel = l_kernel.replace("\r\n", "")
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        l_res = self._ssh_execute("test -e %s; echo $?" % l_file)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Config file is available"
        else:
            l_msg = "Config file %s is not available on host" % l_file
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "cat %s | grep -i --color=never %s" % (l_file, i_config)
        print l_cmd
        l_res = self._ssh_execute(l_cmd)
        print l_res
        try:
            l_val = ((l_res.split("=")[1]).replace("\r\n", ""))
        except:
            print l_val
            l_msg = "config option is not set,exiting..."
            print l_msg
            raise OpTestError(l_msg)
        return l_val

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            l_res = self._ssh_execute("dpkg -S `which %s`" % i_cmd)
            return l_res
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            l_res = self._ssh_execute(l_cmd)
            l_pkg = l_res.replace("\r\n", "")
            print l_pkg
            return l_pkg

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            l_rc = self._ssh_execute("modprobe ibmpowernv; echo $?")
            l_rc = l_rc.replace("\r\n", "")
            if int(l_rc) == 0:
                cmd = "lsmod | grep -i ibmpowernv"
                response = self._ssh_execute(cmd)
                if "ibmpowernv" not in response:
                    l_msg = "ibmpowernv module is not loaded, exiting"
                    raise OpTestError(l_msg)
                else:
                    print "ibmpowernv module is loaded"
                print cmd
                print response
                return BMC_CONST.FW_SUCCESS
            else:
                l_msg = "modprobe failed while loading ibmpowernv,exiting..."
                print l_msg
                raise OpTestError(l_msg)
        else:
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self._ssh_execute("rm -rf %s" % i_dir)
        self._ssh_execute("mkdir %s" % i_dir)
        try:
            print l_cmd
            res = self._ssh_execute(l_cmd)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will load the module using modprobe and verify whether it is loaded or not
    #
    # @param i_module @type string: module name, which we want to load on host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module(self, i_module):
        l_res = self.host_run_command("modprobe %s; echo $?" % i_module)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Error in loading the module %s, modprobe failed" % i_module
            print l_msg
            raise OpTestError(l_msg)
        l_res = self.host_run_command("lsmod | grep -i --color=never %s" %
                                      i_module)
        if l_res.__contains__(i_module):
            print "%s module is loaded" % i_module
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = " %s module is not loaded" % i_module
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will read real time clock(RTC) time using hwclock utility
    #
    # @return l_res @type string: return hwclock value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_hwclock(self):
        print "Reading the hwclock"
        l_res = self.host_run_command("hwclock -r;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return l_res
        else:
            l_msg = "Reading the hwclock failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will read system time using date utility (This will be mantained by kernel)
    #
    # @return l_res @type string: return system time value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_systime(self):
        print "Reading system time using date utility"
        l_res = self.host_run_command("date;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return l_res
        else:
            l_msg = "Reading the system time failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will set hwclock time using the --date option
    #        format should be "2015-01-01 12:12:12"
    #
    # @param i_time @type string: this is the time for setting the hwclock
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_set_hwclock_time(self, i_time):
        print "Setting the hwclock time to %s" % i_time
        l_res = self.host_run_command("hwclock --set --date \'%s\';echo $?" %
                                      i_time)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Setting the hwclock failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will load driver module on host based on the config option
    #        if config value m: built as a module
    #                        y: driver built into kernel itself
    #        else   raises OpTestError
    #
    # @param i_kernel @type string: kernel version to get config file
    #        i_config @type string: config option to check in config file
    #        i_module @type string: driver module to load on host based on config value
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module_based_on_config(self, i_kernel, i_config, i_module):
        l_val = self.host_check_config(i_kernel, i_config)
        if l_val == 'm':
            self.host_load_module(i_module)
        elif l_val == 'y':
            print "Driver built into kernel itself"
        else:
            l_msg = "Config value is changed"
            print l_msg
            raise OpTestError(l_msg)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function will return the list of installed i2c buses on host in two formats
    #        list-by number Ex: ["0","1","2",....]
    #        list-by-name  Ex: ["i2c-0","i2c-1","i2c-2"....]
    #
    # @return l_list @type list: list of i2c buses by number
    #         l_list1 @type list: list of i2c buses by name
    #         or raise OpTestError if not able to get list of i2c buses
    #
    def host_get_list_of_i2c_buses(self):
        l_res = self.host_run_command("i2cdetect -l;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Not able to get list of i2c buses"
            print l_msg
            raise OpTestError(l_msg)
        l_res = self.host_run_command("i2cdetect -l | awk '{print $1}'")
        l_res = l_res.splitlines()
        # list by number Ex: ["0","1","2",....]
        l_list = []
        # list by name Ex: ["i2c-0","i2c-1"...]
        l_list1 = []
        for l_bus in l_res:
            matchObj = re.search("(i2c)-(\d{1,})", l_bus)
            if matchObj:
                l_list.append(matchObj.group(2))
                l_list1.append(l_bus)
            else:
                pass
        return l_list, l_list1

    ##
    # @brief This function will get information of EEPROM chips attached to the i2c buses
    #
    # @return l_res @type string: return EEPROM chips information
    #           else raise OpTestError
    #
    def host_get_info_of_eeprom_chips(self):
        print "Getting the information of EEPROM chips"
        l_res = self.host_run_command("dmesg | grep -i --color=never at24")
        if l_res.__contains__("at24"):
            pass
        else:
            l_res = self.host_run_command("dmesg -C")
            self.host_run_command("rmmod at24")
            self.host_load_module("at24")
            l_res = self.host_run_command("dmesg | grep -i --color=never at24")
            if l_res.__contains__("at24"):
                pass
            else:
                l_msg = "Not able to get at24 info"
                raise OpTestError(l_msg)
        return l_res

    ##
    # @brief It will return list with elements having pairs of eeprom chip addresses and
    #        corresponding i2c bus where the chip is attached. This information is getting
    #        through sysfs interface. format is ["0 0x50","0 0x51","1 0x51","1 0x52"....]
    #
    # @return l_chips @type list: list having pairs of i2c bus number and eeprom chip address.
    #           else raise OpTestError
    #
    def host_get_list_of_eeprom_chips(self):
        l_res = self.host_run_command("find /sys/ -name eeprom;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Not able to get list of eeprom chip addresses through sysfs interface"
            print l_msg
            raise OpTestError(l_msg)
        l_chips = []
        for l_line in l_res:
            if l_line.__contains__("eeprom"):
                matchObj = re.search("/(\d{1,}-\d{4})/eeprom", l_line)
                if matchObj:
                    l_line = matchObj.group(1)
                    i_args = (l_line.replace("-", " "))
                    print i_args
                else:
                    continue
                i_args = re.sub(" 00", " 0x", i_args)
                l_chips.append(i_args)
                print i_args
        return l_chips

    ##
    # @brief The hexdump utility is used to display the specified files.
    #        This function will display in both ASCII+hexadecimal format.
    #
    # @param i_dev @type string: this is the file used as a input to hexdump for display info
    #                            Example file:"/sys/devices/platform/3fc0000000000.xscom:i2cm@a0000:i2c-bus@1/i2c-3/3-0050/eeprom"
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    #
    def host_hexdump(self, i_dev):
        l_res = self.host_run_command("hexdump -C %s;echo $?" % i_dev)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "hexdump failed for device %s" % i_dev
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will clone latest skiboot git repository in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_skiboot_source(self, i_dir):
        l_msg = 'https://github.com/open-power/skiboot.git/'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning skiboot git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will compile xscom-utils in the skiboot directory which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_compile_xscom_utilities(self, i_dir):
        l_cmd = "cd %s/external/xscom-utils; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/external/xscom-utils/getscom; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary getscom is available"
        else:
            l_msg = "getscom bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "test -f %s/external/xscom-utils/putscom; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary putscom is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "putscom bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will compile gard utility in the skiboot directory which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_compile_gard_utility(self, i_dir):
        l_cmd = "cd %s/external/gard; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/external/gard/gard; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary gard is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "gard bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function generates olog.json file from skiboot which is used for
    #        fwts olog test: Run OLOG scan and analysis checks.
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_generate_fwts_olog_json(self, i_dir):
        l_cmd = "%s/external/fwts/generate-fwts-olog %s/ -o %s/olog.json" % (
            i_dir, i_dir, i_dir)
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/olog.json; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "olog.json is available in working directory %s" % i_dir
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "olog.json file is failed to create from skiboot"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will clone latest fwts git repository in i_dir directory
    #
    # @param i_dir @type string: directory where fwts source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_fwts_source(self, i_dir):
        l_msg = 'git://kernel.ubuntu.com/hwe/fwts.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning fwts git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function is used to build fwts tool in the fwts directory
    #        which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where fwts source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_build_fwts_tool(self, i_dir):
        l_cmd = "cd %s/;autoreconf -ivf;./configure; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/src/fwts; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        print l_res
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary fwts is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "fwts bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function is used to get detected pci devices in different user/machine readable formats
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_pci_devices(self):
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES1)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES2)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES3)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES4)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES5)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES6)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_SYSFS_DEVICES)

    ##
    # @brief This function is used to get more pci devices info in verbose mode
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_get_pci_verbose_info(self):
        l_res = self.host_run_command(BMC_CONST.HOST_LIST_PCI_VERBOSE)
        return l_res

    ##
    # @brief This function is used to get minimum usb devices info
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_usb_devices(self):
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES1)
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES2)
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES3)

    ##
    # @brief This function enable only a single core
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_single_core(self):
        self.host_run_command("ppc64_cpu --cores-on=1")
class OpTestHost():
    '''
    An object to manipulate and run things on the host.
    '''
    def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_results_dir,
                 scratch_disk="", proxy="", logfile=sys.stdout,
                 check_ssh_keys=False, known_hosts_file=None):
        self.ip = i_hostip
        self.user = i_hostuser
        self.passwd = i_hostpasswd
        self.util = OpTestUtil()
        self.bmcip = i_bmcip
        self.results_dir = i_results_dir
        self.logfile = logfile
        self.ssh = OpTestSSH(i_hostip, i_hostuser, i_hostpasswd,
                logfile=self.logfile, check_ssh_keys=check_ssh_keys,
                known_hosts_file=known_hosts_file)
        self.scratch_disk = scratch_disk
        self.proxy = proxy
        self.scratch_disk_size = None
        self.conf = OpTestConfiguration.conf
        self.check_ssh_keys = check_ssh_keys
        self.known_hosts_file = known_hosts_file

    def hostname(self):
        return self.ip

    def username(self):
        return self.user

    def password(self):
        return self.passwd

    def set_system(self, system):
        self.ssh.set_system(system)

    def get_scratch_disk(self):
        return self.scratch_disk

    def get_scratch_disk_size(self, console=None):
        if self.scratch_disk_size is not None:
            return self.scratch_disk_size
        if console is None:
            raise Exception("You need to call get_scratch_disk_size() with a console first")
        dev_sdX = console.run_command("readlink -f %s" % self.get_scratch_disk())
        dev_sdX = dev_sdX[0].replace("/dev/","")
        scratch_disk_size = console.run_command("cat /sys/block/%s/size" % dev_sdX)
        # Use the (undocumented) /size sysfs property of nr 512byte sectors
        self.scratch_disk_size = int(scratch_disk_size[0])*512


    def get_proxy(self):
        return self.proxy

    def get_ssh_connection(self):
        return self.ssh

    def get_new_ssh_connection(self, name="temp"):
        #time.sleep(1)
        outsuffix = time.strftime("%Y%m%d%H%M%S")
        filename = "%s-%s.log" % (outsuffix, name)
        logfile = os.path.join(self.results_dir,filename)
        print "Log file: %s" % logfile
        logcmd = "tee %s" % (logfile)
        logcmd = logcmd + "| sed -u -e 's/\\r$//g'|cat -v"
        print "logcmd: %s" % logcmd
        logfile_proc = subprocess.Popen(logcmd,
                                             stdin=subprocess.PIPE,
                                             stderr=subprocess.PIPE,
                                             stdout=subprocess.PIPE,
                                             shell=True)
        print repr(logfile_proc)
        logfile = logfile_proc.stdin
        print "Log file: %s" % logfile
        OpTestLogger.optest_logger_glob.setUpCustomLoggerDebugFile(name, filename)
        ssh = OpTestSSH(self.ip, self.user, self.passwd,
                        logfile=logfile, check_ssh_keys=self.check_ssh_keys,
                        known_hosts_file=self.known_hosts_file, use_parent_logger=False)
        ssh.set_system(self.conf.op_system)
        return ssh

    def host_get_OS_Level(self, console=0):
        '''
        Get the OS version.
        '''
        l_oslevel = self.host_run_command("cat /etc/os-release", timeout=60, console=console)
        return '\n'.join(l_oslevel)

    def host_cold_reset(self):
        '''
        Cold reboot the host
        '''
        log.debug(("Applying Cold reset on host."))
        l_rc = self.ssh.run_command(BMC_CONST.HOST_COLD_RESET, timeout=60)

        self.util.PingFunc(self.bmcip,
                           totalSleepTime=BMC_CONST.PING_RETRY_FOR_STABILITY)

    def host_code_update(self, i_image, imagecomponent):
        '''
        Flash firmware (HPM image) using ipmitool.

        i_image
          hpm file
        i_imagecomponent
          component to be updated from the HPM file.
          Probably BMC_CONST.BMC_FW_IMAGE_UPDATE or BMC_CONST.BMC_PNOR_IMAGE
        '''
        # Copy the hpm file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying hpm file to host failed"
            log.warning(l_msg)
            raise OpTestError(l_msg)

        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + " " + imagecomponent
        log.debug(l_cmd)
        try:
            l_rc = self.ssh.run_command(l_cmd, timeout=1500)
            log.debug(l_rc)
            self.ssh.run_command("rm -rf /tmp/" + i_image.rsplit("/", 1)[1],timeout=120)
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            log.warning(l_msg)
            raise OpTestError(l_msg)

        if(l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            log.warning(l_msg)
            raise OpTestError(l_msg)

    def host_run_command(self, i_cmd, timeout=1500, retry=0, console=0):
        # if we are QEMU use the system console
        if isinstance(self.ssh.system.console, OpTestQemu.QemuConsole) or (console == 1):
          return self.ssh.system.console.run_command(i_cmd, timeout, retry)
        else:
          return self.ssh.run_command(i_cmd, timeout, retry)

    def host_gather_opal_msg_log(self, console=0):
        '''
        Gather OPAL logs (from the host) and store in a file
        '''
        try:
            l_data = '\n'.join(self.host_run_command(BMC_CONST.OPAL_MSG_LOG, console=console))
        except OpTestError:
            l_msg = "Failed to gather OPAL message logs"
            raise OpTestError(l_msg)

        if not self.results_dir:
            log.debug(l_data)
            return

        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Opal_msglog_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        log.debug(fn)
        with open(fn, 'w') as f:
            f.write(l_data)

    def host_check_command(self, *i_cmd, **kwargs):
        '''
        Check if one or more binaries are present on host
        '''
        default_vals = {'console': 0}
        for key in default_vals:
          if key not in kwargs.keys():
            kwargs[key] = default_vals[key]
        l_cmd = 'which ' + ' '.join(i_cmd)
        log.debug(l_cmd)
        try:
            l_res = self.host_run_command(l_cmd, console=kwargs['console'])
        except CommandFailed as c:
            l_msg = "host_check_command: (%s) not present on host. output of '%s': %s" % (','.join(i_cmd), l_cmd, '\n'.join(c.output))
            log.error(l_msg)
            raise OpTestError(l_msg)

        return True

    def host_get_kernel_version(self, console=0):
        '''
        Get Linux kernel version running on the host (using uname).
        '''
        l_kernel = self.host_run_command("uname -a | awk {'print $3'}", timeout=60, console=0)
        l_kernel = ''.join(l_kernel)
        log.debug(l_kernel)
        return l_kernel

    def host_check_config(self, i_kernel, i_config, console=0):
        '''
        This function will checks first for config file for a given kernel version on host,
        if available then check for config option value and return that value
        whether it is y or m...etc.

        sample config option values: ::

          CONFIG_CRYPTO_ZLIB=m
          CONFIG_CRYPTO_LZO=y
          # CONFIG_CRYPTO_842 is not set

        i_kernel
          kernel version
        i_config
          Which config option want to check in config file. e.g. `CONFIG_SENSORS_IBMPOWERNV`

        It will return config option value y or m,
        or raise OpTestError if config file is not available on host
        or raise OpTestError if config option is not set in file.
        '''
        l_file = "/boot/config-%s" % i_kernel
        try:
            l_res = self.host_run_command("test -e %s" % l_file, timeout=60, console=console)
        except CommandFailed:
            raise NoKernelConfig(i_kernel, l_file)

        log.debug("Config file is available")
        l_cmd = "cat %s | grep -i --color=never %s" % (l_file, i_config)
        l_res = self.host_run_command(l_cmd, timeout=60, console=console)
        log.debug(l_res)
        config_opts = {}
        for o in l_res:
            m = re.match('# (.*) is not set', o)
            if m:
                config_opts[m.group(0)]='n'
            else:
                if '=' in o:
                    opt, val = o.split("=")
                    config_opts[opt] = val

        if config_opts.get(i_config) not in ["y","m"]:
                raise KernelConfigNotSet(i_config)

        return config_opts[i_config]

    def host_check_pkg_for_utility(self, i_oslevel, i_cmd, console=0):
        '''
        Check if a package is installed on the host.
        The `i_oslevel` is used to determine if we should use `dpkg` or `rpm` to
        search for the package name.
        '''
        if 'Ubuntu' in i_oslevel:
            return ''.join(self.host_run_command("dpkg -S `which %s`" % i_cmd, timeout=60, console=console))
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            return ''.join(self.host_run_command(l_cmd, timeout=60, console=console))

    def host_load_ibmpowernv(self, i_oslevel):
        '''
        This function loads ibmpowernv driver only on powernv platform
        and also this function works only in root user mode
        '''
        if "PowerKVM" not in i_oslevel:
            o = self.ssh.run_command("modprobe ibmpowernv",timeout=60)
            cmd = "lsmod | grep -i ibmpowernv"
            response = self.ssh.run_command(cmd, timeout=60)
            if "ibmpowernv" not in ''.join(response):
                l_msg = "ibmpowernv module is not loaded, exiting"
                raise OpTestError(l_msg)
            else:
                log.debug("ibmpowernv module is loaded")
            log.debug(cmd)
            log.debug(response)
            return BMC_CONST.FW_SUCCESS

    def host_start_lm_sensor_svc(self, i_oslevel, console=0):
        '''
        This function restarts the lm_sensors service on host using systemctl utility
        systemctl utility is not present in ubuntu, This function will work in remaining all
        other OS'es i.e redhat, sles and PowerKVM
        '''
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd, console=console)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd, console=console)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd, console=console)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                log.error(l_msg)
                raise OpTestError(l_msg)

    def host_clone_linux_source(self, i_dir):
        '''
        It will clone latest linux git repository in i_dir directory.

        i_dir
          directory where linux source will be cloned.
        '''
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone --depth=1 %s %s" % (l_msg, i_dir)
        self.ssh.run_command("rm -rf %s" % i_dir, timeout=300)
        self.ssh.run_command("mkdir %s" % i_dir, timeout=60)
        try:
            log.debug(l_cmd)
            res = self.ssh.run_command(l_cmd, timeout=1500)
            log.debug(res)
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def host_load_module(self, i_module, console=0):
        '''
        It will load the module using modprobe and verify whether it is loaded or not
        '''
        try:
            l_res = self.host_run_command("modprobe %s" % i_module, console=console)
        except CommandFailed as c:
            l_msg = "Error in loading the module %s, modprobe failed: %s" % (i_module,str(c))
            raise OpTestError(l_msg)
        l_res = self.host_run_command("lsmod | grep -i --color=never %s" % i_module, console=console)
        if re.search(i_module, ''.join(l_res)):
            log.debug("%s module is loaded" % i_module)
            return BMC_CONST.FW_SUCCESS
        else:
            raise KernelModuleNotLoaded(i_module)

    def host_read_hwclock(self, console=0):
        '''
        This function will read real time clock(RTC) time using hwclock utility.
        '''
        log.debug("Reading the hwclock")
        self.host_run_command("hwclock -r;echo $?", console=console)

    def host_read_systime(self, console=0):
        '''
        This function will read system time using date utility (This will be mantained by kernel).
        '''
        log.debug("Reading system time using date utility")
        l_res = self.host_run_command("date", console=console)
        return l_res

    def host_set_hwclock_time(self, i_time, console=0):
        '''
        This function will set hwclock time using the --date option
        format should be "2015-01-01 12:12:12"
        '''
        log.debug("Setting the hwclock time to %s" % i_time)
        self.host_run_command("hwclock --set --date \'%s\'" % i_time, console=console)

    ##
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module_based_on_config(self, i_kernel, i_config, i_module, console=0):
        '''
        This function will load driver module on host based on the config option
        if config value

        m
          built as a module
        y
          driver built into kernel itself
        else
          raises OpTestError

        i_kernel
          kernel version to get config file
        i_config
          config option to check in config file
        i_module
          driver module to load on host based on config value
        '''
        l_val = self.host_check_config(i_kernel, i_config, console=console)
        if l_val == 'm':
            self.host_load_module(i_module, console=console)
        elif l_val == 'y':
            log.debug("Driver built into kernel itself")
        elif l_val == 'n':
            raise KernelConfigNotSet(i_config)

    def host_clone_skiboot_source(self, i_dir, console=0):
        '''
        It will clone latest skiboot git repository in i_dir directory

        i_dir
          directory where skiboot source will be cloned
        '''
        l_msg = 'https://github.com/open-power/skiboot.git/'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false", console=console)
        self.host_run_command("rm -rf %s" % i_dir, console=console)
        self.host_run_command("mkdir %s" % i_dir, console=console)
        try:
            log.debug(l_cmd)
            l_res = self.host_run_command(l_cmd, console=console)
            log.debug(l_res)
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning skiboot git repository is failed"
            log.debug(l_msg)
            raise OpTestError(l_msg)

    def host_enable_single_core(self, console=0):
        '''
        This function enable only a single core
        '''
        self.host_run_command("ppc64_cpu --cores-on=1", console=console)

    def host_enable_all_cores(self, console=0):
        '''
        Enables all cores
        '''
        self.host_run_command("ppc64_cpu --cores-on=all", console=console)

    def host_get_list_of_pci_domains(self, console=0):
        '''
        This function is used to get list of PCI PHB domains.
        '''
        self.pci_domains = []
        self.host_run_command("lspci -mm", console=console)
        res = self.host_run_command('lspci -mm | cut -d":" -f1 | sort | uniq', console=console)
        for domain in res:
            if not domain:
                continue
            if len(domain) != 4:
                domain = ''.join(("00", domain))
            domain = 'PCI' + domain
            if not self.pci_domains.__contains__(domain):
                self.pci_domains.append(domain)
        log.debug(self.pci_domains)
        return self.pci_domains

    def host_get_root_phb(self, console=0):
        '''
        This function is used to get the PHB domain of root port where
        the filesystem is mounted(We need to skip this in EEH tests as recovery
        will fail on this domain is expected)
        '''
        cmd = "df -h /boot | awk 'END {print $1}'"
        res = self.host_run_command(cmd, console=console)
        boot_disk = ''.join(res).split("/dev/")[1]
        boot_disk = boot_disk.replace("\r\n", "")
        cmd  = "ls -l /dev/disk/by-path/ | grep %s | awk '{print $(NF-2)}'" % boot_disk
        res = self.host_run_command(cmd, console=console)
        matchObj = re.search(r"\d{4}(?!\d)", '\n'.join(res), re.S)
        if not matchObj:
            raise OpTestError("Not able to find out root phb domain")
        boot_domain = 'PCI' + matchObj.group(0)
        return  boot_domain

    def host_gather_kernel_log(self, console=0):
        '''
        It will gather kernel dmesg logs and store the copy in a logfile
        which will be stored in results dir.
        '''
        try:
            l_data = '\n'.join(self.host_run_command("dmesg", console=console))
        except OpTestError:
            l_msg = "Failed to gather kernel dmesg log"
            raise OpTestError(l_msg)
        if not self.results_dir:
            log.debug(l_data)
            return
        l_res = (time.asctime(time.localtime())).replace(" ", "_")
        l_logFile = "Kernel_dmesg_log_%s.log" % l_res
        fn = os.path.join(self.results_dir, l_logFile)
        log.debug(fn)
        with open(fn, 'w') as f:
            f.write(l_data)
        return BMC_CONST.FW_SUCCESS

    def host_start_opal_errd_daemon(self, console=0):
        '''
        starts opal_errd daemon
        '''
        self.host_run_command("systemctl start opal_errd", console=console)

    def host_stop_opal_errd_daemon(self, console=0):
        '''
        stops opal_errd daemon
        '''
        self.host_run_command("systemctl stop opal_errd", console=console)

    def host_get_status_of_opal_errd_daemon(self, console=0):
        '''
        This function gets the status of opal_errd daemon.
        Raises an exception if not running.
        '''
        res = self.host_run_command("ps -ef | grep -v grep | grep opal_errd | wc -l", console=console)
        log.debug(res)
        if res[0].strip() == "0":
            log.warning("Opal_errd daemon is not running")
            return False
        elif res[0].strip() == "1":
            log.debug("Opal_errd daemon is running")
            return True
        else:
            raise OpTestError("Not able to get status of opal errd daemon")

    def host_list_all_errorlogs(self, console=0):
        '''
        This function lists all error logs in host
        '''
        self.host_run_command("opal-elog-parse -l", console=console)

    def host_list_all_service_action_logs(self, console=0):
        '''
        This function lists all service action logs in host.
        '''
        self.host_run_command("opal-elog-parse -s", console=console)

    def host_get_number_of_errorlogs(self, console=0):
        '''
        This function gets the number of error logs
        '''
        res = self.host_run_command("ls %s | wc -l" % BMC_CONST.OPAL_ELOG_DIR, console=console)
        log.debug(res)
        return res

    def host_clear_error_logs(self, console=0):
        '''
        This function clears/acknowledges all error logs in host
        '''
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_ELOG_DIR, console=console)
        res = self.host_run_command("ls %s -1 --color=never" % BMC_CONST.OPAL_ELOG_SYSFS_DIR, console=console)
        log.debug('\n'.join(res))
        for entry in res:
            entry = entry.strip()
            if entry == '':
                continue
            self.host_run_command("echo 1 > %s/%s/acknowledge" % (BMC_CONST.OPAL_ELOG_SYSFS_DIR, entry), console=console)
        return True

    def host_clear_all_dumps(self, console=0):
        '''
        This function clears/acknowledges all dumps in host.
        '''
        self.host_run_command("rm -f %s/*" % BMC_CONST.OPAL_DUMP_DIR, console=console)
        res = self.host_run_command("ls %s -1 --color=never" % BMC_CONST.OPAL_DUMP_SYSFS_DIR, console=console)
        for entry in res:
            entry = entry.strip()
            if (entry == "initiate_dump") or (entry == ''):
                continue
            else:
                self.host_run_command("echo 1 > %s/%s/acknowledge" % (BMC_CONST.OPAL_DUMP_SYSFS_DIR, entry), console=console)
        return True

    def host_disable_kdump_service(self, os_level, console=0):
        '''
        This function disables kdump service. Needs the OS version (from `/etc/os-release`) to
        know if we should use systemd commands or not.
        '''
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service", console=console)
            try:
                self.host_run_command("systemctl status kdump-tools.service", console=console)
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    log.debug(str(cf))
                    raise OpTestError("kdump-tools service is failed to stop")
        else:
            self.host_run_command("systemctl stop kdump.service", console=console)
            try:
                self.host_run_command("systemctl status kdump.service", console=console)
            except CommandFailed as cf:
                if cf.exitcode == 3:
                    pass
                else:
                    log.debug(str(cf))
                    raise OpTestError("kdump service is failed to stop")

    def host_enable_kdump_service(self, os_level, console=0):
        '''
        disables kdump service, needs `/etc/os-release` to work out service name.
        '''
        if "Ubuntu" in os_level:
            self.host_run_command("systemctl stop kdump-tools.service", console=console)
            self.host_run_command("systemctl start kdump-tools.service", console=console)
            self.host_run_command("systemctl status kdump-tools.service", console=console)
        else:
            self.host_run_command("systemctl stop kdump.service", console=console)
            self.host_run_command("systemctl start kdump.service", console=console)
            self.host_run_command("systemctl status kdump.service", console=console)

    def host_check_sysfs_path_availability(self, path, console=0):
        res = self.host_run_command("ls --color=never %s" % path, console=console)
        if "No such file or directory" in res:
            return False
        return True

    def host_check_dt_node_exist(self, node_path, console=0):
        path = "/proc/device-tree/" + node_path
        res = self.host_run_command("ls %s" % path, console=console)
        if "No such file or directory" in res:
            return False
        return True

    def host_get_list_of_chips(self, console=0):
        res = self.host_run_command("PATH=/usr/local/sbin:$PATH getscom -l", console=console)
        chips = []
        for line in res:
            matchObj = re.search("(\d{8}).*processor", line)
            if matchObj:
                chips.append(matchObj.group(1))
        if not chips:
            raise Exception("Getscom failed to list processor chip ids")
        chips.sort()
        log.debug(chips) # ['00000000', '00000001', '00000010']
        return chips

    def host_get_cores(self, console=0):
        proc_gen = self.host_get_proc_gen(console=console)
        core_ids = {}
        cpu_pirs = self.host_run_command("find /sys/devices/system/cpu/*/pir -exec cat {} \;", console=console)
        for pir in cpu_pirs:
            if proc_gen in ["POWER8", "POWER8E"]:
                core_id = hex((int("0x%s" % pir, 16) >> 3 ) & 0xf)
                chip_id = hex((int("0x%s" % pir, 16) >> 7 ) & 0x3f)
            elif proc_gen in ["POWER9"]:
                core_id =hex((int("0x%s" % pir, 16) >> 2 ) & 0x3f)
                chip_id = hex((int("0x%s" % pir, 16) >> 8 ) & 0x7f)
            else:
                raise OpTestError("Unknown or new processor type")
            core_id = core_id.split('x')[1]
            chip_id = chip_id.split('x')[1]

            if chip_id in core_ids:
                core_ids[chip_id].append(core_id)
            else:
                core_ids[chip_id] = [core_id]

        for i in core_ids:
            core_ids[i] = list(set(core_ids[i]))
        core_ids = sorted(core_ids.iteritems())
        log.debug(core_ids)
        return core_ids

    # Supported on OpenPower and P9 FSP system
    def host_prd_supported(self, bmc_type, console=0):
        if not "FSP" in bmc_type:
            return True

        proc_gen = self.host_get_proc_gen(console=console)
        if proc_gen in ["POWER8", "POWER8E"]:
            return False

        return True

    def host_get_proc_gen(self, console=0):
        try:
            if self.proc_gen:
                pass
        except AttributeError:
            self.proc_gen = ''.join(self.host_run_command("grep '^cpu' /proc/cpuinfo |uniq|sed -e 's/^.*: //;s/[,]* .*//;'", console=console))
        return self.proc_gen

    def host_get_smt(self, console=0):
        self.cpu = self.host_get_proc_gen(console=console)
        if self.cpu in ["POWER8", "POWER8E"]:
            return 8
        elif self.cpu in ["POWER9"]:
            return 4
        else:
            return 1

    def host_get_core_count(self, console=0):
        res = self.host_run_command("lscpu --all -e| wc -l", console=console)
        return int(res[0])/(self.host_get_smt(console=console))

    def host_gather_debug_logs(self, console=0):
        self.host_run_command("grep ',[0-4]\]' /sys/firmware/opal/msglog", console=console)
        self.host_run_command("dmesg -T --level=alert,crit,err,warn", console=console)

    def host_copy_fake_gard(self):
        i_image = os.path.join(self.conf.basedir, "test_binaries", "fake.gard")
        # Copy the fake.gard file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying fake.gard file to host failed"
            log.error(l_msg)
            raise OpTestError(l_msg)

    def copy_test_file_to_host(self, filename, sourcedir="test_binaries",
                               dstdir="/tmp/"):
        i_image = os.path.join(self.conf.basedir, sourcedir, filename)
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, dstdir, self.passwd)
        except subprocess.CalledProcessError as e:
            l_msg = "Copying %s file to host failed" % filename
            log.error(l_msg)
            raise OpTestError(l_msg + str(e))

    def copy_files_from_host(self, sourcepath="", destpath="/tmp/"):
        if sourcepath == "":
            sourcepath = self.conf.output
        try:
            self.util.copyFilesFromDest(self.user,
                                        self.ip, destpath, self.passwd, sourcepath)
        except subprocess.CalledProcessError as e:
            l_msg = "Copying %s file(s) from host failed" % destpath
            log.debug(str(e))
            log.error(l_msg)
            raise OpTestError(l_msg + str(e))

    def host_pflash_get_partition(self, partition, console=0):
        d = self.host_run_command("pflash --info", console=console)
        for line in d:
            s = re.search(partition, line)
            if s:
                m = re.match(r'ID=\d+\s+\S+\s+((0[xX])?[0-9a-fA-F]+)..(0[xX])?[0-9a-fA-F]+\s+\(actual=((0[xX])?[0-9a-fA-F]+)\)\s(\[)?([A-Za-z-]+)?(\])?.*', line)
                if not m:
                    continue
                offset = int(m.group(1), 16)
                length = int(m.group(4), 16)
                ret = {'offset': offset,
                       'length': length
                       }
                flags = m.group(7)
                if flags:
                    ret['flags'] = [x for x in list(flags) if x != '-']
                return ret

    def host_has_capi_fpga_card(self, console=0):
        '''
        Check that host has a CAPI FPGA card
        '''
        l_cmd = "lspci -d \"1014::1200\""
        l_res = self.host_run_command(l_cmd, console=console)
        l_res = " ".join(l_res)
        if (l_res.__contains__('IBM Device')):
            l_msg = "Host has a CAPI FPGA card"
            log.debug(l_msg)
            return True
        else:
            l_msg = "Host has no CAPI FPGA card; skipping test"
            log.warning(l_msg)
            return False

    def host_clone_cxl_tests(self, i_dir, console=0):
        '''
        Clone latest cxl-tests git repository in i_dir directory.

        i_dir
          directory where cxl-tests will be cloned
        '''
        l_msg = "https://github.com/ibm-capi/cxl-tests.git"
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false", console=console)
        self.host_run_command("rm -rf %s" % i_dir, console=console)
        self.host_run_command("mkdir %s" % i_dir, console=console)
        try:
            l_res = self.host_run_command(l_cmd, console=console)
            return True
        except:
            l_msg = "Cloning cxl-tests git repository is failed"
            return False

    def host_build_cxl_tests(self, i_dir, console=0):
        l_cmd = "cd %s; make" % i_dir
        self.host_run_command(l_cmd, console=console)
        l_cmd = "test -x %s/libcxl/libcxl.so" % i_dir
        self.host_run_command(l_cmd, console=console)
        l_cmd = "test -x %s/libcxl_tests; echo $?" % i_dir
        self.host_run_command(l_cmd, console=console)
        l_cmd = "test -x %s/memcpy_afu_ctx; echo $?" % i_dir
        self.host_run_command(l_cmd, console=console)

    def host_check_binary(self, i_dir, i_file, console=0):
        l_cmd = "test -x %s/%s;" % (i_dir, i_file)
        try:
            self.host_run_command(l_cmd, console=console)
            l_msg = "Executable file %s/%s is available" % (i_dir, i_file)
            log.debug(l_msg)
            return True
        except CommandFailed:
            l_msg = "Executable file %s/%s is not present" % (i_dir, i_file)
            log.debug(l_msg)
            return False
示例#49
0
class OpTestHost():

    ##
    # @brief Initialize this object
    #
    # @param i_hostip @type string: IP Address of the host
    # @param i_hostuser @type string: Userid to log into the host
    # @param i_hostpasswd @type string: Password of the userid to log into the host
    # @param i_bmcip @type string: IP Address of the bmc
    # @param i_ffdcDir @type string:specifies the directory under which to save all FFDC data
    #
    def __init__(self, i_hostip, i_hostuser, i_hostpasswd, i_bmcip, i_ffdcDir=None):
        self.ip = i_hostip
        self.user = i_hostuser
        self.passwd = i_hostpasswd
        self.util = OpTestUtil()
        self.bmcip = i_bmcip
        self.cv_ffdcDir = i_ffdcDir


    ##
    #   @brief This method executes the command(i_cmd) on the host using a ssh session
    #
    #   @param i_cmd: @type string: Command to be executed on host through a ssh session
    #   @return command output if command execution is successful else raises OpTestError
    #
    def _ssh_execute(self, i_cmd):

        l_host = self.ip
        l_user = self.user
        l_pwd = self.passwd

        l_output = ''
        ssh_ver = '-2'

        # Flush everything out prior to forking
        sys.stdout.flush()

        # Connect the child controlling terminal to a pseudo-terminal
        try:
            pid, fd = pty.fork()
        except OSError as e:
                # Explicit chain of errors
            l_msg = "Got OSError attempting to fork a pty session for ssh."
            raise OpTestError(l_msg)

        if pid == 0:
            # In child process.  Issue attempt ssh connection to remote host

            arglist = ('/usr/bin/ssh -o StrictHostKeyChecking=no',
                       l_host, ssh_ver, '-k', '-l', l_user, i_cmd)

            try:
                os.execv('/usr/bin/ssh', arglist)
            except Exception as e:
                # Explicit chain of errors
                l_msg = "Can not spawn os.execv for ssh."
                print l_msg
                raise OpTestError(l_msg)

        else:
            # In parent process
            # Polling child process for output
            poll = select.poll()
            poll.register(fd, select.POLLIN)

            start_time = time.time()
            # time.sleep(1)
            while True:
                try:
                    evt = poll.poll()
                    x = os.read(fd, 1024)
                    #print "ssh x= " + x
                    end_time = time.time()
                    if(end_time - start_time > 1500):
                        if(i_cmd.__contains__('updlic') or i_cmd.__contains__('update_flash')):
                            continue
                        else:
                            l_msg = "Timeout occured/SSH request " \
                                    "un-responded even after 25 minutes"
                            print l_msg
                            raise OpTestError(l_msg)

                    if(x.__contains__('(yes/no)')):
                        l_res = "yes\r\n"
                        os.write(fd, l_res)
                    if(x.__contains__('s password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if(x.__contains__('Password:'******''
                        os.write(fd, l_pwd + '\r\n')
                    if(x.__contains__('password')):
                        response = l_pwd + "\r\n"
                        os.write(fd, response)
                    if(x.__contains__('yes')):
                        response = '1' + "\r\n"
                        os.write(fd, response)
                    if(x.__contains__('Connection refused')):
                        print x
                        raise OpTestError(x)
                    if(x.__contains__('Received disconnect from')):
                        self.ssh_ver = '-1'
                    if(x.__contains__('Connection closed by')):
                        print (x)
                        raise OpTestError(x)
                    if(x.__contains__("WARNING: POSSIBLE DNS SPOOFING DETECTED")):
                        print (x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if(x.__contains__("WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED")):
                        print (x)
                        raise OpTestError("Its a RSA key problem : \n" + x)
                    if(x.__contains__("Permission denied")):
                        l_msg = "Wrong Login or Password(" + l_user + "/" + l_pwd + ") :" + x
                        print (l_msg)
                        raise OpTestError(l_msg)
                    if(x.__contains__("Rebooting") or \
                       (x.__contains__("rebooting the system"))):
                        l_output = l_output + x
                        raise OpTestError(l_output)
                    if(x.__contains__("Connection timed out")):
                        l_msg = "Connection timed out/" + \
                            l_host + " is not pingable"
                        print (x)
                        raise OpTestError(l_msg)
                    if(x.__contains__("could not connect to CLI daemon")):
                        print(x)
                        raise OpTestError("Director server is not up/running("
                                          "Do smstop then smstart to restart)")
                    if((x.__contains__("Error:")) and (i_cmd.__contains__('rmsys'))):
                        print(x)
                        raise OpTestError("Error removing:" + l_host)
                    if((x.__contains__("Bad owner or permissions on /root/.ssh/config"))):
                        print(x)
                        raise OpTestError("Bad owner or permissions on /root/.ssh/config,"
                                          "Try 'chmod -R 600 /root/.ssh' & retry operation")

                    l_output = l_output + x
                    # time.sleep(1)
                except OSError:
                    break
        if l_output.__contains__("Name or service not known"):
            reason = 'SSH Failed for :' + l_host + \
                "\n Please provide a valid Hostname"
            print reason
            raise OpTestError(reason)

        # Gather child process status to freeup zombie and
        # Close child file descriptor before return
        if (fd):
            os.waitpid(pid, 0)
            os.close(fd)
        return l_output

    ##
    # @brief Get and Record Ubunto OS level
    #
    # @return l_oslevel @type string: OS level of the host provided
    #         or raise OpTestError
    #
    def host_get_OS_Level(self):

        l_oslevel = self._ssh_execute(BMC_CONST.BMC_GET_OS_RELEASE)
        print l_oslevel
        return l_oslevel


    ##
    # @brief Executes a command on the os of the bmc to protect network setting
    #
    # @return OpTestError if failed
    #
    def host_protect_network_setting(self):
        try:
            l_rc = self._ssh_execute(BMC_CONST.OS_PRESERVE_NETWORK)
        except:
            l_errmsg = "Can't preserve network setting"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief Performs a cold reset onto the host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_cold_reset(self):

        print ("Applying Cold reset on host.")
        l_rc = self._ssh_execute(BMC_CONST.HOST_COLD_RESET)

        # TODO: enable once defect SW331585 is fixed
        '''if BMC_CONST.BMC_PASS_COLD_RESET in l_rc:
            print l_rc
            time.sleep(BMC_CONST.BMC_COLD_RESET_DELAY)
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Cold reset Failed"
            print l_msg
            raise OpTestError(l_msg)'''

        self.util.PingFunc(self.bmcip, BMC_CONST.PING_RETRY_FOR_STABILITY)


    ##
    # @brief Flashes image using ipmitool
    #
    # @param i_image @type string: hpm file including location
    # @param i_imagecomponent @type string: component to be
    #        update from the hpm file BMC_CONST.BMC_FW_IMAGE_UPDATE
    #        or BMC_CONST.BMC_PNOR_IMAGE
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_code_update(self, i_image, imagecomponent):

        # Copy the hpm file to the tmp folder in the host
        try:
            self.util.copyFilesToDest(i_image, self.user,
                                             self.ip, "/tmp/", self.passwd)
        except:
            l_msg = "Copying hpm file to host failed"
            print l_msg
            raise OpTestError(l_msg)

        #self.host_protect_network_setting() #writing to host is not stable
        l_cmd = "\necho y | ipmitool -I usb " + BMC_CONST.BMC_HPM_UPDATE + "/tmp/" \
                + i_image.rsplit("/", 1)[-1] + " " + imagecomponent
        print l_cmd
        try:
            l_rc = self._ssh_execute(l_cmd)
            print l_rc
            self._ssh_execute("rm -rf /tmp/" + i_image.rsplit("/", 1)[1])
        except subprocess.CalledProcessError:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

        if(l_rc.__contains__("Firmware upgrade procedure successful")):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Code Update Failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will run linux command(i_cmd) on host using private interface _ssh_execute()
    #        making this interface is public
    #
    # @param i_cmd @type string: linux command
    #
    # @return command output if command execution is successful else raises OpTestError
    #
    def host_run_command(self, i_cmd):
        try:
            l_res = self._ssh_execute(i_cmd)
        except:
            l_msg = "Command execution on host failed"
            print l_msg
            print sys.exc_info()
            raise OpTestError(l_msg)
        print l_res
        return l_res

    # @brief It will gather OPAL Message logs and store the copy in a logfile
    #        which will be stored in FFDC dir.
    #
    # @return BMC_CONST.FW_SUCCESS  or raise OpTestError
    #
    def host_gather_opal_msg_log(self):
        try:
            l_data = self.host_run_command(BMC_CONST.OPAL_MSG_LOG)
        except OpTestError:
            l_msg = "Failed to gather OPAL message logs"
            raise OpTestError(l_msg)

        l_res = commands.getstatusoutput("date +%Y%m%d_%H%M")
        l_logFile = "Opal_msglog_%s.log" % l_res[1]
        fn = self.cv_ffdcDir + "/" + l_logFile
        with open(fn, 'w') as f:
            f.write(l_data)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief Check if one or more binaries are present on host
    #
    # @param i_cmd @type string: binaries to check for
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_check_command(self, *i_cmd):
        l_cmd = 'which ' + ' '.join(i_cmd) + '; echo $?'
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.splitlines()

        if (int(l_res[-1]) == 0):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "host_check_command: (%s) not present on host. output of '%s': %s" % (','.join(i_cmd), l_cmd, '\n'.join(l_res))
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self._ssh_execute("uname -a | awk {'print $3'}")
        l_kernel = l_kernel.replace("\r\n", "")
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        l_res = self._ssh_execute("test -e %s; echo $?" % l_file)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Config file is available"
        else:
            l_msg = "Config file %s is not available on host" % l_file
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "cat %s | grep -i --color=never %s" % (l_file, i_config)
        print l_cmd
        l_res = self._ssh_execute(l_cmd)
        print l_res
        try:
            l_val = ((l_res.split("=")[1]).replace("\r\n", ""))
        except:
            print l_val
            l_msg = "config option is not set,exiting..."
            print l_msg
            raise OpTestError(l_msg)
        return l_val

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            l_res = self._ssh_execute("dpkg -S `which %s`" % i_cmd)
            return l_res
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            l_res = self._ssh_execute(l_cmd)
            l_pkg = l_res.replace("\r\n", "")
            print l_pkg
            return l_pkg

    ##
    # @brief It will check whether a package is installed in a host OS
    #
    # @param i_oslevel @type string: OS level
    # @param i_package @type string: package name
    #
    # @return BMC_CONST.FW_SUCCESS if package is available
    #         raise OpTestError if package is not available
    #
    def host_check_pkg_availability(self, i_oslevel, i_package):
        if 'Ubuntu' in i_oslevel:
            l_res = self.host_run_command("dpkg -l %s;echo $?" % i_package)
        else:
            l_cmd = "rpm -qa | grep -i %s" % i_package
            l_res = self.host_run_command(l_cmd)
        l_res = l_res.splitlines()
        if (int(l_res[-1]) == 0):
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Package %s is not there in host OS" % i_package
            raise OpTestError(l_msg)

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            l_rc = self._ssh_execute("modprobe ibmpowernv; echo $?")
            l_rc = l_rc.replace("\r\n", "")
            if int(l_rc) == 0:
                cmd = "lsmod | grep -i ibmpowernv"
                response = self._ssh_execute(cmd)
                if "ibmpowernv" not in response:
                    l_msg = "ibmpowernv module is not loaded, exiting"
                    raise OpTestError(l_msg)
                else:
                    print "ibmpowernv module is loaded"
                print cmd
                print response
                return BMC_CONST.FW_SUCCESS
            else:
                l_msg = "modprobe failed while loading ibmpowernv,exiting..."
                print l_msg
                raise OpTestError(l_msg)
        else:
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self._ssh_execute("rm -rf %s" % i_dir)
        self._ssh_execute("mkdir %s" % i_dir)
        try:
            print l_cmd
            res = self._ssh_execute(l_cmd)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief reads msglog for getting Chip and Core information
    #
    # @return @type string: Chip and Core information
    #        else raise OpTestError
    def host_read_msglog_core(self):
        try:
            return self._ssh_execute(BMC_CONST.OS_READ_MSGLOG_CORE)
        except:
            l_errmsg = "Can't get msglog data"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief reads getscom data
    # example:
    # Chip ID  | Rev   | Chip type
    # ---------|-------|--------
    # 80000005 | DD2.0 | Centaur memory buffer
    # 80000004 | DD2.0 | Centaur memory buffer
    # 00000000 | DD2.0 | P8 (Venice) processor
    #
    # @param i_xscom_dir @type string: directory where getscom is installed
    #
    # @return @type string: getscom data
    #        else raise OpTestError
    def host_read_getscom_data(self, i_xscom_dir):
        try:
            l_rc = self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_xscom_dir + BMC_CONST.OS_GETSCOM_LIST)
        except OpTestError as e:
            l_errmsg = "Can't get getscom data"
            print l_errmsg
            raise OpTestError(l_errmsg)

        if("command not found" in l_rc):
            l_errmsg = "Failed to locate getscom. Make sure it is installed in dir: " + i_xscom_dir
            print l_errmsg
            raise OpTestError(l_errmsg)

        return l_rc


    ##
    # @brief injects error using getscom
    #
    # @param i_xscom_dir @type string: directory where putscom is installed
    # param i_error @type string: error to be injected including the location
    #
    # @return output generated after executing putscom command or else raise OpTestError
    #
    def host_putscom(self, i_xscom_dir, i_error):

        print('Injecting Error.')
        l_rc = self._execute_no_return(BMC_CONST.SUDO_COMMAND + i_xscom_dir + BMC_CONST.OS_PUTSCOM_ERROR + i_error)

    ##
    # @brief Clears the gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def host_clear_gard_records(self, i_gard_dir):

        l_rc = self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_gard_dir + BMC_CONST.CLEAR_GARD_CMD)

        if(BMC_CONST.GARD_CLEAR_SUCCESSFUL not in l_rc):
            l_msg = l_rc + '. Failed to clear gard'
            print l_msg
            raise OpTestError(l_msg)

        # Check to make sure no gard records were left
        l_result = self.host_list_gard_records(i_gard_dir)
        if(BMC_CONST.NO_GARD_RECORDS not in l_result):
            l_msg = l_rc + '. Gard records still found after clearing them'
            print l_msg
            raise OpTestError(l_msg)

        return BMC_CONST.FW_SUCCESS


    ##
    # @brief Lists all gard records
    #
    # @param i_gard_dir @type string: directory where putscom is installed
    #
    # @return gard records or else raise OpTestError
    #
    def host_list_gard_records(self, i_gard_dir):
        try:
            return self._ssh_execute(BMC_CONST.SUDO_COMMAND + i_gard_dir + BMC_CONST.LIST_GARD_CMD)
        except:
            l_errmsg = "Can't clear gard records"
            print l_errmsg
            raise OpTestError(l_errmsg)


    ##
    # @brief  Execute a command on the targeted host but don't expect
    #             any return data.
    #
    # @param     i_cmd: @type str: command to be executed
    # @param     i_timeout: @type int:
    #
    # @return   BMC_CONST.FW_SUCCESS or else raise OpTestError
    #
    def _execute_no_return(self, i_cmd, i_timeout=60):

        print('Executing command: ' + i_cmd)
        try:
            p = pxssh.pxssh()
            p.login(self.ip, self.user, self.passwd)
            p.sendline()
            p.prompt()
            p.sendline(i_cmd)
            p.prompt(i_timeout)
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Failed to execute command: " + i_cmd
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief enable/disable cpu states
    #
    # @param i_cpu_state @type string: BMC_CONST.CPU_ENABLE_STATE/
    #                                  BMC_CONST.CPU_DISABLE_STATE
    #
    # @return BMC_CONST.FW_SUCCESS or OpTestError
    #
    def host_disable_enable_cpu_states(self, i_cpu_state):
        try:
            self._ssh_execute(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE1 + "; do echo " + \
                              i_cpu_state  + " > $i; done'")
            self._ssh_execute(BMC_CONST.SUDO_COMMAND + "sh -c 'for i in " + \
                              BMC_CONST.CPU_IDLEMODE_STATE2 + "; do echo " + \
                              i_cpu_state  + " > $i; done'")
            return BMC_CONST.FW_SUCCESS
        except:
            l_errmsg = "Could not enable/disable cpu idle states"
            print l_errmsg
            raise OpTestError(l_errmsg)

    ##
    # @brief It will get the linux kernel version on host
    #
    # @return l_kernel @type string: kernel version of the host provided
    #         or raise OpTestError
    #
    def host_get_kernel_version(self):
        l_kernel = self._ssh_execute("uname -a | awk {'print $3'}")
        l_kernel = l_kernel.replace("\r\n", "")
        print l_kernel
        return l_kernel

    ##
    # @brief This function will checks first for config file for a given kernel version on host,
    #        if available then check for config option value and return that value
    #            whether it is y or m...etc.
    #        sample config option values:
    #        CONFIG_CRYPTO_ZLIB=m
    #        CONFIG_CRYPTO_LZO=y
    #        # CONFIG_CRYPTO_842 is not set
    #
    #
    # @param i_kernel @type string: kernel version
    # @param i_config @type string: Which config option want to check in config file
    #                               Ex:CONFIG_SENSORS_IBMPOWERNV
    #
    # @return l_val @type string: It will return config option value y or m,
    #                             or raise OpTestError if config file is not available on host
    #                             or raise OpTestError if config option is not set in file.
    #
    def host_check_config(self, i_kernel, i_config):
        l_file = "/boot/config-%s" % i_kernel
        l_res = self._ssh_execute("test -e %s; echo $?" % l_file)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Config file is available"
        else:
            l_msg = "Config file %s is not available on host" % l_file
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "cat %s | grep -i --color=never %s" % (l_file, i_config)
        print l_cmd
        l_res = self._ssh_execute(l_cmd)
        print l_res
        try:
            l_val = ((l_res.split("=")[1]).replace("\r\n", ""))
        except:
            print l_val
            l_msg = "config option is not set,exiting..."
            print l_msg
            raise OpTestError(l_msg)
        return l_val

    ##
    # @brief It will return installed package name for given linux command(i_cmd) on host
    #
    # @param i_cmd @type string: linux command
    # @param i_oslevel @type string: OS level
    #
    # @return l_pkg @type string: installed package on host
    #
    def host_check_pkg_for_utility(self, i_oslevel, i_cmd):
        if 'Ubuntu' in i_oslevel:
            l_res = self._ssh_execute("dpkg -S `which %s`" % i_cmd)
            return l_res
        else:
            l_cmd = "rpm -qf `which %s`" % i_cmd
            l_res = self._ssh_execute(l_cmd)
            l_pkg = l_res.replace("\r\n", "")
            print l_pkg
            return l_pkg

    ##
    # @brief This function loads ibmpowernv driver only on powernv platform
    #        and also this function works only in root user mode
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_ibmpowernv(self, i_oslevel):
        if "PowerKVM" not in i_oslevel:
            l_rc = self._ssh_execute("modprobe ibmpowernv; echo $?")
            l_rc = l_rc.replace("\r\n", "")
            if int(l_rc) == 0:
                cmd = "lsmod | grep -i ibmpowernv"
                response = self._ssh_execute(cmd)
                if "ibmpowernv" not in response:
                    l_msg = "ibmpowernv module is not loaded, exiting"
                    raise OpTestError(l_msg)
                else:
                    print "ibmpowernv module is loaded"
                print cmd
                print response
                return BMC_CONST.FW_SUCCESS
            else:
                l_msg = "modprobe failed while loading ibmpowernv,exiting..."
                print l_msg
                raise OpTestError(l_msg)
        else:
            return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function restarts the lm_sensors service on host using systemctl utility
    #        systemctl utility is not present in ubuntu, This function will work in remaining all
    #        other OS'es i.e redhat, sles and PowerKVM
    #
    # @param i_oslevel @type string: OS level
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_start_lm_sensor_svc(self, i_oslevel):
        if 'Ubuntu' in i_oslevel:
            pass
        else:
            try:
                # Start the lm_sensors service
                cmd = "/bin/systemctl stop  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl start  lm_sensors.service"
                self.host_run_command(cmd)
                cmd = "/bin/systemctl status  lm_sensors.service"
                res = self.host_run_command(cmd)
                return BMC_CONST.FW_SUCCESS
            except:
                l_msg = "loading lm_sensors service failed"
                print l_msg
                raise OpTestError(l_msg)

    ##
    # @brief It will clone latest linux git repository in i_dir directory
    #
    # @param i_dir @type string: directory where linux source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_linux_source(self, i_dir):
        l_msg = 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self._ssh_execute("rm -rf %s" % i_dir)
        self._ssh_execute("mkdir %s" % i_dir)
        try:
            print l_cmd
            res = self._ssh_execute(l_cmd)
            print res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning linux git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will load the module using modprobe and verify whether it is loaded or not
    #
    # @param i_module @type string: module name, which we want to load on host
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module(self, i_module):
        l_res = self.host_run_command("modprobe %s; echo $?" % i_module)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Error in loading the module %s, modprobe failed" % i_module
            print l_msg
            raise OpTestError(l_msg)
        l_res = self.host_run_command("lsmod | grep -i --color=never %s" % i_module)
        if l_res.__contains__(i_module):
            print "%s module is loaded" % i_module
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = " %s module is not loaded" % i_module
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will read real time clock(RTC) time using hwclock utility
    #
    # @return l_res @type string: return hwclock value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_hwclock(self):
        print "Reading the hwclock"
        l_res = self.host_run_command("hwclock -r;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return l_res
        else:
            l_msg = "Reading the hwclock failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will read system time using date utility (This will be mantained by kernel)
    #
    # @return l_res @type string: return system time value if command execution successfull
    #           else raise OpTestError
    #
    def host_read_systime(self):
        print "Reading system time using date utility"
        l_res = self.host_run_command("date;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return l_res
        else:
            l_msg = "Reading the system time failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will set hwclock time using the --date option
    #        format should be "2015-01-01 12:12:12"
    #
    # @param i_time @type string: this is the time for setting the hwclock
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_set_hwclock_time(self, i_time):
        print "Setting the hwclock time to %s" % i_time
        l_res = self.host_run_command("hwclock --set --date \'%s\';echo $?" % i_time)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "Setting the hwclock failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will load driver module on host based on the config option
    #        if config value m: built as a module
    #                        y: driver built into kernel itself
    #        else   raises OpTestError
    #
    # @param i_kernel @type string: kernel version to get config file
    #        i_config @type string: config option to check in config file
    #        i_module @type string: driver module to load on host based on config value
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_load_module_based_on_config(self, i_kernel, i_config, i_module):
        l_val = self.host_check_config(i_kernel, i_config)
        if l_val == 'm':
            self.host_load_module(i_module)
        elif l_val == 'y':
            print "Driver built into kernel itself"
        else:
            l_msg = "Config value is changed"
            print l_msg
            raise OpTestError(l_msg)
        return BMC_CONST.FW_SUCCESS

    ##
    # @brief This function will return the list of installed i2c buses on host in two formats
    #        list-by number Ex: ["0","1","2",....]
    #        list-by-name  Ex: ["i2c-0","i2c-1","i2c-2"....]
    #
    # @return l_list @type list: list of i2c buses by number
    #         l_list1 @type list: list of i2c buses by name
    #         or raise OpTestError if not able to get list of i2c buses
    #
    def host_get_list_of_i2c_buses(self):
        l_res = self.host_run_command("i2cdetect -l;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Not able to get list of i2c buses"
            print l_msg
            raise OpTestError(l_msg)
        l_res = self.host_run_command("i2cdetect -l | awk '{print $1}'")
        l_res = l_res.splitlines()
        # list by number Ex: ["0","1","2",....]
        l_list = []
        # list by name Ex: ["i2c-0","i2c-1"...]
        l_list1 = []
        for l_bus in l_res:
            matchObj = re.search("(i2c)-(\d{1,})", l_bus)
            if matchObj:
                l_list.append(matchObj.group(2))
                l_list1.append(l_bus)
            else:
                pass
        return l_list, l_list1

    ##
    # @brief This function will get information of EEPROM chips attached to the i2c buses
    #
    # @return l_res @type string: return EEPROM chips information
    #           else raise OpTestError
    #
    def host_get_info_of_eeprom_chips(self):
        print "Getting the information of EEPROM chips"
        l_res = self.host_run_command("dmesg | grep -i --color=never at24")
        if l_res.__contains__("at24"):
            pass
        else:
            l_res = self.host_run_command("dmesg -C")
            self.host_run_command("rmmod at24")
            self.host_load_module("at24")
            l_res = self.host_run_command("dmesg | grep -i --color=never at24")
            if l_res.__contains__("at24"):
                pass
            else:
                l_msg = "Not able to get at24 info"
                raise OpTestError(l_msg)
        return l_res

    ##
    # @brief It will return list with elements having pairs of eeprom chip addresses and
    #        corresponding i2c bus where the chip is attached. This information is getting
    #        through sysfs interface. format is ["0 0x50","0 0x51","1 0x51","1 0x52"....]
    #
    # @return l_chips @type list: list having pairs of i2c bus number and eeprom chip address.
    #           else raise OpTestError
    #
    def host_get_list_of_eeprom_chips(self):
        l_res = self.host_run_command("find /sys/ -name eeprom;echo $?")
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            pass
        else:
            l_msg = "Not able to get list of eeprom chip addresses through sysfs interface"
            print l_msg
            raise OpTestError(l_msg)
        l_chips = []
        for l_line in l_res:
            if l_line.__contains__("eeprom"):
                matchObj = re.search("/(\d{1,}-\d{4})/eeprom", l_line)
                if matchObj:
                    l_line = matchObj.group(1)
                    i_args = (l_line.replace("-", " "))
                    print i_args
                else:
                    continue
                i_args = re.sub(" 00", " 0x", i_args)
                l_chips.append(i_args)
                print i_args
        return l_chips

    ##
    # @brief The hexdump utility is used to display the specified files.
    #        This function will display in both ASCII+hexadecimal format.
    #
    # @param i_dev @type string: this is the file used as a input to hexdump for display info
    #                            Example file:"/sys/devices/platform/3fc0000000000.xscom:i2cm@a0000:i2c-bus@1/i2c-3/3-0050/eeprom"
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    #
    def host_hexdump(self, i_dev):
        l_res = self.host_run_command("hexdump -C %s;echo $?" % i_dev)
        l_res = l_res.splitlines()
        if int(l_res[-1]) == 0:
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "hexdump failed for device %s" % i_dev
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will clone latest skiboot git repository in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_skiboot_source(self, i_dir):
        l_msg = 'https://github.com/open-power/skiboot.git/'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning skiboot git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will compile xscom-utils in the skiboot directory which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_compile_xscom_utilities(self, i_dir):
        l_cmd = "cd %s/external/xscom-utils; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/external/xscom-utils/getscom; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary getscom is available"
        else:
            l_msg = "getscom bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)
        l_cmd = "test -f %s/external/xscom-utils/putscom; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary putscom is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "putscom bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will compile gard utility in the skiboot directory which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_compile_gard_utility(self, i_dir):
        l_cmd = "cd %s/external/gard; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/external/gard/gard; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary gard is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "gard bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function generates olog.json file from skiboot which is used for
    #        fwts olog test: Run OLOG scan and analysis checks.
    #
    # @param i_dir @type string: directory where skiboot source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_generate_fwts_olog_json(self, i_dir):
        l_cmd = "%s/external/fwts/generate-fwts-olog %s/ -o %s/olog.json" % (i_dir, i_dir, i_dir)
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/olog.json; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "olog.json is available in working directory %s" % i_dir
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "olog.json file is failed to create from skiboot"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief It will clone latest fwts git repository in i_dir directory
    #
    # @param i_dir @type string: directory where fwts source will be cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_clone_fwts_source(self, i_dir):
        l_msg = 'git://kernel.ubuntu.com/hwe/fwts.git'
        l_cmd = "git clone %s %s" % (l_msg, i_dir)
        self.host_run_command("git config --global http.sslverify false")
        self.host_run_command("rm -rf %s" % i_dir)
        self.host_run_command("mkdir %s" % i_dir)
        try:
            print l_cmd
            l_res = self.host_run_command(l_cmd)
            print l_res
            return BMC_CONST.FW_SUCCESS
        except:
            l_msg = "Cloning fwts git repository is failed"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function is used to build fwts tool in the fwts directory
    #        which was cloned in i_dir directory
    #
    # @param i_dir @type string: directory where fwts source was cloned
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_build_fwts_tool(self, i_dir):
        l_cmd = "cd %s/;autoreconf -ivf;./configure; make;" % i_dir
        print l_cmd
        l_res = self.host_run_command(l_cmd)
        l_cmd = "test -f %s/src/fwts; echo $?" % i_dir
        l_res = self.host_run_command(l_cmd)
        print l_res
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary fwts is available"
            return BMC_CONST.FW_SUCCESS
        else:
            l_msg = "fwts bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function is used to get detected pci devices in different user/machine readable formats
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_pci_devices(self):
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES1)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES2)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES3)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES4)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES5)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_DEVICES6)
        self.host_run_command(BMC_CONST.HOST_LIST_PCI_SYSFS_DEVICES)

    ##
    # @brief This function is used to get more pci devices info in verbose mode
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_get_pci_verbose_info(self):
        l_res = self.host_run_command(BMC_CONST.HOST_LIST_PCI_VERBOSE)
        return l_res

    ##
    # @brief This function is used to get minimum usb devices info
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_list_usb_devices(self):
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES1)
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES2)
        self.host_run_command(BMC_CONST.HOST_LIST_USB_DEVICES3)

    ##
    # @brief This function enable only a single core
    #
    # @return BMC_CONST.FW_SUCCESS or raise OpTestError
    #
    def host_enable_single_core(self):
        self.host_run_command("ppc64_cpu --cores-on=1")