blob: 4fc5a3dbdad473434f9e6eddebcd32461504ad3f [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
mblighf36243d2007-10-30 15:36:16 +000054 [-m machine,[machine,...]] # list of machines to pass to control file
mbligh842c6592007-11-05 18:27:23 +000055 [-M machines_file] # list of machines (from a file)
mblighf1c52842007-10-16 15:21:38 +000056 [-c] # control file is a client side control
57 [-r resultsdir] # specify results directory (default '.')
mblighf36243d2007-10-30 15:36:16 +000058 [-i] # reinstall machines before running the job
59 [-I] # reinstall machines after running the job
60 [-b] # reboot all specified machines after the job
mbligh18420c22007-10-16 22:27:14 +000061 [-l label] # label for the job (arbitrary string)
mblighf1c52842007-10-16 15:21:38 +000062 [-u user] # username for the job (email address)
mbligh1d42d4e2007-11-05 22:42:00 +000063 [-v] # verify the machines only
64 [-R] # repair the machines
mblighe5cd0fd2008-01-26 23:07:31 +000065 [-n] # no teeing the status to stdout/stderr
mblighbb421852008-03-11 22:36:16 +000066 [-p] # write pidfile (.autoserv_execute)
mblighf1c52842007-10-16 15:21:38 +000067 <control file> # name of the control file to run
68 [args ...] # args to pass through to the control file
mbligh29aa9702007-08-09 22:41:43 +000069"""
70
mbligh9d1c7302007-10-07 18:53:13 +000071args = sys.argv[1:]
mbligh40f122a2007-11-03 23:08:46 +000072parser = utils.AutoservOptionParser(args)
mbligh9d1c7302007-10-07 18:53:13 +000073
mbligh74fc0462007-11-05 20:24:17 +000074# Get a useful value for running 'USER'
75realuser = os.environ.get('USER')
76if not realuser:
77 realuser = 'anonymous'
78
mbligh40f122a2007-11-03 23:08:46 +000079machines = parser.parse_opts_param('-m', None, split = ',')
mbligh842c6592007-11-05 18:27:23 +000080machines_file = parser.parse_opts_param('-M', None)
mbligh40f122a2007-11-03 23:08:46 +000081results = parser.parse_opts_param('-r', os.path.abspath('.'))
mbligh1d42d4e2007-11-05 22:42:00 +000082results = os.path.abspath(results)
mbligh40f122a2007-11-03 23:08:46 +000083label = parser.parse_opts_param('-l', '')
mbligh74fc0462007-11-05 20:24:17 +000084user = parser.parse_opts_param('-u', realuser)
mbligh40f122a2007-11-03 23:08:46 +000085client = parser.parse_opts('-c')
86reboot = parser.parse_opts('-b')
87install_before = parser.parse_opts('-i')
88install_after = parser.parse_opts('-I')
mbligh1d42d4e2007-11-05 22:42:00 +000089verify = parser.parse_opts('-v')
90repair = parser.parse_opts('-R')
mblighe5cd0fd2008-01-26 23:07:31 +000091no_tee = parser.parse_opts('-n')
mblighbb421852008-03-11 22:36:16 +000092write_pidfile = parser.parse_opts('-p')
mblighc99add62007-09-27 17:02:58 +000093
mblighc99add62007-09-27 17:02:58 +000094
mbligh1d42d4e2007-11-05 22:42:00 +000095if len(parser.args) < 1 and not verify and not repair:
mbligh4ef86232007-10-23 00:09:53 +000096 print usage
97 sys.exit(-1)
mblighc99add62007-09-27 17:02:58 +000098
mbligh842c6592007-11-05 18:27:23 +000099if machines_file:
100 machines = []
101 for m in open(machines_file, 'r').readlines():
102 m = re.sub('#.*', '', m).strip() # remove comments, spaces
103 if m:
104 machines.append(m)
105 print "Read list of machines from file: %s" % machines_file
106 print ','.join(machines)
107
mblighdbf37612007-11-24 19:38:11 +0000108if machines:
109 for machine in machines:
110 if not machine or re.search('\s', machine):
111 print "Invalid machine %s" % str(machine)
112 sys.exit(1)
mbligh535a4542008-02-11 16:26:36 +0000113 machines = list(set(machines))
114 machines.sort()
mblighdbf37612007-11-24 19:38:11 +0000115
mblighe25fd5b2008-01-22 17:23:37 +0000116# We have a control file unless it's just a verify/repair job
117if len(parser.args) > 0:
118 control = parser.args[0]
119else:
120 control = None
mbligh1d42d4e2007-11-05 22:42:00 +0000121
mblighe25fd5b2008-01-22 17:23:37 +0000122job = server_job.server_job(control, parser.args[1:], results, label,
mblighe8b37a92007-12-19 15:54:11 +0000123 user, machines, client)
mblighe5cd0fd2008-01-26 23:07:31 +0000124debug_dir = os.path.join(results, 'debug')
125if no_tee:
126 job.stdout.redirect(os.path.join(debug_dir, 'autoserv.stdout'))
127 job.stderr.redirect(os.path.join(debug_dir, 'autoserv.stderr'))
128else:
129 job.stdout.tee_redirect(os.path.join(debug_dir, 'autoserv.stdout'))
130 job.stderr.tee_redirect(os.path.join(debug_dir, 'autoserv.stderr'))
mblighfaf0cd42007-11-19 16:00:24 +0000131
mblighbb421852008-03-11 22:36:16 +0000132if write_pidfile:
133 pid_file_manager.open_pid_file(results)
mblighe25fd5b2008-01-22 17:23:37 +0000134
mblighbb421852008-03-11 22:36:16 +0000135# run the job
136exit_code = 0
mblighfaf0cd42007-11-19 16:00:24 +0000137try:
mblighbb421852008-03-11 22:36:16 +0000138 if repair:
139 job.repair()
140 elif verify:
141 job.verify()
142 else:
143 job.run(reboot, install_before, install_after)
mblighfaf0cd42007-11-19 16:00:24 +0000144except:
mbligh394ff512008-02-21 16:53:11 +0000145 job.aborted = True
mblighfaf0cd42007-11-19 16:00:24 +0000146 traceback.print_exc()
147
mblighfaf0cd42007-11-19 16:00:24 +0000148if getattr(job, 'aborted', False):
mblighbb421852008-03-11 22:36:16 +0000149 exit_code = 1
150pid_file_manager.close_pid_file(exit_code)
151
152sys.exit(exit_code)