blob: 1ede1d4294ded6a89b94dbdca21ed5813047fad9 [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
mbligh0c3548d2008-02-01 18:08:53 +000013from common.check_version import check_python_version
14check_python_version()
15
mblighfaf0cd42007-11-19 16:00:24 +000016import sys, os, re, server_job, hosts.site_host, utils, traceback
mblighdcd57a82007-07-11 23:06:47 +000017
mblighf1c52842007-10-16 15:21:38 +000018usage = """\
19usage: autoserv
mblighf36243d2007-10-30 15:36:16 +000020 [-m machine,[machine,...]] # list of machines to pass to control file
mbligh842c6592007-11-05 18:27:23 +000021 [-M machines_file] # list of machines (from a file)
mblighf1c52842007-10-16 15:21:38 +000022 [-c] # control file is a client side control
23 [-r resultsdir] # specify results directory (default '.')
mblighf36243d2007-10-30 15:36:16 +000024 [-i] # reinstall machines before running the job
25 [-I] # reinstall machines after running the job
26 [-b] # reboot all specified machines after the job
mbligh18420c22007-10-16 22:27:14 +000027 [-l label] # label for the job (arbitrary string)
mblighf1c52842007-10-16 15:21:38 +000028 [-u user] # username for the job (email address)
mbligh1d42d4e2007-11-05 22:42:00 +000029 [-v] # verify the machines only
30 [-R] # repair the machines
mblighe5cd0fd2008-01-26 23:07:31 +000031 [-n] # no teeing the status to stdout/stderr
mblighf1c52842007-10-16 15:21:38 +000032 <control file> # name of the control file to run
33 [args ...] # args to pass through to the control file
mbligh29aa9702007-08-09 22:41:43 +000034"""
35
mbligh9d1c7302007-10-07 18:53:13 +000036args = sys.argv[1:]
mbligh40f122a2007-11-03 23:08:46 +000037parser = utils.AutoservOptionParser(args)
mbligh9d1c7302007-10-07 18:53:13 +000038
mbligh74fc0462007-11-05 20:24:17 +000039# Get a useful value for running 'USER'
40realuser = os.environ.get('USER')
41if not realuser:
42 realuser = 'anonymous'
43
mbligh40f122a2007-11-03 23:08:46 +000044machines = parser.parse_opts_param('-m', None, split = ',')
mbligh842c6592007-11-05 18:27:23 +000045machines_file = parser.parse_opts_param('-M', None)
mbligh40f122a2007-11-03 23:08:46 +000046results = parser.parse_opts_param('-r', os.path.abspath('.'))
mbligh1d42d4e2007-11-05 22:42:00 +000047results = os.path.abspath(results)
mbligh40f122a2007-11-03 23:08:46 +000048label = parser.parse_opts_param('-l', '')
mbligh74fc0462007-11-05 20:24:17 +000049user = parser.parse_opts_param('-u', realuser)
mbligh40f122a2007-11-03 23:08:46 +000050client = parser.parse_opts('-c')
51reboot = parser.parse_opts('-b')
52install_before = parser.parse_opts('-i')
53install_after = parser.parse_opts('-I')
mbligh1d42d4e2007-11-05 22:42:00 +000054verify = parser.parse_opts('-v')
55repair = parser.parse_opts('-R')
mblighe5cd0fd2008-01-26 23:07:31 +000056no_tee = parser.parse_opts('-n')
mblighc99add62007-09-27 17:02:58 +000057
mblighc99add62007-09-27 17:02:58 +000058
mbligh1d42d4e2007-11-05 22:42:00 +000059if len(parser.args) < 1 and not verify and not repair:
mbligh4ef86232007-10-23 00:09:53 +000060 print usage
61 sys.exit(-1)
mblighc99add62007-09-27 17:02:58 +000062
mbligh842c6592007-11-05 18:27:23 +000063if 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
mblighdbf37612007-11-24 19:38:11 +000072if 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)
mbligh535a4542008-02-11 16:26:36 +000077 machines = list(set(machines))
78 machines.sort()
mblighdbf37612007-11-24 19:38:11 +000079
mblighe25fd5b2008-01-22 17:23:37 +000080# We have a control file unless it's just a verify/repair job
81if len(parser.args) > 0:
82 control = parser.args[0]
83else:
84 control = None
mbligh1d42d4e2007-11-05 22:42:00 +000085
mblighe25fd5b2008-01-22 17:23:37 +000086job = server_job.server_job(control, parser.args[1:], results, label,
mblighe8b37a92007-12-19 15:54:11 +000087 user, machines, client)
mblighe5cd0fd2008-01-26 23:07:31 +000088debug_dir = os.path.join(results, 'debug')
89if no_tee:
90 job.stdout.redirect(os.path.join(debug_dir, 'autoserv.stdout'))
91 job.stderr.redirect(os.path.join(debug_dir, 'autoserv.stderr'))
92else:
93 job.stdout.tee_redirect(os.path.join(debug_dir, 'autoserv.stdout'))
94 job.stderr.tee_redirect(os.path.join(debug_dir, 'autoserv.stderr'))
mblighfaf0cd42007-11-19 16:00:24 +000095
mblighe25fd5b2008-01-22 17:23:37 +000096if repair:
97 job.repair()
98 sys.exit(0)
99elif verify:
100 job.verify()
101 sys.exit(0)
102
mblighfaf0cd42007-11-19 16:00:24 +0000103try:
mblighe8b37a92007-12-19 15:54:11 +0000104 job.run(reboot, install_before, install_after)
mblighfaf0cd42007-11-19 16:00:24 +0000105except:
mbligh394ff512008-02-21 16:53:11 +0000106 job.aborted = True
mblighfaf0cd42007-11-19 16:00:24 +0000107 traceback.print_exc()
108
109# if the job was aborted, return a non-zero error code
110if getattr(job, 'aborted', False):
111 sys.exit(1)