Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 3 | r""" |
| 4 | See help text for details. |
| 5 | """ |
| 6 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 7 | import sys |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 8 | import subprocess |
| 9 | import os |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 10 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 11 | save_dir_path = sys.path.pop(0) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 12 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 13 | modules = ['gen_arg', 'gen_print', 'gen_valid', 'gen_plug_in', 'gen_cmd', 'gen_misc'] |
| 14 | for module in modules: |
| 15 | exec("from " + module + " import *") |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 16 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 17 | sys.path.insert(0, save_dir_path) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 18 | |
| 19 | # Create parser object. |
| 20 | parser = argparse.ArgumentParser( |
| 21 | usage='%(prog)s [OPTIONS]', |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 22 | description="%(prog)s will process the plug-in packages passed to it." |
| 23 | + " A plug-in package is essentially a directory containing" |
| 24 | + " one or more call point programs. Each of these call point" |
| 25 | + " programs must have a prefix of \"cp_\". When calling" |
| 26 | + " %(prog)s, a user must provide a call_point parameter" |
| 27 | + " (described below). For each plug-in package passed," |
| 28 | + " %(prog)s will check for the presence of the specified call" |
| 29 | + " point program in the plug-in directory. If it is found," |
| 30 | + " %(prog)s will run it. It is the responsibility of the" |
| 31 | + " caller to set any environment variables needed by the call" |
| 32 | + " point programs.\n\nAfter each call point program" |
| 33 | + " has been run, %(prog)s will print the following values in" |
| 34 | + " the following formats for use by the calling program:\n" |
| 35 | + " failed_plug_in_name: <failed plug-in value," |
| 36 | + " if any>\n shell_rc: " |
| 37 | + "<shell return code value of last call point program - this" |
| 38 | + " will be printed in hexadecimal format. Also, be aware" |
| 39 | + " that if a call point program returns a value it will be" |
| 40 | + " shifted left 2 bytes (e.g. rc of 2 will be printed as" |
| 41 | + " 0x00000200). That is because the rightmost byte is" |
| 42 | + " reserved for errors in calling the call point program" |
| 43 | + " rather than errors generated by the call point program.>", |
Michael Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 44 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 45 | prefix_chars='-+') |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 46 | |
| 47 | # Create arguments. |
| 48 | parser.add_argument( |
| 49 | 'plug_in_dir_paths', |
| 50 | nargs='?', |
| 51 | default="", |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 52 | help=plug_in_dir_paths_help_text + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 53 | |
| 54 | parser.add_argument( |
| 55 | '--call_point', |
| 56 | default="setup", |
| 57 | required=True, |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 58 | help='The call point program name. This value must not include the' |
| 59 | + ' "cp_" prefix. For each plug-in package passed to this program,' |
| 60 | + ' the specified call_point program will be called if it exists in' |
| 61 | + ' the plug-in directory.' + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 62 | |
| 63 | parser.add_argument( |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 64 | '--allow_shell_rc', |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 65 | default="0x00000000", |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 66 | help='The user may supply a value other than zero to indicate an' |
| 67 | + ' acceptable non-zero return code. For example, if this value' |
| 68 | + ' equals 0x00000200, it means that for each plug-in call point that' |
| 69 | + ' runs, a 0x00000200 will not be counted as a failure. See note' |
| 70 | + ' above regarding left-shifting of return codes.' + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 71 | |
| 72 | parser.add_argument( |
| 73 | '--stop_on_plug_in_failure', |
| 74 | default=1, |
| 75 | type=int, |
| 76 | choices=[1, 0], |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 77 | help='If this parameter is set to 1, this program will stop and return ' |
| 78 | + 'non-zero if the call point program from any plug-in directory ' |
| 79 | + 'fails. Conversely, if it is set to false, this program will run ' |
| 80 | + 'the call point program from each and every plug-in directory ' |
| 81 | + 'regardless of their return values. Typical example cases where ' |
| 82 | + 'you\'d want to run all plug-in call points regardless of success ' |
| 83 | + 'or failure would be "cleanup" or "ffdc" call points.') |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 84 | |
| 85 | parser.add_argument( |
| 86 | '--stop_on_non_zero_rc', |
| 87 | default=0, |
| 88 | type=int, |
| 89 | choices=[1, 0], |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 90 | help='If this parm is set to 1 and a plug-in call point program returns ' |
| 91 | + 'a valid non-zero return code (see "allow_shell_rc" parm above),' |
| 92 | + ' this program will stop processing and return 0 (success). Since' |
| 93 | + ' this constitutes a successful exit, this would normally be used' |
| 94 | + ' where the caller wishes to stop processing if one of the plug-in' |
| 95 | + ' directory call point programs returns a special value indicating' |
| 96 | + ' that some special case has been found. An example might be in' |
| 97 | + ' calling some kind of "check_errl" call point program. Such a' |
| 98 | + ' call point program might return a 2 (i.e. 0x00000200) to indicate' |
| 99 | + ' that a given error log entry was found in an "ignore" list and is' |
| 100 | + ' therefore to be ignored. That being the case, no other' |
| 101 | + ' "check_errl" call point program would need to be called.' |
| 102 | + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 103 | |
| 104 | parser.add_argument( |
| 105 | '--mch_class', |
| 106 | default="obmc", |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 107 | help=mch_class_help_text + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 108 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 109 | # Populate stock_list with options we want. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 110 | stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 111 | |
Michael Walsh | de093d2 | 2019-12-05 17:00:07 -0600 | [diff] [blame] | 112 | original_path = os.environ.get('PATH') |
| 113 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 114 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 115 | def validate_parms(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 116 | r""" |
| 117 | Validate program parameters, etc. Return True or False accordingly. |
| 118 | """ |
| 119 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 120 | valid_value(call_point) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 121 | |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 122 | global allow_shell_rc |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 123 | valid_integer(allow_shell_rc) |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 124 | |
| 125 | # Convert to hex string for consistency in printout. |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 126 | allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0) |
| 127 | set_pgm_arg(allow_shell_rc) |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 128 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 129 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 130 | def run_pgm(plug_in_dir_path, |
| 131 | call_point, |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 132 | allow_shell_rc): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 133 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 134 | Run the call point program in the given plug_in_dir_path. Return the following: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 135 | rc The return code - 0 = PASS, 1 = FAIL. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 136 | shell_rc The shell return code returned by process_plug_in_packages.py. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 137 | failed_plug_in_name The failed plug in name (if any). |
| 138 | |
| 139 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 140 | plug_in_dir_path The directory path where the call_point program may be located. |
| 141 | call_point The call point (e.g. "setup"). This program will look for a program |
| 142 | named "cp_" + call_point in the plug_in_dir_path. If no such call point |
| 143 | program is found, this function returns an rc of 0 (i.e. success). |
| 144 | allow_shell_rc The user may supply a value other than zero to indicate an acceptable |
| 145 | non-zero return code. For example, if this value equals 0x00000200, it |
| 146 | means that for each plug-in call point that runs, a 0x00000200 will not |
| 147 | be counted as a failure. See note above regarding left-shifting of |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 148 | return codes. |
| 149 | """ |
| 150 | |
| 151 | rc = 0 |
| 152 | failed_plug_in_name = "" |
| 153 | shell_rc = 0x00000000 |
| 154 | |
Michael Walsh | 97d5b36 | 2017-05-30 17:57:38 -0500 | [diff] [blame] | 155 | plug_in_name = os.path.basename(os.path.normpath(plug_in_dir_path)) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 156 | cp_prefix = "cp_" |
| 157 | plug_in_pgm_path = plug_in_dir_path + cp_prefix + call_point |
| 158 | if not os.path.exists(plug_in_pgm_path): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 159 | # No such call point in this plug in dir path. This is legal so we return 0, etc. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 160 | return rc, shell_rc, failed_plug_in_name |
| 161 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 162 | print("------------------------------------------------- Starting plug-" |
| 163 | + "in -----------------------------------------------") |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 164 | |
| 165 | print_timen("Running " + plug_in_name + "/" + cp_prefix + call_point + ".") |
Michael Walsh | b3beaa8 | 2019-03-26 16:35:30 -0500 | [diff] [blame] | 166 | |
| 167 | stdout = 1 - quiet |
| 168 | if AUTOBOOT_OPENBMC_NICKNAME != "": |
| 169 | auto_status_file_prefix = AUTOBOOT_OPENBMC_NICKNAME + "." |
Michael Walsh | 97d5b36 | 2017-05-30 17:57:38 -0500 | [diff] [blame] | 170 | else: |
Michael Walsh | b3beaa8 | 2019-03-26 16:35:30 -0500 | [diff] [blame] | 171 | auto_status_file_prefix = "" |
| 172 | auto_status_file_prefix += plug_in_name + ".cp_" + call_point |
| 173 | status_dir_path =\ |
| 174 | add_trailing_slash(os.environ.get("STATUS_DIR_PATH", |
| 175 | os.environ['HOME'] |
| 176 | + "/status/")) |
| 177 | if not os.path.isdir(status_dir_path): |
| 178 | AUTOBOOT_EXECDIR = \ |
| 179 | add_trailing_slash(os.environ.get("AUTOBOOT_EXECDIR", "")) |
| 180 | status_dir_path = AUTOBOOT_EXECDIR + "logs/" |
| 181 | if not os.path.exists(status_dir_path): |
| 182 | os.makedirs(status_dir_path) |
| 183 | status_file_name = auto_status_file_prefix + "." + file_date_time_stamp() \ |
| 184 | + ".status" |
| 185 | auto_status_file_subcmd = "auto_status_file.py --status_dir_path=" \ |
| 186 | + status_dir_path + " --status_file_name=" + status_file_name \ |
| 187 | + " --quiet=1 --show_url=1 --prefix=" \ |
| 188 | + auto_status_file_prefix + " --stdout=" + str(stdout) + " " |
Michael Walsh | 97d5b36 | 2017-05-30 17:57:38 -0500 | [diff] [blame] | 189 | |
Michael Walsh | de093d2 | 2019-12-05 17:00:07 -0600 | [diff] [blame] | 190 | cmd_buf = "PATH=" + plug_in_dir_path.rstrip("/") + ":${PATH}" |
| 191 | print_issuing(cmd_buf) |
| 192 | os.environ['PATH'] = plug_in_dir_path.rstrip("/") + os.pathsep + original_path |
| 193 | cmd_buf = auto_status_file_subcmd + cp_prefix + call_point |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 194 | print_issuing(cmd_buf) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 195 | |
Michael Walsh | be6153b | 2016-12-09 13:36:22 -0600 | [diff] [blame] | 196 | sub_proc = subprocess.Popen(cmd_buf, shell=True) |
| 197 | sub_proc.communicate() |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 198 | shell_rc = sub_proc.returncode |
Michael Walsh | c33ef37 | 2017-01-10 11:46:29 -0600 | [diff] [blame] | 199 | # Shift to left. |
| 200 | shell_rc *= 0x100 |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 201 | if shell_rc != 0 and shell_rc != allow_shell_rc: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 202 | rc = 1 |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 203 | failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 204 | if shell_rc != 0: |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 205 | failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point |
Michael Walsh | b3beaa8 | 2019-03-26 16:35:30 -0500 | [diff] [blame] | 206 | if failed_plug_in_name != "" and not stdout: |
| 207 | # Use tail to avoid double-printing of status_file_url. |
| 208 | shell_cmd("tail -n +2 " + status_dir_path + status_file_name, quiet=1, |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 209 | print_output=1) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 210 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 211 | print("------------------------------------------------- Ending plug-in" |
| 212 | + " -------------------------------------------------") |
Michael Walsh | 97d5b36 | 2017-05-30 17:57:38 -0500 | [diff] [blame] | 213 | if failed_plug_in_name != "": |
| 214 | print_var(failed_plug_in_name) |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 215 | print_var(shell_rc, hexa()) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 216 | |
| 217 | return rc, shell_rc, failed_plug_in_name |
| 218 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 219 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 220 | def main(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 221 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 222 | gen_setup() |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 223 | |
Michael Walsh | de093d2 | 2019-12-05 17:00:07 -0600 | [diff] [blame] | 224 | set_term_options(term_requests='children') |
| 225 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 226 | # Access program parameter globals. |
| 227 | global plug_in_dir_paths |
| 228 | global mch_class |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 229 | global allow_shell_rc |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 230 | global stop_on_plug_in_failure |
| 231 | global stop_on_non_zero_rc |
| 232 | |
| 233 | plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths, |
| 234 | mch_class) |
| 235 | |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 236 | qprint_var(plug_in_packages_list) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 237 | qprint("\n") |
| 238 | |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 239 | allow_shell_rc = int(allow_shell_rc, 0) |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 240 | shell_rc = 0 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 241 | failed_plug_in_name = "" |
| 242 | |
Michael Walsh | 97d5b36 | 2017-05-30 17:57:38 -0500 | [diff] [blame] | 243 | global AUTOBOOT_OPENBMC_NICKNAME |
Michael Walsh | b3beaa8 | 2019-03-26 16:35:30 -0500 | [diff] [blame] | 244 | AUTOBOOT_OPENBMC_NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "") |
| 245 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 246 | ret_code = 0 |
| 247 | for plug_in_dir_path in plug_in_packages_list: |
| 248 | rc, shell_rc, failed_plug_in_name = \ |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 249 | run_pgm(plug_in_dir_path, call_point, allow_shell_rc) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 250 | if rc != 0: |
| 251 | ret_code = 1 |
| 252 | if stop_on_plug_in_failure: |
| 253 | break |
| 254 | if shell_rc != 0 and stop_on_non_zero_rc: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 255 | qprint_time("Stopping on non-zero shell return code as requested" |
| 256 | + " by caller.\n") |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 257 | break |
| 258 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 259 | if ret_code != 0: |
Michael Walsh | 8c5a8a8 | 2018-10-30 13:17:23 -0500 | [diff] [blame] | 260 | print_error("At least one plug-in failed.\n") |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 261 | exit(1) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 262 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 263 | |
Michael Walsh | 60d08f3 | 2019-11-25 16:51:39 -0600 | [diff] [blame] | 264 | main() |