blob: 872eb16690c84c7a65a0ec127d831b78c4f5f748 [file] [log] [blame]
mblighdcc04992007-07-26 19:42:55 +00001#!/usr/bin/python -u
2# monitor_queue <client> <spool_directory> <resultsdir> [<conmux_server>]
3import os, time, sys
4from subprocess import *
5import tempfile
6
7if (len(sys.argv) < 4):
8 print "Usage: monitor_queue <client> <spool_directory> <resultsdir> [<conmux_server>]"
9 sys.exit(1)
10(client, spooldir, resultsdir) = sys.argv[1:4]
11if len(sys.argv) == 5:
12 console = sys.argv[4]
13else:
14 console = None
15if not os.path.exists(spooldir):
16 print "spooldir %s does not exist" % spooldir
17 sys.exit(1)
18if not os.path.exists(resultsdir):
19 print "resultsdir %s does not exist" % resultsdir
20 sys.exit(1)
21
22
23def pick_job(jobs):
24 """Pick the next job to run. Currently we just pick the oldest job
25 However, this would be the place to put prioritizations."""
26 if not jobs:
27 return None
28 return sorted(jobs, key=lambda x:os.stat(x).st_mtime, reverse=True)[0]
29
30
31def __create_autoserv_wrapper(control_path, results):
32 """Create an autoserv file that runs an autotest file at
33 control_path on client and outputs the results in results."""
34 # Create an autoserv control file to run this autotest control file
35 tmpfd, tmpname = tempfile.mkstemp()
36 tmp = os.fdopen(tmpfd, 'w')
37 if console:
38 print >> tmp, "host = hosts.ConmuxSSHHost('%s', \
39 server='%s')" % (client, console)
40 else:
41 print >> tmp, "host = hosts.ConmuxSSHHost('%s')" % client
42 print >> tmp, "at = autotest.Autotest()"
43 # TODO(stutsman): at.install(host) here?
44 print >> tmp, "at.run('%s', '%s', host)" % (control_path, results)
45 tmp.close()
46 return tmpname
47
48
49def run_job(control):
50 """Runs a control file from the spooldir.
51 Args:
52 control: A path to a control file. It is assumed to be an
53 Autotest control file in which case it will automatically
54 be wrapped with autoserv control commands and run with
55 autoserv. If the file name ends with .srv the wrapping
56 procedure will be skipped and the autoserv file will be
57 run directly.
58
59 Return:
60 The return code from the autoserv process.
61 """
62 # Make sure all the output directories are all setup
63 results = os.path.join(resultsdir, control)
64 if os.path.exists(results):
65 print "Resultsdir %s already present, " % results,
66 results = "%s.%d" % (results, int(time.time()))
67 print "changing to %s" % results
68 os.mkdir(results)
69 debug = os.path.join(results, 'debug')
70 os.mkdir(debug)
71
72 # If this is an autoserv file then don't create the wrapper control
73 is_autoserv_ctl = control.endswith('.srv')
74 control_path = os.path.abspath(os.path.join(spooldir, control))
75 # Otherwise create a tmp autoserv file just to launch the AT ctl file
76 if not is_autoserv_ctl:
77 control_path = __create_autoserv_wrapper(control_path, results)
78
79 # Now run the job
80 exedir = os.path.abspath(os.path.dirname(sys.argv[0]))
81 autoserv_exe = os.path.abspath(os.path.join(exedir,
82 '..',
83 'server',
84 'autoserv'))
85 autoserv_cmd = ' '.join([autoserv_exe, control_path])
86
87 autoserv_log = open(os.path.join(debug, 'server.log'), 'w');
88 p = Popen(autoserv_cmd, shell=True, stdout=autoserv_log, stderr=STDOUT)
89 (pid, ret) = os.waitpid(p.pid, 0)
90 autoserv_log.close()
91
92 # If this was a tempfile then clean it up
93 if not is_autoserv_ctl:
94 os.unlink(control_path)
95 print "Completed job: %s (%d) " % (control, ret)
96
97 return ret
98
99
100dir = os.path.abspath(os.path.dirname(sys.argv[0]))
101runjob = os.path.join(dir, 'runjob')
102os.chdir(spooldir)
103print "monitoring spool directory: " + spooldir
104while True:
105 jobs = [j for j in os.listdir(spooldir) if not j.startswith('.')]
106 next_job = pick_job(jobs)
107 if not next_job:
108 time.sleep(10)
109 continue
110 ret = run_job(next_job)
111 if ret == 1: # Ooops. the machine is stuffed.
112 print "WARNING: client %s failed verify_machine" % client
113 sleep(600)
114 continue
115 os.remove(next_job)