blob: 1239a65905cbc86888c7fe5eb85f5565746e9965 [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
mblighf7243e12008-03-05 16:01:21 +000016import sys, os, re, server_job, hosts.site_host, utils, traceback, signal
17
mblighbb421852008-03-11 22:36:16 +000018class PidFileManager(object):
19 pid_file = None
20
21 def open_pid_file(self, results_dir):
22 pid_file_path = os.path.join(results_dir, '.autoserv_execute')
23 assert not os.path.exists(pid_file_path)
24 self.pid_file = open(pid_file_path, 'w')
25 self.pid_file.write(str(os.getpid()) + '\n')
26 self.pid_file.flush()
27
28
29 def close_pid_file(self, exit_code, signal_code=0):
30 if not self.pid_file:
31 return
32 real_exit_code = (exit_code << 8) | (signal_code & 0xFF)
33 self.pid_file.write(str(real_exit_code) + '\n')
34 self.pid_file.close()
35
36
37pid_file_manager = PidFileManager()
38
39
mblighf7243e12008-03-05 16:01:21 +000040# Create separate process group
41os.setpgrp()
42
mblighbb421852008-03-11 22:36:16 +000043# Implement SIGTERM handler
mblighf7243e12008-03-05 16:01:21 +000044def handle_sigint(signum, frame):
mblighbb421852008-03-11 22:36:16 +000045 pid_file_manager.close_pid_file(1, signal.SIGTERM)
mbligh38c2d032008-03-07 00:26:57 +000046 os.killpg(os.getpgrp(), signal.SIGKILL)
mblighbb421852008-03-11 22:36:16 +000047
mblighf7243e12008-03-05 16:01:21 +000048# Set signal handler
49signal.signal(signal.SIGTERM, handle_sigint)
50
mblighdcd57a82007-07-11 23:06:47 +000051
mblighf1c52842007-10-16 15:21:38 +000052usage = """\
53usage: autoserv
mbligh9e618782008-03-13 15:35:09 +000054 [-h, --help] # This help message
mblighf36243d2007-10-30 15:36:16 +000055 [-m machine,[machine,...]] # list of machines to pass to control file
mbligh842c6592007-11-05 18:27:23 +000056 [-M machines_file] # list of machines (from a file)
mblighf1c52842007-10-16 15:21:38 +000057 [-c] # control file is a client side control
58 [-r resultsdir] # specify results directory (default '.')
mblighf36243d2007-10-30 15:36:16 +000059 [-i] # reinstall machines before running the job
60 [-I] # reinstall machines after running the job
61 [-b] # reboot all specified machines after the job
mbligh18420c22007-10-16 22:27:14 +000062 [-l label] # label for the job (arbitrary string)
mblighf1c52842007-10-16 15:21:38 +000063 [-u user] # username for the job (email address)
mbligh1d42d4e2007-11-05 22:42:00 +000064 [-v] # verify the machines only
65 [-R] # repair the machines
mblighe5cd0fd2008-01-26 23:07:31 +000066 [-n] # no teeing the status to stdout/stderr
mblighbb421852008-03-11 22:36:16 +000067 [-p] # write pidfile (.autoserv_execute)
mblighf1c52842007-10-16 15:21:38 +000068 <control file> # name of the control file to run
69 [args ...] # args to pass through to the control file
mbligh29aa9702007-08-09 22:41:43 +000070"""
71
mbligh9d1c7302007-10-07 18:53:13 +000072args = sys.argv[1:]
mbligh40f122a2007-11-03 23:08:46 +000073parser = utils.AutoservOptionParser(args)
mbligh9d1c7302007-10-07 18:53:13 +000074
mbligh74fc0462007-11-05 20:24:17 +000075# Get a useful value for running 'USER'
76realuser = os.environ.get('USER')
77if not realuser:
78 realuser = 'anonymous'
79
mbligh40f122a2007-11-03 23:08:46 +000080machines = parser.parse_opts_param('-m', None, split = ',')
mbligh842c6592007-11-05 18:27:23 +000081machines_file = parser.parse_opts_param('-M', None)
mbligh40f122a2007-11-03 23:08:46 +000082results = parser.parse_opts_param('-r', os.path.abspath('.'))
mbligh1d42d4e2007-11-05 22:42:00 +000083results = os.path.abspath(results)
mbligh40f122a2007-11-03 23:08:46 +000084label = parser.parse_opts_param('-l', '')
mbligh74fc0462007-11-05 20:24:17 +000085user = parser.parse_opts_param('-u', realuser)
mbligh40f122a2007-11-03 23:08:46 +000086client = parser.parse_opts('-c')
87reboot = parser.parse_opts('-b')
88install_before = parser.parse_opts('-i')
89install_after = parser.parse_opts('-I')
mbligh1d42d4e2007-11-05 22:42:00 +000090verify = parser.parse_opts('-v')
91repair = parser.parse_opts('-R')
mblighe5cd0fd2008-01-26 23:07:31 +000092no_tee = parser.parse_opts('-n')
mbligh9e618782008-03-13 15:35:09 +000093help = parser.parse_opts('-h') or parser.parse_opts('--help')
mblighbb421852008-03-11 22:36:16 +000094write_pidfile = parser.parse_opts('-p')
mblighc99add62007-09-27 17:02:58 +000095
mbligh9e618782008-03-13 15:35:09 +000096if help is True:
97 print usage
98 sys.exit(0)
mblighc99add62007-09-27 17:02:58 +000099
mbligh1d42d4e2007-11-05 22:42:00 +0000100if len(parser.args) < 1 and not verify and not repair:
mbligh4ef86232007-10-23 00:09:53 +0000101 print usage
102 sys.exit(-1)
mblighc99add62007-09-27 17:02:58 +0000103
mbligh842c6592007-11-05 18:27:23 +0000104if machines_file:
105 machines = []
106 for m in open(machines_file, 'r').readlines():
107 m = re.sub('#.*', '', m).strip() # remove comments, spaces
108 if m:
109 machines.append(m)
110 print "Read list of machines from file: %s" % machines_file
111 print ','.join(machines)
112
mblighdbf37612007-11-24 19:38:11 +0000113if machines:
114 for machine in machines:
115 if not machine or re.search('\s', machine):
116 print "Invalid machine %s" % str(machine)
117 sys.exit(1)
mbligh535a4542008-02-11 16:26:36 +0000118 machines = list(set(machines))
119 machines.sort()
mblighdbf37612007-11-24 19:38:11 +0000120
mblighe25fd5b2008-01-22 17:23:37 +0000121# We have a control file unless it's just a verify/repair job
122if len(parser.args) > 0:
123 control = parser.args[0]
124else:
125 control = None
mbligh1d42d4e2007-11-05 22:42:00 +0000126
mblighe25fd5b2008-01-22 17:23:37 +0000127job = server_job.server_job(control, parser.args[1:], results, label,
mblighe8b37a92007-12-19 15:54:11 +0000128 user, machines, client)
mblighe5cd0fd2008-01-26 23:07:31 +0000129debug_dir = os.path.join(results, 'debug')
130if no_tee:
131 job.stdout.redirect(os.path.join(debug_dir, 'autoserv.stdout'))
132 job.stderr.redirect(os.path.join(debug_dir, 'autoserv.stderr'))
133else:
134 job.stdout.tee_redirect(os.path.join(debug_dir, 'autoserv.stdout'))
135 job.stderr.tee_redirect(os.path.join(debug_dir, 'autoserv.stderr'))
mblighfaf0cd42007-11-19 16:00:24 +0000136
mblighbb421852008-03-11 22:36:16 +0000137if write_pidfile:
138 pid_file_manager.open_pid_file(results)
mblighe25fd5b2008-01-22 17:23:37 +0000139
mblighbb421852008-03-11 22:36:16 +0000140# run the job
141exit_code = 0
mblighfaf0cd42007-11-19 16:00:24 +0000142try:
mblighbb421852008-03-11 22:36:16 +0000143 if repair:
144 job.repair()
145 elif verify:
146 job.verify()
147 else:
148 job.run(reboot, install_before, install_after)
mblighfaf0cd42007-11-19 16:00:24 +0000149except:
mbligh394ff512008-02-21 16:53:11 +0000150 job.aborted = True
mblighfaf0cd42007-11-19 16:00:24 +0000151 traceback.print_exc()
152
mblighfaf0cd42007-11-19 16:00:24 +0000153if getattr(job, 'aborted', False):
mblighbb421852008-03-11 22:36:16 +0000154 exit_code = 1
155pid_file_manager.close_pid_file(exit_code)
156
157sys.exit(exit_code)