blob: 9cc5aa504afa833c18b0a0ac9c5ea2347ad783e2 [file] [log] [blame]
mbligh6203ace2007-10-04 21:54:24 +00001#!/usr/bin/python -u
mbligh1ffd5dc2008-11-25 13:24:05 +00002# Copyright 2007-2008 Martin J. Bligh <mbligh@google.com>, Google Inc.
mbligh82648e52008-11-20 16:54:25 +00003# Released under the GPL v2
mblighdcd57a82007-07-11 23:06:47 +00004
mblighc8949b82007-07-23 16:33:58 +00005"""
mblighf1c52842007-10-16 15:21:38 +00006Run an control file through the server side engine
mblighdcd57a82007-07-11 23:06:47 +00007"""
mbligh1ffd5dc2008-11-25 13:24:05 +00008
showardb18134f2009-03-20 20:52:18 +00009import sys, os, re, traceback, signal, time, logging, logging.config
mbligh1ffd5dc2008-11-25 13:24:05 +000010
mblighf5427bb2008-04-09 15:55:57 +000011import common
jadmanskif22fea82008-11-26 20:57:07 +000012from autotest_lib.server import server_job, utils, autoserv_parser, autotest
showardb18134f2009-03-20 20:52:18 +000013from autotest_lib.client.common_lib import pidfile
mbligh92c0fc22008-11-20 16:52:23 +000014
mbligha46678d2008-05-01 20:00:01 +000015def run_autoserv(pid_file_manager, results, parser):
jadmanski0afbb632008-06-06 21:10:57 +000016 # send stdin to /dev/null
17 dev_null = os.open(os.devnull, os.O_RDONLY)
18 os.dup2(dev_null, sys.stdin.fileno())
19 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000020
jadmanski0afbb632008-06-06 21:10:57 +000021 # Create separate process group
22 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000023
jadmanski0afbb632008-06-06 21:10:57 +000024 # Implement SIGTERM handler
25 def handle_sigint(signum, frame):
mblighff7d61f2008-12-22 14:53:35 +000026 if pid_file_manager:
27 pid_file_manager.close_file(1, signal.SIGTERM)
jadmanski0afbb632008-06-06 21:10:57 +000028 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000029
jadmanski0afbb632008-06-06 21:10:57 +000030 # Set signal handler
31 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000032
jadmanski0afbb632008-06-06 21:10:57 +000033 # Get a useful value for running 'USER'
34 realuser = os.environ.get('USER')
35 if not realuser:
36 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000037
mblighcce191f2008-09-19 20:31:03 +000038 if parser.options.machines:
39 machines = parser.options.machines.replace(',', ' ').strip().split()
40 else:
41 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000042 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000043 label = parser.options.label
mbligh374f3412009-05-13 21:29:45 +000044 group_name = parser.options.group_name
mblighb2bea302008-07-24 20:25:57 +000045 user = parser.options.user
46 client = parser.options.client
47 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000048 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000049 install_after = parser.options.install_after
50 verify = parser.options.verify
51 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000052 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000053 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000054 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000055 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000056 ssh_user = parser.options.ssh_user
57 ssh_port = parser.options.ssh_port
58 ssh_pass = parser.options.ssh_pass
jadmanskidef0c3c2009-03-25 20:07:10 +000059 collect_crashinfo = parser.options.collect_crashinfo
mbligha46678d2008-05-01 20:00:01 +000060
mblighb2bea302008-07-24 20:25:57 +000061 # can't be both a client and a server side test
62 if client and server:
63 print "Can not specify a test as both server and client!"
64 sys.exit(1)
65
jadmanskidef0c3c2009-03-25 20:07:10 +000066 if len(parser.args) < 1 and not (verify or repair or cleanup
67 or collect_crashinfo):
jadmanski0afbb632008-06-06 21:10:57 +000068 print parser.parser.print_help()
69 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +000070
showard45ae8192008-11-05 19:32:53 +000071 # We have a control file unless it's just a verify/repair/cleanup job
jadmanski0afbb632008-06-06 21:10:57 +000072 if len(parser.args) > 0:
73 control = parser.args[0]
74 else:
75 control = None
mbligha46678d2008-05-01 20:00:01 +000076
jadmanski0afbb632008-06-06 21:10:57 +000077 if machines_file:
78 machines = []
79 for m in open(machines_file, 'r').readlines():
80 # remove comments, spaces
81 m = re.sub('#.*', '', m).strip()
82 if m:
83 machines.append(m)
84 print "Read list of machines from file: %s" % machines_file
85 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +000086
jadmanski0afbb632008-06-06 21:10:57 +000087 if machines:
88 for machine in machines:
89 if not machine or re.search('\s', machine):
90 print "Invalid machine %s" % str(machine)
91 sys.exit(1)
92 machines = list(set(machines))
93 machines.sort()
mbligha46678d2008-05-01 20:00:01 +000094
mbligh374f3412009-05-13 21:29:45 +000095 if group_name and len(machines) < 2:
96 print ("-G %r may only be supplied with more than one machine."
97 % group_name)
98 sys.exit(1)
99
jadmanski0afbb632008-06-06 21:10:57 +0000100 job = server_job.server_job(control, parser.args[1:], results, label,
101 user, machines, client, parse_job,
mbligh374f3412009-05-13 21:29:45 +0000102 ssh_user, ssh_port, ssh_pass,
103 group_name=group_name)
mbligh80e1eba2008-11-19 00:26:18 +0000104 if results:
105 debug_dir = os.path.join(results, 'debug')
106 stdout = os.path.join(debug_dir, 'autoserv.stdout')
107 stderr = os.path.join(debug_dir, 'autoserv.stderr')
108 if no_tee:
109 job.stdout.redirect(stdout)
110 job.stderr.redirect(stderr)
111 else:
112 job.stdout.tee_redirect(stdout)
113 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000114
mbligh161fe6f2008-06-19 16:26:04 +0000115 # perform checks
116 job.precheck()
117
jadmanski0afbb632008-06-06 21:10:57 +0000118 # run the job
119 exit_code = 0
120 try:
mbligh332000a2009-06-08 16:47:28 +0000121 try:
122 if repair:
123 job.repair(host_protection)
124 elif verify:
125 job.verify()
126 else:
jadmanskidef0c3c2009-03-25 20:07:10 +0000127 job.run(cleanup, install_before, install_after,
128 only_collect_crashinfo=collect_crashinfo)
mbligh332000a2009-06-08 16:47:28 +0000129 finally:
130 while job.hosts:
131 host = job.hosts.pop()
132 host.close()
jadmanski0afbb632008-06-06 21:10:57 +0000133 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000134 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000135 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000136
mblighff7d61f2008-12-22 14:53:35 +0000137 if pid_file_manager:
138 pid_file_manager.num_tests_failed = job.num_tests_failed
139 pid_file_manager.close_file(exit_code)
jadmanskie0dffc32008-12-15 17:30:30 +0000140 job.cleanup_parser()
showard21baa452008-10-21 00:08:39 +0000141
jadmanski27b37ea2008-10-29 23:54:31 +0000142 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000143
144
145def main():
jadmanski0afbb632008-06-06 21:10:57 +0000146 # grab the parser
147 parser = autoserv_parser.autoserv_parser
mbligha5cb4062009-02-17 15:53:39 +0000148 parser.parse_args()
mbligha46678d2008-05-01 20:00:01 +0000149
jadmanski0afbb632008-06-06 21:10:57 +0000150 if len(sys.argv) == 1:
151 parser.parser.print_help()
152 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000153
mbligh80e1eba2008-11-19 00:26:18 +0000154 results = parser.options.results
155 if not parser.options.no_logging:
156 if not results:
157 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
158 results = os.path.abspath(results)
jadmanskidef0c3c2009-03-25 20:07:10 +0000159 resultdir_exists = os.path.exists(os.path.join(results, 'control.srv'))
160 if not parser.options.collect_crashinfo and resultdir_exists:
mbligh80e1eba2008-11-19 00:26:18 +0000161 error = "Error: results directory already exists: %s\n" % results
162 sys.stderr.write(error)
163 sys.exit(1)
mbligha788dc42009-03-26 21:10:16 +0000164
165 # Now that we certified that there's no leftover results dir from
166 # previous jobs, lets create the result dir since the logging system
167 # needs to create the log file in there.
168 if not os.path.isdir(results):
169 os.makedirs(results)
170 os.environ['AUTOSERV_RESULTS'] = results
171 serverdir = os.path.dirname(__file__)
172 logging.config.fileConfig('%s/debug_server.ini' % serverdir)
173 logging.info("Results placed in %s" % results)
174 else:
175 # If we supply -N, no results dir will be generated, so
176 # we'll configure the logging system on code.
177 stamp = '[%(asctime)s - %(levelname)-8s] %(message)s'
178 root_logger = logging.getLogger()
179 formatter = logging.Formatter(stamp, datefmt='%H:%M:%S')
mbligh9554eb42009-04-08 21:13:44 +0000180 # Let's verify if we already have handlers for the root logger
181 # at this point.
182 if len(root_logger.handlers) == 0:
183 autoserv_handler = logging.StreamHandler(sys.stdout,)
184 autoserv_handler.setFormatter(formatter)
185 root_logger.addHandler(autoserv_handler)
186 else:
187 # If we already have any handlers up at this point, let's
188 # just configure this one we already have.
189 root_logger.handlers[0].setFormatter(formatter)
190
191 # When the -N flag is being used, we are assuming DEBUG level for the
192 # execution. We could read the level from the configuration file,
193 # but I am not sure if this is the right way to go, since we are doing
194 # all the configuration on code (lmr).
mbligha788dc42009-03-26 21:10:16 +0000195 root_logger.setLevel(logging.DEBUG)
196
mbligh10717632008-11-19 00:21:57 +0000197
mbligh80e1eba2008-11-19 00:26:18 +0000198 if parser.options.write_pidfile:
showardd3dc1992009-04-22 21:01:40 +0000199 if parser.options.collect_crashinfo:
200 pidfile_label = 'collect_crashinfo'
201 else:
202 pidfile_label = 'autoserv'
203 pid_file_manager = pidfile.PidFileManager(pidfile_label, results)
jadmanskid5ab8c52008-12-03 16:27:07 +0000204 pid_file_manager.open_file()
mblighff7d61f2008-12-22 14:53:35 +0000205 else:
206 pid_file_manager = None
mbligha46678d2008-05-01 20:00:01 +0000207
jadmanskif22fea82008-11-26 20:57:07 +0000208 autotest.BaseAutotest.set_install_in_tmpdir(
209 parser.options.install_in_tmpdir)
210
jadmanski0afbb632008-06-06 21:10:57 +0000211 exit_code = 0
212 try:
213 try:
mbligh10717632008-11-19 00:21:57 +0000214 run_autoserv(pid_file_manager, results, parser)
jadmanski0afbb632008-06-06 21:10:57 +0000215 except SystemExit, e:
216 exit_code = e.code
217 except:
218 traceback.print_exc()
219 # If we don't know what happened, we'll classify it as
220 # an 'abort' and return 1.
221 exit_code = 1
222 finally:
mblighff7d61f2008-12-22 14:53:35 +0000223 if pid_file_manager:
224 pid_file_manager.close_file(exit_code)
jadmanski0afbb632008-06-06 21:10:57 +0000225 sys.exit(exit_code)
mblighfaf0cd42007-11-19 16:00:24 +0000226
mblighbb421852008-03-11 22:36:16 +0000227
mbligha46678d2008-05-01 20:00:01 +0000228if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000229 main()