blob: 0d3a997de03c66a1a091a82d18a51b57af2f90d3 [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
showard75cdfee2009-06-10 17:40:41 +00009import sys, os, re, traceback, signal, time, logging
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
showard75cdfee2009-06-10 17:40:41 +000013from autotest_lib.server import server_logging_config
14from autotest_lib.client.common_lib import pidfile, logging_manager
mbligh92c0fc22008-11-20 16:52:23 +000015
mbligha46678d2008-05-01 20:00:01 +000016def run_autoserv(pid_file_manager, results, parser):
jadmanski0afbb632008-06-06 21:10:57 +000017 # send stdin to /dev/null
18 dev_null = os.open(os.devnull, os.O_RDONLY)
19 os.dup2(dev_null, sys.stdin.fileno())
20 os.close(dev_null)
mblighdbf37612007-11-24 19:38:11 +000021
jadmanski0afbb632008-06-06 21:10:57 +000022 # Create separate process group
23 os.setpgrp()
mbligh1d42d4e2007-11-05 22:42:00 +000024
jadmanski0afbb632008-06-06 21:10:57 +000025 # Implement SIGTERM handler
26 def handle_sigint(signum, frame):
mblighff7d61f2008-12-22 14:53:35 +000027 if pid_file_manager:
28 pid_file_manager.close_file(1, signal.SIGTERM)
jadmanski0afbb632008-06-06 21:10:57 +000029 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighfaf0cd42007-11-19 16:00:24 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 # Set signal handler
32 signal.signal(signal.SIGTERM, handle_sigint)
mblighe25fd5b2008-01-22 17:23:37 +000033
jadmanski0afbb632008-06-06 21:10:57 +000034 # Get a useful value for running 'USER'
35 realuser = os.environ.get('USER')
36 if not realuser:
37 realuser = 'anonymous'
mbligha46678d2008-05-01 20:00:01 +000038
mblighcce191f2008-09-19 20:31:03 +000039 if parser.options.machines:
40 machines = parser.options.machines.replace(',', ' ').strip().split()
41 else:
42 machines = []
jadmanski0afbb632008-06-06 21:10:57 +000043 machines_file = parser.options.machines_file
mblighb2bea302008-07-24 20:25:57 +000044 label = parser.options.label
mbligh374f3412009-05-13 21:29:45 +000045 group_name = parser.options.group_name
mblighb2bea302008-07-24 20:25:57 +000046 user = parser.options.user
47 client = parser.options.client
48 server = parser.options.server
jadmanski0afbb632008-06-06 21:10:57 +000049 install_before = parser.options.install_before
mblighb2bea302008-07-24 20:25:57 +000050 install_after = parser.options.install_after
51 verify = parser.options.verify
52 repair = parser.options.repair
showard45ae8192008-11-05 19:32:53 +000053 cleanup = parser.options.cleanup
mblighb2bea302008-07-24 20:25:57 +000054 no_tee = parser.options.no_tee
jadmanski0afbb632008-06-06 21:10:57 +000055 parse_job = parser.options.parse_job
jadmanskifbc1f0a2008-07-09 14:12:54 +000056 host_protection = parser.options.host_protection
jadmanski0afbb632008-06-06 21:10:57 +000057 ssh_user = parser.options.ssh_user
58 ssh_port = parser.options.ssh_port
59 ssh_pass = parser.options.ssh_pass
jadmanskidef0c3c2009-03-25 20:07:10 +000060 collect_crashinfo = parser.options.collect_crashinfo
mbligha46678d2008-05-01 20:00:01 +000061
mblighb2bea302008-07-24 20:25:57 +000062 # can't be both a client and a server side test
63 if client and server:
64 print "Can not specify a test as both server and client!"
65 sys.exit(1)
66
jadmanskidef0c3c2009-03-25 20:07:10 +000067 if len(parser.args) < 1 and not (verify or repair or cleanup
68 or collect_crashinfo):
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
mbligh374f3412009-05-13 21:29:45 +000096 if group_name and len(machines) < 2:
97 print ("-G %r may only be supplied with more than one machine."
98 % group_name)
99 sys.exit(1)
100
jadmanski0afbb632008-06-06 21:10:57 +0000101 job = server_job.server_job(control, parser.args[1:], results, label,
102 user, machines, client, parse_job,
mbligh374f3412009-05-13 21:29:45 +0000103 ssh_user, ssh_port, ssh_pass,
104 group_name=group_name)
showard75cdfee2009-06-10 17:40:41 +0000105 job.logging.start_logging()
mbligha46678d2008-05-01 20:00:01 +0000106
mbligh161fe6f2008-06-19 16:26:04 +0000107 # perform checks
108 job.precheck()
109
jadmanski0afbb632008-06-06 21:10:57 +0000110 # run the job
111 exit_code = 0
112 try:
mbligh332000a2009-06-08 16:47:28 +0000113 try:
114 if repair:
115 job.repair(host_protection)
116 elif verify:
117 job.verify()
118 else:
jadmanskidef0c3c2009-03-25 20:07:10 +0000119 job.run(cleanup, install_before, install_after,
120 only_collect_crashinfo=collect_crashinfo)
mbligh332000a2009-06-08 16:47:28 +0000121 finally:
122 while job.hosts:
123 host = job.hosts.pop()
124 host.close()
jadmanski0afbb632008-06-06 21:10:57 +0000125 except:
jadmanski27b37ea2008-10-29 23:54:31 +0000126 exit_code = 1
jadmanski0afbb632008-06-06 21:10:57 +0000127 traceback.print_exc()
mbligha46678d2008-05-01 20:00:01 +0000128
mblighff7d61f2008-12-22 14:53:35 +0000129 if pid_file_manager:
130 pid_file_manager.num_tests_failed = job.num_tests_failed
131 pid_file_manager.close_file(exit_code)
jadmanskie0dffc32008-12-15 17:30:30 +0000132 job.cleanup_parser()
showard21baa452008-10-21 00:08:39 +0000133
jadmanski27b37ea2008-10-29 23:54:31 +0000134 sys.exit(exit_code)
mbligha46678d2008-05-01 20:00:01 +0000135
136
137def main():
jadmanski0afbb632008-06-06 21:10:57 +0000138 # grab the parser
139 parser = autoserv_parser.autoserv_parser
mbligha5cb4062009-02-17 15:53:39 +0000140 parser.parse_args()
mbligha46678d2008-05-01 20:00:01 +0000141
jadmanski0afbb632008-06-06 21:10:57 +0000142 if len(sys.argv) == 1:
143 parser.parser.print_help()
144 sys.exit(1)
mbligha6f13082008-06-05 23:53:46 +0000145
showard75cdfee2009-06-10 17:40:41 +0000146 if parser.options.no_logging:
147 results = None
148 else:
149 results = parser.options.results
mbligh80e1eba2008-11-19 00:26:18 +0000150 if not results:
151 results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S')
152 results = os.path.abspath(results)
jadmanskidef0c3c2009-03-25 20:07:10 +0000153 resultdir_exists = os.path.exists(os.path.join(results, 'control.srv'))
154 if not parser.options.collect_crashinfo and resultdir_exists:
mbligh80e1eba2008-11-19 00:26:18 +0000155 error = "Error: results directory already exists: %s\n" % results
156 sys.stderr.write(error)
157 sys.exit(1)
mbligha788dc42009-03-26 21:10:16 +0000158
159 # Now that we certified that there's no leftover results dir from
160 # previous jobs, lets create the result dir since the logging system
161 # needs to create the log file in there.
162 if not os.path.isdir(results):
163 os.makedirs(results)
showard75cdfee2009-06-10 17:40:41 +0000164
165 logging_manager.configure_logging(
166 server_logging_config.ServerLoggingConfig(), results_dir=results,
167 use_console=not parser.options.no_tee)
168 if results:
mbligha788dc42009-03-26 21:10:16 +0000169 logging.info("Results placed in %s" % results)
mbligh10717632008-11-19 00:21:57 +0000170
mbligh80e1eba2008-11-19 00:26:18 +0000171 if parser.options.write_pidfile:
showardd3dc1992009-04-22 21:01:40 +0000172 if parser.options.collect_crashinfo:
173 pidfile_label = 'collect_crashinfo'
174 else:
175 pidfile_label = 'autoserv'
176 pid_file_manager = pidfile.PidFileManager(pidfile_label, results)
jadmanskid5ab8c52008-12-03 16:27:07 +0000177 pid_file_manager.open_file()
mblighff7d61f2008-12-22 14:53:35 +0000178 else:
179 pid_file_manager = None
mbligha46678d2008-05-01 20:00:01 +0000180
jadmanskif22fea82008-11-26 20:57:07 +0000181 autotest.BaseAutotest.set_install_in_tmpdir(
182 parser.options.install_in_tmpdir)
183
jadmanski0afbb632008-06-06 21:10:57 +0000184 exit_code = 0
185 try:
186 try:
mbligh10717632008-11-19 00:21:57 +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:
mblighff7d61f2008-12-22 14:53:35 +0000196 if pid_file_manager:
197 pid_file_manager.close_file(exit_code)
jadmanski0afbb632008-06-06 21:10:57 +0000198 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()