blob: b6ba572d01ce90e64d767af266aa316f64ed8312 [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
mbligh0b8c32d2008-10-29 16:46:04 +000013from autotest_lib.client.common_lib import debug
14
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 +000019class PidFileManager(object):
jadmanski0afbb632008-06-06 21:10:57 +000020 pid_file = None
showard21baa452008-10-21 00:08:39 +000021 num_tests_failed = 0
mbligh74fc0462007-11-05 20:24:17 +000022
jadmanski0afbb632008-06-06 21:10:57 +000023 def open_pid_file(self, results_dir):
24 pid_file_path = os.path.join(results_dir, '.autoserv_execute')
25 assert not os.path.exists(pid_file_path)
26 self.pid_file = open(pid_file_path, 'w')
27 self.pid_file.write(str(os.getpid()) + '\n')
28 self.pid_file.flush()
mblighc99add62007-09-27 17:02:58 +000029
mblighc99add62007-09-27 17:02:58 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 def close_pid_file(self, exit_code, signal_code=0):
32 if not self.pid_file:
33 return
34 real_exit_code = (exit_code << 8) | (signal_code & 0xFF)
35 self.pid_file.write(str(real_exit_code) + '\n')
showard21baa452008-10-21 00:08:39 +000036 self.pid_file.write(str(self.num_tests_failed) + '\n')
jadmanski0afbb632008-06-06 21:10:57 +000037 self.pid_file.close()
38 self.pid_file = None
mblighc99add62007-09-27 17:02:58 +000039
mbligh842c6592007-11-05 18:27:23 +000040
mbligha46678d2008-05-01 20:00:01 +000041def run_autoserv(pid_file_manager, results, parser):
jadmanski0afbb632008-06-06 21:10:57 +000042 # send stdin to /dev/null
43 dev_null = os.open(os.devnull, os.O_RDONLY)
44 os.dup2(dev_null, sys.stdin.fileno())
45 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000046
jadmanski0afbb632008-06-06 21:10:57 +000047 # Create separate process group
48 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000049
jadmanski0afbb632008-06-06 21:10:57 +000050 # Implement SIGTERM handler
51 def handle_sigint(signum, frame):
52 pid_file_manager.close_pid_file(1, signal.SIGTERM)
53 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000054
jadmanski0afbb632008-06-06 21:10:57 +000055 # Set signal handler
56 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000057
jadmanski0afbb632008-06-06 21:10:57 +000058 # Get a useful value for running 'USER'
59 realuser = os.environ.get('USER')
60 if not realuser:
61 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000062
mblighcce191f2008-09-19 20:31:03 +000063 if parser.options.machines:
64 machines = parser.options.machines.replace(',', ' ').strip().split()
65 else:
66 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000067 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000068 label = parser.options.label
69 user = parser.options.user
70 client = parser.options.client
71 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000072 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000073 install_after = parser.options.install_after
74 verify = parser.options.verify
75 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000076 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000077 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000078 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000079 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000080 ssh_user = parser.options.ssh_user
81 ssh_port = parser.options.ssh_port
82 ssh_pass = parser.options.ssh_pass
mbligha46678d2008-05-01 20:00:01 +000083
mblighb2bea302008-07-24 20:25:57 +000084 # can't be both a client and a server side test
85 if client and server:
86 print "Can not specify a test as both server and client!"
87 sys.exit(1)
88
showard45ae8192008-11-05 19:32:53 +000089 if len(parser.args) < 1 and not (verify or repair or cleanup):
jadmanski0afbb632008-06-06 21:10:57 +000090 print parser.parser.print_help()
91 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +000092
showard45ae8192008-11-05 19:32:53 +000093 # We have a control file unless it's just a verify/repair/cleanup job
jadmanski0afbb632008-06-06 21:10:57 +000094 if len(parser.args) > 0:
95 control = parser.args[0]
96 else:
97 control = None
mbligha46678d2008-05-01 20:00:01 +000098
jadmanski0afbb632008-06-06 21:10:57 +000099 if machines_file:
100 machines = []
101 for m in open(machines_file, 'r').readlines():
102 # remove comments, spaces
103 m = re.sub('#.*', '', m).strip()
104 if m:
105 machines.append(m)
106 print "Read list of machines from file: %s" % machines_file
107 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +0000108
jadmanski0afbb632008-06-06 21:10:57 +0000109 if machines:
110 for machine in machines:
111 if not machine or re.search('\s', machine):
112 print "Invalid machine %s" % str(machine)
113 sys.exit(1)
114 machines = list(set(machines))
115 machines.sort()
mbligha46678d2008-05-01 20:00:01 +0000116
jadmanski0afbb632008-06-06 21:10:57 +0000117 job = server_job.server_job(control, parser.args[1:], results, label,
118 user, machines, client, parse_job,
119 ssh_user, ssh_port, ssh_pass)
mbligh80e1eba2008-11-19 00:26:18 +0000120 if results:
121 debug_dir = os.path.join(results, 'debug')
122 stdout = os.path.join(debug_dir, 'autoserv.stdout')
123 stderr = os.path.join(debug_dir, 'autoserv.stderr')
124 if no_tee:
125 job.stdout.redirect(stdout)
126 job.stderr.redirect(stderr)
127 else:
128 job.stdout.tee_redirect(stdout)
129 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000130
mbligh161fe6f2008-06-19 16:26:04 +0000131 # perform checks
132 job.precheck()
133
jadmanski0afbb632008-06-06 21:10:57 +0000134 # run the job
135 exit_code = 0
136 try:
137 if repair:
jadmanskifbc1f0a2008-07-09 14:12:54 +0000138 job.repair(host_protection)
jadmanski0afbb632008-06-06 21:10:57 +0000139 elif verify:
140 job.verify()
141 else:
142 try:
showard45ae8192008-11-05 19:32:53 +0000143 job.run(cleanup, install_before, install_after)
jadmanski0afbb632008-06-06 21:10:57 +0000144 finally:
145 job.cleanup_parser()
jadmanski53aaf382008-11-17 16:22:31 +0000146 while job.hosts:
147 host = job.hosts.pop()
148 host.close()
jadmanski0afbb632008-06-06 21:10:57 +0000149 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000150 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000151 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000152
showard21baa452008-10-21 00:08:39 +0000153 pid_file_manager.num_tests_failed = job.num_tests_failed
154
jadmanski27b37ea2008-10-29 23:54:31 +0000155 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000156
157
158def main():
jadmanski0afbb632008-06-06 21:10:57 +0000159 pid_file_manager = PidFileManager()
mbligha46678d2008-05-01 20:00:01 +0000160
jadmanski0afbb632008-06-06 21:10:57 +0000161 # grab the parser
162 parser = autoserv_parser.autoserv_parser
mbligha46678d2008-05-01 20:00:01 +0000163
jadmanski0afbb632008-06-06 21:10:57 +0000164 if len(sys.argv) == 1:
165 parser.parser.print_help()
166 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000167
mbligh80e1eba2008-11-19 00:26:18 +0000168 results = parser.options.results
169 if not parser.options.no_logging:
170 if not results:
171 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
172 results = os.path.abspath(results)
173 if os.path.exists(os.path.join(results, 'control.srv')):
174 error = "Error: results directory already exists: %s\n" % results
175 sys.stderr.write(error)
176 sys.exit(1)
177 print "Results placed in %s" % results
mbligh10717632008-11-19 00:21:57 +0000178
mbligh80e1eba2008-11-19 00:26:18 +0000179 if parser.options.write_pidfile:
jadmanski0afbb632008-06-06 21:10:57 +0000180 pid_file_manager.open_pid_file(results)
mbligha46678d2008-05-01 20:00:01 +0000181
jadmanskif22fea82008-11-26 20:57:07 +0000182 autotest.BaseAutotest.set_install_in_tmpdir(
183 parser.options.install_in_tmpdir)
184
jadmanski0afbb632008-06-06 21:10:57 +0000185 exit_code = 0
186 try:
187 try:
mbligh10717632008-11-19 00:21:57 +0000188 run_autoserv(pid_file_manager, results, parser)
jadmanski0afbb632008-06-06 21:10:57 +0000189 except SystemExit, e:
190 exit_code = e.code
191 except:
192 traceback.print_exc()
193 # If we don't know what happened, we'll classify it as
194 # an 'abort' and return 1.
195 exit_code = 1
196 finally:
197 pid_file_manager.close_pid_file(exit_code)
198 sys.exit(exit_code)
mblighfaf0cd42007-11-19 16:00:24 +0000199
mblighbb421852008-03-11 22:36:16 +0000200
mbligha46678d2008-05-01 20:00:01 +0000201if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000202 main()