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 | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame^] | 26 | [-v] # verify the machines only |
| 27 | [-R] # repair the machines |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 28 | <control file> # name of the control file to run |
| 29 | [args ...] # args to pass through to the control file |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 30 | """ |
| 31 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 32 | args = sys.argv[1:] |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 33 | parser = utils.AutoservOptionParser(args) |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 34 | |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 35 | # Get a useful value for running 'USER' |
| 36 | realuser = os.environ.get('USER') |
| 37 | if not realuser: |
| 38 | realuser = 'anonymous' |
| 39 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 40 | machines = parser.parse_opts_param('-m', None, split = ',') |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 41 | machines_file = parser.parse_opts_param('-M', None) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 42 | results = parser.parse_opts_param('-r', os.path.abspath('.')) |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame^] | 43 | results = os.path.abspath(results) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 44 | label = parser.parse_opts_param('-l', '') |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 45 | user = parser.parse_opts_param('-u', realuser) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 46 | client = parser.parse_opts('-c') |
| 47 | reboot = parser.parse_opts('-b') |
| 48 | install_before = parser.parse_opts('-i') |
| 49 | install_after = parser.parse_opts('-I') |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame^] | 50 | verify = parser.parse_opts('-v') |
| 51 | repair = parser.parse_opts('-R') |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 52 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 53 | if getattr(hosts.site_host, 'site_parse_options', None): |
| 54 | hosts.site_host.site_parse_options(parser) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 55 | |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame^] | 56 | if len(parser.args) < 1 and not verify and not repair: |
mbligh | 4ef8623 | 2007-10-23 00:09:53 +0000 | [diff] [blame] | 57 | print usage |
| 58 | sys.exit(-1) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 59 | |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 60 | if machines_file: |
| 61 | machines = [] |
| 62 | for m in open(machines_file, 'r').readlines(): |
| 63 | m = re.sub('#.*', '', m).strip() # remove comments, spaces |
| 64 | if m: |
| 65 | machines.append(m) |
| 66 | print "Read list of machines from file: %s" % machines_file |
| 67 | print ','.join(machines) |
| 68 | |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame^] | 69 | if repair: |
| 70 | server_job.repair_machines(machines) |
| 71 | |
| 72 | if verify or repair: |
| 73 | server_job.verify_machines(machines) |
| 74 | sys.exit(0) |
| 75 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 76 | job = server_job.server_job(parser.args[0], parser.args[1:], results, label, |
| 77 | user, client) |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 78 | job.stdout.tee_redirect(os.path.join(results, 'debug', 'autoserv.stdout')) |
| 79 | job.stderr.tee_redirect(os.path.join(results, 'debug', 'autoserv.stderr')) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 80 | job.run(machines, reboot, install_before, install_after) |