blob: 2327f755f1bbdfb340266e18c8b6503dee616f40 [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'
15ipmi_cipher_suite = BuiltIn().get_variable_value("${IPMI_CIPHER_LEVEL}", '3')
16ipmi_username = BuiltIn().get_variable_value("${IPMI_USERNAME}", "root")
17ipmi_password = BuiltIn().get_variable_value("${IPMI_PASSWORD}", "0penBmc")
18ipmi_host = BuiltIn().get_variable_value("${OPENBMC_HOST}")
19
20# Create a list of the required IPMI options.
21ipmi_required_options = ['I', 'C', 'U', 'P', 'H']
22# The following dictionary maps the ipmitool option names (e.g. "I") to our
23# more descriptive names (e.g. "interface") for the required options.
24ipmi_option_name_map = {
25 'I': 'interface',
26 'C': 'cipher_suite',
27 'U': 'username',
28 'P': 'password',
29 'H': 'host',
30}
31
32
33def create_ipmi_ext_command_string(command, **options):
Michael Walsh19621ba2018-12-03 17:16:02 -060034 r"""
Michael Walsh355197a2019-01-21 15:06:10 -060035 Create and return an IPMI external command string which is fit to be run
36 from a bash command line.
Michael Walsh19621ba2018-12-03 17:16:02 -060037
Michael Walsh355197a2019-01-21 15:06:10 -060038 Example:
George Keishingf4027652019-01-10 23:58:29 -060039
Michael Walsh355197a2019-01-21 15:06:10 -060040 ipmi_ext_cmd = create_ipmi_ext_command_string('power status')
Michael Walsh19621ba2018-12-03 17:16:02 -060041
Michael Walsh355197a2019-01-21 15:06:10 -060042 Result:
43 ipmitool -I lanplus -C 3 -P ******** -H x.x.x.x power status
Michael Walsh19621ba2018-12-03 17:16:02 -060044
Michael Walsh355197a2019-01-21 15:06:10 -060045 Example:
46
47 ipmi_ext_cmd = create_ipmi_ext_command_string('power status', C='4')
48
49 Result:
50 ipmitool -I lanplus -C 4 -P ******** -H x.x.x.x power status
Michael Walsh19621ba2018-12-03 17:16:02 -060051
52 Description of argument(s):
Michael Walsh355197a2019-01-21 15:06:10 -060053 command The ipmitool command (e.g. 'power status').
54 options Any desired options that are understood by
55 ipmitool (see iptmitool's help text for a
56 complete list). If the caller does NOT
57 provide any of several required options
58 (e.g. "P", i.e. password), this function
59 will include them on the caller's behalf
60 using default values.
Michael Walsh19621ba2018-12-03 17:16:02 -060061 """
62
Michael Walsh355197a2019-01-21 15:06:10 -060063 new_options = collections.OrderedDict()
64 for option in ipmi_required_options:
65 if option in options:
66 # If the caller has specified this particular option, use it in
67 # preference to the default value.
68 new_options[option] = options[option]
69 # Delete the value from the caller's options.
70 del options[option]
71 else:
72 # The caller hasn't specified this required option so specify it
73 # for them using the global value.
74 var_name = 'ipmi_' + ipmi_option_name_map[option]
75 value = eval(var_name)
76 new_options[option] = value
77 # Include the remainder of the caller's options in the new options
78 # dictionary.
79 for key, value in options.items():
80 new_options[key] = value
Michael Walsh19621ba2018-12-03 17:16:02 -060081
Michael Walsh355197a2019-01-21 15:06:10 -060082 return gc.create_command_string('ipmitool', command, new_options)
Michael Walsh19621ba2018-12-03 17:16:02 -060083
Michael Walsh355197a2019-01-21 15:06:10 -060084
85def verify_ipmi_user_parm_accepted():
86 r"""
87 Deterimine whether the OBMC accepts the '-U' ipmitool option and adjust
88 the global ipmi_required_options accordingly.
89 """
90
91 # Assumption: "U" is in the global ipmi_required_options.
92 global ipmi_required_options
Michael Walsh19621ba2018-12-03 17:16:02 -060093 print_output = 0
Michael Walsh355197a2019-01-21 15:06:10 -060094
95 command_string = create_ipmi_ext_command_string('power status')
96 rc, stdout = gc.shell_cmd(command_string,
97 print_output=print_output,
98 show_err=0,
99 ignore_err=1)
100 gp.qprint_var(rc, 1)
101 if rc == 0:
102 # The OBMC accepts the ipmitool "-U" option so new further work needs
103 # to be done.
Michael Walsh19621ba2018-12-03 17:16:02 -0600104 return
105
Michael Walsh355197a2019-01-21 15:06:10 -0600106 # Remove the "U" option from ipmi_required_options to allow us to create a
107 # command string without the "U" option.
108 if 'U' in ipmi_required_options:
109 del ipmi_required_options[ipmi_required_options.index('U')]
110 command_string = create_ipmi_ext_command_string('power status')
111 rc, stdout = gc.shell_cmd(command_string,
112 print_output=print_output,
113 show_err=0,
114 ignore_err=1)
115 gp.qprint_var(rc, 1)
116 if rc == 0:
117 # The "U" option has been removed from the ipmi_required_options
118 # global variable.
Michael Walsh19621ba2018-12-03 17:16:02 -0600119 return
120
Michael Walsh355197a2019-01-21 15:06:10 -0600121 message = "Unable to run ipmitool (with or without the '-U' option).\n"
122 gp.print_error(message)
123
124 # Revert to original ipmi_required_options by inserting 'U' right before
125 # 'P'.
126 ipmi_required_options.insert(ipmi_required_options.index('P'), 'U')
Michael Walsh19621ba2018-12-03 17:16:02 -0600127
128
Michael Walsh355197a2019-01-21 15:06:10 -0600129def ipmi_setup():
130 r"""
131 Perform all required setup for running iptmitool commands.
132 """
133
134 verify_ipmi_user_parm_accepted()
135
136
137ipmi_setup()
138
139
140def process_ipmi_user_options(command):
141 r"""
142 Return the buffer with any ipmi_user_options pre-pended.
143
144 Description of argument(s):
145 command An IPMI command (e.g. "power status").
146 """
147
148 ipmi_user_options = BuiltIn().get_variable_value("${IPMI_USER_OPTIONS}", '')
149 if ipmi_user_options == "":
150 return command
151 return ipmi_user_options + " " + command