blob: 47b068d0612b5a4290b57b1099e1c936881fde2c [file] [log] [blame]
mbligh6203ace2007-10-04 21:54:24 +00001#!/usr/bin/python -u
mblighdcd57a82007-07-11 23:06:47 +00002#
3# Copyright 2007 Google Inc. Released under the GPL v2
4
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"""
8
mblighf1c52842007-10-16 15:21:38 +00009__author__ = """\
10mbligh@google.com (Martin J. Bligh)
mblighc8949b82007-07-23 16:33:58 +000011"""
mblighdcd57a82007-07-11 23:06:47 +000012
mbligh7cd30fd2008-10-21 16:25:19 +000013import sys, os, re, traceback, signal, time
mbligh0c3548d2008-02-01 18:08:53 +000014
mblighf5427bb2008-04-09 15:55:57 +000015import common
mblighcce191f2008-09-19 20:31:03 +000016from autotest_lib.server import server_job, utils, autoserv_parser
mbligh0b8c32d2008-10-29 16:46:04 +000017from autotest_lib.client.common_lib import debug
18
19
20debug.configure(module='server')
mbligh29aa9702007-08-09 22:41:43 +000021
mbligh9d1c7302007-10-07 18:53:13 +000022
mbligha46678d2008-05-01 20:00:01 +000023class PidFileManager(object):
jadmanski0afbb632008-06-06 21:10:57 +000024 pid_file = None
showard21baa452008-10-21 00:08:39 +000025 num_tests_failed = 0
mbligh74fc0462007-11-05 20:24:17 +000026
jadmanski0afbb632008-06-06 21:10:57 +000027 def open_pid_file(self, results_dir):
28 pid_file_path = os.path.join(results_dir, '.autoserv_execute')
29 assert not os.path.exists(pid_file_path)
30 self.pid_file = open(pid_file_path, 'w')
31 self.pid_file.write(str(os.getpid()) + '\n')
32 self.pid_file.flush()
mblighc99add62007-09-27 17:02:58 +000033
mblighc99add62007-09-27 17:02:58 +000034
jadmanski0afbb632008-06-06 21:10:57 +000035 def close_pid_file(self, exit_code, signal_code=0):
36 if not self.pid_file:
37 return
38 real_exit_code = (exit_code << 8) | (signal_code & 0xFF)
39 self.pid_file.write(str(real_exit_code) + '\n')
showard21baa452008-10-21 00:08:39 +000040 self.pid_file.write(str(self.num_tests_failed) + '\n')
jadmanski0afbb632008-06-06 21:10:57 +000041 self.pid_file.close()
42 self.pid_file = None
mblighc99add62007-09-27 17:02:58 +000043
mbligh842c6592007-11-05 18:27:23 +000044
mbligha46678d2008-05-01 20:00:01 +000045def run_autoserv(pid_file_manager, results, parser):
jadmanski0afbb632008-06-06 21:10:57 +000046 # send stdin to /dev/null
47 dev_null = os.open(os.devnull, os.O_RDONLY)
48 os.dup2(dev_null, sys.stdin.fileno())
49 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000050
jadmanski0afbb632008-06-06 21:10:57 +000051 # Create separate process group
52 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000053
jadmanski0afbb632008-06-06 21:10:57 +000054 # Implement SIGTERM handler
55 def handle_sigint(signum, frame):
56 pid_file_manager.close_pid_file(1, signal.SIGTERM)
57 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000058
jadmanski0afbb632008-06-06 21:10:57 +000059 # Set signal handler
60 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000061
jadmanski0afbb632008-06-06 21:10:57 +000062 # Get a useful value for running 'USER'
63 realuser = os.environ.get('USER')
64 if not realuser:
65 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000066
mblighcce191f2008-09-19 20:31:03 +000067
68
69 if parser.options.machines:
70 machines = parser.options.machines.replace(',', ' ').strip().split()
71 else:
72 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000073 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000074 label = parser.options.label
75 user = parser.options.user
76 client = parser.options.client
77 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000078 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000079 install_after = parser.options.install_after
80 verify = parser.options.verify
81 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000082 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000083 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000084 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000085 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000086 ssh_user = parser.options.ssh_user
87 ssh_port = parser.options.ssh_port
88 ssh_pass = parser.options.ssh_pass
mbligha46678d2008-05-01 20:00:01 +000089
mblighb2bea302008-07-24 20:25:57 +000090 # can't be both a client and a server side test
91 if client and server:
92 print "Can not specify a test as both server and client!"
93 sys.exit(1)
94
showard45ae8192008-11-05 19:32:53 +000095 if len(parser.args) < 1 and not (verify or repair or cleanup):
jadmanski0afbb632008-06-06 21:10:57 +000096 print parser.parser.print_help()
97 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +000098
showard45ae8192008-11-05 19:32:53 +000099 # We have a control file unless it's just a verify/repair/cleanup job
jadmanski0afbb632008-06-06 21:10:57 +0000100 if len(parser.args) > 0:
101 control = parser.args[0]
102 else:
103 control = None
mbligha46678d2008-05-01 20:00:01 +0000104
jadmanski0afbb632008-06-06 21:10:57 +0000105 if machines_file:
106 machines = []
107 for m in open(machines_file, 'r').readlines():
108 # remove comments, spaces
109 m = re.sub('#.*', '', m).strip()
110 if m:
111 machines.append(m)
112 print "Read list of machines from file: %s" % machines_file
113 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +0000114
jadmanski0afbb632008-06-06 21:10:57 +0000115 if machines:
116 for machine in machines:
117 if not machine or re.search('\s', machine):
118 print "Invalid machine %s" % str(machine)
119 sys.exit(1)
120 machines = list(set(machines))
121 machines.sort()
mbligha46678d2008-05-01 20:00:01 +0000122
jadmanski0afbb632008-06-06 21:10:57 +0000123 job = server_job.server_job(control, parser.args[1:], results, label,
124 user, machines, client, parse_job,
125 ssh_user, ssh_port, ssh_pass)
126 debug_dir = os.path.join(results, 'debug')
127 stdout = os.path.join(debug_dir, 'autoserv.stdout')
128 stderr = os.path.join(debug_dir, 'autoserv.stderr')
129 if no_tee:
130 job.stdout.redirect(stdout)
131 job.stderr.redirect(stderr)
132 else:
133 job.stdout.tee_redirect(stdout)
134 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000135
mbligh161fe6f2008-06-19 16:26:04 +0000136 # perform checks
137 job.precheck()
138
jadmanski0afbb632008-06-06 21:10:57 +0000139 # run the job
140 exit_code = 0
141 try:
142 if repair:
jadmanskifbc1f0a2008-07-09 14:12:54 +0000143 job.repair(host_protection)
jadmanski0afbb632008-06-06 21:10:57 +0000144 elif verify:
145 job.verify()
146 else:
147 try:
showard45ae8192008-11-05 19:32:53 +0000148 job.run(cleanup, install_before, install_after)
jadmanski0afbb632008-06-06 21:10:57 +0000149 finally:
150 job.cleanup_parser()
151 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000152 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000153 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000154
showard21baa452008-10-21 00:08:39 +0000155 pid_file_manager.num_tests_failed = job.num_tests_failed
156
jadmanski27b37ea2008-10-29 23:54:31 +0000157 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000158
159
160def main():
jadmanski0afbb632008-06-06 21:10:57 +0000161 pid_file_manager = PidFileManager()
mbligha46678d2008-05-01 20:00:01 +0000162
jadmanski0afbb632008-06-06 21:10:57 +0000163 # grab the parser
164 parser = autoserv_parser.autoserv_parser
mbligha46678d2008-05-01 20:00:01 +0000165
jadmanski0afbb632008-06-06 21:10:57 +0000166 if len(sys.argv) == 1:
167 parser.parser.print_help()
168 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000169
mbligh49acee52008-11-05 00:16:09 +0000170 results = parser.options.results
171 if not results:
172 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
173 results = os.path.abspath(results)
174 if os.path.exists(os.path.join(results, 'control.srv')):
175 error = "Error: results directory already exists: %s\n" % results
176 sys.stderr.write(error)
177 sys.exit(1)
178 print "Results placed in %s" % results
179
jadmanski0afbb632008-06-06 21:10:57 +0000180 write_pidfile = parser.options.write_pidfile
181 if write_pidfile:
182 pid_file_manager.open_pid_file(results)
mbligha46678d2008-05-01 20:00:01 +0000183
jadmanski0afbb632008-06-06 21:10:57 +0000184 exit_code = 0
185 try:
186 try:
mbligh49acee52008-11-05 00:16:09 +0000187 run_autoserv(pid_file_manager, results, parser)
jadmanski0afbb632008-06-06 21:10:57 +0000188 except SystemExit, e:
189 exit_code = e.code
190 except:
191 traceback.print_exc()
192 # If we don't know what happened, we'll classify it as
193 # an 'abort' and return 1.
194 exit_code = 1
195 finally:
196 pid_file_manager.close_pid_file(exit_code)
197 sys.exit(exit_code)
mblighfaf0cd42007-11-19 16:00:24 +0000198
mblighbb421852008-03-11 22:36:16 +0000199
mbligha46678d2008-05-01 20:00:01 +0000200if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000201 main()