blob: b8c69e55408b5565eaa64d4b2e2898c7e0762e5c [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
44 user = parser.options.user
45 client = parser.options.client
46 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000047 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000048 install_after = parser.options.install_after
49 verify = parser.options.verify
50 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000051 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000052 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000053 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000054 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000055 ssh_user = parser.options.ssh_user
56 ssh_port = parser.options.ssh_port
57 ssh_pass = parser.options.ssh_pass
jadmanskidef0c3c2009-03-25 20:07:10 +000058 collect_crashinfo = parser.options.collect_crashinfo
mbligha46678d2008-05-01 20:00:01 +000059
mblighb2bea302008-07-24 20:25:57 +000060 # can't be both a client and a server side test
61 if client and server:
62 print "Can not specify a test as both server and client!"
63 sys.exit(1)
64
jadmanskidef0c3c2009-03-25 20:07:10 +000065 if len(parser.args) < 1 and not (verify or repair or cleanup
66 or collect_crashinfo):
jadmanski0afbb632008-06-06 21:10:57 +000067 print parser.parser.print_help()
68 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +000069
showard45ae8192008-11-05 19:32:53 +000070 # We have a control file unless it's just a verify/repair/cleanup job
jadmanski0afbb632008-06-06 21:10:57 +000071 if len(parser.args) > 0:
72 control = parser.args[0]
73 else:
74 control = None
mbligha46678d2008-05-01 20:00:01 +000075
jadmanski0afbb632008-06-06 21:10:57 +000076 if machines_file:
77 machines = []
78 for m in open(machines_file, 'r').readlines():
79 # remove comments, spaces
80 m = re.sub('#.*', '', m).strip()
81 if m:
82 machines.append(m)
83 print "Read list of machines from file: %s" % machines_file
84 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +000085
jadmanski0afbb632008-06-06 21:10:57 +000086 if machines:
87 for machine in machines:
88 if not machine or re.search('\s', machine):
89 print "Invalid machine %s" % str(machine)
90 sys.exit(1)
91 machines = list(set(machines))
92 machines.sort()
mbligha46678d2008-05-01 20:00:01 +000093
jadmanski0afbb632008-06-06 21:10:57 +000094 job = server_job.server_job(control, parser.args[1:], results, label,
95 user, machines, client, parse_job,
96 ssh_user, ssh_port, ssh_pass)
mbligh80e1eba2008-11-19 00:26:18 +000097 if results:
98 debug_dir = os.path.join(results, 'debug')
99 stdout = os.path.join(debug_dir, 'autoserv.stdout')
100 stderr = os.path.join(debug_dir, 'autoserv.stderr')
101 if no_tee:
102 job.stdout.redirect(stdout)
103 job.stderr.redirect(stderr)
104 else:
105 job.stdout.tee_redirect(stdout)
106 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000107
mbligh161fe6f2008-06-19 16:26:04 +0000108 # perform checks
109 job.precheck()
110
jadmanski0afbb632008-06-06 21:10:57 +0000111 # run the job
112 exit_code = 0
113 try:
114 if repair:
jadmanskifbc1f0a2008-07-09 14:12:54 +0000115 job.repair(host_protection)
jadmanski0afbb632008-06-06 21:10:57 +0000116 elif verify:
117 job.verify()
118 else:
119 try:
jadmanskidef0c3c2009-03-25 20:07:10 +0000120 job.run(cleanup, install_before, install_after,
121 only_collect_crashinfo=collect_crashinfo)
jadmanski0afbb632008-06-06 21:10:57 +0000122 finally:
jadmanski53aaf382008-11-17 16:22:31 +0000123 while job.hosts:
124 host = job.hosts.pop()
125 host.close()
jadmanski0afbb632008-06-06 21:10:57 +0000126 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000127 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000128 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000129
mblighff7d61f2008-12-22 14:53:35 +0000130 if pid_file_manager:
131 pid_file_manager.num_tests_failed = job.num_tests_failed
132 pid_file_manager.close_file(exit_code)
jadmanskie0dffc32008-12-15 17:30:30 +0000133 job.cleanup_parser()
showard21baa452008-10-21 00:08:39 +0000134
jadmanski27b37ea2008-10-29 23:54:31 +0000135 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000136
137
138def main():
jadmanski0afbb632008-06-06 21:10:57 +0000139 # grab the parser
140 parser = autoserv_parser.autoserv_parser
mbligha5cb4062009-02-17 15:53:39 +0000141 parser.parse_args()
mbligha46678d2008-05-01 20:00:01 +0000142
jadmanski0afbb632008-06-06 21:10:57 +0000143 if len(sys.argv) == 1:
144 parser.parser.print_help()
145 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000146
mbligh80e1eba2008-11-19 00:26:18 +0000147 results = parser.options.results
148 if not parser.options.no_logging:
149 if not results:
150 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
151 results = os.path.abspath(results)
jadmanskidef0c3c2009-03-25 20:07:10 +0000152 resultdir_exists = os.path.exists(os.path.join(results, 'control.srv'))
153 if not parser.options.collect_crashinfo and resultdir_exists:
mbligh80e1eba2008-11-19 00:26:18 +0000154 error = "Error: results directory already exists: %s\n" % results
155 sys.stderr.write(error)
156 sys.exit(1)
mbligha788dc42009-03-26 21:10:16 +0000157
158 # Now that we certified that there's no leftover results dir from
159 # previous jobs, lets create the result dir since the logging system
160 # needs to create the log file in there.
161 if not os.path.isdir(results):
162 os.makedirs(results)
163 os.environ['AUTOSERV_RESULTS'] = results
164 serverdir = os.path.dirname(__file__)
165 logging.config.fileConfig('%s/debug_server.ini' % serverdir)
166 logging.info("Results placed in %s" % results)
167 else:
168 # If we supply -N, no results dir will be generated, so
169 # we'll configure the logging system on code.
170 stamp = '[%(asctime)s - %(levelname)-8s] %(message)s'
171 root_logger = logging.getLogger()
172 formatter = logging.Formatter(stamp, datefmt='%H:%M:%S')
mbligh9554eb42009-04-08 21:13:44 +0000173 # Let's verify if we already have handlers for the root logger
174 # at this point.
175 if len(root_logger.handlers) == 0:
176 autoserv_handler = logging.StreamHandler(sys.stdout,)
177 autoserv_handler.setFormatter(formatter)
178 root_logger.addHandler(autoserv_handler)
179 else:
180 # If we already have any handlers up at this point, let's
181 # just configure this one we already have.
182 root_logger.handlers[0].setFormatter(formatter)
183
184 # When the -N flag is being used, we are assuming DEBUG level for the
185 # execution. We could read the level from the configuration file,
186 # but I am not sure if this is the right way to go, since we are doing
187 # all the configuration on code (lmr).
mbligha788dc42009-03-26 21:10:16 +0000188 root_logger.setLevel(logging.DEBUG)
189
mbligh10717632008-11-19 00:21:57 +0000190
mbligh80e1eba2008-11-19 00:26:18 +0000191 if parser.options.write_pidfile:
showardd3dc1992009-04-22 21:01:40 +0000192 if parser.options.collect_crashinfo:
193 pidfile_label = 'collect_crashinfo'
194 else:
195 pidfile_label = 'autoserv'
196 pid_file_manager = pidfile.PidFileManager(pidfile_label, results)
jadmanskid5ab8c52008-12-03 16:27:07 +0000197 pid_file_manager.open_file()
mblighff7d61f2008-12-22 14:53:35 +0000198 else:
199 pid_file_manager = None
mbligha46678d2008-05-01 20:00:01 +0000200
jadmanskif22fea82008-11-26 20:57:07 +0000201 autotest.BaseAutotest.set_install_in_tmpdir(
202 parser.options.install_in_tmpdir)
203
jadmanski0afbb632008-06-06 21:10:57 +0000204 exit_code = 0
205 try:
206 try:
mbligh10717632008-11-19 00:21:57 +0000207 run_autoserv(pid_file_manager, results, parser)
jadmanski0afbb632008-06-06 21:10:57 +0000208 except SystemExit, e:
209 exit_code = e.code
210 except:
211 traceback.print_exc()
212 # If we don't know what happened, we'll classify it as
213 # an 'abort' and return 1.
214 exit_code = 1
215 finally:
mblighff7d61f2008-12-22 14:53:35 +0000216 if pid_file_manager:
217 pid_file_manager.close_file(exit_code)
jadmanski0afbb632008-06-06 21:10:57 +0000218 sys.exit(exit_code)
mblighfaf0cd42007-11-19 16:00:24 +0000219
mblighbb421852008-03-11 22:36:16 +0000220
mbligha46678d2008-05-01 20:00:01 +0000221if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000222 main()