mbligh | 6203ace | 2007-10-04 21:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 5 | """ |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 6 | Run an control file through the server side engine |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 9 | __author__ = """\ |
| 10 | mbligh@google.com (Martin J. Bligh) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 11 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 12 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 13 | import sys, os, re, server_job, hosts.site_host, utils |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 14 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 15 | usage = """\ |
| 16 | usage: autoserv |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 17 | [-m machine,[machine,...]] # list of machines to pass to control file |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 18 | [-M machines_file] # list of machines (from a file) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 19 | [-c] # control file is a client side control |
| 20 | [-r resultsdir] # specify results directory (default '.') |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 21 | [-i] # reinstall machines before running the job |
| 22 | [-I] # reinstall machines after running the job |
| 23 | [-b] # reboot all specified machines after the job |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 24 | [-l label] # label for the job (arbitrary string) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 25 | [-u user] # username for the job (email address) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 26 | <control file> # name of the control file to run |
| 27 | [args ...] # args to pass through to the control file |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 30 | args = sys.argv[1:] |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 31 | parser = utils.AutoservOptionParser(args) |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 32 | |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 33 | # Get a useful value for running 'USER' |
| 34 | realuser = os.environ.get('USER') |
| 35 | if not realuser: |
| 36 | realuser = 'anonymous' |
| 37 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 38 | machines = parser.parse_opts_param('-m', None, split = ',') |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 39 | machines_file = parser.parse_opts_param('-M', None) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 40 | results = parser.parse_opts_param('-r', os.path.abspath('.')) |
| 41 | label = parser.parse_opts_param('-l', '') |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 42 | user = parser.parse_opts_param('-u', realuser) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 43 | client = parser.parse_opts('-c') |
| 44 | reboot = parser.parse_opts('-b') |
| 45 | install_before = parser.parse_opts('-i') |
| 46 | install_after = parser.parse_opts('-I') |
mbligh | fdbe7d9 | 2007-11-05 18:15:57 +0000 | [diff] [blame] | 47 | results = os.path.abspath(results) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 48 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 49 | if getattr(hosts.site_host, 'site_parse_options', None): |
| 50 | hosts.site_host.site_parse_options(parser) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 51 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 52 | if len(parser.args) < 1: |
mbligh | 4ef8623 | 2007-10-23 00:09:53 +0000 | [diff] [blame] | 53 | print usage |
| 54 | sys.exit(-1) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 55 | |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 56 | if machines_file: |
| 57 | machines = [] |
| 58 | for m in open(machines_file, 'r').readlines(): |
| 59 | m = re.sub('#.*', '', m).strip() # remove comments, spaces |
| 60 | if m: |
| 61 | machines.append(m) |
| 62 | print "Read list of machines from file: %s" % machines_file |
| 63 | print ','.join(machines) |
| 64 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 65 | job = server_job.server_job(parser.args[0], parser.args[1:], results, label, |
| 66 | user, client) |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 67 | job.stdout.tee_redirect(os.path.join(results, 'debug', 'autoserv.stdout')) |
| 68 | job.stderr.tee_redirect(os.path.join(results, 'debug', 'autoserv.stderr')) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 69 | job.run(machines, reboot, install_before, install_after) |