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 | """ |
| 6 | Run an autoserv control file |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 8 | -m machine[,machine,machine] : specify machines to run on |
| 9 | -c : assume control file is a client control file |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 10 | -b : Reboot machine at end of test run |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 11 | """ |
| 12 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 13 | __author__ = """ |
| 14 | mbligh@google.com (Martin J. Bligh), |
| 15 | poirier@google.com (Benjamin Poirier), |
| 16 | stutsman@google.com (Ryan Stutsman) |
| 17 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 18 | |
mbligh | e267685 | 2007-10-13 00:00:49 +0000 | [diff] [blame] | 19 | import sys, os, re |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 20 | |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 21 | preamble = """\ |
| 22 | import os, sys |
| 23 | |
| 24 | import errors, hosts, autotest, kvm |
| 25 | import source_kernel, rpm_kernel, deb_kernel |
| 26 | from subcommand import * |
mbligh | 03dd079 | 2007-08-28 10:26:46 +0000 | [diff] [blame] | 27 | from utils import run, get_tmp_dir, sh_escape |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 28 | |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 29 | """ |
| 30 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 31 | client_wrapper = """ |
| 32 | at = autotest.Autotest() |
mbligh | 9ff1a3a | 2007-10-06 21:16:34 +0000 | [diff] [blame] | 33 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 34 | def run_client(machine): |
| 35 | host = hosts.SSHHost(machine) |
| 36 | at.run(control, host=host) |
| 37 | |
| 38 | if len(machines) > 1: |
| 39 | parallel_simple(run_client, machines) |
| 40 | else: |
| 41 | run_client(machines[0]) |
| 42 | """ |
| 43 | |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 44 | cleanup="""\ |
| 45 | def cleanup(machine): |
| 46 | host = hosts.SSHHost(machine, initialize=False) |
| 47 | host.reboot() |
| 48 | |
| 49 | parallel_simple(cleanup, machines) |
| 50 | """ |
| 51 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 52 | # Later, autotest.py will want to figure out what the top level autodir is. |
| 53 | # They won't be able to use abspath later, if we change directory - as it works |
| 54 | # relative to whatever your current path is. Hence we make argv[0] absolute now. |
| 55 | sys.argv[0] = os.path.abspath(sys.argv[0]) |
| 56 | args = sys.argv[1:] |
| 57 | |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 58 | def run(control_file, machines, args, client, reboot): |
mbligh | 5fadbfe | 2007-10-06 21:12:06 +0000 | [diff] [blame] | 59 | namespace = dict({ 'machines' : machines, 'args' : args }) |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 60 | control = open(control_file, 'r').read() |
mbligh | e267685 | 2007-10-13 00:00:49 +0000 | [diff] [blame] | 61 | control = re.sub("\r\n", "\n", control) |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 62 | |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 63 | try: |
| 64 | if client: |
| 65 | namespace['control'] = control |
| 66 | exec(preamble + client_wrapper, namespace, namespace) |
| 67 | else: |
| 68 | exec(preamble + control, namespace, namespace) |
| 69 | finally: |
| 70 | if reboot: |
mbligh | e267685 | 2007-10-13 00:00:49 +0000 | [diff] [blame] | 71 | exec(preamble + cleanup, namespace, namespace) |
mbligh | 5fadbfe | 2007-10-06 21:12:06 +0000 | [diff] [blame] | 72 | |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 73 | def usage(): |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 74 | print "usage: autoserv [-b] [-c] <control file> [-m machine,[machine,...]] [args ...]" |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 75 | sys.exit(1) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 76 | |
mbligh | 5fadbfe | 2007-10-06 21:12:06 +0000 | [diff] [blame] | 77 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 78 | # We can't use the general getopt methods here, as there will be unknown |
| 79 | # extra arguments that we pass down into the control file instead. |
| 80 | # Thus we process the arguments by hand, for which we are duly repentant. |
| 81 | if args.count('-m'): |
| 82 | idx = args.index('-m') |
| 83 | machines = args[idx+1].split(',') |
| 84 | args[idx:idx+2] = [] # delete '-m machines' |
| 85 | else: |
mbligh | 95fca57 | 2007-09-27 17:11:00 +0000 | [diff] [blame] | 86 | machines = None |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 87 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 88 | if args.count('-c'): |
| 89 | client = True |
| 90 | idx = args.index('-c') |
| 91 | args[idx:idx+1] = [] # delete '-c' |
| 92 | else: |
| 93 | client = False |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 94 | |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 95 | if args.count('-b'): |
| 96 | reboot = True |
| 97 | idx = args.index('-b') |
| 98 | args[idx:idx+1] = [] # delete '-b' |
| 99 | else: |
| 100 | reboot = False |
| 101 | |
mbligh | 9d1c730 | 2007-10-07 18:53:13 +0000 | [diff] [blame] | 102 | if len(args) < 1: |
| 103 | usage() |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 104 | |
mbligh | cc2c3a6 | 2007-10-12 00:34:31 +0000 | [diff] [blame] | 105 | run(args[0], machines, args[1:], client, reboot) |