blob: fc9f64bd63baa0aab8d87ea2b72ce190b5336c94 [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
3import sys
George Keishing36efbc02018-12-12 10:18:23 -06004try:
5 import __builtin__
6except ImportError:
7 import builtins as __builtin__
Michael Walsh7423c012016-10-04 10:27:21 -05008import subprocess
9import os
10import argparse
11
12# python puts the program's directory path in sys.path[0]. In other words,
13# the user ordinarily has no way to override python's choice of a module from
14# its own dir. We want to have that ability in our environment. However, we
15# don't want to break any established python modules that depend on this
16# behavior. So, we'll save the value from sys.path[0], delete it, import our
17# modules and then restore sys.path to its original value.
18
19save_path_0 = sys.path[0]
20del sys.path[0]
21
22from gen_print import *
23from gen_valid import *
24from gen_arg import *
25from gen_plug_in import *
Michael Walsh97d5b362017-05-30 17:57:38 -050026from gen_cmd import *
Michael Walsh8c5a8a82018-10-30 13:17:23 -050027from gen_misc import *
Michael Walsh7423c012016-10-04 10:27:21 -050028
29# Restore sys.path[0].
30sys.path.insert(0, save_path_0)
31# I use this variable in calls to print_var.
32hex = 1
33
Michael Walsh7423c012016-10-04 10:27:21 -050034# Create parser object to process command line parameters and args.
35
36# Create parser object.
37parser = argparse.ArgumentParser(
38 usage='%(prog)s [OPTIONS]',
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050039 description="%(prog)s will process the plug-in packages passed to it."
40 + " A plug-in package is essentially a directory containing"
41 + " one or more call point programs. Each of these call point"
42 + " programs must have a prefix of \"cp_\". When calling"
43 + " %(prog)s, a user must provide a call_point parameter"
44 + " (described below). For each plug-in package passed,"
45 + " %(prog)s will check for the presence of the specified call"
46 + " point program in the plug-in directory. If it is found,"
47 + " %(prog)s will run it. It is the responsibility of the"
48 + " caller to set any environment variables needed by the call"
49 + " point programs.\n\nAfter each call point program"
50 + " has been run, %(prog)s will print the following values in"
51 + " the following formats for use by the calling program:\n"
52 + " failed_plug_in_name: <failed plug-in value,"
53 + " if any>\n shell_rc: "
54 + "<shell return code value of last call point program - this"
55 + " will be printed in hexadecimal format. Also, be aware"
56 + " that if a call point program returns a value it will be"
57 + " shifted left 2 bytes (e.g. rc of 2 will be printed as"
58 + " 0x00000200). That is because the rightmost byte is"
59 + " reserved for errors in calling the call point program"
60 + " rather than errors generated by the call point program.>",
Michael Walshd0741f82017-12-21 14:04:21 -060061 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walshc33ef372017-01-10 11:46:29 -060062 prefix_chars='-+')
Michael Walsh7423c012016-10-04 10:27:21 -050063
64# Create arguments.
65parser.add_argument(
66 'plug_in_dir_paths',
67 nargs='?',
68 default="",
Michael Walshc33ef372017-01-10 11:46:29 -060069 help=plug_in_dir_paths_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050070
71parser.add_argument(
72 '--call_point',
73 default="setup",
74 required=True,
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050075 help='The call point program name. This value must not include the'
76 + ' "cp_" prefix. For each plug-in package passed to this program,'
77 + ' the specified call_point program will be called if it exists in'
78 + ' the plug-in directory.' + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050079
80parser.add_argument(
Michael Walshed18ec72017-06-27 10:15:31 -050081 '--allow_shell_rc',
Michael Walsh7423c012016-10-04 10:27:21 -050082 default="0x00000000",
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050083 help='The user may supply a value other than zero to indicate an'
84 + ' acceptable non-zero return code. For example, if this value'
85 + ' equals 0x00000200, it means that for each plug-in call point that'
86 + ' runs, a 0x00000200 will not be counted as a failure. See note'
87 + ' above regarding left-shifting of return codes.' + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050088
89parser.add_argument(
90 '--stop_on_plug_in_failure',
91 default=1,
92 type=int,
93 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050094 help='If this parameter is set to 1, this program will stop and return '
95 + 'non-zero if the call point program from any plug-in directory '
96 + 'fails. Conversely, if it is set to false, this program will run '
97 + 'the call point program from each and every plug-in directory '
98 + 'regardless of their return values. Typical example cases where '
99 + 'you\'d want to run all plug-in call points regardless of success '
100 + 'or failure would be "cleanup" or "ffdc" call points.')
Michael Walsh7423c012016-10-04 10:27:21 -0500101
102parser.add_argument(
103 '--stop_on_non_zero_rc',
104 default=0,
105 type=int,
106 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500107 help='If this parm is set to 1 and a plug-in call point program returns '
108 + 'a valid non-zero return code (see "allow_shell_rc" parm above),'
109 + ' this program will stop processing and return 0 (success). Since'
110 + ' this constitutes a successful exit, this would normally be used'
111 + ' where the caller wishes to stop processing if one of the plug-in'
112 + ' directory call point programs returns a special value indicating'
113 + ' that some special case has been found. An example might be in'
114 + ' calling some kind of "check_errl" call point program. Such a'
115 + ' call point program might return a 2 (i.e. 0x00000200) to indicate'
116 + ' that a given error log entry was found in an "ignore" list and is'
117 + ' therefore to be ignored. That being the case, no other'
118 + ' "check_errl" call point program would need to be called.'
119 + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -0500120
121parser.add_argument(
122 '--mch_class',
123 default="obmc",
Michael Walshc33ef372017-01-10 11:46:29 -0600124 help=mch_class_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -0500125
126# The stock_list will be passed to gen_get_options. We populate it with the
127# names of stock parm options we want. These stock parms are pre-defined by
128# gen_get_options.
129stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
Michael Walsh7423c012016-10-04 10:27:21 -0500130
131
Michael Walsh7423c012016-10-04 10:27:21 -0500132def exit_function(signal_number=0,
133 frame=None):
Michael Walsh7423c012016-10-04 10:27:21 -0500134 r"""
135 Execute whenever the program ends normally or with the signals that we
136 catch (i.e. TERM, INT).
137 """
138
139 dprint_executing()
140 dprint_var(signal_number)
141
142 qprint_pgm_footer()
143
Michael Walsh7423c012016-10-04 10:27:21 -0500144
Michael Walsh7423c012016-10-04 10:27:21 -0500145def signal_handler(signal_number, frame):
Michael Walsh7423c012016-10-04 10:27:21 -0500146 r"""
147 Handle signals. Without a function to catch a SIGTERM or SIGINT, our
148 program would terminate immediately with return code 143 and without
149 calling our exit_function.
150 """
151
152 # Our convention is to set up exit_function with atexit.registr() so
153 # there is no need to explicitly call exit_function from here.
154
155 dprint_executing()
156
157 # Calling exit prevents us from returning to the code that was running
158 # when we received the signal.
159 exit(0)
160
Michael Walsh7423c012016-10-04 10:27:21 -0500161
Michael Walsh7423c012016-10-04 10:27:21 -0500162def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -0500163 r"""
164 Validate program parameters, etc. Return True or False accordingly.
165 """
166
167 if not valid_value(call_point):
168 return False
169
Michael Walshed18ec72017-06-27 10:15:31 -0500170 global allow_shell_rc
171 if not valid_integer(allow_shell_rc):
Michael Walshc33ef372017-01-10 11:46:29 -0600172 return False
173
174 # Convert to hex string for consistency in printout.
Michael Walshed18ec72017-06-27 10:15:31 -0500175 allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0)
176 set_pgm_arg(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600177
Michael Walsh7423c012016-10-04 10:27:21 -0500178 gen_post_validation(exit_function, signal_handler)
179
180 return True
181
Michael Walsh7423c012016-10-04 10:27:21 -0500182
Michael Walsh7423c012016-10-04 10:27:21 -0500183def run_pgm(plug_in_dir_path,
184 call_point,
Michael Walshed18ec72017-06-27 10:15:31 -0500185 allow_shell_rc):
Michael Walsh7423c012016-10-04 10:27:21 -0500186 r"""
187 Run the call point program in the given plug_in_dir_path. Return the
188 following:
189 rc The return code - 0 = PASS, 1 = FAIL.
190 shell_rc The shell return code returned by
191 process_plug_in_packages.py.
192 failed_plug_in_name The failed plug in name (if any).
193
194 Description of arguments:
195 plug_in_dir_path The directory path where the call_point
196 program may be located.
197 call_point The call point (e.g. "setup"). This
198 program will look for a program named
199 "cp_" + call_point in the
200 plug_in_dir_path. If no such call point
201 program is found, this function returns an
202 rc of 0 (i.e. success).
Michael Walshed18ec72017-06-27 10:15:31 -0500203 allow_shell_rc The user may supply a value other than
Michael Walsh7423c012016-10-04 10:27:21 -0500204 zero to indicate an acceptable non-zero
205 return code. For example, if this value
206 equals 0x00000200, it means that for each
207 plug-in call point that runs, a 0x00000200
208 will not be counted as a failure. See
209 note above regarding left-shifting of
210 return codes.
211 """
212
Michael Walsh97d5b362017-05-30 17:57:38 -0500213 global autoscript
214
Michael Walsh7423c012016-10-04 10:27:21 -0500215 rc = 0
216 failed_plug_in_name = ""
217 shell_rc = 0x00000000
218
Michael Walsh97d5b362017-05-30 17:57:38 -0500219 plug_in_name = os.path.basename(os.path.normpath(plug_in_dir_path))
Michael Walsh7423c012016-10-04 10:27:21 -0500220 cp_prefix = "cp_"
221 plug_in_pgm_path = plug_in_dir_path + cp_prefix + call_point
222 if not os.path.exists(plug_in_pgm_path):
223 # No such call point in this plug in dir path. This is legal so we
224 # return 0, etc.
225 return rc, shell_rc, failed_plug_in_name
226
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500227 print("------------------------------------------------- Starting plug-"
228 + "in -----------------------------------------------")
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500229
230 print_timen("Running " + plug_in_name + "/" + cp_prefix + call_point + ".")
Michael Walsh97d5b362017-05-30 17:57:38 -0500231 if autoscript:
232 stdout = 1 - quiet
233 if AUTOBOOT_OPENBMC_NICKNAME != "":
234 autoscript_prefix = AUTOBOOT_OPENBMC_NICKNAME + "."
235 else:
236 autoscript_prefix = ""
237 autoscript_prefix += plug_in_name + ".cp_" + call_point
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500238 status_dir_path =\
239 add_trailing_slash(os.environ.get("STATUS_DIR_PATH",
240 os.environ['HOME']
241 + "/autoipl/status/"))
242 status_file_name = autoscript_prefix + "." + file_date_time_stamp() \
243 + ".status"
244 autoscript_subcmd = "autoscript --status_dir_path=" + status_dir_path\
245 + " --status_file_name=" + status_file_name\
246 + " --quiet=1 --show_url=y --prefix=" +\
Michael Walsh97d5b362017-05-30 17:57:38 -0500247 autoscript_prefix + " --stdout=" + str(stdout) + " -- "
248 else:
249 autoscript_subcmd = ""
250
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500251 cmd_buf = "PATH=" + plug_in_dir_path.rstrip("/") + ":${PATH} ; " +\
252 autoscript_subcmd + cp_prefix + call_point
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500253 print_issuing(cmd_buf)
Michael Walsh7423c012016-10-04 10:27:21 -0500254
Michael Walshbe6153b2016-12-09 13:36:22 -0600255 sub_proc = subprocess.Popen(cmd_buf, shell=True)
256 sub_proc.communicate()
Michael Walsh7423c012016-10-04 10:27:21 -0500257 shell_rc = sub_proc.returncode
Michael Walshc33ef372017-01-10 11:46:29 -0600258 # Shift to left.
259 shell_rc *= 0x100
Michael Walshed18ec72017-06-27 10:15:31 -0500260 if shell_rc != 0 and shell_rc != allow_shell_rc:
Michael Walsh7423c012016-10-04 10:27:21 -0500261 rc = 1
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500262 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500263 if shell_rc != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500264 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
265 if failed_plug_in_name != "" and autoscript and not stdout:
266 shell_cmd("cat " + status_dir_path + status_file_name, quiet=1,
267 print_output=1)
Michael Walsh7423c012016-10-04 10:27:21 -0500268
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500269 print("------------------------------------------------- Ending plug-in"
270 + " -------------------------------------------------")
Michael Walsh97d5b362017-05-30 17:57:38 -0500271 if failed_plug_in_name != "":
272 print_var(failed_plug_in_name)
273 print_var(shell_rc, hex)
Michael Walsh7423c012016-10-04 10:27:21 -0500274
275 return rc, shell_rc, failed_plug_in_name
276
Michael Walsh7423c012016-10-04 10:27:21 -0500277
Michael Walsh7423c012016-10-04 10:27:21 -0500278def main():
Michael Walsh7423c012016-10-04 10:27:21 -0500279 r"""
280 This is the "main" function. The advantage of having this function vs
281 just doing this in the true mainline is that you can:
282 - Declare local variables
283 - Use "return" instead of "exit".
284 - Indent 4 chars like you would in any function.
285 This makes coding more consistent, i.e. it's easy to move code from here
286 into a function and vice versa.
287 """
288
289 if not gen_get_options(parser, stock_list):
290 return False
291
292 if not validate_parms():
293 return False
294
295 qprint_pgm_header()
296
297 # Access program parameter globals.
298 global plug_in_dir_paths
299 global mch_class
Michael Walshed18ec72017-06-27 10:15:31 -0500300 global allow_shell_rc
Michael Walsh7423c012016-10-04 10:27:21 -0500301 global stop_on_plug_in_failure
302 global stop_on_non_zero_rc
303
304 plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths,
305 mch_class)
306
307 qpvar(plug_in_packages_list)
Michael Walsh7423c012016-10-04 10:27:21 -0500308 qprint("\n")
309
Michael Walshed18ec72017-06-27 10:15:31 -0500310 allow_shell_rc = int(allow_shell_rc, 0)
Michael Walsha6723f22016-11-22 11:12:01 -0600311 shell_rc = 0
Michael Walsh7423c012016-10-04 10:27:21 -0500312 failed_plug_in_name = ""
313
Michael Walsh97d5b362017-05-30 17:57:38 -0500314 # If the autoscript program is present, we will use it to direct call point
315 # program output to a separate status file. This keeps the output of the
316 # main program (i.e. OBMC Boot Test) cleaner and yet preserves call point
317 # output if it is needed for debug.
318 global autoscript
319 global AUTOBOOT_OPENBMC_NICKNAME
320 autoscript = 0
321 AUTOBOOT_OPENBMC_NICKNAME = ""
322 rc, out_buf = cmd_fnc("which autoscript", quiet=1, print_output=0,
323 show_err=0)
324 if rc == 0:
325 autoscript = 1
326 AUTOBOOT_OPENBMC_NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME",
327 "")
Michael Walsh7423c012016-10-04 10:27:21 -0500328 ret_code = 0
329 for plug_in_dir_path in plug_in_packages_list:
330 rc, shell_rc, failed_plug_in_name = \
Michael Walshed18ec72017-06-27 10:15:31 -0500331 run_pgm(plug_in_dir_path, call_point, allow_shell_rc)
Michael Walsh7423c012016-10-04 10:27:21 -0500332 if rc != 0:
333 ret_code = 1
334 if stop_on_plug_in_failure:
335 break
336 if shell_rc != 0 and stop_on_non_zero_rc:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500337 qprint_time("Stopping on non-zero shell return code as requested"
338 + " by caller.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500339 break
340
341 if ret_code == 0:
342 return True
343 else:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500344 print_error("At least one plug-in failed.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500345 return False
346
Michael Walsh7423c012016-10-04 10:27:21 -0500347
Michael Walsh7423c012016-10-04 10:27:21 -0500348# Main
349
350if not main():
351 exit(1)