mbligh | 6203ace | 2007-10-04 21:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 2 | # Copyright 2007-2008 Martin J. Bligh <mbligh@google.com>, Google Inc. |
mbligh | 82648e5 | 2008-11-20 16:54:25 +0000 | [diff] [blame] | 3 | # Released under the GPL v2 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 4 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 5 | """ |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 6 | Run an control file through the server side engine |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 8 | |
mbligh | 7cd30fd | 2008-10-21 16:25:19 +0000 | [diff] [blame] | 9 | import sys, os, re, traceback, signal, time |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 10 | |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 11 | import common |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame^] | 12 | from autotest_lib.server import server_job, utils, autoserv_parser, autotest |
mbligh | 0b8c32d | 2008-10-29 16:46:04 +0000 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import debug |
| 14 | |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 15 | |
mbligh | 0b8c32d | 2008-10-29 16:46:04 +0000 | [diff] [blame] | 16 | debug.configure(module='server') |
mbligh | 29aa970 | 2007-08-09 22:41:43 +0000 | [diff] [blame] | 17 | |
mbligh | 92c0fc2 | 2008-11-20 16:52:23 +0000 | [diff] [blame] | 18 | |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 19 | class PidFileManager(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 20 | pid_file = None |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 21 | num_tests_failed = 0 |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 22 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 23 | def open_pid_file(self, results_dir): |
| 24 | pid_file_path = os.path.join(results_dir, '.autoserv_execute') |
| 25 | assert not os.path.exists(pid_file_path) |
| 26 | self.pid_file = open(pid_file_path, 'w') |
| 27 | self.pid_file.write(str(os.getpid()) + '\n') |
| 28 | self.pid_file.flush() |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 29 | |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 30 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 31 | def close_pid_file(self, exit_code, signal_code=0): |
| 32 | if not self.pid_file: |
| 33 | return |
| 34 | real_exit_code = (exit_code << 8) | (signal_code & 0xFF) |
| 35 | self.pid_file.write(str(real_exit_code) + '\n') |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 36 | self.pid_file.write(str(self.num_tests_failed) + '\n') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 37 | self.pid_file.close() |
| 38 | self.pid_file = None |
mbligh | c99add6 | 2007-09-27 17:02:58 +0000 | [diff] [blame] | 39 | |
mbligh | 842c659 | 2007-11-05 18:27:23 +0000 | [diff] [blame] | 40 | |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 41 | def run_autoserv(pid_file_manager, results, parser): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | # send stdin to /dev/null |
| 43 | dev_null = os.open(os.devnull, os.O_RDONLY) |
| 44 | os.dup2(dev_null, sys.stdin.fileno()) |
| 45 | os.close(dev_null) |
mbligh | dbf3761 | 2007-11-24 19:38:11 +0000 | [diff] [blame] | 46 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 47 | # Create separate process group |
| 48 | os.setpgrp() |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 49 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | # Implement SIGTERM handler |
| 51 | def handle_sigint(signum, frame): |
| 52 | pid_file_manager.close_pid_file(1, signal.SIGTERM) |
| 53 | os.killpg(os.getpgrp(), signal.SIGKILL) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 54 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 55 | # Set signal handler |
| 56 | signal.signal(signal.SIGTERM, handle_sigint) |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 57 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 58 | # Get a useful value for running 'USER' |
| 59 | realuser = os.environ.get('USER') |
| 60 | if not realuser: |
| 61 | realuser = 'anonymous' |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 62 | |
mbligh | cce191f | 2008-09-19 20:31:03 +0000 | [diff] [blame] | 63 | if parser.options.machines: |
| 64 | machines = parser.options.machines.replace(',', ' ').strip().split() |
| 65 | else: |
| 66 | machines = [] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 67 | machines_file = parser.options.machines_file |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 68 | label = parser.options.label |
| 69 | user = parser.options.user |
| 70 | client = parser.options.client |
| 71 | server = parser.options.server |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 72 | install_before = parser.options.install_before |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 73 | install_after = parser.options.install_after |
| 74 | verify = parser.options.verify |
| 75 | repair = parser.options.repair |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 76 | cleanup = parser.options.cleanup |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 77 | no_tee = parser.options.no_tee |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 78 | parse_job = parser.options.parse_job |
jadmanski | fbc1f0a | 2008-07-09 14:12:54 +0000 | [diff] [blame] | 79 | host_protection = parser.options.host_protection |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 80 | ssh_user = parser.options.ssh_user |
| 81 | ssh_port = parser.options.ssh_port |
| 82 | ssh_pass = parser.options.ssh_pass |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 83 | |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 84 | # can't be both a client and a server side test |
| 85 | if client and server: |
| 86 | print "Can not specify a test as both server and client!" |
| 87 | sys.exit(1) |
| 88 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 89 | if len(parser.args) < 1 and not (verify or repair or cleanup): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 90 | print parser.parser.print_help() |
| 91 | sys.exit(1) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 92 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 93 | # We have a control file unless it's just a verify/repair/cleanup job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 94 | if len(parser.args) > 0: |
| 95 | control = parser.args[0] |
| 96 | else: |
| 97 | control = None |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 98 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 99 | if machines_file: |
| 100 | machines = [] |
| 101 | for m in open(machines_file, 'r').readlines(): |
| 102 | # remove comments, spaces |
| 103 | m = re.sub('#.*', '', m).strip() |
| 104 | if m: |
| 105 | machines.append(m) |
| 106 | print "Read list of machines from file: %s" % machines_file |
| 107 | print ','.join(machines) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 108 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 109 | if machines: |
| 110 | for machine in machines: |
| 111 | if not machine or re.search('\s', machine): |
| 112 | print "Invalid machine %s" % str(machine) |
| 113 | sys.exit(1) |
| 114 | machines = list(set(machines)) |
| 115 | machines.sort() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 116 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 117 | job = server_job.server_job(control, parser.args[1:], results, label, |
| 118 | user, machines, client, parse_job, |
| 119 | ssh_user, ssh_port, ssh_pass) |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 120 | if results: |
| 121 | debug_dir = os.path.join(results, 'debug') |
| 122 | stdout = os.path.join(debug_dir, 'autoserv.stdout') |
| 123 | stderr = os.path.join(debug_dir, 'autoserv.stderr') |
| 124 | if no_tee: |
| 125 | job.stdout.redirect(stdout) |
| 126 | job.stderr.redirect(stderr) |
| 127 | else: |
| 128 | job.stdout.tee_redirect(stdout) |
| 129 | job.stderr.tee_redirect(stderr) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 130 | |
mbligh | 161fe6f | 2008-06-19 16:26:04 +0000 | [diff] [blame] | 131 | # perform checks |
| 132 | job.precheck() |
| 133 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 134 | # run the job |
| 135 | exit_code = 0 |
| 136 | try: |
| 137 | if repair: |
jadmanski | fbc1f0a | 2008-07-09 14:12:54 +0000 | [diff] [blame] | 138 | job.repair(host_protection) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | elif verify: |
| 140 | job.verify() |
| 141 | else: |
| 142 | try: |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 143 | job.run(cleanup, install_before, install_after) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 144 | finally: |
| 145 | job.cleanup_parser() |
jadmanski | 53aaf38 | 2008-11-17 16:22:31 +0000 | [diff] [blame] | 146 | while job.hosts: |
| 147 | host = job.hosts.pop() |
| 148 | host.close() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 149 | except: |
jadmanski | 27b37ea | 2008-10-29 23:54:31 +0000 | [diff] [blame] | 150 | exit_code = 1 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 151 | traceback.print_exc() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 152 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 153 | pid_file_manager.num_tests_failed = job.num_tests_failed |
| 154 | |
jadmanski | 27b37ea | 2008-10-29 23:54:31 +0000 | [diff] [blame] | 155 | sys.exit(exit_code) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | def main(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 159 | pid_file_manager = PidFileManager() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 160 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 161 | # grab the parser |
| 162 | parser = autoserv_parser.autoserv_parser |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 163 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | if len(sys.argv) == 1: |
| 165 | parser.parser.print_help() |
| 166 | sys.exit(1) |
mbligh | a6f1308 | 2008-06-05 23:53:46 +0000 | [diff] [blame] | 167 | |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 168 | results = parser.options.results |
| 169 | if not parser.options.no_logging: |
| 170 | if not results: |
| 171 | results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S') |
| 172 | results = os.path.abspath(results) |
| 173 | if os.path.exists(os.path.join(results, 'control.srv')): |
| 174 | error = "Error: results directory already exists: %s\n" % results |
| 175 | sys.stderr.write(error) |
| 176 | sys.exit(1) |
| 177 | print "Results placed in %s" % results |
mbligh | 1071763 | 2008-11-19 00:21:57 +0000 | [diff] [blame] | 178 | |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 179 | if parser.options.write_pidfile: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 180 | pid_file_manager.open_pid_file(results) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 181 | |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame^] | 182 | autotest.BaseAutotest.set_install_in_tmpdir( |
| 183 | parser.options.install_in_tmpdir) |
| 184 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 185 | exit_code = 0 |
| 186 | try: |
| 187 | try: |
mbligh | 1071763 | 2008-11-19 00:21:57 +0000 | [diff] [blame] | 188 | run_autoserv(pid_file_manager, results, parser) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 189 | except SystemExit, e: |
| 190 | exit_code = e.code |
| 191 | except: |
| 192 | traceback.print_exc() |
| 193 | # If we don't know what happened, we'll classify it as |
| 194 | # an 'abort' and return 1. |
| 195 | exit_code = 1 |
| 196 | finally: |
| 197 | pid_file_manager.close_pid_file(exit_code) |
| 198 | sys.exit(exit_code) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 199 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 200 | |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 201 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | main() |