blob: 994189263c3022a9c71aafde609cfd65c13696f9 [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
Michael Walsh410b1782019-10-22 15:56:18 -050012# python puts the program's directory path in sys.path[0]. In other words, the user ordinarily has no way
13# to override python's choice of a module from its own dir. We want to have that ability in our environment.
14# However, we don't want to break any established python modules that depend on this behavior. So, we'll
15# save the value from sys.path[0], delete it, import our modules and then restore sys.path to its original
16# value.
Michael Walsh7423c012016-10-04 10:27:21 -050017
18save_path_0 = sys.path[0]
19del sys.path[0]
20
21from gen_print import *
22from gen_valid import *
23from gen_arg import *
24from gen_plug_in import *
Michael Walsh97d5b362017-05-30 17:57:38 -050025from gen_cmd import *
Michael Walsh8c5a8a82018-10-30 13:17:23 -050026from gen_misc import *
Michael Walsh7423c012016-10-04 10:27:21 -050027
28# Restore sys.path[0].
29sys.path.insert(0, save_path_0)
Michael Walsh7423c012016-10-04 10:27:21 -050030
Michael Walsh7423c012016-10-04 10:27:21 -050031# Create parser object to process command line parameters and args.
32
33# Create parser object.
34parser = argparse.ArgumentParser(
35 usage='%(prog)s [OPTIONS]',
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050036 description="%(prog)s will process the plug-in packages passed to it."
37 + " A plug-in package is essentially a directory containing"
38 + " one or more call point programs. Each of these call point"
39 + " programs must have a prefix of \"cp_\". When calling"
40 + " %(prog)s, a user must provide a call_point parameter"
41 + " (described below). For each plug-in package passed,"
42 + " %(prog)s will check for the presence of the specified call"
43 + " point program in the plug-in directory. If it is found,"
44 + " %(prog)s will run it. It is the responsibility of the"
45 + " caller to set any environment variables needed by the call"
46 + " point programs.\n\nAfter each call point program"
47 + " has been run, %(prog)s will print the following values in"
48 + " the following formats for use by the calling program:\n"
49 + " failed_plug_in_name: <failed plug-in value,"
50 + " if any>\n shell_rc: "
51 + "<shell return code value of last call point program - this"
52 + " will be printed in hexadecimal format. Also, be aware"
53 + " that if a call point program returns a value it will be"
54 + " shifted left 2 bytes (e.g. rc of 2 will be printed as"
55 + " 0x00000200). That is because the rightmost byte is"
56 + " reserved for errors in calling the call point program"
57 + " rather than errors generated by the call point program.>",
Michael Walshd0741f82017-12-21 14:04:21 -060058 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walshc33ef372017-01-10 11:46:29 -060059 prefix_chars='-+')
Michael Walsh7423c012016-10-04 10:27:21 -050060
61# Create arguments.
62parser.add_argument(
63 'plug_in_dir_paths',
64 nargs='?',
65 default="",
Michael Walshc33ef372017-01-10 11:46:29 -060066 help=plug_in_dir_paths_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050067
68parser.add_argument(
69 '--call_point',
70 default="setup",
71 required=True,
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050072 help='The call point program name. This value must not include the'
73 + ' "cp_" prefix. For each plug-in package passed to this program,'
74 + ' the specified call_point program will be called if it exists in'
75 + ' the plug-in directory.' + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050076
77parser.add_argument(
Michael Walshed18ec72017-06-27 10:15:31 -050078 '--allow_shell_rc',
Michael Walsh7423c012016-10-04 10:27:21 -050079 default="0x00000000",
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050080 help='The user may supply a value other than zero to indicate an'
81 + ' acceptable non-zero return code. For example, if this value'
82 + ' equals 0x00000200, it means that for each plug-in call point that'
83 + ' runs, a 0x00000200 will not be counted as a failure. See note'
84 + ' above regarding left-shifting of return codes.' + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050085
86parser.add_argument(
87 '--stop_on_plug_in_failure',
88 default=1,
89 type=int,
90 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050091 help='If this parameter is set to 1, this program will stop and return '
92 + 'non-zero if the call point program from any plug-in directory '
93 + 'fails. Conversely, if it is set to false, this program will run '
94 + 'the call point program from each and every plug-in directory '
95 + 'regardless of their return values. Typical example cases where '
96 + 'you\'d want to run all plug-in call points regardless of success '
97 + 'or failure would be "cleanup" or "ffdc" call points.')
Michael Walsh7423c012016-10-04 10:27:21 -050098
99parser.add_argument(
100 '--stop_on_non_zero_rc',
101 default=0,
102 type=int,
103 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500104 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)
Michael Walsh7423c012016-10-04 10:27:21 -0500117
118parser.add_argument(
119 '--mch_class',
120 default="obmc",
Michael Walshc33ef372017-01-10 11:46:29 -0600121 help=mch_class_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -0500122
Michael Walsh410b1782019-10-22 15:56:18 -0500123# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we
124# want. These stock parms are pre-defined by gen_get_options.
Michael Walsh7423c012016-10-04 10:27:21 -0500125stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
Michael Walsh7423c012016-10-04 10:27:21 -0500126
127
Michael Walsh7423c012016-10-04 10:27:21 -0500128def exit_function(signal_number=0,
129 frame=None):
Michael Walsh7423c012016-10-04 10:27:21 -0500130 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500131 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh7423c012016-10-04 10:27:21 -0500132 """
133
134 dprint_executing()
135 dprint_var(signal_number)
136
137 qprint_pgm_footer()
138
Michael Walsh7423c012016-10-04 10:27:21 -0500139
Michael Walsh7423c012016-10-04 10:27:21 -0500140def signal_handler(signal_number, frame):
Michael Walsh7423c012016-10-04 10:27:21 -0500141 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500142 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
143 with return code 143 and without calling our exit_function.
Michael Walsh7423c012016-10-04 10:27:21 -0500144 """
145
Michael Walsh410b1782019-10-22 15:56:18 -0500146 # Our convention is to set up exit_function with atexit.registr() so there is no need to explicitly call
147 # exit_function from here.
Michael Walsh7423c012016-10-04 10:27:21 -0500148
149 dprint_executing()
150
Michael Walsh410b1782019-10-22 15:56:18 -0500151 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh7423c012016-10-04 10:27:21 -0500152 exit(0)
153
Michael Walsh7423c012016-10-04 10:27:21 -0500154
Michael Walsh7423c012016-10-04 10:27:21 -0500155def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -0500156 r"""
157 Validate program parameters, etc. Return True or False accordingly.
158 """
159
160 if not valid_value(call_point):
161 return False
162
Michael Walshed18ec72017-06-27 10:15:31 -0500163 global allow_shell_rc
164 if not valid_integer(allow_shell_rc):
Michael Walshc33ef372017-01-10 11:46:29 -0600165 return False
166
167 # Convert to hex string for consistency in printout.
Michael Walshed18ec72017-06-27 10:15:31 -0500168 allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0)
169 set_pgm_arg(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600170
Michael Walsh7423c012016-10-04 10:27:21 -0500171 gen_post_validation(exit_function, signal_handler)
172
173 return True
174
Michael Walsh7423c012016-10-04 10:27:21 -0500175
Michael Walsh7423c012016-10-04 10:27:21 -0500176def run_pgm(plug_in_dir_path,
177 call_point,
Michael Walshed18ec72017-06-27 10:15:31 -0500178 allow_shell_rc):
Michael Walsh7423c012016-10-04 10:27:21 -0500179 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500180 Run the call point program in the given plug_in_dir_path. Return the following:
Michael Walsh7423c012016-10-04 10:27:21 -0500181 rc The return code - 0 = PASS, 1 = FAIL.
Michael Walsh410b1782019-10-22 15:56:18 -0500182 shell_rc The shell return code returned by process_plug_in_packages.py.
Michael Walsh7423c012016-10-04 10:27:21 -0500183 failed_plug_in_name The failed plug in name (if any).
184
185 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500186 plug_in_dir_path The directory path where the call_point program may be located.
187 call_point The call point (e.g. "setup"). This program will look for a program
188 named "cp_" + call_point in the plug_in_dir_path. If no such call point
189 program is found, this function returns an rc of 0 (i.e. success).
190 allow_shell_rc The user may supply a value other than zero to indicate an acceptable
191 non-zero return code. For example, if this value equals 0x00000200, it
192 means that for each plug-in call point that runs, a 0x00000200 will not
193 be counted as a failure. See note above regarding left-shifting of
Michael Walsh7423c012016-10-04 10:27:21 -0500194 return codes.
195 """
196
197 rc = 0
198 failed_plug_in_name = ""
199 shell_rc = 0x00000000
200
Michael Walsh97d5b362017-05-30 17:57:38 -0500201 plug_in_name = os.path.basename(os.path.normpath(plug_in_dir_path))
Michael Walsh7423c012016-10-04 10:27:21 -0500202 cp_prefix = "cp_"
203 plug_in_pgm_path = plug_in_dir_path + cp_prefix + call_point
204 if not os.path.exists(plug_in_pgm_path):
Michael Walsh410b1782019-10-22 15:56:18 -0500205 # 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 -0500206 return rc, shell_rc, failed_plug_in_name
207
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500208 print("------------------------------------------------- Starting plug-"
209 + "in -----------------------------------------------")
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500210
211 print_timen("Running " + plug_in_name + "/" + cp_prefix + call_point + ".")
Michael Walshb3beaa82019-03-26 16:35:30 -0500212
213 stdout = 1 - quiet
214 if AUTOBOOT_OPENBMC_NICKNAME != "":
215 auto_status_file_prefix = AUTOBOOT_OPENBMC_NICKNAME + "."
Michael Walsh97d5b362017-05-30 17:57:38 -0500216 else:
Michael Walshb3beaa82019-03-26 16:35:30 -0500217 auto_status_file_prefix = ""
218 auto_status_file_prefix += plug_in_name + ".cp_" + call_point
219 status_dir_path =\
220 add_trailing_slash(os.environ.get("STATUS_DIR_PATH",
221 os.environ['HOME']
222 + "/status/"))
223 if not os.path.isdir(status_dir_path):
224 AUTOBOOT_EXECDIR = \
225 add_trailing_slash(os.environ.get("AUTOBOOT_EXECDIR", ""))
226 status_dir_path = AUTOBOOT_EXECDIR + "logs/"
227 if not os.path.exists(status_dir_path):
228 os.makedirs(status_dir_path)
229 status_file_name = auto_status_file_prefix + "." + file_date_time_stamp() \
230 + ".status"
231 auto_status_file_subcmd = "auto_status_file.py --status_dir_path=" \
232 + status_dir_path + " --status_file_name=" + status_file_name \
233 + " --quiet=1 --show_url=1 --prefix=" \
234 + auto_status_file_prefix + " --stdout=" + str(stdout) + " "
Michael Walsh97d5b362017-05-30 17:57:38 -0500235
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500236 cmd_buf = "PATH=" + plug_in_dir_path.rstrip("/") + ":${PATH} ; " +\
Michael Walshb3beaa82019-03-26 16:35:30 -0500237 auto_status_file_subcmd + cp_prefix + call_point
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500238 print_issuing(cmd_buf)
Michael Walsh7423c012016-10-04 10:27:21 -0500239
Michael Walshbe6153b2016-12-09 13:36:22 -0600240 sub_proc = subprocess.Popen(cmd_buf, shell=True)
241 sub_proc.communicate()
Michael Walsh7423c012016-10-04 10:27:21 -0500242 shell_rc = sub_proc.returncode
Michael Walshc33ef372017-01-10 11:46:29 -0600243 # Shift to left.
244 shell_rc *= 0x100
Michael Walshed18ec72017-06-27 10:15:31 -0500245 if shell_rc != 0 and shell_rc != allow_shell_rc:
Michael Walsh7423c012016-10-04 10:27:21 -0500246 rc = 1
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500247 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500248 if shell_rc != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500249 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walshb3beaa82019-03-26 16:35:30 -0500250 if failed_plug_in_name != "" and not stdout:
251 # Use tail to avoid double-printing of status_file_url.
252 shell_cmd("tail -n +2 " + status_dir_path + status_file_name, quiet=1,
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500253 print_output=1)
Michael Walsh7423c012016-10-04 10:27:21 -0500254
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500255 print("------------------------------------------------- Ending plug-in"
256 + " -------------------------------------------------")
Michael Walsh97d5b362017-05-30 17:57:38 -0500257 if failed_plug_in_name != "":
258 print_var(failed_plug_in_name)
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500259 print_var(shell_rc, hexa())
Michael Walsh7423c012016-10-04 10:27:21 -0500260
261 return rc, shell_rc, failed_plug_in_name
262
Michael Walsh7423c012016-10-04 10:27:21 -0500263
Michael Walsh7423c012016-10-04 10:27:21 -0500264def main():
Michael Walsh7423c012016-10-04 10:27:21 -0500265 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500266 This is the "main" function. The advantage of having this function vs just doing this in the true
267 mainline is that you can:
Michael Walsh7423c012016-10-04 10:27:21 -0500268 - Declare local variables
269 - Use "return" instead of "exit".
270 - Indent 4 chars like you would in any function.
Michael Walsh410b1782019-10-22 15:56:18 -0500271 This makes coding more consistent, i.e. it's easy to move code from here into a function and vice versa.
Michael Walsh7423c012016-10-04 10:27:21 -0500272 """
273
274 if not gen_get_options(parser, stock_list):
275 return False
276
277 if not validate_parms():
278 return False
279
280 qprint_pgm_header()
281
282 # Access program parameter globals.
283 global plug_in_dir_paths
284 global mch_class
Michael Walshed18ec72017-06-27 10:15:31 -0500285 global allow_shell_rc
Michael Walsh7423c012016-10-04 10:27:21 -0500286 global stop_on_plug_in_failure
287 global stop_on_non_zero_rc
288
289 plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths,
290 mch_class)
291
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500292 qprint_var(plug_in_packages_list)
Michael Walsh7423c012016-10-04 10:27:21 -0500293 qprint("\n")
294
Michael Walshed18ec72017-06-27 10:15:31 -0500295 allow_shell_rc = int(allow_shell_rc, 0)
Michael Walsha6723f22016-11-22 11:12:01 -0600296 shell_rc = 0
Michael Walsh7423c012016-10-04 10:27:21 -0500297 failed_plug_in_name = ""
298
Michael Walsh97d5b362017-05-30 17:57:38 -0500299 global AUTOBOOT_OPENBMC_NICKNAME
Michael Walshb3beaa82019-03-26 16:35:30 -0500300 AUTOBOOT_OPENBMC_NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "")
301
Michael Walsh7423c012016-10-04 10:27:21 -0500302 ret_code = 0
303 for plug_in_dir_path in plug_in_packages_list:
304 rc, shell_rc, failed_plug_in_name = \
Michael Walshed18ec72017-06-27 10:15:31 -0500305 run_pgm(plug_in_dir_path, call_point, allow_shell_rc)
Michael Walsh7423c012016-10-04 10:27:21 -0500306 if rc != 0:
307 ret_code = 1
308 if stop_on_plug_in_failure:
309 break
310 if shell_rc != 0 and stop_on_non_zero_rc:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500311 qprint_time("Stopping on non-zero shell return code as requested"
312 + " by caller.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500313 break
314
315 if ret_code == 0:
316 return True
317 else:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500318 print_error("At least one plug-in failed.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500319 return False
320
Michael Walsh7423c012016-10-04 10:27:21 -0500321
Michael Walsh7423c012016-10-04 10:27:21 -0500322# Main
323
324if not main():
325 exit(1)