blob: 9484c136c1eaa0c6cd241c81ca5b7e03e999afe5 [file] [log] [blame]
Michael Walsh19621ba2018-12-03 17:16:02 -06001#!/usr/bin/env python
2
3r"""
4A python companion file for ipmi_client.robot.
5"""
6
Michael Walsh355197a2019-01-21 15:06:10 -06007import collections
Michael Walsh19621ba2018-12-03 17:16:02 -06008import gen_print as gp
9import gen_cmd as gc
10from robot.libraries.BuiltIn import BuiltIn
11
12
Michael Walsh355197a2019-01-21 15:06:10 -060013# Set default values for required IPMI options.
14ipmi_interface = 'lanplus'
Rahul Maheshwari3aeae4e2020-04-03 07:45:50 -050015ipmi_cipher_suite = BuiltIn().get_variable_value("${IPMI_CIPHER_LEVEL}", '17')
George Keishing75f55dc2021-03-29 10:48:09 -050016ipmi_timeout = BuiltIn().get_variable_value("${IPMI_TIMEOUT}", '3')
George Keishinge33ad1d2019-12-09 11:17:36 -060017ipmi_port = BuiltIn().get_variable_value("${IPMI_PORT}", '623')
Michael Walsh355197a2019-01-21 15:06:10 -060018ipmi_username = BuiltIn().get_variable_value("${IPMI_USERNAME}", "root")
19ipmi_password = BuiltIn().get_variable_value("${IPMI_PASSWORD}", "0penBmc")
20ipmi_host = BuiltIn().get_variable_value("${OPENBMC_HOST}")
21
22# Create a list of the required IPMI options.
George Keishing75f55dc2021-03-29 10:48:09 -050023ipmi_required_options = ['I', 'C', 'N', 'p', 'U', 'P', 'H']
Michael Walsh355197a2019-01-21 15:06:10 -060024# The following dictionary maps the ipmitool option names (e.g. "I") to our
25# more descriptive names (e.g. "interface") for the required options.
26ipmi_option_name_map = {
27 'I': 'interface',
28 'C': 'cipher_suite',
George Keishing75f55dc2021-03-29 10:48:09 -050029 'N': 'timeout',
George Keishinge33ad1d2019-12-09 11:17:36 -060030 'p': 'port',
Michael Walsh355197a2019-01-21 15:06:10 -060031 'U': 'username',
32 'P': 'password',
33 'H': 'host',
34}
35
36
37def create_ipmi_ext_command_string(command, **options):
Michael Walsh19621ba2018-12-03 17:16:02 -060038 r"""
Michael Walsh355197a2019-01-21 15:06:10 -060039 Create and return an IPMI external command string which is fit to be run
40 from a bash command line.
Michael Walsh19621ba2018-12-03 17:16:02 -060041
Michael Walsh355197a2019-01-21 15:06:10 -060042 Example:
George Keishingf4027652019-01-10 23:58:29 -060043
Michael Walsh355197a2019-01-21 15:06:10 -060044 ipmi_ext_cmd = create_ipmi_ext_command_string('power status')
Michael Walsh19621ba2018-12-03 17:16:02 -060045
Michael Walsh355197a2019-01-21 15:06:10 -060046 Result:
George Keishinge33ad1d2019-12-09 11:17:36 -060047 ipmitool -I lanplus -C 3 -p 623 -P ******** -H x.x.x.x power status
Michael Walsh19621ba2018-12-03 17:16:02 -060048
Michael Walsh355197a2019-01-21 15:06:10 -060049 Example:
50
51 ipmi_ext_cmd = create_ipmi_ext_command_string('power status', C='4')
52
53 Result:
George Keishinge33ad1d2019-12-09 11:17:36 -060054 ipmitool -I lanplus -C 4 -p 623 -P ******** -H x.x.x.x power status
Michael Walsh19621ba2018-12-03 17:16:02 -060055
56 Description of argument(s):
Michael Walsh355197a2019-01-21 15:06:10 -060057 command The ipmitool command (e.g. 'power status').
58 options Any desired options that are understood by
59 ipmitool (see iptmitool's help text for a
60 complete list). If the caller does NOT
61 provide any of several required options
62 (e.g. "P", i.e. password), this function
63 will include them on the caller's behalf
64 using default values.
Michael Walsh19621ba2018-12-03 17:16:02 -060065 """
66
Michael Walsh355197a2019-01-21 15:06:10 -060067 new_options = collections.OrderedDict()
68 for option in ipmi_required_options:
69 if option in options:
70 # If the caller has specified this particular option, use it in
71 # preference to the default value.
72 new_options[option] = options[option]
73 # Delete the value from the caller's options.
74 del options[option]
75 else:
76 # The caller hasn't specified this required option so specify it
77 # for them using the global value.
78 var_name = 'ipmi_' + ipmi_option_name_map[option]
79 value = eval(var_name)
80 new_options[option] = value
81 # Include the remainder of the caller's options in the new options
82 # dictionary.
83 for key, value in options.items():
84 new_options[key] = value
Michael Walsh19621ba2018-12-03 17:16:02 -060085
Michael Walsh355197a2019-01-21 15:06:10 -060086 return gc.create_command_string('ipmitool', command, new_options)
Michael Walsh19621ba2018-12-03 17:16:02 -060087
Michael Walsh355197a2019-01-21 15:06:10 -060088
89def verify_ipmi_user_parm_accepted():
90 r"""
91 Deterimine whether the OBMC accepts the '-U' ipmitool option and adjust
92 the global ipmi_required_options accordingly.
93 """
94
95 # Assumption: "U" is in the global ipmi_required_options.
96 global ipmi_required_options
Michael Walsh19621ba2018-12-03 17:16:02 -060097 print_output = 0
Michael Walsh355197a2019-01-21 15:06:10 -060098
99 command_string = create_ipmi_ext_command_string('power status')
100 rc, stdout = gc.shell_cmd(command_string,
101 print_output=print_output,
102 show_err=0,
103 ignore_err=1)
104 gp.qprint_var(rc, 1)
105 if rc == 0:
106 # The OBMC accepts the ipmitool "-U" option so new further work needs
107 # to be done.
Michael Walsh19621ba2018-12-03 17:16:02 -0600108 return
109
Michael Walsh355197a2019-01-21 15:06:10 -0600110 # Remove the "U" option from ipmi_required_options to allow us to create a
111 # command string without the "U" option.
112 if 'U' in ipmi_required_options:
113 del ipmi_required_options[ipmi_required_options.index('U')]
114 command_string = create_ipmi_ext_command_string('power status')
115 rc, stdout = gc.shell_cmd(command_string,
116 print_output=print_output,
117 show_err=0,
118 ignore_err=1)
119 gp.qprint_var(rc, 1)
120 if rc == 0:
121 # The "U" option has been removed from the ipmi_required_options
122 # global variable.
Michael Walsh19621ba2018-12-03 17:16:02 -0600123 return
124
Michael Walsh355197a2019-01-21 15:06:10 -0600125 message = "Unable to run ipmitool (with or without the '-U' option).\n"
126 gp.print_error(message)
127
128 # Revert to original ipmi_required_options by inserting 'U' right before
129 # 'P'.
130 ipmi_required_options.insert(ipmi_required_options.index('P'), 'U')
Michael Walsh19621ba2018-12-03 17:16:02 -0600131
132
Michael Walsh355197a2019-01-21 15:06:10 -0600133def ipmi_setup():
134 r"""
135 Perform all required setup for running iptmitool commands.
136 """
137
138 verify_ipmi_user_parm_accepted()
139
140
141ipmi_setup()
142
143
144def process_ipmi_user_options(command):
145 r"""
146 Return the buffer with any ipmi_user_options pre-pended.
147
148 Description of argument(s):
149 command An IPMI command (e.g. "power status").
150 """
151
152 ipmi_user_options = BuiltIn().get_variable_value("${IPMI_USER_OPTIONS}", '')
153 if ipmi_user_options == "":
154 return command
155 return ipmi_user_options + " " + command