autoserv revision f1c52841de1cad263a0ec83ee551deeff0cc0bb2
1#!/usr/bin/python -u
2#
3# Copyright 2007 Google Inc. Released under the GPL v2
4
5"""
6Run an control file through the server side engine
7"""
8
9__author__ = """\
10mbligh@google.com (Martin J. Bligh)
11"""
12
13import sys, os, re, server_job
14
15usage = """\
16usage: autoserv
17	[-b]                       # reboot all specified machines after the job
18	[-c]                       # control file is a client side control
19	[-r resultsdir]            # specify results directory (default '.')
20	[-t tag]                   # tag for the job (arbitrary string)
21	[-u user]                  # username for the job (email address)
22	[-m machine,[machine,...]] # list of machines to pass to control file
23	<control file>             # name of the control file to run
24	[args ...]                 # args to pass through to the control file
25"""
26
27args = sys.argv[1:]
28
29# We can't use the general getopt methods here, as there will be unknown
30# extra arguments that we pass down into the control file instead.
31# Thus we process the arguments by hand, for which we are duly repentant.
32# Making a single function here just makes it harder to read. Suck it up.
33def parse_opts(flag):
34	if args.count(flag):
35		idx = args.index(flag)
36		args[idx : idx+1] = []
37		return True
38	else:
39		return False
40
41
42def parse_opts_param(flag, default = None, split = False):
43	if args.count(flag):
44		idx = args.index(flag)
45		ret = args[idx+1]
46		args[idx : idx+2] = []
47		if split:
48			return ret.split(split)
49		else:
50			return ret
51	else:
52		return default
53
54
55machines = parse_opts_param('-m', None, split = ',')
56results  = parse_opts_param('-r', os.path.abspath('.'))
57tag	 = parse_opts_param('-t', '')
58user     = parse_opts_param('-u', 'anonymous')
59client   = parse_opts('-c')
60reboot   = parse_opts('-b')
61
62if len(args) < 1:
63	usage()
64
65job = server_job.server_job(args[0], args[1:], results, tag, user, client)
66job.run(machines, reboot)
67