George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | A python companion file for ipmi_client.robot. |
| 5 | """ |
| 6 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 7 | import collections |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 9 | import gen_cmd as gc |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 10 | import gen_print as gp |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 11 | from robot.libraries.BuiltIn import BuiltIn |
| 12 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 13 | # Set default values for required IPMI options. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 14 | ipmi_interface = "lanplus" |
| 15 | ipmi_cipher_suite = BuiltIn().get_variable_value("${IPMI_CIPHER_LEVEL}", "17") |
| 16 | ipmi_timeout = BuiltIn().get_variable_value("${IPMI_TIMEOUT}", "3") |
| 17 | ipmi_port = BuiltIn().get_variable_value("${IPMI_PORT}", "623") |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 18 | ipmi_username = BuiltIn().get_variable_value("${IPMI_USERNAME}", "root") |
| 19 | ipmi_password = BuiltIn().get_variable_value("${IPMI_PASSWORD}", "0penBmc") |
| 20 | ipmi_host = BuiltIn().get_variable_value("${OPENBMC_HOST}") |
| 21 | |
| 22 | # Create a list of the required IPMI options. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 23 | ipmi_required_options = ["I", "C", "N", "p", "U", "P", "H"] |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 24 | # The following dictionary maps the ipmitool option names (e.g. "I") to our |
| 25 | # more descriptive names (e.g. "interface") for the required options. |
| 26 | ipmi_option_name_map = { |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 27 | "I": "interface", |
| 28 | "C": "cipher_suite", |
| 29 | "N": "timeout", |
| 30 | "p": "port", |
| 31 | "U": "username", |
| 32 | "P": "password", |
| 33 | "H": "host", |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | def create_ipmi_ext_command_string(command, **options): |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 38 | r""" |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 39 | Create and return an IPMI external command string which is fit to be run |
| 40 | from a bash command line. |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 41 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 42 | Example: |
George Keishing | f402765 | 2019-01-10 23:58:29 -0600 | [diff] [blame] | 43 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 44 | ipmi_ext_cmd = create_ipmi_ext_command_string('power status') |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 45 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 46 | Result: |
George Keishing | e33ad1d | 2019-12-09 11:17:36 -0600 | [diff] [blame] | 47 | ipmitool -I lanplus -C 3 -p 623 -P ******** -H x.x.x.x power status |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 48 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 49 | Example: |
| 50 | |
| 51 | ipmi_ext_cmd = create_ipmi_ext_command_string('power status', C='4') |
| 52 | |
| 53 | Result: |
George Keishing | e33ad1d | 2019-12-09 11:17:36 -0600 | [diff] [blame] | 54 | ipmitool -I lanplus -C 4 -p 623 -P ******** -H x.x.x.x power status |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 55 | |
| 56 | Description of argument(s): |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 57 | 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 Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 65 | """ |
| 66 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 67 | new_options = collections.OrderedDict() |
| 68 | for option in ipmi_required_options: |
George Keishing | 2b32c10 | 2021-03-29 14:59:00 -0500 | [diff] [blame] | 69 | # This is to prevent boot table "-N 10" vs user input timeout. |
| 70 | if " -N " in command and option == "N": |
| 71 | continue |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 72 | if option in options: |
| 73 | # If the caller has specified this particular option, use it in |
| 74 | # preference to the default value. |
| 75 | new_options[option] = options[option] |
| 76 | # Delete the value from the caller's options. |
| 77 | del options[option] |
| 78 | else: |
| 79 | # The caller hasn't specified this required option so specify it |
| 80 | # for them using the global value. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 81 | var_name = "ipmi_" + ipmi_option_name_map[option] |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 82 | value = eval(var_name) |
| 83 | new_options[option] = value |
| 84 | # Include the remainder of the caller's options in the new options |
| 85 | # dictionary. |
| 86 | for key, value in options.items(): |
| 87 | new_options[key] = value |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 88 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 89 | return gc.create_command_string("ipmitool", command, new_options) |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 90 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 91 | |
| 92 | def verify_ipmi_user_parm_accepted(): |
| 93 | r""" |
George Keishing | 9bfdf8a | 2024-04-19 11:09:07 +0530 | [diff] [blame] | 94 | Determine whether the OBMC accepts the '-U' ipmitool option and adjust |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 95 | the global ipmi_required_options accordingly. |
| 96 | """ |
| 97 | |
| 98 | # Assumption: "U" is in the global ipmi_required_options. |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 99 | print_output = 0 |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 100 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 101 | command_string = create_ipmi_ext_command_string("power status") |
| 102 | rc, stdout = gc.shell_cmd( |
| 103 | command_string, print_output=print_output, show_err=0, ignore_err=1 |
| 104 | ) |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 105 | gp.qprint_var(rc, 1) |
| 106 | if rc == 0: |
| 107 | # The OBMC accepts the ipmitool "-U" option so new further work needs |
| 108 | # to be done. |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 109 | return |
| 110 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 111 | # Remove the "U" option from ipmi_required_options to allow us to create a |
| 112 | # command string without the "U" option. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 113 | if "U" in ipmi_required_options: |
| 114 | del ipmi_required_options[ipmi_required_options.index("U")] |
| 115 | command_string = create_ipmi_ext_command_string("power status") |
| 116 | rc, stdout = gc.shell_cmd( |
| 117 | command_string, print_output=print_output, show_err=0, ignore_err=1 |
| 118 | ) |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 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 Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 123 | return |
| 124 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 125 | 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'. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 130 | ipmi_required_options.insert(ipmi_required_options.index("P"), "U") |
Michael Walsh | 19621ba | 2018-12-03 17:16:02 -0600 | [diff] [blame] | 131 | |
| 132 | |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 133 | def ipmi_setup(): |
| 134 | r""" |
| 135 | Perform all required setup for running iptmitool commands. |
| 136 | """ |
| 137 | |
| 138 | verify_ipmi_user_parm_accepted() |
| 139 | |
| 140 | |
| 141 | ipmi_setup() |
| 142 | |
| 143 | |
| 144 | def process_ipmi_user_options(command): |
| 145 | r""" |
George Keishing | e16f158 | 2022-12-15 07:32:21 -0600 | [diff] [blame] | 146 | Return the buffer with any ipmi_user_options prepended. |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 147 | |
| 148 | Description of argument(s): |
| 149 | command An IPMI command (e.g. "power status"). |
| 150 | """ |
| 151 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 152 | ipmi_user_options = BuiltIn().get_variable_value( |
| 153 | "${IPMI_USER_OPTIONS}", "" |
| 154 | ) |
Michael Walsh | 355197a | 2019-01-21 15:06:10 -0600 | [diff] [blame] | 155 | if ipmi_user_options == "": |
| 156 | return command |
| 157 | return ipmi_user_options + " " + command |