xref: /openbmc/openbmc-test-automation/lib/ipmi_client.py (revision f329f73269a3a4e50d12d1681596447e7fb3e6cd)
1#!/usr/bin/env python
2
3r"""
4A python companion file for ipmi_client.robot.
5"""
6
7import gen_print as gp
8import gen_cmd as gc
9from robot.libraries.BuiltIn import BuiltIn
10
11
12def build_ipmi_ext_cmd(quiet=None):
13    r"""
14    Build the global IPMI_EXT_CMD variable.
15
16    If global variable IPMI_EXT_CMD already has a value, this keyword will
17    simply return without taking any action.
18
19    This keyword is designed for use by keywords which use the IPMI_EXT_CMD
20    variable (e.g. 'Run External IPMI Raw Command').  This keyword is
21    warranted because the ipmitool program may or may not accept the -U (i.e.
22    username) parameter depending on the version of code loaded on the BMC.
23    This keyword will determine whether the "-U" parameter should be used and
24    create IPMI_EXT_CMD accordingly.
25
26    Furthermore, this keyword will run the command to create the 'root' IPMI
27    username.
28
29    Description of argument(s):
30    # quiet                         Indicates whether this keyword should run
31    #                               without any output to the console.
32    """
33
34    ipmi_ext_cmd = BuiltIn().get_variable_value("${IPMI_EXT_CMD}", "")
35    if ipmi_ext_cmd != "":
36        return
37
38    quiet = int(gp.get_var_value(quiet, 0))
39    openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}")
40    ipmi_username = BuiltIn().get_variable_value("${IPMI_USERNAME}", "root")
41    ipmi_password = BuiltIn().get_variable_value("${IPMI_PASSWORD}",
42                                                 "0penBmc")
43    ipmi_cipher_level = BuiltIn().get_variable_value("${IPMI_CIPHER_LEVEL}",
44                                                     "3")
45
46    old_ipmi_ext_cmd = "ipmitool -I lanplus -C " + str(ipmi_cipher_level)\
47        + " -P " + ipmi_password
48    new_ipmi_ext_cmd = "ipmitool -I lanplus -C " + str(ipmi_cipher_level)\
49        + " -U " + ipmi_username + " -P " + ipmi_password
50    # Use a basic ipmitool command to help us determine whether the BMC will
51    # accept the -U parm.
52    ipmi_cmd = "power status"
53    ipmi_cmd_suffix = " -H " + openbmc_host + " " + ipmi_cmd
54    print_output = 0
55    cmd_buf = new_ipmi_ext_cmd + ipmi_cmd_suffix
56    new_rc, stdout = gc.shell_cmd(cmd_buf,
57                                  print_output=print_output,
58                                  show_err=0,
59                                  ignore_err=1)
60    gp.qprint_varx("rc", new_rc, 1)
61    if new_rc == 0:
62        ipmi_ext_cmd = new_ipmi_ext_cmd
63        BuiltIn().set_global_variable("${IPMI_EXT_CMD}", ipmi_ext_cmd)
64        return
65
66    cmd_buf = old_ipmi_ext_cmd + ipmi_cmd_suffix
67    old_rc, stdout = gc.shell_cmd(cmd_buf,
68                                  print_output=print_output,
69                                  show_err=0,
70                                  ignore_err=1)
71    gp.qprint_varx("rc", old_rc, 1)
72
73    if old_rc == 0:
74        ipmi_ext_cmd = old_ipmi_ext_cmd
75        BuiltIn().set_global_variable("${IPMI_EXT_CMD}", ipmi_ext_cmd)
76        return
77
78    message = "Unable to run ipmitool, (with or without the '-U' parm)."
79    BuiltIn().fail(message)
80
81
82build_ipmi_ext_cmd()
83