Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module contains functions having to do with machine state: get_state, |
| 5 | check_state, wait_state, etc. |
| 6 | |
| 7 | The 'State' is a composite of many pieces of data. Therefore, the functions |
| 8 | in this module define state as an ordered dictionary. Here is an example of |
| 9 | some test output showing machine state: |
| 10 | |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 11 | default_state: |
Michael Walsh | 65b1254 | 2017-02-03 15:34:38 -0600 | [diff] [blame] | 12 | default_state[chassis]: On |
Michael Walsh | 01975fa | 2017-08-20 20:51:36 -0500 | [diff] [blame] | 13 | default_state[boot_progress]: OSStart |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 14 | default_state[operating_system]: BootComplete |
Michael Walsh | 65b1254 | 2017-02-03 15:34:38 -0600 | [diff] [blame] | 15 | default_state[host]: Running |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 16 | default_state[os_ping]: 1 |
| 17 | default_state[os_login]: 1 |
| 18 | default_state[os_run_cmd]: 1 |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 19 | |
| 20 | Different users may very well have different needs when inquiring about |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 21 | state. Support for new pieces of state information may be added to this |
| 22 | module as needed. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 23 | |
| 24 | By using the wait_state function, a caller can start a boot and then wait for |
| 25 | a precisely defined state to indicate that the boot has succeeded. If |
| 26 | the boot fails, they can see exactly why by looking at the current state as |
| 27 | compared with the expected state. |
| 28 | """ |
| 29 | |
| 30 | import gen_print as gp |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 31 | import gen_valid as gv |
Michael Walsh | 16cbb7f | 2017-02-02 15:54:16 -0600 | [diff] [blame] | 32 | import gen_robot_utils as gru |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 33 | import gen_cmd as gc |
Michael Walsh | 6a9bd14 | 2018-07-24 16:10:29 -0500 | [diff] [blame] | 34 | import bmc_ssh_utils as bsu |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 35 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 36 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 37 | from robot.utils import DotDict |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 38 | |
| 39 | import re |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 40 | import os |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 41 | import sys |
| 42 | import imp |
| 43 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 44 | |
Michael Walsh | 5a5868a | 2018-10-31 15:20:04 -0500 | [diff] [blame] | 45 | # NOTE: Avoid importing utils.robot because utils.robot imports state.py |
| 46 | # (indirectly) which will cause failures. |
| 47 | gru.my_import_resource("rest_client.robot") |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 48 | |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 49 | base_path = os.path.dirname(os.path.dirname( |
| 50 | imp.find_module("gen_robot_print")[1])) + os.sep |
| 51 | sys.path.append(base_path + "data/") |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 52 | |
Michael Walsh | 940d691 | 2017-10-27 12:32:33 -0500 | [diff] [blame] | 53 | # Previously, I had this coded: |
| 54 | # import variables as var |
| 55 | # However, we ran into a problem where a robot program did this... |
| 56 | # Variables ../../lib/ras/variables.py |
| 57 | # Prior to doing this... |
| 58 | # Library ../lib/state.py |
| 59 | |
| 60 | # This caused the wrong variables.py file to be selected. Attempts to fix this |
| 61 | # have failed so far. For the moment, we will hard-code the value we need from |
| 62 | # the file. |
| 63 | |
| 64 | SYSTEM_STATE_URI = "/xyz/openbmc_project/state/" |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 65 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 66 | # The BMC code has recently been changed as far as what states are defined and |
| 67 | # what the state values can be. This module now has a means of processing both |
| 68 | # the old style state (i.e. OBMC_STATES_VERSION = 0) and the new style (i.e. |
Michael Walsh | 16cbb7f | 2017-02-02 15:54:16 -0600 | [diff] [blame] | 69 | # OBMC_STATES_VERSION = 1). |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 70 | # The caller can set environment variable OBMC_STATES_VERSION to dictate |
| 71 | # whether we're processing old or new style states. If OBMC_STATES_VERSION is |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 72 | # not set it will default to 1. |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 73 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 74 | # As of the present moment, OBMC_STATES_VERSION of 0 is for cold that is so old |
| 75 | # that it is no longer worthwhile to maintain. The OBMC_STATES_VERSION 0 code |
| 76 | # is being removed but the OBMC_STATES_VERSION value will stay for now in the |
| 77 | # event that it is needed in the future. |
| 78 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 79 | OBMC_STATES_VERSION = int(os.environ.get('OBMC_STATES_VERSION', 1)) |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 80 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 81 | # When a user calls get_state w/o specifying req_states, default_req_states |
| 82 | # is used as its value. |
| 83 | default_req_states = ['rest', |
| 84 | 'chassis', |
| 85 | 'bmc', |
| 86 | 'boot_progress', |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 87 | 'operating_system', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 88 | 'host', |
| 89 | 'os_ping', |
| 90 | 'os_login', |
| 91 | 'os_run_cmd'] |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 92 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 93 | # valid_req_states is a list of sub states supported by the get_state function. |
| 94 | # valid_req_states, default_req_states and master_os_up_match are used by the |
| 95 | # get_state function. |
| 96 | valid_req_states = ['ping', |
| 97 | 'packet_loss', |
| 98 | 'uptime', |
| 99 | 'epoch_seconds', |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 100 | 'elapsed_boot_time', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 101 | 'rest', |
| 102 | 'chassis', |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 103 | 'requested_chassis', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 104 | 'bmc', |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 105 | 'requested_bmc', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 106 | 'boot_progress', |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 107 | 'operating_system', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 108 | 'host', |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 109 | 'requested_host', |
| 110 | 'attempts_left', |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 111 | 'os_ping', |
| 112 | 'os_login', |
| 113 | 'os_run_cmd'] |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 114 | |
| 115 | # valid_os_req_states and default_os_req_states are used by the os_get_state |
| 116 | # function. |
| 117 | # valid_os_req_states is a list of state information supported by the |
| 118 | # get_os_state function. |
| 119 | valid_os_req_states = ['os_ping', |
| 120 | 'os_login', |
| 121 | 'os_run_cmd'] |
| 122 | # When a user calls get_os_state w/o specifying req_states, |
| 123 | # default_os_req_states is used as its value. |
| 124 | default_os_req_states = ['os_ping', |
| 125 | 'os_login', |
| 126 | 'os_run_cmd'] |
| 127 | |
| 128 | # Presently, some BMCs appear to not keep time very well. This environment |
| 129 | # variable directs the get_state function to use either the BMC's epoch time |
| 130 | # or the local epoch time. |
| 131 | USE_BMC_EPOCH_TIME = int(os.environ.get('USE_BMC_EPOCH_TIME', 0)) |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 132 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 133 | # Useful state constant definition(s). |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 134 | # default_state is an initial value which may be of use to callers. |
| 135 | default_state = DotDict([('rest', '1'), |
| 136 | ('chassis', 'On'), |
| 137 | ('bmc', 'Ready'), |
Michael Walsh | 01975fa | 2017-08-20 20:51:36 -0500 | [diff] [blame] | 138 | ('boot_progress', 'OSStart'), |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 139 | ('operating_system', 'BootComplete'), |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 140 | ('host', 'Running'), |
| 141 | ('os_ping', '1'), |
| 142 | ('os_login', '1'), |
| 143 | ('os_run_cmd', '1')]) |
| 144 | |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 145 | # A match state for checking that the system is at "standby". |
| 146 | standby_match_state = DotDict([('rest', '^1$'), |
| 147 | ('chassis', '^Off$'), |
| 148 | ('bmc', '^Ready$'), |
George Keishing | 6a69d26 | 2019-04-03 03:45:27 -0500 | [diff] [blame] | 149 | ('boot_progress', '^Off|Unspecified$'), |
| 150 | ('operating_system', '^Inactive$'), |
| 151 | ('host', '^Off$')]) |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 152 | |
| 153 | # A match state for checking that the system is at "os running". |
| 154 | os_running_match_state = DotDict([('chassis', '^On$'), |
| 155 | ('bmc', '^Ready$'), |
| 156 | ('boot_progress', |
| 157 | 'FW Progress, Starting OS|OSStart'), |
| 158 | ('operating_system', 'BootComplete'), |
| 159 | ('host', '^Running$'), |
| 160 | ('os_ping', '^1$'), |
| 161 | ('os_login', '^1$'), |
| 162 | ('os_run_cmd', '^1$')]) |
| 163 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 164 | # A master dictionary to determine whether the os may be up. |
| 165 | master_os_up_match = DotDict([('chassis', '^On$'), |
| 166 | ('bmc', '^Ready$'), |
| 167 | ('boot_progress', |
Michael Walsh | 01975fa | 2017-08-20 20:51:36 -0500 | [diff] [blame] | 168 | 'FW Progress, Starting OS|OSStart'), |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 169 | ('operating_system', 'BootComplete'), |
Michael Walsh | 192d5e7 | 2018-11-01 14:09:11 -0500 | [diff] [blame] | 170 | ('host', '^Running|Quiesced$')]) |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 171 | |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 172 | invalid_state_match = DotDict([('rest', '^$'), |
| 173 | ('chassis', '^$'), |
| 174 | ('bmc', '^$'), |
| 175 | ('boot_progress', '^$'), |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 176 | ('operating_system', '^$'), |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 177 | ('host', '^$')]) |
| 178 | |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 179 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 180 | def return_state_constant(state_name='default_state'): |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 181 | r""" |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 182 | Return the named state dictionary constant. |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 183 | """ |
| 184 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 185 | return eval(state_name) |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 186 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 187 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 188 | def anchor_state(state): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 189 | r""" |
| 190 | Add regular expression anchors ("^" and "$") to the beginning and end of |
| 191 | each item in the state dictionary passed in. Return the resulting |
| 192 | dictionary. |
| 193 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 194 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 195 | state A dictionary such as the one returned by the get_state() |
| 196 | function. |
| 197 | """ |
| 198 | |
Michael Walsh | 2ce067a | 2017-02-27 14:24:07 -0600 | [diff] [blame] | 199 | anchored_state = state.copy() |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 200 | for key in anchored_state.keys(): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 201 | anchored_state[key] = "^" + str(anchored_state[key]) + "$" |
| 202 | |
| 203 | return anchored_state |
| 204 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 205 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 206 | def strip_anchor_state(state): |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 207 | r""" |
| 208 | Strip regular expression anchors ("^" and "$") from the beginning and end |
| 209 | of each item in the state dictionary passed in. Return the resulting |
| 210 | dictionary. |
| 211 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 212 | Description of argument(s): |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 213 | state A dictionary such as the one returned by the get_state() |
| 214 | function. |
| 215 | """ |
| 216 | |
Michael Walsh | 2ce067a | 2017-02-27 14:24:07 -0600 | [diff] [blame] | 217 | stripped_state = state.copy() |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 218 | for key in stripped_state.keys(): |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 219 | stripped_state[key] = stripped_state[key].strip("^$") |
| 220 | |
| 221 | return stripped_state |
| 222 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 223 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 224 | def expressions_key(): |
| 225 | r""" |
| 226 | Return expressions key constant. |
| 227 | """ |
| 228 | return '<expressions>' |
| 229 | |
| 230 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 231 | def compare_states(state, |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 232 | match_state, |
| 233 | match_type='and'): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 234 | r""" |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 235 | Compare 2 state dictionaries. Return True if they match and False if they |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 236 | don't. Note that the match_state dictionary does not need to have an entry |
| 237 | corresponding to each entry in the state dictionary. But for each entry |
| 238 | that it does have, the corresponding state entry will be checked for a |
| 239 | match. |
| 240 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 241 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 242 | state A state dictionary such as the one returned by the |
| 243 | get_state function. |
| 244 | match_state A dictionary whose key/value pairs are "state field"/ |
| 245 | "state value". The state value is interpreted as a |
| 246 | regular expression. Every value in this dictionary is |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 247 | considered. When match_type is 'and', if each and every |
| 248 | comparison matches, the two dictionaries are considered to |
| 249 | be matching. If match_type is 'or', if any two of the |
| 250 | elements compared match, the two dictionaries are |
| 251 | considered to be matching. |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 252 | |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 253 | This value may also be any string accepted by |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 254 | return_state_constant (e.g. "standby_match_state"). In |
| 255 | such a case this function will call return_state_constant |
| 256 | to convert it to a proper dictionary as described above. |
| 257 | |
| 258 | Finally, one special value is accepted for the key field: |
| 259 | expression_key(). If such an entry exists, its value is |
| 260 | taken to be a list of expressions to be evaluated. These |
| 261 | expressions may reference state dictionary entries by |
| 262 | simply coding them in standard python syntax (e.g. |
| 263 | state['key1']). What follows is an example expression: |
| 264 | |
| 265 | "int(float(state['uptime'])) < int(state['elapsed_boot_time'])" |
| 266 | |
| 267 | In this example, if the state dictionary's 'uptime' entry |
| 268 | is less than its 'elapsed_boot_time' entry, it would |
| 269 | qualify as a match. |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 270 | match_type This may be 'and' or 'or'. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 271 | """ |
| 272 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 273 | error_message = gv.valid_value(match_type, valid_values=['and', 'or']) |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 274 | if error_message != "": |
| 275 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 276 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 277 | try: |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 278 | match_state = return_state_constant(match_state) |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 279 | except TypeError: |
| 280 | pass |
Michael Walsh | 7dc885b | 2018-03-14 17:51:59 -0500 | [diff] [blame] | 281 | |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 282 | default_match = (match_type == 'and') |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 283 | for key, match_state_value in match_state.items(): |
Michael Walsh | 97df71c | 2017-03-27 14:33:24 -0500 | [diff] [blame] | 284 | # Blank match_state_value means "don't care". |
| 285 | if match_state_value == "": |
| 286 | continue |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 287 | if key == expressions_key(): |
| 288 | for expr in match_state_value: |
| 289 | # Use python interpreter to evaluate the expression. |
| 290 | match = eval(expr) |
| 291 | if match != default_match: |
| 292 | return match |
| 293 | else: |
| 294 | try: |
| 295 | match = (re.match(match_state_value, str(state[key])) is not None) |
| 296 | except KeyError: |
| 297 | match = False |
| 298 | if match != default_match: |
| 299 | return match |
Michael Walsh | 45ca6e4 | 2017-09-14 17:29:12 -0500 | [diff] [blame] | 300 | |
| 301 | return default_match |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 302 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 303 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 304 | def get_os_state(os_host="", |
| 305 | os_username="", |
| 306 | os_password="", |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 307 | req_states=default_os_req_states, |
| 308 | os_up=True, |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 309 | quiet=None): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 310 | r""" |
| 311 | Get component states for the operating system such as ping, login, |
| 312 | etc, put them into a dictionary and return them to the caller. |
| 313 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 314 | Note that all substate values are strings. |
| 315 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 316 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 317 | os_host The DNS name or IP address of the operating system. |
| 318 | This defaults to global ${OS_HOST}. |
| 319 | os_username The username to be used to login to the OS. |
| 320 | This defaults to global ${OS_USERNAME}. |
| 321 | os_password The password to be used to login to the OS. |
| 322 | This defaults to global ${OS_PASSWORD}. |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 323 | req_states This is a list of states whose values are being requested by |
| 324 | the caller. |
| 325 | os_up If the caller knows that the os can't possibly be up, it can |
| 326 | improve performance by passing os_up=False. This function |
| 327 | will then simply return default values for all requested os |
| 328 | sub states. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 329 | quiet Indicates whether status details (e.g. curl commands) should |
| 330 | be written to the console. |
| 331 | Defaults to either global value of ${QUIET} or to 1. |
| 332 | """ |
| 333 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 334 | quiet = int(gp.get_var_value(quiet, 0)) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 335 | |
| 336 | # Set parm defaults where necessary and validate all parms. |
| 337 | if os_host == "": |
| 338 | os_host = BuiltIn().get_variable_value("${OS_HOST}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 339 | error_message = gv.valid_value(os_host, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 340 | if error_message != "": |
| 341 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 342 | |
| 343 | if os_username == "": |
| 344 | os_username = BuiltIn().get_variable_value("${OS_USERNAME}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 345 | error_message = gv.valid_value(os_username, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 346 | if error_message != "": |
| 347 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 348 | |
| 349 | if os_password == "": |
| 350 | os_password = BuiltIn().get_variable_value("${OS_PASSWORD}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 351 | error_message = gv.valid_value(os_password, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 352 | if error_message != "": |
| 353 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 354 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 355 | invalid_req_states = [sub_state for sub_state in req_states |
| 356 | if sub_state not in valid_os_req_states] |
| 357 | if len(invalid_req_states) > 0: |
| 358 | error_message = "The following req_states are not supported:\n" +\ |
| 359 | gp.sprint_var(invalid_req_states) |
| 360 | BuiltIn().fail(gp.sprint_error(error_message)) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 361 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 362 | # Initialize all substate values supported by this function. |
| 363 | os_ping = 0 |
| 364 | os_login = 0 |
| 365 | os_run_cmd = 0 |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 366 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 367 | if os_up: |
| 368 | if 'os_ping' in req_states: |
| 369 | # See if the OS pings. |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 370 | rc, out_buf = gc.shell_cmd("ping -c 1 -w 2 " + os_host, |
| 371 | print_output=0, show_err=0, |
| 372 | ignore_err=1) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 373 | if rc == 0: |
| 374 | os_ping = 1 |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 375 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 376 | # Programming note: All attributes which do not require an ssh login |
| 377 | # should have been processed by this point. |
| 378 | master_req_login = ['os_login', 'os_run_cmd'] |
| 379 | req_login = [sub_state for sub_state in req_states if sub_state in |
| 380 | master_req_login] |
Michael Walsh | 97df71c | 2017-03-27 14:33:24 -0500 | [diff] [blame] | 381 | must_login = (len(req_login) > 0) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 382 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 383 | if must_login: |
Michael Walsh | 6a9bd14 | 2018-07-24 16:10:29 -0500 | [diff] [blame] | 384 | output, stderr, rc = bsu.os_execute_command("uptime", quiet=quiet, |
Michael Walsh | 7fc3397 | 2018-08-07 14:55:03 -0500 | [diff] [blame] | 385 | ignore_err=1, |
| 386 | time_out=20) |
Michael Walsh | 6a9bd14 | 2018-07-24 16:10:29 -0500 | [diff] [blame] | 387 | if rc == 0: |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 388 | os_login = 1 |
Michael Walsh | 6a9bd14 | 2018-07-24 16:10:29 -0500 | [diff] [blame] | 389 | os_run_cmd = 1 |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 390 | else: |
Michael Walsh | 6a9bd14 | 2018-07-24 16:10:29 -0500 | [diff] [blame] | 391 | gp.dprint_vars(output, stderr) |
| 392 | gp.dprint_vars(rc, 1) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 393 | |
| 394 | os_state = DotDict() |
| 395 | for sub_state in req_states: |
| 396 | cmd_buf = "os_state['" + sub_state + "'] = str(" + sub_state + ")" |
| 397 | exec(cmd_buf) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 398 | |
| 399 | return os_state |
| 400 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 401 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 402 | def get_state(openbmc_host="", |
| 403 | openbmc_username="", |
| 404 | openbmc_password="", |
| 405 | os_host="", |
| 406 | os_username="", |
| 407 | os_password="", |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 408 | req_states=default_req_states, |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 409 | quiet=None): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 410 | r""" |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 411 | Get component states such as chassis state, bmc state, etc, put them into a |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 412 | dictionary and return them to the caller. |
| 413 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 414 | Note that all substate values are strings. |
| 415 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 416 | Note: If elapsed_boot_time is included in req_states, it is the caller's |
| 417 | duty to call set_start_boot_seconds() in order to set global |
| 418 | start_boot_seconds. elapsed_boot_time is the current time minus |
| 419 | start_boot_seconds. |
| 420 | |
| 421 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 422 | openbmc_host The DNS name or IP address of the BMC. |
| 423 | This defaults to global ${OPENBMC_HOST}. |
| 424 | openbmc_username The username to be used to login to the BMC. |
| 425 | This defaults to global ${OPENBMC_USERNAME}. |
| 426 | openbmc_password The password to be used to login to the BMC. |
| 427 | This defaults to global ${OPENBMC_PASSWORD}. |
| 428 | os_host The DNS name or IP address of the operating system. |
| 429 | This defaults to global ${OS_HOST}. |
| 430 | os_username The username to be used to login to the OS. |
| 431 | This defaults to global ${OS_USERNAME}. |
| 432 | os_password The password to be used to login to the OS. |
| 433 | This defaults to global ${OS_PASSWORD}. |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 434 | req_states This is a list of states whose values are being requested |
| 435 | by the caller. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 436 | quiet Indicates whether status details (e.g. curl commands) |
| 437 | should be written to the console. |
| 438 | Defaults to either global value of ${QUIET} or to 1. |
| 439 | """ |
| 440 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 441 | quiet = int(gp.get_var_value(quiet, 0)) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 442 | |
| 443 | # Set parm defaults where necessary and validate all parms. |
| 444 | if openbmc_host == "": |
| 445 | openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 446 | error_message = gv.valid_value(openbmc_host, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 447 | if error_message != "": |
| 448 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 449 | |
| 450 | if openbmc_username == "": |
| 451 | openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 452 | error_message = gv.valid_value(openbmc_username, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 453 | if error_message != "": |
| 454 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 455 | |
| 456 | if openbmc_password == "": |
| 457 | openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}") |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 458 | error_message = gv.valid_value(openbmc_password, invalid_values=[None, ""]) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 459 | if error_message != "": |
| 460 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 461 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 462 | # NOTE: OS parms are optional. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 463 | if os_host == "": |
| 464 | os_host = BuiltIn().get_variable_value("${OS_HOST}") |
| 465 | if os_host is None: |
| 466 | os_host = "" |
| 467 | |
| 468 | if os_username is "": |
| 469 | os_username = BuiltIn().get_variable_value("${OS_USERNAME}") |
| 470 | if os_username is None: |
| 471 | os_username = "" |
| 472 | |
| 473 | if os_password is "": |
| 474 | os_password = BuiltIn().get_variable_value("${OS_PASSWORD}") |
| 475 | if os_password is None: |
| 476 | os_password = "" |
| 477 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 478 | invalid_req_states = [sub_state for sub_state in req_states |
| 479 | if sub_state not in valid_req_states] |
| 480 | if len(invalid_req_states) > 0: |
| 481 | error_message = "The following req_states are not supported:\n" +\ |
| 482 | gp.sprint_var(invalid_req_states) |
| 483 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 484 | |
| 485 | # Initialize all substate values supported by this function. |
| 486 | ping = 0 |
| 487 | packet_loss = '' |
| 488 | uptime = '' |
| 489 | epoch_seconds = '' |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 490 | elapsed_boot_time = '' |
Michael Walsh | 2b269de | 2017-10-09 11:18:11 -0500 | [diff] [blame] | 491 | rest = '' |
| 492 | chassis = '' |
| 493 | requested_chassis = '' |
| 494 | bmc = '' |
| 495 | requested_bmc = '' |
| 496 | boot_progress = '' |
| 497 | operating_system = '' |
| 498 | host = '' |
| 499 | requested_host = '' |
| 500 | attempts_left = '' |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 501 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 502 | # Get the component states. |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 503 | if 'ping' in req_states: |
| 504 | # See if the OS pings. |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 505 | rc, out_buf = gc.shell_cmd("ping -c 1 -w 2 " + openbmc_host, |
| 506 | print_output=0, show_err=0, |
| 507 | ignore_err=1) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 508 | if rc == 0: |
| 509 | ping = 1 |
| 510 | |
| 511 | if 'packet_loss' in req_states: |
| 512 | # See if the OS pings. |
| 513 | cmd_buf = "ping -c 5 -w 5 " + openbmc_host +\ |
| 514 | " | egrep 'packet loss' | sed -re 's/.* ([0-9]+)%.*/\\1/g'" |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 515 | rc, out_buf = gc.shell_cmd(cmd_buf, |
| 516 | print_output=0, show_err=0, |
| 517 | ignore_err=1) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 518 | if rc == 0: |
| 519 | packet_loss = out_buf.rstrip("\n") |
| 520 | |
Michael Walsh | e53e47a | 2017-06-30 17:03:24 -0500 | [diff] [blame] | 521 | if 'uptime' in req_states: |
Michael Walsh | fa76593 | 2017-10-13 14:07:22 -0500 | [diff] [blame] | 522 | # Sometimes reading uptime results in a blank value. Call with |
| 523 | # wait_until_keyword_succeeds to ensure a non-blank value is obtained. |
| 524 | remote_cmd_buf = "read uptime filler 2>/dev/null < /proc/uptime" +\ |
| 525 | " && [ ! -z \"${uptime}\" ] && echo ${uptime}" |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 526 | cmd_buf = ["BMC Execute Command", |
Michael Walsh | 888d85a | 2019-04-18 11:03:28 -0500 | [diff] [blame] | 527 | re.sub('\\$', '\\$', remote_cmd_buf), 'quiet=1', |
| 528 | 'test_mode=0'] |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 529 | gp.qprint_issuing(cmd_buf, 0) |
| 530 | gp.qprint_issuing(remote_cmd_buf, 0) |
Michael Walsh | fa76593 | 2017-10-13 14:07:22 -0500 | [diff] [blame] | 531 | try: |
| 532 | stdout, stderr, rc =\ |
Michael Walsh | caccd85 | 2017-11-01 17:58:41 -0500 | [diff] [blame] | 533 | BuiltIn().wait_until_keyword_succeeds("10 sec", "0 sec", |
Michael Walsh | fa76593 | 2017-10-13 14:07:22 -0500 | [diff] [blame] | 534 | *cmd_buf) |
Michael Walsh | 97df71c | 2017-03-27 14:33:24 -0500 | [diff] [blame] | 535 | if rc == 0 and stderr == "": |
| 536 | uptime = stdout |
Michael Walsh | fa76593 | 2017-10-13 14:07:22 -0500 | [diff] [blame] | 537 | except AssertionError as my_assertion_error: |
| 538 | pass |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 539 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 540 | if 'epoch_seconds' in req_states or 'elapsed_boot_time' in req_states: |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 541 | date_cmd_buf = "date -u +%s" |
| 542 | if USE_BMC_EPOCH_TIME: |
Michael Walsh | e53e47a | 2017-06-30 17:03:24 -0500 | [diff] [blame] | 543 | cmd_buf = ["BMC Execute Command", date_cmd_buf, 'quiet=${1}'] |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 544 | if not quiet: |
Michael Walsh | edb5c94 | 2019-03-28 12:40:50 -0500 | [diff] [blame] | 545 | gp.print_issuing(cmd_buf) |
Michael Walsh | 97df71c | 2017-03-27 14:33:24 -0500 | [diff] [blame] | 546 | status, ret_values = \ |
| 547 | BuiltIn().run_keyword_and_ignore_error(*cmd_buf) |
| 548 | if status == "PASS": |
| 549 | stdout, stderr, rc = ret_values |
| 550 | if rc == 0 and stderr == "": |
| 551 | epoch_seconds = stdout.rstrip("\n") |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 552 | else: |
| 553 | shell_rc, out_buf = gc.cmd_fnc_u(date_cmd_buf, |
Michael Walsh | fa76593 | 2017-10-13 14:07:22 -0500 | [diff] [blame] | 554 | quiet=quiet, |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 555 | print_output=0) |
| 556 | if shell_rc == 0: |
| 557 | epoch_seconds = out_buf.rstrip("\n") |
| 558 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 559 | if 'elapsed_boot_time' in req_states: |
| 560 | global start_boot_seconds |
| 561 | elapsed_boot_time = int(epoch_seconds) - start_boot_seconds |
| 562 | |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 563 | master_req_rest = ['rest', 'host', 'requested_host', 'operating_system', |
| 564 | 'attempts_left', 'boot_progress', 'chassis', |
| 565 | 'requested_chassis' 'bmc' 'requested_bmc'] |
| 566 | |
Michael Walsh | b95eb54 | 2017-03-31 09:39:20 -0500 | [diff] [blame] | 567 | req_rest = [sub_state for sub_state in req_states if sub_state in |
| 568 | master_req_rest] |
| 569 | need_rest = (len(req_rest) > 0) |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 570 | state = DotDict() |
| 571 | if need_rest: |
Michael Walsh | 940d691 | 2017-10-27 12:32:33 -0500 | [diff] [blame] | 572 | cmd_buf = ["Read Properties", SYSTEM_STATE_URI + "enumerate", |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 573 | "quiet=${" + str(quiet) + "}"] |
Michael Walsh | edb5c94 | 2019-03-28 12:40:50 -0500 | [diff] [blame] | 574 | gp.dprint_issuing(cmd_buf) |
Michael Walsh | 97df71c | 2017-03-27 14:33:24 -0500 | [diff] [blame] | 575 | status, ret_values = \ |
| 576 | BuiltIn().run_keyword_and_ignore_error(*cmd_buf) |
| 577 | if status == "PASS": |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 578 | state['rest'] = '1' |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 579 | else: |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 580 | state['rest'] = '0' |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 581 | |
Michael Walsh | 2b269de | 2017-10-09 11:18:11 -0500 | [diff] [blame] | 582 | if int(state['rest']): |
| 583 | for url_path in ret_values: |
| 584 | for attr_name in ret_values[url_path]: |
| 585 | # Create a state key value based on the attr_name. |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 586 | try: |
Michael Walsh | 2b269de | 2017-10-09 11:18:11 -0500 | [diff] [blame] | 587 | ret_values[url_path][attr_name] = \ |
| 588 | re.sub(r'.*\.', "", |
| 589 | ret_values[url_path][attr_name]) |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 590 | except TypeError: |
| 591 | pass |
Michael Walsh | 2b269de | 2017-10-09 11:18:11 -0500 | [diff] [blame] | 592 | # Do some key name manipulations. |
| 593 | new_attr_name = re.sub(r'^Current|(State|Transition)$', |
| 594 | "", attr_name) |
| 595 | new_attr_name = re.sub(r'BMC', r'Bmc', new_attr_name) |
| 596 | new_attr_name = re.sub(r'([A-Z][a-z])', r'_\1', |
| 597 | new_attr_name) |
| 598 | new_attr_name = new_attr_name.lower().lstrip("_") |
| 599 | new_attr_name = re.sub(r'power', r'chassis', new_attr_name) |
| 600 | if new_attr_name in req_states: |
| 601 | state[new_attr_name] = ret_values[url_path][attr_name] |
Michael Walsh | b95eb54 | 2017-03-31 09:39:20 -0500 | [diff] [blame] | 602 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 603 | for sub_state in req_states: |
Michael Walsh | 5674922 | 2017-09-29 15:26:07 -0500 | [diff] [blame] | 604 | if sub_state in state: |
| 605 | continue |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 606 | if sub_state.startswith("os_"): |
| 607 | # We pass "os_" requests on to get_os_state. |
| 608 | continue |
| 609 | cmd_buf = "state['" + sub_state + "'] = str(" + sub_state + ")" |
| 610 | exec(cmd_buf) |
| 611 | |
| 612 | if os_host == "": |
| 613 | # The caller has not specified an os_host so as far as we're concerned, |
| 614 | # it doesn't exist. |
| 615 | return state |
| 616 | |
| 617 | os_req_states = [sub_state for sub_state in req_states |
| 618 | if sub_state.startswith('os_')] |
| 619 | |
| 620 | if len(os_req_states) > 0: |
| 621 | # The caller has specified an os_host and they have requested |
| 622 | # information on os substates. |
| 623 | |
| 624 | # Based on the information gathered on bmc, we'll try to make a |
| 625 | # determination of whether the os is even up. We'll pass the result |
| 626 | # of that assessment to get_os_state to enhance performance. |
| 627 | os_up_match = DotDict() |
| 628 | for sub_state in master_os_up_match: |
| 629 | if sub_state in req_states: |
| 630 | os_up_match[sub_state] = master_os_up_match[sub_state] |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 631 | os_up = compare_states(state, os_up_match) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 632 | os_state = get_os_state(os_host=os_host, |
| 633 | os_username=os_username, |
| 634 | os_password=os_password, |
| 635 | req_states=os_req_states, |
| 636 | os_up=os_up, |
| 637 | quiet=quiet) |
| 638 | # Append os_state dictionary to ours. |
| 639 | state.update(os_state) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 640 | |
| 641 | return state |
| 642 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 643 | |
Michael Walsh | fd5a868 | 2019-02-01 14:28:42 -0600 | [diff] [blame] | 644 | exit_wait_early_message = "" |
| 645 | |
| 646 | |
| 647 | def set_exit_wait_early_message(value): |
| 648 | r""" |
| 649 | Set global exit_wait_early_message to the indicated value. |
| 650 | |
| 651 | This is a mechanism by which the programmer can do an early exit from |
| 652 | wait_until_keyword_succeeds() based on some special condition. |
| 653 | |
| 654 | Description of argument(s): |
| 655 | value The value to assign to the global |
| 656 | exit_wait_early_message. |
| 657 | """ |
| 658 | |
| 659 | global exit_wait_early_message |
| 660 | exit_wait_early_message = value |
| 661 | |
| 662 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 663 | def check_state(match_state, |
| 664 | invert=0, |
| 665 | print_string="", |
| 666 | openbmc_host="", |
| 667 | openbmc_username="", |
| 668 | openbmc_password="", |
| 669 | os_host="", |
| 670 | os_username="", |
| 671 | os_password="", |
| 672 | quiet=None): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 673 | r""" |
| 674 | Check that the Open BMC machine's composite state matches the specified |
| 675 | state. On success, this keyword returns the machine's composite state as a |
| 676 | dictionary. |
| 677 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 678 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 679 | match_state A dictionary whose key/value pairs are "state field"/ |
| 680 | "state value". The state value is interpreted as a |
| 681 | regular expression. Example call from robot: |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 682 | ${match_state}= Create Dictionary chassis=^On$ |
| 683 | ... bmc=^Ready$ |
Michael Walsh | 01975fa | 2017-08-20 20:51:36 -0500 | [diff] [blame] | 684 | ... boot_progress=^OSStart$ |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 685 | ${state}= Check State &{match_state} |
| 686 | invert If this flag is set, this function will succeed if the |
| 687 | states do NOT match. |
| 688 | print_string This function will print this string to the console prior |
| 689 | to getting the state. |
| 690 | openbmc_host The DNS name or IP address of the BMC. |
| 691 | This defaults to global ${OPENBMC_HOST}. |
| 692 | openbmc_username The username to be used to login to the BMC. |
| 693 | This defaults to global ${OPENBMC_USERNAME}. |
| 694 | openbmc_password The password to be used to login to the BMC. |
| 695 | This defaults to global ${OPENBMC_PASSWORD}. |
| 696 | os_host The DNS name or IP address of the operating system. |
| 697 | This defaults to global ${OS_HOST}. |
| 698 | os_username The username to be used to login to the OS. |
| 699 | This defaults to global ${OS_USERNAME}. |
| 700 | os_password The password to be used to login to the OS. |
| 701 | This defaults to global ${OS_PASSWORD}. |
| 702 | quiet Indicates whether status details should be written to the |
| 703 | console. Defaults to either global value of ${QUIET} or |
| 704 | to 1. |
| 705 | """ |
| 706 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 707 | quiet = int(gp.get_var_value(quiet, 0)) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 708 | |
Michael Walsh | edb5c94 | 2019-03-28 12:40:50 -0500 | [diff] [blame] | 709 | gp.gp_print(print_string) |
| 710 | |
| 711 | try: |
| 712 | match_state = return_state_constant(match_state) |
| 713 | except TypeError: |
| 714 | pass |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 715 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 716 | req_states = list(match_state.keys()) |
| 717 | # Remove special-case match key from req_states. |
| 718 | if expressions_key() in req_states: |
| 719 | req_states.remove(expressions_key()) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 720 | # Initialize state. |
| 721 | state = get_state(openbmc_host=openbmc_host, |
| 722 | openbmc_username=openbmc_username, |
| 723 | openbmc_password=openbmc_password, |
| 724 | os_host=os_host, |
| 725 | os_username=os_username, |
| 726 | os_password=os_password, |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 727 | req_states=req_states, |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 728 | quiet=quiet) |
| 729 | if not quiet: |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 730 | gp.print_var(state) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 731 | |
Michael Walsh | fd5a868 | 2019-02-01 14:28:42 -0600 | [diff] [blame] | 732 | if exit_wait_early_message != "": |
| 733 | # The exit_wait_early_message has been set by a signal handler so we |
| 734 | # will exit "successfully". It is incumbent upon the calling function |
| 735 | # (e.g. wait_state) to check/clear this variable and to fail |
| 736 | # appropriately. |
| 737 | return state |
| 738 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 739 | match = compare_states(state, match_state) |
| 740 | |
| 741 | if invert and match: |
| 742 | fail_msg = "The current state of the machine matches the match" +\ |
| 743 | " state:\n" + gp.sprint_varx("state", state) |
| 744 | BuiltIn().fail("\n" + gp.sprint_error(fail_msg)) |
| 745 | elif not invert and not match: |
| 746 | fail_msg = "The current state of the machine does NOT match the" +\ |
| 747 | " match state:\n" +\ |
| 748 | gp.sprint_varx("state", state) |
| 749 | BuiltIn().fail("\n" + gp.sprint_error(fail_msg)) |
| 750 | |
| 751 | return state |
| 752 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 753 | |
Michael Walsh | f893ba0 | 2017-01-10 10:28:05 -0600 | [diff] [blame] | 754 | def wait_state(match_state=(), |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 755 | wait_time="1 min", |
| 756 | interval="1 second", |
| 757 | invert=0, |
| 758 | openbmc_host="", |
| 759 | openbmc_username="", |
| 760 | openbmc_password="", |
| 761 | os_host="", |
| 762 | os_username="", |
| 763 | os_password="", |
| 764 | quiet=None): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 765 | r""" |
| 766 | Wait for the Open BMC machine's composite state to match the specified |
| 767 | state. On success, this keyword returns the machine's composite state as |
| 768 | a dictionary. |
| 769 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 770 | Description of argument(s): |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 771 | match_state A dictionary whose key/value pairs are "state field"/ |
| 772 | "state value". See check_state (above) for details. |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 773 | This value may also be any string accepted by |
| 774 | return_state_constant (e.g. "standby_match_state"). |
| 775 | In such a case this function will call |
| 776 | return_state_constant to convert it to a proper |
| 777 | dictionary as described above. |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 778 | wait_time The total amount of time to wait for the desired state. |
| 779 | This value may be expressed in Robot Framework's time |
| 780 | format (e.g. 1 minute, 2 min 3 s, 4.5). |
| 781 | interval The amount of time between state checks. |
| 782 | This value may be expressed in Robot Framework's time |
| 783 | format (e.g. 1 minute, 2 min 3 s, 4.5). |
| 784 | invert If this flag is set, this function will for the state of |
| 785 | the machine to cease to match the match state. |
| 786 | openbmc_host The DNS name or IP address of the BMC. |
| 787 | This defaults to global ${OPENBMC_HOST}. |
| 788 | openbmc_username The username to be used to login to the BMC. |
| 789 | This defaults to global ${OPENBMC_USERNAME}. |
| 790 | openbmc_password The password to be used to login to the BMC. |
| 791 | This defaults to global ${OPENBMC_PASSWORD}. |
| 792 | os_host The DNS name or IP address of the operating system. |
| 793 | This defaults to global ${OS_HOST}. |
| 794 | os_username The username to be used to login to the OS. |
| 795 | This defaults to global ${OS_USERNAME}. |
| 796 | os_password The password to be used to login to the OS. |
| 797 | This defaults to global ${OS_PASSWORD}. |
| 798 | quiet Indicates whether status details should be written to the |
| 799 | console. Defaults to either global value of ${QUIET} or |
| 800 | to 1. |
| 801 | """ |
| 802 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 803 | quiet = int(gp.get_var_value(quiet, 0)) |
| 804 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 805 | try: |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 806 | match_state = return_state_constant(match_state) |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 807 | except TypeError: |
| 808 | pass |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 809 | |
| 810 | if not quiet: |
| 811 | if invert: |
| 812 | alt_text = "cease to " |
| 813 | else: |
| 814 | alt_text = "" |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 815 | gp.print_timen("Checking every " + str(interval) + " for up to " |
| 816 | + str(wait_time) + " for the state of the machine to " |
| 817 | + alt_text + "match the state shown below.") |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 818 | gp.print_var(match_state) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 819 | |
Michael Walsh | f893ba0 | 2017-01-10 10:28:05 -0600 | [diff] [blame] | 820 | if quiet: |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 821 | print_string = "" |
Michael Walsh | f893ba0 | 2017-01-10 10:28:05 -0600 | [diff] [blame] | 822 | else: |
Michael Walsh | 341c21e | 2017-01-17 16:25:20 -0600 | [diff] [blame] | 823 | print_string = "#" |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 824 | |
| 825 | debug = int(BuiltIn().get_variable_value("${debug}", "0")) |
| 826 | if debug: |
| 827 | # In debug we print state so no need to print the "#". |
| 828 | print_string = "" |
| 829 | check_state_quiet = 1 - debug |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 830 | cmd_buf = ["Check State", match_state, "invert=${" + str(invert) + "}", |
Michael Walsh | f893ba0 | 2017-01-10 10:28:05 -0600 | [diff] [blame] | 831 | "print_string=" + print_string, "openbmc_host=" + openbmc_host, |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 832 | "openbmc_username=" + openbmc_username, |
| 833 | "openbmc_password=" + openbmc_password, "os_host=" + os_host, |
| 834 | "os_username=" + os_username, "os_password=" + os_password, |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 835 | "quiet=${" + str(check_state_quiet) + "}"] |
Michael Walsh | edb5c94 | 2019-03-28 12:40:50 -0500 | [diff] [blame] | 836 | gp.dprint_issuing(cmd_buf) |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 837 | try: |
| 838 | state = BuiltIn().wait_until_keyword_succeeds(wait_time, interval, |
| 839 | *cmd_buf) |
| 840 | except AssertionError as my_assertion_error: |
| 841 | gp.printn() |
| 842 | message = my_assertion_error.args[0] |
| 843 | BuiltIn().fail(message) |
| 844 | |
Michael Walsh | fd5a868 | 2019-02-01 14:28:42 -0600 | [diff] [blame] | 845 | if exit_wait_early_message: |
| 846 | # The global exit_wait_early_message was set by a signal handler |
| 847 | # indicating that we should fail. |
| 848 | message = exit_wait_early_message |
| 849 | # Clear the exit_wait_early_message variable for future use. |
| 850 | set_exit_wait_early_message("") |
| 851 | BuiltIn().fail(gp.sprint_error(message)) |
| 852 | |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 853 | if not quiet: |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 854 | gp.printn() |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 855 | if invert: |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 856 | gp.print_timen("The states no longer match:") |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 857 | else: |
Michael Walsh | 3eb5002 | 2017-03-21 11:27:30 -0500 | [diff] [blame] | 858 | gp.print_timen("The states match:") |
| 859 | gp.print_var(state) |
Michael Walsh | 70369fd | 2016-11-22 11:25:57 -0600 | [diff] [blame] | 860 | |
| 861 | return state |
| 862 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 863 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 864 | def set_start_boot_seconds(value=0): |
| 865 | global start_boot_seconds |
| 866 | start_boot_seconds = int(value) |
| 867 | |
| 868 | |
| 869 | set_start_boot_seconds(0) |
| 870 | |
| 871 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 872 | def wait_for_comm_cycle(start_boot_seconds, |
| 873 | quiet=None): |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 874 | r""" |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 875 | Wait for the BMC uptime to be less than elapsed_boot_time. |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 876 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 877 | This function will tolerate an expected loss of communication to the BMC. |
| 878 | This function is useful when some kind of reboot has been initiated by the |
| 879 | caller. |
| 880 | |
| 881 | Description of argument(s): |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 882 | start_boot_seconds The time that the boot test started. The format is the |
| 883 | epoch time in seconds, i.e. the number of seconds since |
| 884 | 1970-01-01 00:00:00 UTC. This value should be obtained |
| 885 | from the BMC so that it is not dependent on any kind of |
| 886 | synchronization between this machine and the target BMC |
| 887 | This will allow this program to work correctly even in |
| 888 | a simulated environment. This value should be obtained |
| 889 | by the caller prior to initiating a reboot. It can be |
| 890 | obtained as follows: |
| 891 | state = st.get_state(req_states=['epoch_seconds']) |
| 892 | """ |
| 893 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 894 | quiet = int(gp.get_var_value(quiet, 0)) |
| 895 | |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 896 | # Validate parms. |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 897 | error_message = gv.valid_integer(start_boot_seconds) |
| 898 | if error_message: |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 899 | BuiltIn().fail(gp.sprint_error(error_message)) |
| 900 | |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 901 | # Wait for uptime to be less than elapsed_boot_time. |
| 902 | set_start_boot_seconds(start_boot_seconds) |
| 903 | expr = 'int(float(state[\'uptime\'])) < int(state[\'elapsed_boot_time\'])' |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 904 | match_state = DotDict([('uptime', '^[0-9\\.]+$'), |
Michael Walsh | 2a0df68 | 2019-09-27 17:19:27 -0500 | [diff] [blame] | 905 | ('elapsed_boot_time', '^[0-9]+$'), |
| 906 | (expressions_key(), [expr])]) |
| 907 | wait_state(match_state, wait_time="8 mins", interval="5 seconds") |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 908 | |
Michael Walsh | 619aa33 | 2017-04-12 15:56:51 -0500 | [diff] [blame] | 909 | gp.qprint_timen("Verifying that REST API interface is working.") |
Michael Walsh | b95eb54 | 2017-03-31 09:39:20 -0500 | [diff] [blame] | 910 | match_state = DotDict([('rest', '^1$')]) |
Michael Walsh | 8fae6ea | 2017-02-20 16:14:44 -0600 | [diff] [blame] | 911 | state = wait_state(match_state, wait_time="5 mins", interval="2 seconds") |