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