blob: aef075442ec67e7533718424a5b5ab37db969923 [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
mbligh7cd30fd2008-10-21 16:25:19 +00009import sys, os, re, traceback, signal, time
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
jadmanskid5ab8c52008-12-03 16:27:07 +000013from autotest_lib.client.common_lib import debug, pidfile
mbligh0b8c32d2008-10-29 16:46:04 +000014
mbligh1ffd5dc2008-11-25 13:24:05 +000015
mbligh0b8c32d2008-10-29 16:46:04 +000016debug.configure(module='server')
mbligh29aa9702007-08-09 22:41:43 +000017
mbligh92c0fc22008-11-20 16:52:23 +000018
mbligha46678d2008-05-01 20:00:01 +000019def run_autoserv(pid_file_manager, results, parser):
jadmanski0afbb632008-06-06 21:10:57 +000020 # send stdin to /dev/null
21 dev_null = os.open(os.devnull, os.O_RDONLY)
22 os.dup2(dev_null, sys.stdin.fileno())
23 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000024
jadmanski0afbb632008-06-06 21:10:57 +000025 # Create separate process group
26 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000027
jadmanski0afbb632008-06-06 21:10:57 +000028 # Implement SIGTERM handler
29 def handle_sigint(signum, frame):
mblighff7d61f2008-12-22 14:53:35 +000030 if pid_file_manager:
31 pid_file_manager.close_file(1, signal.SIGTERM)
jadmanski0afbb632008-06-06 21:10:57 +000032 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000033
jadmanski0afbb632008-06-06 21:10:57 +000034 # Set signal handler
35 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000036
jadmanski0afbb632008-06-06 21:10:57 +000037 # Get a useful value for running 'USER'
38 realuser = os.environ.get('USER')
39 if not realuser:
40 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000041
mblighcce191f2008-09-19 20:31:03 +000042 if parser.options.machines:
43 machines = parser.options.machines.replace(',', ' ').strip().split()
44 else:
45 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000046 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000047 label = parser.options.label
48 user = parser.options.user
49 client = parser.options.client
50 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000051 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000052 install_after = parser.options.install_after
53 verify = parser.options.verify
54 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000055 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000056 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000057 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000058 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000059 ssh_user = parser.options.ssh_user
60 ssh_port = parser.options.ssh_port
61 ssh_pass = parser.options.ssh_pass
mbligha46678d2008-05-01 20:00:01 +000062
mblighb2bea302008-07-24 20:25:57 +000063 # can't be both a client and a server side test
64 if client and server:
65 print "Can not specify a test as both server and client!"
66 sys.exit(1)
67
showard45ae8192008-11-05 19:32:53 +000068 if len(parser.args) < 1 and not (verify or repair or cleanup):
jadmanski0afbb632008-06-06 21:10:57 +000069 print parser.parser.print_help()
70 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +000071
showard45ae8192008-11-05 19:32:53 +000072 # We have a control file unless it's just a verify/repair/cleanup job
jadmanski0afbb632008-06-06 21:10:57 +000073 if len(parser.args) > 0:
74 control = parser.args[0]
75 else:
76 control = None
mbligha46678d2008-05-01 20:00:01 +000077
jadmanski0afbb632008-06-06 21:10:57 +000078 if machines_file:
79 machines = []
80 for m in open(machines_file, 'r').readlines():
81 # remove comments, spaces
82 m = re.sub('#.*', '', m).strip()
83 if m:
84 machines.append(m)
85 print "Read list of machines from file: %s" % machines_file
86 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +000087
jadmanski0afbb632008-06-06 21:10:57 +000088 if machines:
89 for machine in machines:
90 if not machine or re.search('\s', machine):
91 print "Invalid machine %s" % str(machine)
92 sys.exit(1)
93 machines = list(set(machines))
94 machines.sort()
mbligha46678d2008-05-01 20:00:01 +000095
jadmanski0afbb632008-06-06 21:10:57 +000096 job = server_job.server_job(control, parser.args[1:], results, label,
97 user, machines, client, parse_job,
98 ssh_user, ssh_port, ssh_pass)
mbligh80e1eba2008-11-19 00:26:18 +000099 if results:
100 debug_dir = os.path.join(results, 'debug')
101 stdout = os.path.join(debug_dir, 'autoserv.stdout')
102 stderr = os.path.join(debug_dir, 'autoserv.stderr')
103 if no_tee:
104 job.stdout.redirect(stdout)
105 job.stderr.redirect(stderr)
106 else:
107 job.stdout.tee_redirect(stdout)
108 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000109
mbligh161fe6f2008-06-19 16:26:04 +0000110 # perform checks
111 job.precheck()
112
jadmanski0afbb632008-06-06 21:10:57 +0000113 # run the job
114 exit_code = 0
115 try:
116 if repair:
jadmanskifbc1f0a2008-07-09 14:12:54 +0000117 job.repair(host_protection)
jadmanski0afbb632008-06-06 21:10:57 +0000118 elif verify:
119 job.verify()
120 else:
121 try:
showard45ae8192008-11-05 19:32:53 +0000122 job.run(cleanup, install_before, install_after)
jadmanski0afbb632008-06-06 21:10:57 +0000123 finally:
jadmanski53aaf382008-11-17 16:22:31 +0000124 while job.hosts:
125 host = job.hosts.pop()
126 host.close()
jadmanski0afbb632008-06-06 21:10:57 +0000127 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000128 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000129 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000130
mblighff7d61f2008-12-22 14:53:35 +0000131 if pid_file_manager:
132 pid_file_manager.num_tests_failed = job.num_tests_failed
133 pid_file_manager.close_file(exit_code)
jadmanskie0dffc32008-12-15 17:30:30 +0000134 job.cleanup_parser()
showard21baa452008-10-21 00:08:39 +0000135
jadmanski27b37ea2008-10-29 23:54:31 +0000136 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000137
138
139def main():
jadmanski0afbb632008-06-06 21:10:57 +0000140 # grab the parser
141 parser = autoserv_parser.autoserv_parser
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)
152 if os.path.exists(os.path.join(results, 'control.srv')):
153 error = "Error: results directory already exists: %s\n" % results
154 sys.stderr.write(error)
155 sys.exit(1)
156 print "Results placed in %s" % results
mbligh10717632008-11-19 00:21:57 +0000157
mbligh80e1eba2008-11-19 00:26:18 +0000158 if parser.options.write_pidfile:
mblighff7d61f2008-12-22 14:53:35 +0000159 pid_file_manager = pidfile.PidFileManager("autoserv", results)
jadmanskid5ab8c52008-12-03 16:27:07 +0000160 pid_file_manager.open_file()
mblighff7d61f2008-12-22 14:53:35 +0000161 else:
162 pid_file_manager = None
mbligha46678d2008-05-01 20:00:01 +0000163
jadmanskif22fea82008-11-26 20:57:07 +0000164 autotest.BaseAutotest.set_install_in_tmpdir(
165 parser.options.install_in_tmpdir)
166
jadmanski0afbb632008-06-06 21:10:57 +0000167 exit_code = 0
168 try:
169 try:
mbligh10717632008-11-19 00:21:57 +0000170 run_autoserv(pid_file_manager, results, parser)
jadmanski0afbb632008-06-06 21:10:57 +0000171 except SystemExit, e:
172 exit_code = e.code
173 except:
174 traceback.print_exc()
175 # If we don't know what happened, we'll classify it as
176 # an 'abort' and return 1.
177 exit_code = 1
178 finally:
mblighff7d61f2008-12-22 14:53:35 +0000179 if pid_file_manager:
180 pid_file_manager.close_file(exit_code)
jadmanski0afbb632008-06-06 21:10:57 +0000181 sys.exit(exit_code)
mblighfaf0cd42007-11-19 16:00:24 +0000182
mblighbb421852008-03-11 22:36:16 +0000183
mbligha46678d2008-05-01 20:00:01 +0000184if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000185 main()