blob: 0df92d020b25b59eca1f1fbbbf288727e9115000 [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"""
mblighf1c52842007-10-16 15:21:38 +00006Run an control file through the server side engine
mblighdcd57a82007-07-11 23:06:47 +00007"""
8
mblighf1c52842007-10-16 15:21:38 +00009__author__ = """\
10mbligh@google.com (Martin J. Bligh)
mblighc8949b82007-07-23 16:33:58 +000011"""
mblighdcd57a82007-07-11 23:06:47 +000012
mbligh40f122a2007-11-03 23:08:46 +000013import sys, os, re, server_job, hosts.site_host, utils
mblighdcd57a82007-07-11 23:06:47 +000014
mblighf1c52842007-10-16 15:21:38 +000015usage = """\
16usage: autoserv
mblighf36243d2007-10-30 15:36:16 +000017 [-m machine,[machine,...]] # list of machines to pass to control file
mbligh842c6592007-11-05 18:27:23 +000018 [-M machines_file] # list of machines (from a file)
mblighf1c52842007-10-16 15:21:38 +000019 [-c] # control file is a client side control
20 [-r resultsdir] # specify results directory (default '.')
mblighf36243d2007-10-30 15:36:16 +000021 [-i] # reinstall machines before running the job
22 [-I] # reinstall machines after running the job
23 [-b] # reboot all specified machines after the job
mbligh18420c22007-10-16 22:27:14 +000024 [-l label] # label for the job (arbitrary string)
mblighf1c52842007-10-16 15:21:38 +000025 [-u user] # username for the job (email address)
mblighf1c52842007-10-16 15:21:38 +000026 <control file> # name of the control file to run
27 [args ...] # args to pass through to the control file
mbligh29aa9702007-08-09 22:41:43 +000028"""
29
mbligh9d1c7302007-10-07 18:53:13 +000030args = sys.argv[1:]
mbligh40f122a2007-11-03 23:08:46 +000031parser = utils.AutoservOptionParser(args)
mbligh9d1c7302007-10-07 18:53:13 +000032
mbligh74fc0462007-11-05 20:24:17 +000033# Get a useful value for running 'USER'
34realuser = os.environ.get('USER')
35if not realuser:
36 realuser = 'anonymous'
37
mbligh40f122a2007-11-03 23:08:46 +000038machines = parser.parse_opts_param('-m', None, split = ',')
mbligh842c6592007-11-05 18:27:23 +000039machines_file = parser.parse_opts_param('-M', None)
mbligh40f122a2007-11-03 23:08:46 +000040results = parser.parse_opts_param('-r', os.path.abspath('.'))
41label = parser.parse_opts_param('-l', '')
mbligh74fc0462007-11-05 20:24:17 +000042user = parser.parse_opts_param('-u', realuser)
mbligh40f122a2007-11-03 23:08:46 +000043client = parser.parse_opts('-c')
44reboot = parser.parse_opts('-b')
45install_before = parser.parse_opts('-i')
46install_after = parser.parse_opts('-I')
mblighfdbe7d92007-11-05 18:15:57 +000047results = os.path.abspath(results)
mblighc99add62007-09-27 17:02:58 +000048
mbligh40f122a2007-11-03 23:08:46 +000049if getattr(hosts.site_host, 'site_parse_options', None):
50 hosts.site_host.site_parse_options(parser)
mblighc99add62007-09-27 17:02:58 +000051
mbligh40f122a2007-11-03 23:08:46 +000052if len(parser.args) < 1:
mbligh4ef86232007-10-23 00:09:53 +000053 print usage
54 sys.exit(-1)
mblighc99add62007-09-27 17:02:58 +000055
mbligh842c6592007-11-05 18:27:23 +000056if machines_file:
57 machines = []
58 for m in open(machines_file, 'r').readlines():
59 m = re.sub('#.*', '', m).strip() # remove comments, spaces
60 if m:
61 machines.append(m)
62 print "Read list of machines from file: %s" % machines_file
63 print ','.join(machines)
64
mbligh40f122a2007-11-03 23:08:46 +000065job = server_job.server_job(parser.args[0], parser.args[1:], results, label,
66 user, client)
mbligh3f4bced2007-11-05 17:55:53 +000067job.stdout.tee_redirect(os.path.join(results, 'debug', 'autoserv.stdout'))
68job.stderr.tee_redirect(os.path.join(results, 'debug', 'autoserv.stderr'))
mblighf36243d2007-10-30 15:36:16 +000069job.run(machines, reboot, install_before, install_after)