blob: d0e22f3b0d3cf9aac6d9e8c8b0952338b004b91d [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
Michael Walsh60d08f32019-11-25 16:51:39 -06003r"""
4See help text for details.
5"""
6
Michael Walsh7423c012016-10-04 10:27:21 -05007import sys
Michael Walsh7423c012016-10-04 10:27:21 -05008import subprocess
9import os
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
Michael Walsh60d08f32019-11-25 16:51:39 -060013modules = ['gen_arg', 'gen_print', 'gen_valid', 'gen_plug_in', 'gen_cmd', 'gen_misc']
14for module in modules:
15 exec("from " + module + " import *")
Michael Walsh7423c012016-10-04 10:27:21 -050016
Michael Walsh60d08f32019-11-25 16:51:39 -060017sys.path.insert(0, save_dir_path)
Michael Walsh7423c012016-10-04 10:27:21 -050018
19# Create parser object.
20parser = argparse.ArgumentParser(
21 usage='%(prog)s [OPTIONS]',
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050022 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 Walshd0741f82017-12-21 14:04:21 -060044 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walshc33ef372017-01-10 11:46:29 -060045 prefix_chars='-+')
Michael Walsh7423c012016-10-04 10:27:21 -050046
47# Create arguments.
48parser.add_argument(
49 'plug_in_dir_paths',
50 nargs='?',
51 default="",
Michael Walshc33ef372017-01-10 11:46:29 -060052 help=plug_in_dir_paths_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050053
54parser.add_argument(
55 '--call_point',
56 default="setup",
57 required=True,
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050058 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 Walsh7423c012016-10-04 10:27:21 -050062
63parser.add_argument(
Michael Walshed18ec72017-06-27 10:15:31 -050064 '--allow_shell_rc',
Michael Walsh7423c012016-10-04 10:27:21 -050065 default="0x00000000",
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050066 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 Walsh7423c012016-10-04 10:27:21 -050071
72parser.add_argument(
73 '--stop_on_plug_in_failure',
74 default=1,
75 type=int,
76 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050077 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 Walsh7423c012016-10-04 10:27:21 -050084
85parser.add_argument(
86 '--stop_on_non_zero_rc',
87 default=0,
88 type=int,
89 choices=[1, 0],
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050090 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 Walsh7423c012016-10-04 10:27:21 -0500103
104parser.add_argument(
105 '--mch_class',
106 default="obmc",
Michael Walshc33ef372017-01-10 11:46:29 -0600107 help=mch_class_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -0500108
Michael Walsh60d08f32019-11-25 16:51:39 -0600109# Populate stock_list with options we want.
Michael Walsh7423c012016-10-04 10:27:21 -0500110stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
Michael Walsh7423c012016-10-04 10:27:21 -0500111
112
Michael Walsh7423c012016-10-04 10:27:21 -0500113def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -0500114 r"""
115 Validate program parameters, etc. Return True or False accordingly.
116 """
117
Michael Walsh60d08f32019-11-25 16:51:39 -0600118 valid_value(call_point)
Michael Walsh7423c012016-10-04 10:27:21 -0500119
Michael Walshed18ec72017-06-27 10:15:31 -0500120 global allow_shell_rc
Michael Walsh60d08f32019-11-25 16:51:39 -0600121 valid_integer(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600122
123 # Convert to hex string for consistency in printout.
Michael Walshed18ec72017-06-27 10:15:31 -0500124 allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0)
125 set_pgm_arg(allow_shell_rc)
Michael Walshc33ef372017-01-10 11:46:29 -0600126
Michael Walsh7423c012016-10-04 10:27:21 -0500127
Michael Walsh7423c012016-10-04 10:27:21 -0500128def run_pgm(plug_in_dir_path,
129 call_point,
Michael Walshed18ec72017-06-27 10:15:31 -0500130 allow_shell_rc):
Michael Walsh7423c012016-10-04 10:27:21 -0500131 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500132 Run the call point program in the given plug_in_dir_path. Return the following:
Michael Walsh7423c012016-10-04 10:27:21 -0500133 rc The return code - 0 = PASS, 1 = FAIL.
Michael Walsh410b1782019-10-22 15:56:18 -0500134 shell_rc The shell return code returned by process_plug_in_packages.py.
Michael Walsh7423c012016-10-04 10:27:21 -0500135 failed_plug_in_name The failed plug in name (if any).
136
137 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500138 plug_in_dir_path The directory path where the call_point program may be located.
139 call_point The call point (e.g. "setup"). This program will look for a program
140 named "cp_" + call_point in the plug_in_dir_path. If no such call point
141 program is found, this function returns an rc of 0 (i.e. success).
142 allow_shell_rc The user may supply a value other than zero to indicate an acceptable
143 non-zero return code. For example, if this value equals 0x00000200, it
144 means that for each plug-in call point that runs, a 0x00000200 will not
145 be counted as a failure. See note above regarding left-shifting of
Michael Walsh7423c012016-10-04 10:27:21 -0500146 return codes.
147 """
148
149 rc = 0
150 failed_plug_in_name = ""
151 shell_rc = 0x00000000
152
Michael Walsh97d5b362017-05-30 17:57:38 -0500153 plug_in_name = os.path.basename(os.path.normpath(plug_in_dir_path))
Michael Walsh7423c012016-10-04 10:27:21 -0500154 cp_prefix = "cp_"
155 plug_in_pgm_path = plug_in_dir_path + cp_prefix + call_point
156 if not os.path.exists(plug_in_pgm_path):
Michael Walsh410b1782019-10-22 15:56:18 -0500157 # 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 -0500158 return rc, shell_rc, failed_plug_in_name
159
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500160 print("------------------------------------------------- Starting plug-"
161 + "in -----------------------------------------------")
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500162
163 print_timen("Running " + plug_in_name + "/" + cp_prefix + call_point + ".")
Michael Walshb3beaa82019-03-26 16:35:30 -0500164
165 stdout = 1 - quiet
166 if AUTOBOOT_OPENBMC_NICKNAME != "":
167 auto_status_file_prefix = AUTOBOOT_OPENBMC_NICKNAME + "."
Michael Walsh97d5b362017-05-30 17:57:38 -0500168 else:
Michael Walshb3beaa82019-03-26 16:35:30 -0500169 auto_status_file_prefix = ""
170 auto_status_file_prefix += plug_in_name + ".cp_" + call_point
171 status_dir_path =\
172 add_trailing_slash(os.environ.get("STATUS_DIR_PATH",
173 os.environ['HOME']
174 + "/status/"))
175 if not os.path.isdir(status_dir_path):
176 AUTOBOOT_EXECDIR = \
177 add_trailing_slash(os.environ.get("AUTOBOOT_EXECDIR", ""))
178 status_dir_path = AUTOBOOT_EXECDIR + "logs/"
179 if not os.path.exists(status_dir_path):
180 os.makedirs(status_dir_path)
181 status_file_name = auto_status_file_prefix + "." + file_date_time_stamp() \
182 + ".status"
183 auto_status_file_subcmd = "auto_status_file.py --status_dir_path=" \
184 + status_dir_path + " --status_file_name=" + status_file_name \
185 + " --quiet=1 --show_url=1 --prefix=" \
186 + auto_status_file_prefix + " --stdout=" + str(stdout) + " "
Michael Walsh97d5b362017-05-30 17:57:38 -0500187
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500188 cmd_buf = "PATH=" + plug_in_dir_path.rstrip("/") + ":${PATH} ; " +\
Michael Walshb3beaa82019-03-26 16:35:30 -0500189 auto_status_file_subcmd + cp_prefix + call_point
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500190 print_issuing(cmd_buf)
Michael Walsh7423c012016-10-04 10:27:21 -0500191
Michael Walshbe6153b2016-12-09 13:36:22 -0600192 sub_proc = subprocess.Popen(cmd_buf, shell=True)
193 sub_proc.communicate()
Michael Walsh7423c012016-10-04 10:27:21 -0500194 shell_rc = sub_proc.returncode
Michael Walshc33ef372017-01-10 11:46:29 -0600195 # Shift to left.
196 shell_rc *= 0x100
Michael Walshed18ec72017-06-27 10:15:31 -0500197 if shell_rc != 0 and shell_rc != allow_shell_rc:
Michael Walsh7423c012016-10-04 10:27:21 -0500198 rc = 1
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500199 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500200 if shell_rc != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500201 failed_plug_in_name = plug_in_name + "/" + cp_prefix + call_point
Michael Walshb3beaa82019-03-26 16:35:30 -0500202 if failed_plug_in_name != "" and not stdout:
203 # Use tail to avoid double-printing of status_file_url.
204 shell_cmd("tail -n +2 " + status_dir_path + status_file_name, quiet=1,
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500205 print_output=1)
Michael Walsh7423c012016-10-04 10:27:21 -0500206
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500207 print("------------------------------------------------- Ending plug-in"
208 + " -------------------------------------------------")
Michael Walsh97d5b362017-05-30 17:57:38 -0500209 if failed_plug_in_name != "":
210 print_var(failed_plug_in_name)
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500211 print_var(shell_rc, hexa())
Michael Walsh7423c012016-10-04 10:27:21 -0500212
213 return rc, shell_rc, failed_plug_in_name
214
Michael Walsh7423c012016-10-04 10:27:21 -0500215
Michael Walsh7423c012016-10-04 10:27:21 -0500216def main():
Michael Walsh7423c012016-10-04 10:27:21 -0500217
Michael Walsh60d08f32019-11-25 16:51:39 -0600218 gen_setup()
Michael Walsh7423c012016-10-04 10:27:21 -0500219
220 # Access program parameter globals.
221 global plug_in_dir_paths
222 global mch_class
Michael Walshed18ec72017-06-27 10:15:31 -0500223 global allow_shell_rc
Michael Walsh7423c012016-10-04 10:27:21 -0500224 global stop_on_plug_in_failure
225 global stop_on_non_zero_rc
226
227 plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths,
228 mch_class)
229
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500230 qprint_var(plug_in_packages_list)
Michael Walsh7423c012016-10-04 10:27:21 -0500231 qprint("\n")
232
Michael Walshed18ec72017-06-27 10:15:31 -0500233 allow_shell_rc = int(allow_shell_rc, 0)
Michael Walsha6723f22016-11-22 11:12:01 -0600234 shell_rc = 0
Michael Walsh7423c012016-10-04 10:27:21 -0500235 failed_plug_in_name = ""
236
Michael Walsh97d5b362017-05-30 17:57:38 -0500237 global AUTOBOOT_OPENBMC_NICKNAME
Michael Walshb3beaa82019-03-26 16:35:30 -0500238 AUTOBOOT_OPENBMC_NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "")
239
Michael Walsh7423c012016-10-04 10:27:21 -0500240 ret_code = 0
241 for plug_in_dir_path in plug_in_packages_list:
242 rc, shell_rc, failed_plug_in_name = \
Michael Walshed18ec72017-06-27 10:15:31 -0500243 run_pgm(plug_in_dir_path, call_point, allow_shell_rc)
Michael Walsh7423c012016-10-04 10:27:21 -0500244 if rc != 0:
245 ret_code = 1
246 if stop_on_plug_in_failure:
247 break
248 if shell_rc != 0 and stop_on_non_zero_rc:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500249 qprint_time("Stopping on non-zero shell return code as requested"
250 + " by caller.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500251 break
252
Michael Walsh60d08f32019-11-25 16:51:39 -0600253 if ret_code != 0:
Michael Walsh8c5a8a82018-10-30 13:17:23 -0500254 print_error("At least one plug-in failed.\n")
Michael Walsh60d08f32019-11-25 16:51:39 -0600255 exit(1)
Michael Walsh7423c012016-10-04 10:27:21 -0500256
Michael Walsh7423c012016-10-04 10:27:21 -0500257
Michael Walsh60d08f32019-11-25 16:51:39 -0600258main()