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 | 0c3548d | 2008-02-01 18:08:53 +0000 | [diff] [blame] | 13 | from common.check_version import check_python_version |
| 14 | check_python_version() |
| 15 | |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 16 | import sys, os, re, server_job, hosts.site_host, utils, traceback |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 17 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 18 | usage = """\ |
| 19 | usage: autoserv |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 20 | [-m machine,[machine,...]] # list of machines to pass to control file |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 21 | [-M machines_file] # list of machines (from a file) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 22 | [-c] # control file is a client side control |
| 23 | [-r resultsdir] # specify results directory (default '.') |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 24 | [-i] # reinstall machines before running the job |
| 25 | [-I] # reinstall machines after running the job |
| 26 | [-b] # reboot all specified machines after the job |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 27 | [-l label] # label for the job (arbitrary string) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 28 | [-u user] # username for the job (email address) |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 29 | [-v] # verify the machines only |
| 30 | [-R] # repair the machines |
mbligh | e5cd0fd | 2008-01-26 23:07:31 +0000 | [diff] [blame] | 31 | [-n] # no teeing the status to stdout/stderr |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 32 | <control file> # name of the control file to run |
| 33 | [args ...] # args to pass through to the control file |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 34 | """ |
| 35 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 36 | args = sys.argv[1:] |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 37 | parser = utils.AutoservOptionParser(args) |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 38 | |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 39 | # Get a useful value for running 'USER' |
| 40 | realuser = os.environ.get('USER') |
| 41 | if not realuser: |
| 42 | realuser = 'anonymous' |
| 43 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 44 | machines = parser.parse_opts_param('-m', None, split = ',') |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 45 | machines_file = parser.parse_opts_param('-M', None) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 46 | results = parser.parse_opts_param('-r', os.path.abspath('.')) |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 47 | results = os.path.abspath(results) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 48 | label = parser.parse_opts_param('-l', '') |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 49 | user = parser.parse_opts_param('-u', realuser) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 50 | client = parser.parse_opts('-c') |
| 51 | reboot = parser.parse_opts('-b') |
| 52 | install_before = parser.parse_opts('-i') |
| 53 | install_after = parser.parse_opts('-I') |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 54 | verify = parser.parse_opts('-v') |
| 55 | repair = parser.parse_opts('-R') |
mbligh | e5cd0fd | 2008-01-26 23:07:31 +0000 | [diff] [blame] | 56 | no_tee = parser.parse_opts('-n') |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 57 | |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 58 | |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 59 | if len(parser.args) < 1 and not verify and not repair: |
mbligh | 4ef8623 | 2007-10-23 00:09:53 +0000 | [diff] [blame] | 60 | print usage |
| 61 | sys.exit(-1) |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 62 | |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 63 | if machines_file: |
| 64 | machines = [] |
| 65 | for m in open(machines_file, 'r').readlines(): |
| 66 | m = re.sub('#.*', '', m).strip() # remove comments, spaces |
| 67 | if m: |
| 68 | machines.append(m) |
| 69 | print "Read list of machines from file: %s" % machines_file |
| 70 | print ','.join(machines) |
| 71 | |
mbligh | dbf3761 | 2007-11-24 19:38:11 +0000 | [diff] [blame] | 72 | if machines: |
| 73 | for machine in machines: |
| 74 | if not machine or re.search('\s', machine): |
| 75 | print "Invalid machine %s" % str(machine) |
| 76 | sys.exit(1) |
mbligh | 535a454 | 2008-02-11 16:26:36 +0000 | [diff] [blame] | 77 | machines = list(set(machines)) |
| 78 | machines.sort() |
mbligh | dbf3761 | 2007-11-24 19:38:11 +0000 | [diff] [blame] | 79 | |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 80 | # We have a control file unless it's just a verify/repair job |
| 81 | if len(parser.args) > 0: |
| 82 | control = parser.args[0] |
| 83 | else: |
| 84 | control = None |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 85 | |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 86 | job = server_job.server_job(control, parser.args[1:], results, label, |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 87 | user, machines, client) |
mbligh | e5cd0fd | 2008-01-26 23:07:31 +0000 | [diff] [blame] | 88 | debug_dir = os.path.join(results, 'debug') |
| 89 | if no_tee: |
| 90 | job.stdout.redirect(os.path.join(debug_dir, 'autoserv.stdout')) |
| 91 | job.stderr.redirect(os.path.join(debug_dir, 'autoserv.stderr')) |
| 92 | else: |
| 93 | job.stdout.tee_redirect(os.path.join(debug_dir, 'autoserv.stdout')) |
| 94 | job.stderr.tee_redirect(os.path.join(debug_dir, 'autoserv.stderr')) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 95 | |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 96 | if repair: |
| 97 | job.repair() |
| 98 | sys.exit(0) |
| 99 | elif verify: |
| 100 | job.verify() |
| 101 | sys.exit(0) |
| 102 | |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 103 | try: |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 104 | job.run(reboot, install_before, install_after) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 105 | except: |
mbligh | 394ff51 | 2008-02-21 16:53:11 +0000 | [diff] [blame] | 106 | job.aborted = True |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 107 | traceback.print_exc() |
| 108 | |
| 109 | # if the job was aborted, return a non-zero error code |
| 110 | if getattr(job, 'aborted', False): |
| 111 | sys.exit(1) |