blob: dc996d23e58d0229ab05c56da3df0cf4f4619e92 [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):
mblighadfe4312008-10-29 22:10:47 +000046 if not results:
47 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
48 results = os.path.abspath(results)
49 if os.path.exists(os.path.join(results, 'control.srv')):
50 error = "Error: results directory already exists: %s\n" % results
51 sys.stderr.write(error)
52 sys.exit(1)
53 print "Results placed in %s" % results
54
jadmanski0afbb632008-06-06 21:10:57 +000055 # send stdin to /dev/null
56 dev_null = os.open(os.devnull, os.O_RDONLY)
57 os.dup2(dev_null, sys.stdin.fileno())
58 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000059
jadmanski0afbb632008-06-06 21:10:57 +000060 # Create separate process group
61 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000062
jadmanski0afbb632008-06-06 21:10:57 +000063 # Implement SIGTERM handler
64 def handle_sigint(signum, frame):
65 pid_file_manager.close_pid_file(1, signal.SIGTERM)
66 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000067
jadmanski0afbb632008-06-06 21:10:57 +000068 # Set signal handler
69 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000070
jadmanski0afbb632008-06-06 21:10:57 +000071 # Get a useful value for running 'USER'
72 realuser = os.environ.get('USER')
73 if not realuser:
74 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000075
mblighcce191f2008-09-19 20:31:03 +000076
77
78 if parser.options.machines:
79 machines = parser.options.machines.replace(',', ' ').strip().split()
80 else:
81 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000082 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000083 label = parser.options.label
84 user = parser.options.user
85 client = parser.options.client
86 server = parser.options.server
87 reboot = parser.options.reboot
jadmanski0afbb632008-06-06 21:10:57 +000088 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000089 install_after = parser.options.install_after
90 verify = parser.options.verify
91 repair = parser.options.repair
92 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000093 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000094 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000095 ssh_user = parser.options.ssh_user
96 ssh_port = parser.options.ssh_port
97 ssh_pass = parser.options.ssh_pass
mbligha46678d2008-05-01 20:00:01 +000098
mblighb2bea302008-07-24 20:25:57 +000099 # can't be both a client and a server side test
100 if client and server:
101 print "Can not specify a test as both server and client!"
102 sys.exit(1)
103
jadmanski0afbb632008-06-06 21:10:57 +0000104 if len(parser.args) < 1 and not verify and not repair:
105 print parser.parser.print_help()
106 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +0000107
jadmanski0afbb632008-06-06 21:10:57 +0000108 # We have a control file unless it's just a verify/repair job
109 if len(parser.args) > 0:
110 control = parser.args[0]
111 else:
112 control = None
mbligha46678d2008-05-01 20:00:01 +0000113
jadmanski0afbb632008-06-06 21:10:57 +0000114 if machines_file:
115 machines = []
116 for m in open(machines_file, 'r').readlines():
117 # remove comments, spaces
118 m = re.sub('#.*', '', m).strip()
119 if m:
120 machines.append(m)
121 print "Read list of machines from file: %s" % machines_file
122 print ','.join(machines)
mbligha46678d2008-05-01 20:00:01 +0000123
jadmanski0afbb632008-06-06 21:10:57 +0000124 if machines:
125 for machine in machines:
126 if not machine or re.search('\s', machine):
127 print "Invalid machine %s" % str(machine)
128 sys.exit(1)
129 machines = list(set(machines))
130 machines.sort()
mbligha46678d2008-05-01 20:00:01 +0000131
jadmanski0afbb632008-06-06 21:10:57 +0000132 job = server_job.server_job(control, parser.args[1:], results, label,
133 user, machines, client, parse_job,
134 ssh_user, ssh_port, ssh_pass)
135 debug_dir = os.path.join(results, 'debug')
136 stdout = os.path.join(debug_dir, 'autoserv.stdout')
137 stderr = os.path.join(debug_dir, 'autoserv.stderr')
138 if no_tee:
139 job.stdout.redirect(stdout)
140 job.stderr.redirect(stderr)
141 else:
142 job.stdout.tee_redirect(stdout)
143 job.stderr.tee_redirect(stderr)
mbligha46678d2008-05-01 20:00:01 +0000144
mbligh161fe6f2008-06-19 16:26:04 +0000145 # perform checks
146 job.precheck()
147
jadmanski0afbb632008-06-06 21:10:57 +0000148 # run the job
149 exit_code = 0
150 try:
151 if repair:
jadmanskifbc1f0a2008-07-09 14:12:54 +0000152 job.repair(host_protection)
jadmanski0afbb632008-06-06 21:10:57 +0000153 elif verify:
154 job.verify()
155 else:
156 try:
157 job.run(reboot, install_before, install_after)
158 finally:
159 job.cleanup_parser()
160 except:
161 job.aborted = True
162 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000163
showard21baa452008-10-21 00:08:39 +0000164 pid_file_manager.num_tests_failed = job.num_tests_failed
165
jadmanski0afbb632008-06-06 21:10:57 +0000166 if getattr(job, 'aborted', False):
167 sys.exit(1)
mbligha46678d2008-05-01 20:00:01 +0000168
169
170def main():
jadmanski0afbb632008-06-06 21:10:57 +0000171 pid_file_manager = PidFileManager()
mbligha46678d2008-05-01 20:00:01 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 # grab the parser
174 parser = autoserv_parser.autoserv_parser
mbligha46678d2008-05-01 20:00:01 +0000175
jadmanski0afbb632008-06-06 21:10:57 +0000176 if len(sys.argv) == 1:
177 parser.parser.print_help()
178 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000179
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:
mblighadfe4312008-10-29 22:10:47 +0000187 run_autoserv(pid_file_manager, parser.options.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()