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(ipmi_cipher_level=None, 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 with the following exception:
18
19    If ipmi_cipher_level is is anything but None, this function will continue
20    on and re-build the IPMI_EXT_CMD variable.
21
22    This keyword is designed for use by keywords which use the IPMI_EXT_CMD
23    variable (e.g. 'Run External IPMI Raw Command').  This keyword is
24    warranted because the ipmitool program may or may not accept the -U (i.e.
25    username) parameter depending on the version of code loaded on the BMC.
26    This keyword will determine whether the "-U" parameter should be used and
27    create IPMI_EXT_CMD accordingly.
28
29    Furthermore, this keyword will run the command to create the 'root' IPMI
30    username.
31
32    Description of argument(s):
33    # ipmi_cipher_level             IPMI cipher level value
34    #                               (e.g. "1", "2", "3", "15", "16", "17").
35    # quiet                         Indicates whether this keyword should run
36    #                               without any output to the console.
37    """
38
39    ipmi_ext_cmd = BuiltIn().get_variable_value("${IPMI_EXT_CMD}", "")
40    if ipmi_ext_cmd != "" and not ipmi_cipher_level:
41        return
42
43    quiet = int(gp.get_var_value(quiet, 0))
44    openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}")
45    ipmi_username = BuiltIn().get_variable_value("${IPMI_USERNAME}", "root")
46    ipmi_password = BuiltIn().get_variable_value("${IPMI_PASSWORD}",
47                                                 "0penBmc")
48    if not ipmi_cipher_level:
49        ipmi_cipher_level = BuiltIn().get_variable_value("${IPMI_CIPHER_LEVEL}",
50                                                         "3")
51
52    old_ipmi_ext_cmd = "ipmitool -I lanplus -C " + str(ipmi_cipher_level)\
53        + " -P " + ipmi_password
54    new_ipmi_ext_cmd = "ipmitool -I lanplus -C " + str(ipmi_cipher_level)\
55        + " -U " + ipmi_username + " -P " + ipmi_password
56    # Use a basic ipmitool command to help us determine whether the BMC will
57    # accept the -U parm.
58    ipmi_cmd = "power status"
59    ipmi_cmd_suffix = " -H " + openbmc_host + " " + ipmi_cmd
60    print_output = 0
61    cmd_buf = new_ipmi_ext_cmd + ipmi_cmd_suffix
62    new_rc, stdout = gc.shell_cmd(cmd_buf,
63                                  print_output=print_output,
64                                  show_err=0,
65                                  ignore_err=1)
66    gp.qprint_varx("rc", new_rc, 1)
67    if new_rc == 0:
68        ipmi_ext_cmd = new_ipmi_ext_cmd
69        BuiltIn().set_global_variable("${IPMI_EXT_CMD}", ipmi_ext_cmd)
70        return
71
72    cmd_buf = old_ipmi_ext_cmd + ipmi_cmd_suffix
73    old_rc, stdout = gc.shell_cmd(cmd_buf,
74                                  print_output=print_output,
75                                  show_err=0,
76                                  ignore_err=1)
77    gp.qprint_varx("rc", old_rc, 1)
78
79    if old_rc == 0:
80        ipmi_ext_cmd = old_ipmi_ext_cmd
81        BuiltIn().set_global_variable("${IPMI_EXT_CMD}", ipmi_ext_cmd)
82        return
83
84    message = "Unable to run ipmitool, (with or without the '-U' parm)."
85    BuiltIn().fail(message)
86
87
88build_ipmi_ext_cmd()
89