def compare_states(state, match_state, match_type='and'): r""" Compare 2 state dictionaries. Return True if they match and False if they don't. Note that the match_state dictionary does not need to have an entry corresponding to each entry in the state dictionary. But for each entry that it does have, the corresponding state entry will be checked for a match. Description of arguments: state A state dictionary such as the one returned by the get_state function. match_state A dictionary whose key/value pairs are "state field"/ "state value". The state value is interpreted as a regular expression. Every value in this dictionary is considered. When match_type is 'and', if each and every comparison matches, the two dictionaries are considered to be matching. If match_type is 'or', if any two of the elements compared match, the two dictionaries are considered to be matching. This value may also be any string accepted by return_state_constant (e.g. "standby_match_state"). In such a case this function will call return_state_constant to convert it to a proper dictionary as described above. match_type This may be 'and' or 'or'. """ error_message = gv.svalid_value(match_type, var_name="match_type", valid_values=['and', 'or']) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) try: match_state = return_state_constant(match_state) except TypeError: pass default_match = (match_type == 'and') for key, match_state_value in match_state.items(): # Blank match_state_value means "don't care". if match_state_value == "": continue try: match = (re.match(match_state_value, str(state[key])) is not None) except KeyError: match = False if match != default_match: return match return default_match
def return_state_constant(state_name='default'): r""" Return default state dictionary. default_state is an initial value which may be of use to callers. """ error_message = gv.svalid_value(state_name, var_name='state_name', valid_values=valid_state_constants) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if state_name == 'default': return default_state elif state_name == 'standby_match_state': return standby_match_state
def valid_boot_list(boot_list, valid_boot_types): r""" Verify that each entry in boot_list is a supported boot test. Description of arguments: boot_list An array (i.e. list) of boot test types (e.g. "BMC Power On"). valid_boot_types A list of valid boot types such as that returned by create_valid_boot_list. """ for boot_name in boot_list: boot_name = boot_name.strip(" ") error_message = gv.svalid_value(boot_name, valid_values=valid_boot_types, var_name="boot_name") if error_message != "": BuiltIn().fail(gp.sprint_error(error_message))
def rvalid_value(var_name, invalid_values=[], valid_values=[]): r""" Validate a robot value. This function is the robot wrapper for gen_valid.svalid_value. Description of arguments: var_name The name of the variable whose value is to be validated. invalid_values A list of invalid values. If var_value is equal to any of these, it is invalid. Note that if you specify anything for invalid_values (below), the valid_values list is not even processed. valid_values A list of invalid values. var_value must be equal to one of these values to be considered valid. If either the invalid_values or the valid_values parms are not of type "list", they will be processed as python code in order to generate a list. This allows the robot programmer to essentially specify a list literal. For example, the robot code could contain the following: Rvalid Value var1 valid_values=['one', 'two'] Examples of robot calls and corresponding output: Robot code... rvalid_value MY_PARM Output... #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. #it's undefined). or if it is defined but blank: Output... #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an #invalid value: MY_PARM: It must NOT be one of the following values: invalid_values: invalid_values[0]: <blank> Robot code... ${invalid_values}= Create List one two three ${MY_PARM}= Set Variable one rvalid_value MY_PARM invalid_values=${invalid_values} Output... #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an #invalid value: MY_PARM: one It must NOT be one of the following values: invalid_values: invalid_values[0]: one invalid_values[1]: two invalid_values[2]: three """ # Note: get_variable_value() seems to have no trouble with local variables. var_value = BuiltIn().get_variable_value("${" + var_name + "}") if type(valid_values) is not list: # Evaluate python syntax to convert to a list. exec("valid_values = " + valid_values) if type(invalid_values) is not list: # Evaluate python syntax to convert to a list. exec("invalid_values = " + invalid_values) if var_value is None: var_value = "" error_message = "Variable \"" + var_name +\ "\" not found (i.e. it's undefined).\n" else: error_message = gv.svalid_value(var_value, invalid_values, valid_values, var_name) if not error_message == "": error_message = gp.sprint_error_report(error_message) BuiltIn().fail(error_message)
def get_state(openbmc_host="", openbmc_username="", openbmc_password="", os_host="", os_username="", os_password="", req_states=default_req_states, quiet=None): r""" Get component states such as chassis state, bmc state, etc, put them into a dictionary and return them to the caller. Note that all substate values are strings. Description of arguments: openbmc_host The DNS name or IP address of the BMC. This defaults to global ${OPENBMC_HOST}. openbmc_username The username to be used to login to the BMC. This defaults to global ${OPENBMC_USERNAME}. openbmc_password The password to be used to login to the BMC. This defaults to global ${OPENBMC_PASSWORD}. os_host The DNS name or IP address of the operating system. This defaults to global ${OS_HOST}. os_username The username to be used to login to the OS. This defaults to global ${OS_USERNAME}. os_password The password to be used to login to the OS. This defaults to global ${OS_PASSWORD}. req_states This is a list of states whose values are being requested by the caller. quiet Indicates whether status details (e.g. curl commands) should be written to the console. Defaults to either global value of ${QUIET} or to 1. """ quiet = int(gp.get_var_value(quiet, 0)) # Set parm defaults where necessary and validate all parms. if openbmc_host == "": openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}") error_message = gv.svalid_value(openbmc_host, var_name="openbmc_host", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if openbmc_username == "": openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}") error_message = gv.svalid_value(openbmc_username, var_name="openbmc_username", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if openbmc_password == "": openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}") error_message = gv.svalid_value(openbmc_password, var_name="openbmc_password", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) # NOTE: OS parms are optional. if os_host == "": os_host = BuiltIn().get_variable_value("${OS_HOST}") if os_host is None: os_host = "" if os_username is "": os_username = BuiltIn().get_variable_value("${OS_USERNAME}") if os_username is None: os_username = "" if os_password is "": os_password = BuiltIn().get_variable_value("${OS_PASSWORD}") if os_password is None: os_password = "" invalid_req_states = [ sub_state for sub_state in req_states if sub_state not in valid_req_states ] if len(invalid_req_states) > 0: error_message = "The following req_states are not supported:\n" +\ gp.sprint_var(invalid_req_states) BuiltIn().fail(gp.sprint_error(error_message)) # Initialize all substate values supported by this function. ping = 0 packet_loss = '' uptime = '' epoch_seconds = '' rest = '' chassis = '' requested_chassis = '' bmc = '' requested_bmc = '' boot_progress = '' operating_system = '' host = '' requested_host = '' attempts_left = '' # Get the component states. if 'ping' in req_states: # See if the OS pings. cmd_buf = "ping -c 1 -w 2 " + openbmc_host if not quiet: gp.pissuing(cmd_buf) rc, out_buf = commands.getstatusoutput(cmd_buf) if rc == 0: ping = 1 if 'packet_loss' in req_states: # See if the OS pings. cmd_buf = "ping -c 5 -w 5 " + openbmc_host +\ " | egrep 'packet loss' | sed -re 's/.* ([0-9]+)%.*/\\1/g'" if not quiet: gp.pissuing(cmd_buf) rc, out_buf = commands.getstatusoutput(cmd_buf) if rc == 0: packet_loss = out_buf.rstrip("\n") if 'uptime' in req_states: # Sometimes reading uptime results in a blank value. Call with # wait_until_keyword_succeeds to ensure a non-blank value is obtained. remote_cmd_buf = "read uptime filler 2>/dev/null < /proc/uptime" +\ " && [ ! -z \"${uptime}\" ] && echo ${uptime}" cmd_buf = [ "BMC Execute Command", re.sub('\\$', '\\$', remote_cmd_buf), 'quiet=1' ] if not quiet: grp.rpissuing_keyword(cmd_buf) grp.rpissuing(remote_cmd_buf) try: stdout, stderr, rc =\ BuiltIn().wait_until_keyword_succeeds("10 sec", "0 sec", *cmd_buf) if rc == 0 and stderr == "": uptime = stdout except AssertionError as my_assertion_error: pass if 'epoch_seconds' in req_states: date_cmd_buf = "date -u +%s" if USE_BMC_EPOCH_TIME: cmd_buf = ["BMC Execute Command", date_cmd_buf, 'quiet=${1}'] if not quiet: grp.rpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": stdout, stderr, rc = ret_values if rc == 0 and stderr == "": epoch_seconds = stdout.rstrip("\n") else: shell_rc, out_buf = gc.cmd_fnc_u(date_cmd_buf, quiet=quiet, print_output=0) if shell_rc == 0: epoch_seconds = out_buf.rstrip("\n") master_req_rest = [ 'rest', 'host', 'requested_host', 'operating_system', 'attempts_left', 'boot_progress', 'chassis', 'requested_chassis' 'bmc' 'requested_bmc' ] req_rest = [ sub_state for sub_state in req_states if sub_state in master_req_rest ] need_rest = (len(req_rest) > 0) state = DotDict() if need_rest: cmd_buf = [ "Read Properties", SYSTEM_STATE_URI + "enumerate", "quiet=${" + str(quiet) + "}" ] grp.rdpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": state['rest'] = '1' else: state['rest'] = '0' if int(state['rest']): for url_path in ret_values: for attr_name in ret_values[url_path]: # Create a state key value based on the attr_name. if isinstance(ret_values[url_path][attr_name], unicode): ret_values[url_path][attr_name] = \ re.sub(r'.*\.', "", ret_values[url_path][attr_name]) # Do some key name manipulations. new_attr_name = re.sub(r'^Current|(State|Transition)$', "", attr_name) new_attr_name = re.sub(r'BMC', r'Bmc', new_attr_name) new_attr_name = re.sub(r'([A-Z][a-z])', r'_\1', new_attr_name) new_attr_name = new_attr_name.lower().lstrip("_") new_attr_name = re.sub(r'power', r'chassis', new_attr_name) if new_attr_name in req_states: state[new_attr_name] = ret_values[url_path][attr_name] for sub_state in req_states: if sub_state in state: continue if sub_state.startswith("os_"): # We pass "os_" requests on to get_os_state. continue cmd_buf = "state['" + sub_state + "'] = str(" + sub_state + ")" exec(cmd_buf) if os_host == "": # The caller has not specified an os_host so as far as we're concerned, # it doesn't exist. return state os_req_states = [ sub_state for sub_state in req_states if sub_state.startswith('os_') ] if len(os_req_states) > 0: # The caller has specified an os_host and they have requested # information on os substates. # Based on the information gathered on bmc, we'll try to make a # determination of whether the os is even up. We'll pass the result # of that assessment to get_os_state to enhance performance. os_up_match = DotDict() for sub_state in master_os_up_match: if sub_state in req_states: os_up_match[sub_state] = master_os_up_match[sub_state] os_up = compare_states(state, os_up_match) os_state = get_os_state(os_host=os_host, os_username=os_username, os_password=os_password, req_states=os_req_states, os_up=os_up, quiet=quiet) # Append os_state dictionary to ours. state.update(os_state) return state
def get_os_state(os_host="", os_username="", os_password="", req_states=default_os_req_states, os_up=True, quiet=None): r""" Get component states for the operating system such as ping, login, etc, put them into a dictionary and return them to the caller. Note that all substate values are strings. Description of arguments: os_host The DNS name or IP address of the operating system. This defaults to global ${OS_HOST}. os_username The username to be used to login to the OS. This defaults to global ${OS_USERNAME}. os_password The password to be used to login to the OS. This defaults to global ${OS_PASSWORD}. req_states This is a list of states whose values are being requested by the caller. os_up If the caller knows that the os can't possibly be up, it can improve performance by passing os_up=False. This function will then simply return default values for all requested os sub states. quiet Indicates whether status details (e.g. curl commands) should be written to the console. Defaults to either global value of ${QUIET} or to 1. """ quiet = int(gp.get_var_value(quiet, 0)) # Set parm defaults where necessary and validate all parms. if os_host == "": os_host = BuiltIn().get_variable_value("${OS_HOST}") error_message = gv.svalid_value(os_host, var_name="os_host", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if os_username == "": os_username = BuiltIn().get_variable_value("${OS_USERNAME}") error_message = gv.svalid_value(os_username, var_name="os_username", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if os_password == "": os_password = BuiltIn().get_variable_value("${OS_PASSWORD}") error_message = gv.svalid_value(os_password, var_name="os_password", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) invalid_req_states = [ sub_state for sub_state in req_states if sub_state not in valid_os_req_states ] if len(invalid_req_states) > 0: error_message = "The following req_states are not supported:\n" +\ gp.sprint_var(invalid_req_states) BuiltIn().fail(gp.sprint_error(error_message)) # Initialize all substate values supported by this function. os_ping = 0 os_login = 0 os_run_cmd = 0 if os_up: if 'os_ping' in req_states: # See if the OS pings. cmd_buf = "ping -c 1 -w 2 " + os_host if not quiet: gp.pissuing(cmd_buf) rc, out_buf = commands.getstatusoutput(cmd_buf) if rc == 0: os_ping = 1 # Programming note: All attributes which do not require an ssh login # should have been processed by this point. master_req_login = ['os_login', 'os_run_cmd'] req_login = [ sub_state for sub_state in req_states if sub_state in master_req_login ] must_login = (len(req_login) > 0) if must_login: output, stderr, rc = bsu.os_execute_command("uptime", quiet=quiet, ignore_err=1) if rc == 0: os_login = 1 os_run_cmd = 1 else: gp.dprint_vars(output, stderr) gp.dprint_vars(rc, 1) os_state = DotDict() for sub_state in req_states: cmd_buf = "os_state['" + sub_state + "'] = str(" + sub_state + ")" exec(cmd_buf) return os_state
def cmd_fnc(cmd_buf, quiet=None, test_mode=None, debug=0, print_output=1, show_err=1): r""" Run the given command in a shell and return the shell return code. Description of arguments: cmd_buf The command string to be run in a shell. quiet Indicates whether this function should run the pissuing() function prints an "Issuing: <cmd string>" to stdout. test_mode If test_mode is set, this function will not actually run the command. debug If debug is set, this function will print extra debug info. print_output If this is set, this function will print the stdout/stderr generated by the shell command. show_err If show_err is set, this function will print a standardized error report if the shell command returns non-zero. """ quiet = int(gm.global_default(quiet, 0)) test_mode = int(gm.global_default(test_mode, 0)) if debug: gp.print_vars(cmd_buf, quiet, test_mode, debug) err_msg = gv.svalid_value(cmd_buf) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.pissuing(cmd_buf, test_mode) if test_mode: return 0, "" sub_proc = subprocess.Popen(cmd_buf, bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out_buf = "" for line in sub_proc.stdout: out_buf += line if not print_output: continue if robot_env: grp.rprint(line) else: sys.stdout.write(line) if print_output and not robot_env: sys.stdout.flush() sub_proc.communicate() shell_rc = sub_proc.returncode if shell_rc != 0 and show_err: if robot_env: grp.rprint_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) else: gp.print_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) return shell_rc, out_buf
def set_ffdc_defaults(ffdc_dir_path=None, ffdc_prefix=None): r""" Set a default value for ffdc_dir_path and ffdc_prefix if they don't already have values. Return both values. Description of arguments: ffdc_dir_path The dir path where FFDC data should be put. ffdc_prefix The prefix to be given to each FFDC file name generated. NOTE: If global variable ffdc_dir_path_style is set to ${1}, this function will create default values in a newer way. Otherwise, its behavior will remain unchanged. """ # Note: Several subordinate functions like 'Get Test Dir and Name' and # 'Header Message' expect global variable FFDC_TIME to be set. cmd_buf = ["Get Current Time Stamp"] grp.rdpissuing_keyword(cmd_buf) FFDC_TIME = BuiltIn().run_keyword(*cmd_buf) BuiltIn().set_global_variable("${FFDC_TIME}", FFDC_TIME) ffdc_dir_path_style = BuiltIn().get_variable_value( "${ffdc_dir_path_style}") if ffdc_dir_path is None: if ffdc_dir_path_style: try: ffdc_dir_path = os.environ['FFDC_DIR_PATH'] except KeyError: ffdc_dir_path = os.path.dirname( BuiltIn().get_variable_value("${LOG_FILE}")) + "/" else: FFDC_LOG_PATH = os.getcwd() + "/logs/" if FFDC_LOG_PATH is None: FFDC_LOG_PATH = "" if FFDC_LOG_PATH == "": FFDC_LOG_PATH = os.path.dirname( BuiltIn().get_variable_value("${LOG_FILE}")) + "/" error_message = gv.svalid_value(FFDC_LOG_PATH, var_name="FFDC_LOG_PATH") if error_message != "": error_message = grp.sprint_error_report(error_message) BuiltIn().fail(error_message) FFDC_LOG_PATH = os.path.normpath(FFDC_LOG_PATH) + os.sep cmd_buf = ["Get Test Dir and Name"] grp.rpissuing_keyword(cmd_buf) suitename, testname = BuiltIn().run_keyword(*cmd_buf) ffdc_dir_path = FFDC_LOG_PATH + suitename + "/" + testname + "/" # Add trailing slash. ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep if ffdc_prefix is None: FFDC_TIME = BuiltIn().get_variable_value("${FFDC_TIME}") if ffdc_prefix is None: if ffdc_dir_path_style: OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}") OPENBMC_NICKNAME = BuiltIn().get_variable_value( "${OPENBMC_NICKNAME}", default=OPENBMC_HOST) ffdc_prefix = OPENBMC_NICKNAME + "." + FFDC_TIME[2:8] + "." +\ FFDC_TIME[8:14] + "." else: ffdc_prefix = FFDC_TIME + "_" BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path) BuiltIn().set_global_variable("${FFDC_PREFIX}", ffdc_prefix) return ffdc_dir_path, ffdc_prefix
def rvalid_value(var_name, invalid_values=[], valid_values=[]): r""" Validate a robot value. This function is the robot wrapper for gen_robot_print.svalid_value. Description of arguments: var_name The name of the variable whose value is to be validated. invalid_values A list of invalid values. If var_value is equal to any of these, it is invalid. Note that if you specify anything for invalid_values (below), the valid_values list is not even processed. valid_values A list of invalid values. var_value must be equal to one of these values to be considered valid. Examples of robot calls and corresponding output: Robot code... rvalid_value MY_PARM Output... #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. #it's undefined). or if it is defined but blank: Output... #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an #invalid value: MY_PARM: It must NOT be one of the following values: invalid_values: invalid_values[0]: <blank> Robot code... ${invalid_values}= Create List one two three ${MY_PARM}= Set Variable one rvalid_value MY_PARM invalid_values=${invalid_values} Output... #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an #invalid value: MY_PARM: one It must NOT be one of the following values: invalid_values: invalid_values[0]: one invalid_values[1]: two invalid_values[2]: three """ # Note: get_variable_value() seems to have no trouble with local variables. var_value = BuiltIn().get_variable_value("${" + var_name + "}") if var_value is None: var_value = "" error_message = "Variable \"" + var_name +\ "\" not found (i.e. it's undefined).\n" else: error_message = gv.svalid_value(var_value, invalid_values, valid_values, var_name) if not error_message == "": error_message = grp.sprint_error_report(error_message) BuiltIn().fail(error_message)
def cmd_fnc(cmd_buf, quiet=None, test_mode=None, debug=0, print_output=1, show_err=1, return_stderr=0): r""" Run the given command in a shell and return the shell return code and the output. Description of arguments: cmd_buf The command string to be run in a shell. quiet Indicates whether this function should run the print_issuing() function which prints "Issuing: <cmd string>" to stdout. test_mode If test_mode is set, this function will not actually run the command. If print_output is set, it will print "(test_mode) Issuing: <cmd string>" to stdout. debug If debug is set, this function will print extra debug info. print_output If this is set, this function will print the stdout/stderr generated by the shell command. show_err If show_err is set, this function will print a standardized error report if the shell command returns non-zero. return_stderr If return_stderr is set, this function will process the stdout and stderr streams from the shell command separately. It will also return stderr in addition to the return code and the stdout. """ # Determine default values. quiet = int(gm.global_default(quiet, 0)) test_mode = int(gm.global_default(test_mode, 0)) if debug: gp.print_vars(cmd_buf, quiet, test_mode, debug) err_msg = gv.svalid_value(cmd_buf) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.pissuing(cmd_buf, test_mode) if test_mode: if return_stderr: return 0, "", "" else: return 0, "" if return_stderr: err_buf = "" stderr = subprocess.PIPE else: stderr = subprocess.STDOUT sub_proc = subprocess.Popen(cmd_buf, bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=stderr) out_buf = "" if return_stderr: for line in sub_proc.stderr: err_buf += line if not print_output: continue if robot_env: grp.rprint(line) else: sys.stdout.write(line) for line in sub_proc.stdout: out_buf += line if not print_output: continue if robot_env: grp.rprint(line) else: sys.stdout.write(line) if print_output and not robot_env: sys.stdout.flush() sub_proc.communicate() shell_rc = sub_proc.returncode if shell_rc != 0 and show_err: err_msg = "The prior command failed.\n" + gp.sprint_var(shell_rc, 1) if not print_output: err_msg += "out_buf:\n" + out_buf if robot_env: grp.rprint_error_report(err_msg) else: gp.print_error_report(err_msg) if return_stderr: return shell_rc, out_buf, err_buf else: return shell_rc, out_buf
def get_state(openbmc_host="", openbmc_username="", openbmc_password="", os_host="", os_username="", os_password="", req_states=default_req_states, quiet=None): r""" Get component states such as chassis state, bmc state, etc, put them into a dictionary and return them to the caller. Note that all substate values are strings. Description of arguments: openbmc_host The DNS name or IP address of the BMC. This defaults to global ${OPENBMC_HOST}. openbmc_username The username to be used to login to the BMC. This defaults to global ${OPENBMC_USERNAME}. openbmc_password The password to be used to login to the BMC. This defaults to global ${OPENBMC_PASSWORD}. os_host The DNS name or IP address of the operating system. This defaults to global ${OS_HOST}. os_username The username to be used to login to the OS. This defaults to global ${OS_USERNAME}. os_password The password to be used to login to the OS. This defaults to global ${OS_PASSWORD}. req_states This is a list of states whose values are being requested by the caller. quiet Indicates whether status details (e.g. curl commands) should be written to the console. Defaults to either global value of ${QUIET} or to 1. """ quiet = int(gp.get_var_value(quiet, 0)) # Set parm defaults where necessary and validate all parms. if openbmc_host == "": openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}") error_message = gv.svalid_value(openbmc_host, var_name="openbmc_host", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if openbmc_username == "": openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}") error_message = gv.svalid_value(openbmc_username, var_name="openbmc_username", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) if openbmc_password == "": openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}") error_message = gv.svalid_value(openbmc_password, var_name="openbmc_password", invalid_values=[None, ""]) if error_message != "": BuiltIn().fail(gp.sprint_error(error_message)) # NOTE: OS parms are optional. if os_host == "": os_host = BuiltIn().get_variable_value("${OS_HOST}") if os_host is None: os_host = "" if os_username is "": os_username = BuiltIn().get_variable_value("${OS_USERNAME}") if os_username is None: os_username = "" if os_password is "": os_password = BuiltIn().get_variable_value("${OS_PASSWORD}") if os_password is None: os_password = "" invalid_req_states = [ sub_state for sub_state in req_states if sub_state not in valid_req_states ] if len(invalid_req_states) > 0: error_message = "The following req_states are not supported:\n" +\ gp.sprint_var(invalid_req_states) BuiltIn().fail(gp.sprint_error(error_message)) # Initialize all substate values supported by this function. ping = 0 packet_loss = '' uptime = '' epoch_seconds = '' rest = '1' chassis = '' bmc = '' boot_progress = '' host = '' # Get the component states. if 'ping' in req_states: # See if the OS pings. cmd_buf = "ping -c 1 -w 2 " + openbmc_host if not quiet: gp.pissuing(cmd_buf) rc, out_buf = commands.getstatusoutput(cmd_buf) if rc == 0: ping = 1 if 'packet_loss' in req_states: # See if the OS pings. cmd_buf = "ping -c 5 -w 5 " + openbmc_host +\ " | egrep 'packet loss' | sed -re 's/.* ([0-9]+)%.*/\\1/g'" if not quiet: gp.pissuing(cmd_buf) rc, out_buf = commands.getstatusoutput(cmd_buf) if rc == 0: packet_loss = out_buf.rstrip("\n") if 'uptime' in req_states: cmd_buf = [ "BMC Execute Command", "cat /proc/uptime | cut -f 1 -d ' '", 'quiet=${1}' ] if not quiet: grp.rpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": stdout, stderr, rc = ret_values if rc == 0 and stderr == "": uptime = stdout if 'epoch_seconds' in req_states: date_cmd_buf = "date -u +%s" if USE_BMC_EPOCH_TIME: cmd_buf = ["BMC Execute Command", date_cmd_buf, 'quiet=${1}'] if not quiet: grp.rpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": stdout, stderr, rc = ret_values if rc == 0 and stderr == "": epoch_seconds = stdout.rstrip("\n") else: shell_rc, out_buf = gc.cmd_fnc_u(date_cmd_buf, quiet=1, print_output=0) if shell_rc == 0: epoch_seconds = out_buf.rstrip("\n") master_req_rest = ['rest', 'chassis', 'bmc', 'boot_progress', 'host'] req_rest = [ sub_state for sub_state in req_states if sub_state in master_req_rest ] need_rest = (len(req_rest) > 0) # Though we could try to determine 'rest' state on any of several calls, # for simplicity, we'll use 'chassis' to figure it out (even if the caller # hasn't explicitly asked for 'chassis'). if 'chassis' in req_states or need_rest: cmd_buf = ["Get Chassis Power State", "quiet=${" + str(quiet) + "}"] grp.rdpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": chassis = ret_values chassis = re.sub(r'.*\.', "", chassis) rest = '1' else: rest = ret_values if rest == '1': if 'bmc' in req_states: if OBMC_STATES_VERSION == 0: qualifier = "utils" else: # This will not be supported much longer. qualifier = "state_manager" cmd_buf = [ qualifier + ".Get BMC State", "quiet=${" + str(quiet) + "}" ] grp.rdpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": bmc = ret_values if 'boot_progress' in req_states: cmd_buf = ["Get Boot Progress", "quiet=${" + str(quiet) + "}"] grp.rdpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": boot_progress = ret_values if 'host' in req_states: if OBMC_STATES_VERSION > 0: cmd_buf = ["Get Host State", "quiet=${" + str(quiet) + "}"] grp.rdpissuing_keyword(cmd_buf) status, ret_values = \ BuiltIn().run_keyword_and_ignore_error(*cmd_buf) if status == "PASS": host = ret_values # Strip everything up to the final period. host = re.sub(r'.*\.', "", host) state = DotDict() for sub_state in req_states: if sub_state.startswith("os_"): # We pass "os_" requests on to get_os_state. continue cmd_buf = "state['" + sub_state + "'] = str(" + sub_state + ")" exec(cmd_buf) if os_host == "": # The caller has not specified an os_host so as far as we're concerned, # it doesn't exist. return state os_req_states = [ sub_state for sub_state in req_states if sub_state.startswith('os_') ] if len(os_req_states) > 0: # The caller has specified an os_host and they have requested # information on os substates. # Based on the information gathered on bmc, we'll try to make a # determination of whether the os is even up. We'll pass the result # of that assessment to get_os_state to enhance performance. os_up_match = DotDict() for sub_state in master_os_up_match: if sub_state in req_states: os_up_match[sub_state] = master_os_up_match[sub_state] os_up = compare_states(state, os_up_match) os_state = get_os_state(os_host=os_host, os_username=os_username, os_password=os_password, req_states=os_req_states, os_up=os_up, quiet=quiet) # Append os_state dictionary to ours. state.update(os_state) return state
def shell_cmd(command_string, quiet=None, print_output=1, show_err=1, test_mode=0, time_out=None, max_attempts=1, retry_sleep_time=5, allowed_shell_rcs=[0], ignore_err=None, return_stderr=0, fork=0): r""" Run the given command string in a shell and return a tuple consisting of the shell return code and the output. Description of argument(s): command_string The command string to be run in a shell (e.g. "ls /tmp"). quiet If set to 0, this function will print "Issuing: <cmd string>" to stdout. When the quiet argument is set to None, this function will assign a default value by searching upward in the stack for the quiet variable value. If no such value is found, quiet is set to 0. print_output If this is set, this function will print the stdout/stderr generated by the shell command to stdout. show_err If show_err is set, this function will print a standardized error report if the shell command fails (i.e. if the shell command returns a shell_rc that is not in allowed_shell_rcs). Note: Error text is only printed if ALL attempts to run the command_string fail. In other words, if the command execution is ultimately successful, initial failures are hidden. test_mode If test_mode is set, this function will not actually run the command. If print_output is also set, this function will print "(test_mode) Issuing: <cmd string>" to stdout. A caller should call shell_cmd directly if they wish to have the command string run unconditionally. They should call the t_shell_cmd wrapper (defined below) if they wish to run the command string only if the prevailing test_mode variable is set to 0. time_out A time-out value expressed in seconds. If the command string has not finished executing within <time_out> seconds, it will be halted and counted as an error. max_attempts The max number of attempts that should be made to run the command string. retry_sleep_time The number of seconds to sleep between attempts. allowed_shell_rcs A list of integers indicating which shell_rc values are not to be considered errors. ignore_err Ignore error means that a failure encountered by running the command string will not be raised as a python exception. When the ignore_err argument is set to None, this function will assign a default value by searching upward in the stack for the ignore_err variable value. If no such value is found, ignore_err is set to 1. return_stderr If return_stderr is set, this function will process the stdout and stderr streams from the shell command separately. In such a case, the tuple returned by this function will consist of three values rather than just two: rc, stdout, stderr. fork Run the command string asynchronously (i.e. don't wait for status of the child process and don't try to get stdout/stderr). """ # Assign default values to some of the arguments to this function. quiet = int(gm.dft(quiet, gp.get_stack_var('quiet', 0))) ignore_err = int(gm.dft(ignore_err, gp.get_stack_var('ignore_err', 1))) err_msg = gv.svalid_value(command_string) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.print_issuing(command_string, test_mode) if test_mode: if return_stderr: return 0, "", "" else: return 0, "" # Convert each list entry to a signed value. allowed_shell_rcs = [gm.to_signed(x) for x in allowed_shell_rcs] if return_stderr: stderr = subprocess.PIPE else: stderr = subprocess.STDOUT shell_rc = 0 out_buf = "" err_buf = "" # Write all output to func_history_stdout rather than directly to stdout. # This allows us to decide what to print after all attempts to run the # command string have been made. func_history_stdout will contain the # complete stdout history from the current invocation of this function. func_history_stdout = "" for attempt_num in range(1, max_attempts + 1): sub_proc = subprocess.Popen(command_string, bufsize=1, shell=True, executable='/bin/bash', stdout=subprocess.PIPE, stderr=stderr) out_buf = "" err_buf = "" # Output from this loop iteration is written to func_stdout for later # processing. func_stdout = "" if fork: break command_timed_out = False if time_out is not None: # Designate a SIGALRM handling function and set alarm. signal.signal(signal.SIGALRM, shell_cmd_timed_out) signal.alarm(time_out) try: if return_stderr: for line in sub_proc.stderr: err_buf += line if not print_output: continue func_stdout += line for line in sub_proc.stdout: out_buf += line if not print_output: continue func_stdout += line except IOError: command_timed_out = True sub_proc.communicate() shell_rc = sub_proc.returncode # Restore the original SIGALRM handler and clear the alarm. signal.signal(signal.SIGALRM, original_sigalrm_handler) signal.alarm(0) if shell_rc in allowed_shell_rcs: break err_msg = "The prior shell command failed.\n" if command_timed_out: err_msg += gp.sprint_var(command_timed_out) err_msg += gp.sprint_var(time_out) err_msg += gp.sprint_varx("child_pid", sub_proc.pid) err_msg += gp.sprint_var(attempt_num) err_msg += gp.sprint_var(shell_rc, 1) err_msg += gp.sprint_var(allowed_shell_rcs, 1) if not print_output: if return_stderr: err_msg += "err_buf:\n" + err_buf err_msg += "out_buf:\n" + out_buf if show_err: if robot_env: func_stdout += grp.sprint_error_report(err_msg) else: func_stdout += gp.sprint_error_report(err_msg) func_history_stdout += func_stdout if attempt_num < max_attempts: func_history_stdout += gp.sprint_issuing("time.sleep(" + str(retry_sleep_time) + ")") time.sleep(retry_sleep_time) if shell_rc not in allowed_shell_rcs: func_stdout = func_history_stdout if robot_env: grp.rprint(func_stdout) else: sys.stdout.write(func_stdout) sys.stdout.flush() if shell_rc not in allowed_shell_rcs: if not ignore_err: if robot_env: BuiltIn().fail(err_msg) else: raise ValueError("The prior shell command failed.\n") if return_stderr: return shell_rc, out_buf, err_buf else: return shell_rc, out_buf