blob: f46f92056b281938e05521487c74df66dbc18c22 [file] [log] [blame]
mbligh6203ace2007-10-04 21:54:24 +00001#!/usr/bin/python -u
mblighdcd57a82007-07-11 23:06:47 +00002#
3# Copyright 2007 Google Inc. Released under the GPL v2
4
mblighc8949b82007-07-23 16:33:58 +00005"""
6Run an autoserv control file
mblighdcd57a82007-07-11 23:06:47 +00007
mbligh9d1c7302007-10-07 18:53:13 +00008-m machine[,machine,machine] : specify machines to run on
9-c : assume control file is a client control file
mblighcc2c3a62007-10-12 00:34:31 +000010-b : Reboot machine at end of test run
mblighdcd57a82007-07-11 23:06:47 +000011"""
12
mblighc8949b82007-07-23 16:33:58 +000013__author__ = """
14mbligh@google.com (Martin J. Bligh),
15poirier@google.com (Benjamin Poirier),
16stutsman@google.com (Ryan Stutsman)
17"""
mblighdcd57a82007-07-11 23:06:47 +000018
mblighe2676852007-10-13 00:00:49 +000019import sys, os, re
mblighdcd57a82007-07-11 23:06:47 +000020
mbligh29aa9702007-08-09 22:41:43 +000021preamble = """\
22import os, sys
23
24import errors, hosts, autotest, kvm
25import source_kernel, rpm_kernel, deb_kernel
26from subcommand import *
mbligh03dd0792007-08-28 10:26:46 +000027from utils import run, get_tmp_dir, sh_escape
mbligh9d1c7302007-10-07 18:53:13 +000028
mbligh29aa9702007-08-09 22:41:43 +000029"""
30
mbligh9d1c7302007-10-07 18:53:13 +000031client_wrapper = """
32at = autotest.Autotest()
mbligh9ff1a3a2007-10-06 21:16:34 +000033
mbligh9d1c7302007-10-07 18:53:13 +000034def run_client(machine):
35 host = hosts.SSHHost(machine)
36 at.run(control, host=host)
37
38if len(machines) > 1:
39 parallel_simple(run_client, machines)
40else:
41 run_client(machines[0])
42"""
43
mblighcc2c3a62007-10-12 00:34:31 +000044cleanup="""\
45def cleanup(machine):
46 host = hosts.SSHHost(machine, initialize=False)
47 host.reboot()
48
49parallel_simple(cleanup, machines)
50"""
51
mbligh9d1c7302007-10-07 18:53:13 +000052# 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.
55sys.argv[0] = os.path.abspath(sys.argv[0])
56args = sys.argv[1:]
57
mblighcc2c3a62007-10-12 00:34:31 +000058def run(control_file, machines, args, client, reboot):
mbligh5fadbfe2007-10-06 21:12:06 +000059 namespace = dict({ 'machines' : machines, 'args' : args })
mbligh9d1c7302007-10-07 18:53:13 +000060 control = open(control_file, 'r').read()
mblighe2676852007-10-13 00:00:49 +000061 control = re.sub("\r\n", "\n", control)
mbligh9d1c7302007-10-07 18:53:13 +000062
mblighcc2c3a62007-10-12 00:34:31 +000063 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:
mblighe2676852007-10-13 00:00:49 +000071 exec(preamble + cleanup, namespace, namespace)
mbligh5fadbfe2007-10-06 21:12:06 +000072
mblighc99add62007-09-27 17:02:58 +000073def usage():
mblighcc2c3a62007-10-12 00:34:31 +000074 print "usage: autoserv [-b] [-c] <control file> [-m machine,[machine,...]] [args ...]"
mbligh9d1c7302007-10-07 18:53:13 +000075 sys.exit(1)
mblighdcd57a82007-07-11 23:06:47 +000076
mbligh5fadbfe2007-10-06 21:12:06 +000077
mbligh9d1c7302007-10-07 18:53:13 +000078# 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.
81if args.count('-m'):
82 idx = args.index('-m')
83 machines = args[idx+1].split(',')
84 args[idx:idx+2] = [] # delete '-m machines'
85else:
mbligh95fca572007-09-27 17:11:00 +000086 machines = None
mblighc99add62007-09-27 17:02:58 +000087
mbligh9d1c7302007-10-07 18:53:13 +000088if args.count('-c'):
89 client = True
90 idx = args.index('-c')
91 args[idx:idx+1] = [] # delete '-c'
92else:
93 client = False
mblighc99add62007-09-27 17:02:58 +000094
mblighcc2c3a62007-10-12 00:34:31 +000095if args.count('-b'):
96 reboot = True
97 idx = args.index('-b')
98 args[idx:idx+1] = [] # delete '-b'
99else:
100 reboot = False
101
mbligh9d1c7302007-10-07 18:53:13 +0000102if len(args) < 1:
103 usage()
mblighc99add62007-09-27 17:02:58 +0000104
mblighcc2c3a62007-10-12 00:34:31 +0000105run(args[0], machines, args[1:], client, reboot)