blob: febbd53f3d22d92277dbd19c7d83e64bff423fcb [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh7423c012016-10-04 10:27:21 -05002
Michael Walsh60d08f32019-11-25 16:51:39 -06003r"""
4See help text for details.
5"""
6
George Keishinge635ddc2022-12-08 07:38:02 -06007import os
Patrick Williams20f38712022-12-08 06:18:26 -06008import subprocess
9import sys
Michael Walsh7423c012016-10-04 10:27:21 -050010
Michael Walsh60d08f32019-11-25 16:51:39 -060011save_dir_path = sys.path.pop(0)
Michael Walsh7423c012016-10-04 10:27:21 -050012
Patrick Williams20f38712022-12-08 06:18:26 -060013modules = [
14 "gen_arg",
15 "gen_print",
16 "gen_valid",
17 "gen_plug_in",
18 "gen_cmd",
19 "gen_misc",
20]
Michael Walsh60d08f32019-11-25 16:51:39 -060021for module in modules:
22 exec("from " + module + " import *")
Michael Walsh7423c012016-10-04 10:27:21 -050023
Michael Walsh60d08f32019-11-25 16:51:39 -060024sys.path.insert(0, save_dir_path)
Michael Walsh7423c012016-10-04 10:27:21 -050025
26# Create parser object.
27parser = argparse.ArgumentParser(
Patrick Williams20f38712022-12-08 06:18:26 -060028 usage="%(prog)s [OPTIONS]",
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050029 description="%(prog)s will process the plug-in packages passed to it."
Patrick Williams20f38712022-12-08 06:18:26 -060030 + " A plug-in package is essentially a directory containing"
31 + " one or more call point programs. Each of these call point"
32 + ' programs must have a prefix of "cp_". When calling'
33 + " %(prog)s, a user must provide a call_point parameter"
34 + " (described below). For each plug-in package passed,"
35 + " %(prog)s will check for the presence of the specified call"
36 + " point program in the plug-in directory. If it is found,"
37 + " %(prog)s will run it. It is the responsibility of the"
38 + " caller to set any environment variables needed by the call"
39 + " point programs.\n\nAfter each call point program"
40 + " has been run, %(prog)s will print the following values in"
41 + " the following formats for use by the calling program:\n"
42 + " failed_plug_in_name: <failed plug-in value,"
43 + " if any>\n shell_rc: "
44 + "<shell return code value of last call point program - this"
45 + " will be printed in hexadecimal format. Also, be aware"
46 + " that if a call point program returns a value it will be"
47 + " shifted left 2 bytes (e.g. rc of 2 will be printed as"
48 + " 0x00000200). That is because the rightmost byte is"
49 + " reserved for errors in calling the call point program"
50 + " rather than errors generated by the call point program.>",
Michael Walshd0741f82017-12-21 14:04:21 -060051 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Patrick Williams20f38712022-12-08 06:18:26 -060052 prefix_chars="-+",
53)
Michael Walsh7423c012016-10-04 10:27:21 -050054
55# Create arguments.
56parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060057 "plug_in_dir_paths",
58 nargs="?",
Michael Walsh7423c012016-10-04 10:27:21 -050059 default="",
Patrick Williams20f38712022-12-08 06:18:26 -060060 help=plug_in_dir_paths_help_text + default_string,
61)
Michael Walsh7423c012016-10-04 10:27:21 -050062
63parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060064 "--call_point",
Michael Walsh7423c012016-10-04 10:27:21 -050065 default="setup",
66 required=True,
Patrick Williams20f38712022-12-08 06:18:26 -060067 help="The call point program name. This value must not include the"
68 + ' "cp_" prefix. For each plug-in package passed to this program,'
69 + " the specified call_point program will be called if it exists in"
70 + " the plug-in directory."
71 + default_string,
72)
Michael Walsh7423c012016-10-04 10:27:21 -050073
74parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060075 "--allow_shell_rc",
Michael Walsh7423c012016-10-04 10:27:21 -050076 default="0x00000000",
Patrick Williams20f38712022-12-08 06:18:26 -060077 help="The user may supply a value other than zero to indicate an"
78 + " acceptable non-zero return code. For example, if this value"
79 + " equals 0x00000200, it means that for each plug-in call point that"
80 + " runs, a 0x00000200 will not be counted as a failure. See note"
81 + " above regarding left-shifting of return codes."
82 + default_string,
83)
Michael Walsh7423c012016-10-04 10:27:21 -050084
85parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060086 "--stop_on_plug_in_failure",
Michael Walsh7423c012016-10-04 10:27:21 -050087 default=1,
88 type=int,
89 choices=[1, 0],
Patrick Williams20f38712022-12-08 06:18:26 -060090 help="If this parameter is set to 1, this program will stop and return "
91 + "non-zero if the call point program from any plug-in directory "
92 + "fails. Conversely, if it is set to false, this program will run "
93 + "the call point program from each and every plug-in directory "
94 + "regardless of their return values. Typical example cases where "
95 + "you'd want to run all plug-in call points regardless of success "
96 + 'or failure would be "cleanup" or "ffdc" call points.',
97)
Michael Walsh7423c012016-10-04 10:27:21 -050098
99parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -0600100 "--stop_on_non_zero_rc",
Michael Walsh7423c012016-10-04 10:27:21 -0500101 default=0,
102 type=int,
103 choices=[1, 0],
Patrick Williams20f38712022-12-08 06:18:26 -0600104 help="If this parm is set to 1 and a plug-in call point program returns "
105 + 'a valid non-zero return code (see "allow_shell_rc" parm above),'
106 + " this program will stop processing and return 0 (success). Since"
107 + " this constitutes a successful exit, this would normally be used"
108 + " where the caller wishes to stop processing if one of the plug-in"
109 + " directory call point programs returns a special value indicating"
110 + " that some special case has been found. An example might be in"
111 + ' calling some kind of "check_errl" call point program. Such a'
112 + " call point program might return a 2 (i.e. 0x00000200) to indicate"
113 + ' that a given error log entry was found in an "ignore" list and is'
114 + " therefore to be ignored. That being the case, no other"
115 + ' "check_errl" call point program would need to be called.'
116 + default_string,
117)
Michael Walsh7423c012016-10-04 10:27:21 -0500118
119parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -0600120 "--mch_class", default="obmc", help=mch_class_help_text + default_string
121)
Michael Walsh7423c012016-10-04 10:27:21 -0500122
Michael Walsh60d08f32019-11-25 16:51:39 -0600123# Populate stock_list with options we want.
Michael Walsh7423c012016-10-04 10:27:21 -0500124stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
Michael Walsh7423c012016-10-04 10:27:21 -0500125
Patrick Williams20f38712022-12-08 06:18:26 -0600126original_path = os.environ.get("PATH")
Michael Walshde093d22019-12-05 17:00:07 -0600127
Michael Walsh7423c012016-10-04 10:27:21 -0500128
Michael Walsh7423c012016-10-04 10:27:21 -0500129def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -0500130 r"""
131 Validate program parameters, etc. Return True or False accordingly.
132 """
133
Michael Walsh60d08f32019-11-25 16:51:39 -0600134 valid_value(call_point)
Michael Walsh7423c012016-10-04 10:27:21 -0500135
Michael Walshed18ec72017-06-27 10:15:31 -0500136 global allow_shell_rc
Michael Walsh60d08f32019-11-25 16:51:39 -0600137 valid_integer(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600138
139 # Convert to hex string for consistency in printout.
Michael Walshed18ec72017-06-27 10:15:31 -0500140 allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0)
141 set_pgm_arg(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600142
Michael Walsh7423c012016-10-04 10:27:21 -0500143
Patrick Williams20f38712022-12-08 06:18:26 -0600144def run_pgm(plug_in_dir_path, call_point, allow_shell_rc):
Michael Walsh7423c012016-10-04 10:27:21 -0500145 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500146 Run the call point program in the given plug_in_dir_path. Return the following:
Michael Walsh7423c012016-10-04 10:27:21 -0500147 rc The return code - 0 = PASS, 1 = FAIL.
Michael Walsh410b1782019-10-22 15:56:18 -0500148 shell_rc The shell return code returned by process_plug_in_packages.py.
Michael Walsh7423c012016-10-04 10:27:21 -0500149 failed_plug_in_name The failed plug in name (if any).
150
151 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500152 plug_in_dir_path The directory path where the call_point program may be located.
153 call_point The call point (e.g. "setup"). This program will look for a program
154 named "cp_" + call_point in the plug_in_dir_path. If no such call point
155 program is found, this function returns an rc of 0 (i.e. success).
156 allow_shell_rc The user may supply a value other than zero to indicate an acceptable
157 non-zero return code. For example, if this value equals 0x00000200, it
158 means that for each plug-in call point that runs, a 0x00000200 will not
159 be counted as a failure. See note above regarding left-shifting of
Michael Walsh7423c012016-10-04 10:27:21 -0500160 return codes.
161 """
162
163 rc = 0
164 failed_plug_in_name = ""
165 shell_rc = 0x00000000
166
Michael Walsh97d5b362017-05-30 17:57:38 -0500167 plug_in_name = os.path.basename(os.path.normpath(plug_in_dir_path))
Michael Walsh7423c012016-10-04 10:27:21 -0500168 cp_prefix = "cp_"
169 plug_in_pgm_path = plug_in_dir_path + cp_prefix + call_point
170 if not os.path.exists(plug_in_pgm_path):
Michael Walsh410b1782019-10-22 15:56:18 -0500171 # No such call point in this plug in dir path. This is legal so we return 0, etc.
Michael Walsh7423c012016-10-04 10:27:21 -0500172 return rc, shell_rc, failed_plug_in_name
173
Patrick Williams20f38712022-12-08 06:18:26 -0600174 print(
175 "------------------------------------------------- Starting plug-"
176 + "in -----------------------------------------------"
177 )
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500178
179 print_timen("Running " + plug_in_name + "/" + cp_prefix + call_point + ".")
Michael Walshb3beaa82019-03-26 16:35:30 -0500180
181 stdout = 1 - quiet
182 if AUTOBOOT_OPENBMC_NICKNAME != "":
183 auto_status_file_prefix = AUTOBOOT_OPENBMC_NICKNAME + "."
Michael Walsh97d5b362017-05-30 17:57:38 -0500184 else:
Michael Walshb3beaa82019-03-26 16:35:30 -0500185 auto_status_file_prefix = ""
186 auto_status_file_prefix += plug_in_name + ".cp_" + call_point
Patrick Williams20f38712022-12-08 06:18:26 -0600187 status_dir_path = add_trailing_slash(
188 os.environ.get("STATUS_DIR_PATH", os.environ["HOME"] + "/status/")
189 )
Michael Walshb3beaa82019-03-26 16:35:30 -0500190 if not os.path.isdir(status_dir_path):
Patrick Williams20f38712022-12-08 06:18:26 -0600191 AUTOBOOT_EXECDIR = add_trailing_slash(
192 os.environ.get("AUTOBOOT_EXECDIR", "")
193 )
Michael Walshb3beaa82019-03-26 16:35:30 -0500194 status_dir_path = AUTOBOOT_EXECDIR + "logs/"
195 if not os.path.exists(status_dir_path):
196 os.makedirs(status_dir_path)
Patrick Williams20f38712022-12-08 06:18:26 -0600197 status_file_name = (
198 auto_status_file_prefix + "." + file_date_time_stamp() + ".status"
199 )
200 auto_status_file_subcmd = (
201 "auto_status_file.py --status_dir_path="
202 + status_dir_path
203 + " --status_file_name="
204 + status_file_name
205 + " --quiet=1 --show_url=1 --prefix="
206 + auto_status_file_prefix
207 + " --stdout="
208 + str(stdout)
209 + " "
210 )
Michael Walsh97d5b362017-05-30 17:57:38 -0500211
Michael Walshde093d22019-12-05 17:00:07 -0600212 cmd_buf = "PATH=" + plug_in_dir_path.rstrip("/") + ":${PATH}"
213 print_issuing(cmd_buf)
Patrick Williams20f38712022-12-08 06:18:26 -0600214 os.environ["PATH"] = (
215 plug_in_dir_path.rstrip("/") + os.pathsep + original_path
216 )
Michael Walshde093d22019-12-05 17:00:07 -0600217 cmd_buf = auto_status_file_subcmd + cp_prefix + call_point
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500218 print_issuing(cmd_buf)
Michael Walsh7423c012016-10-04 10:27:21 -0500219
Michael Walshbe6153b2016-12-09 13:36:22 -0600220 sub_proc = subprocess.Popen(cmd_buf, shell=True)
221 sub_proc.communicate()
Michael Walsh7423c012016-10-04 10:27:21 -0500222 shell_rc = sub_proc.returncode
Michael Walshc33ef372017-01-10 11:46:29 -0600223 # Shift to left.
224 shell_rc *= 0x100
Michael Walshed18ec72017-06-27 10:15:31 -0500225 if shell_rc != 0 and shell_rc != allow_shell_rc:
Michael Walsh7423c012016-10-04 10:27:21 -0500226 rc = 1
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500227 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500228 if shell_rc != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500229 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walshb3beaa82019-03-26 16:35:30 -0500230 if failed_plug_in_name != "" and not stdout:
231 # Use tail to avoid double-printing of status_file_url.
Patrick Williams20f38712022-12-08 06:18:26 -0600232 shell_cmd(
233 "tail -n +2 " + status_dir_path + status_file_name,
234 quiet=1,
235 print_output=1,
236 )
Michael Walsh7423c012016-10-04 10:27:21 -0500237
Patrick Williams20f38712022-12-08 06:18:26 -0600238 print(
239 "------------------------------------------------- Ending plug-in"
240 + " -------------------------------------------------"
241 )
Michael Walsh97d5b362017-05-30 17:57:38 -0500242 if failed_plug_in_name != "":
243 print_var(failed_plug_in_name)
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500244 print_var(shell_rc, hexa())
Michael Walsh7423c012016-10-04 10:27:21 -0500245
246 return rc, shell_rc, failed_plug_in_name
247
Michael Walsh7423c012016-10-04 10:27:21 -0500248
Michael Walsh7423c012016-10-04 10:27:21 -0500249def main():
Michael Walsh60d08f32019-11-25 16:51:39 -0600250 gen_setup()
Michael Walsh7423c012016-10-04 10:27:21 -0500251
Patrick Williams20f38712022-12-08 06:18:26 -0600252 set_term_options(term_requests="children")
Michael Walshde093d22019-12-05 17:00:07 -0600253
Michael Walsh7423c012016-10-04 10:27:21 -0500254 # Access program parameter globals.
255 global plug_in_dir_paths
256 global mch_class
Michael Walshed18ec72017-06-27 10:15:31 -0500257 global allow_shell_rc
Michael Walsh7423c012016-10-04 10:27:21 -0500258 global stop_on_plug_in_failure
259 global stop_on_non_zero_rc
260
Patrick Williams20f38712022-12-08 06:18:26 -0600261 plug_in_packages_list = return_plug_in_packages_list(
262 plug_in_dir_paths, mch_class
263 )
Michael Walsh7423c012016-10-04 10:27:21 -0500264
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500265 qprint_var(plug_in_packages_list)
Michael Walsh7423c012016-10-04 10:27:21 -0500266 qprint("\n")
267
Michael Walshed18ec72017-06-27 10:15:31 -0500268 allow_shell_rc = int(allow_shell_rc, 0)
Michael Walsha6723f22016-11-22 11:12:01 -0600269 shell_rc = 0
Michael Walsh7423c012016-10-04 10:27:21 -0500270 failed_plug_in_name = ""
271
Michael Walsh97d5b362017-05-30 17:57:38 -0500272 global AUTOBOOT_OPENBMC_NICKNAME
Michael Walshb3beaa82019-03-26 16:35:30 -0500273 AUTOBOOT_OPENBMC_NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "")
274
Michael Walsh7423c012016-10-04 10:27:21 -0500275 ret_code = 0
276 for plug_in_dir_path in plug_in_packages_list:
Patrick Williams20f38712022-12-08 06:18:26 -0600277 rc, shell_rc, failed_plug_in_name = run_pgm(
278 plug_in_dir_path, call_point, allow_shell_rc
279 )
Michael Walsh7423c012016-10-04 10:27:21 -0500280 if rc != 0:
281 ret_code = 1
282 if stop_on_plug_in_failure:
283 break
284 if shell_rc != 0 and stop_on_non_zero_rc:
Patrick Williams20f38712022-12-08 06:18:26 -0600285 qprint_time(
286 "Stopping on non-zero shell return code as requested"
287 + " by caller.\n"
288 )
Michael Walsh7423c012016-10-04 10:27:21 -0500289 break
290
Michael Walsh60d08f32019-11-25 16:51:39 -0600291 if ret_code != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500292 print_error("At least one plug-in failed.\n")
Michael Walsh60d08f32019-11-25 16:51:39 -0600293 exit(1)
Michael Walsh7423c012016-10-04 10:27:21 -0500294
Michael Walsh7423c012016-10-04 10:27:21 -0500295
Michael Walsh60d08f32019-11-25 16:51:39 -0600296main()