mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | """ |
| 4 | Autotest scheduler |
| 5 | """ |
| 6 | __author__ = "Paul Turner <pjt@google.com>" |
| 7 | |
| 8 | import os, sys, tempfile, shutil, MySQLdb, time, traceback, subprocess, Queue |
mbligh | cadb353 | 2008-04-15 17:46:26 +0000 | [diff] [blame] | 9 | import optparse, signal, smtplib, socket, datetime, stat, pwd, errno |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 10 | from common import global_config |
| 11 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 12 | RESULTS_DIR = '.' |
| 13 | AUTOSERV_NICE_LEVEL = 10 |
| 14 | |
| 15 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 16 | |
| 17 | if os.environ.has_key('AUTOTEST_DIR'): |
| 18 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
| 19 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 20 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 21 | |
| 22 | if AUTOTEST_SERVER_DIR not in sys.path: |
| 23 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
| 24 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 25 | AUTOSERV_PID_FILE = '.autoserv_execute' |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 26 | # how long to wait for autoserv to write a pidfile |
| 27 | PIDFILE_TIMEOUT = 5 * 60 # 5 min |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 28 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 29 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 30 | _shutdown = False |
| 31 | _notify_email = None |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 32 | _autoserv_path = 'autoserv' |
| 33 | _testing_mode = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def main(): |
| 37 | usage = 'usage: %prog [options] results_dir' |
| 38 | |
| 39 | parser = optparse.OptionParser(usage) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 40 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 41 | action='store_true') |
| 42 | parser.add_option('--logfile', help='Set a log file that all stdout ' + |
| 43 | 'should be redirected to. Stderr will go to this ' + |
| 44 | 'file + ".err"') |
| 45 | parser.add_option('--notify', help='Set an email address to be ' + |
| 46 | 'notified of exceptions') |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 47 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 48 | 'test and should use dummy autoserv and no parsing', |
| 49 | action='store_true') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 50 | (options, args) = parser.parse_args() |
| 51 | if len(args) != 1: |
| 52 | parser.print_usage() |
| 53 | return |
| 54 | |
| 55 | global RESULTS_DIR |
| 56 | RESULTS_DIR = args[0] |
| 57 | |
| 58 | global _notify_email |
| 59 | _notify_email = options.notify |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 60 | |
| 61 | if options.test: |
| 62 | global _autoserv_path |
| 63 | _autoserv_path = 'autoserv_dummy' |
| 64 | global _testing_mode |
| 65 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 66 | |
| 67 | init(options.logfile) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 68 | dispatcher = Dispatcher() |
| 69 | dispatcher.do_initial_recovery(recover_hosts=options.recover_hosts) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 70 | |
| 71 | try: |
| 72 | while not _shutdown: |
| 73 | dispatcher.tick() |
| 74 | time.sleep(20) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 75 | except: |
| 76 | log_stacktrace("Uncaught exception; terminating monitor_db") |
| 77 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 78 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def handle_sigint(signum, frame): |
| 82 | global _shutdown |
| 83 | _shutdown = True |
| 84 | print "Shutdown request received." |
| 85 | |
| 86 | |
| 87 | def init(logfile): |
| 88 | if logfile: |
| 89 | enable_logging(logfile) |
| 90 | print "%s> dispatcher starting" % time.strftime("%X %x") |
| 91 | print "My PID is %d" % os.getpid() |
| 92 | |
| 93 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 94 | global _db |
| 95 | _db = DatabaseConn() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 96 | |
| 97 | print "Setting signal handler" |
| 98 | signal.signal(signal.SIGINT, handle_sigint) |
| 99 | |
| 100 | print "Connected! Running..." |
| 101 | |
| 102 | |
| 103 | def enable_logging(logfile): |
| 104 | out_file = logfile |
| 105 | err_file = "%s.err" % logfile |
| 106 | print "Enabling logging to %s (%s)" % (out_file, err_file) |
| 107 | out_fd = open(out_file, "a", buffering=0) |
| 108 | err_fd = open(err_file, "a", buffering=0) |
| 109 | |
| 110 | os.dup2(out_fd.fileno(), sys.stdout.fileno()) |
| 111 | os.dup2(err_fd.fileno(), sys.stderr.fileno()) |
| 112 | |
| 113 | sys.stdout = out_fd |
| 114 | sys.stderr = err_fd |
| 115 | |
| 116 | |
| 117 | def idle_hosts(): |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 118 | rows = _db.execute(""" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 119 | SELECT * FROM hosts h WHERE |
| 120 | id NOT IN (SELECT host_id FROM host_queue_entries WHERE active) AND ( |
| 121 | (id IN (SELECT host_id FROM host_queue_entries WHERE not complete AND not active)) |
| 122 | OR |
| 123 | (id IN (SELECT DISTINCT hl.host_id FROM host_queue_entries hqe |
| 124 | INNER JOIN hosts_labels hl ON hqe.meta_host=hl.label_id WHERE not hqe.complete AND not hqe.active)) |
| 125 | ) |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 126 | AND locked=false AND invalid=false |
| 127 | AND (h.status IS null OR h.status='Ready') """) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 128 | hosts = [Host(row=i) for i in rows] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 129 | return hosts |
| 130 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 131 | def queue_entries_to_abort(): |
| 132 | rows = _db.execute(""" |
| 133 | SELECT * FROM host_queue_entries WHERE status='Abort'; |
| 134 | """) |
| 135 | qe = [HostQueueEntry(row=i) for i in rows] |
| 136 | return qe |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 137 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 138 | def remove_file_or_dir(path): |
| 139 | if stat.S_ISDIR(os.stat(path).st_mode): |
| 140 | # directory |
| 141 | shutil.rmtree(path) |
| 142 | else: |
| 143 | # file |
| 144 | os.remove(path) |
| 145 | |
| 146 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 147 | class DatabaseConn: |
| 148 | def __init__(self): |
| 149 | self.reconnect_wait = 20 |
| 150 | self.conn = None |
| 151 | self.cur = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 152 | |
mbligh | 4eb2df2 | 2008-03-13 15:39:29 +0000 | [diff] [blame] | 153 | import MySQLdb.converters |
| 154 | self.convert_dict = MySQLdb.converters.conversions |
| 155 | self.convert_dict.setdefault(bool, self.convert_boolean) |
| 156 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 157 | self.connect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 158 | |
| 159 | |
mbligh | 4eb2df2 | 2008-03-13 15:39:29 +0000 | [diff] [blame] | 160 | @staticmethod |
| 161 | def convert_boolean(boolean, conversion_dict): |
| 162 | 'Convert booleans to integer strings' |
| 163 | return str(int(boolean)) |
| 164 | |
| 165 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 166 | def connect(self): |
| 167 | self.disconnect() |
| 168 | |
| 169 | # get global config and parse for info |
| 170 | c = global_config.global_config |
| 171 | dbase = "AUTOTEST_WEB" |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 172 | DB_HOST = c.get_config_value(dbase, "host") |
| 173 | DB_SCHEMA = c.get_config_value(dbase, "database") |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 174 | |
| 175 | global _testing_mode |
| 176 | if _testing_mode: |
| 177 | DB_SCHEMA = 'stresstest_autotest_web' |
| 178 | |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 179 | DB_USER = c.get_config_value(dbase, "user") |
| 180 | DB_PASS = c.get_config_value(dbase, "password") |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 181 | |
| 182 | while not self.conn: |
| 183 | try: |
mbligh | 4eb2df2 | 2008-03-13 15:39:29 +0000 | [diff] [blame] | 184 | self.conn = MySQLdb.connect( |
| 185 | host=DB_HOST, user=DB_USER, passwd=DB_PASS, |
| 186 | db=DB_SCHEMA, conv=self.convert_dict) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 187 | |
| 188 | self.conn.autocommit(True) |
| 189 | self.cur = self.conn.cursor() |
| 190 | except MySQLdb.OperationalError: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 191 | traceback.print_exc() |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 192 | print "Can't connect to MYSQL; reconnecting" |
| 193 | time.sleep(self.reconnect_wait) |
| 194 | self.disconnect() |
| 195 | |
| 196 | |
| 197 | def disconnect(self): |
| 198 | if self.conn: |
| 199 | self.conn.close() |
| 200 | self.conn = None |
| 201 | self.cur = None |
| 202 | |
| 203 | |
| 204 | def execute(self, *args, **dargs): |
| 205 | while (True): |
| 206 | try: |
| 207 | self.cur.execute(*args, **dargs) |
| 208 | return self.cur.fetchall() |
| 209 | except MySQLdb.OperationalError: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 210 | traceback.print_exc() |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 211 | print "MYSQL connection died; reconnecting" |
| 212 | time.sleep(self.reconnect_wait) |
| 213 | self.connect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 214 | |
| 215 | |
mbligh | dbdac6c | 2008-03-05 15:49:58 +0000 | [diff] [blame] | 216 | def generate_parse_command(results_dir, flags=""): |
| 217 | parse = os.path.abspath(os.path.join(AUTOTEST_TKO_DIR, 'parse')) |
| 218 | output = os.path.abspath(os.path.join(results_dir, '.parse.log')) |
| 219 | cmd = "%s %s -r -o %s > %s 2>&1 &" |
| 220 | return cmd % (parse, flags, results_dir, output) |
| 221 | |
| 222 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 223 | def parse_results(results_dir, flags=""): |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 224 | if _testing_mode: |
| 225 | return |
mbligh | dbdac6c | 2008-03-05 15:49:58 +0000 | [diff] [blame] | 226 | os.system(generate_parse_command(results_dir, flags)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 227 | |
| 228 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 229 | def send_notify_email(subject, message): |
| 230 | if not _notify_email: |
| 231 | return |
| 232 | |
| 233 | message = "%s / %s / %s\n%s" % (socket.gethostname(), os.getpid(), |
| 234 | time.strftime("%X %x"), message) |
| 235 | sender = pwd.getpwuid(os.getuid())[0] # see os.getlogin() online docs |
| 236 | msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % ( |
| 237 | sender, _notify_email, subject, message) |
| 238 | mailer = smtplib.SMTP('localhost') |
| 239 | mailer.sendmail(sender, _notify_email, msg) |
| 240 | mailer.quit() |
| 241 | |
| 242 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 243 | def log_stacktrace(reason): |
| 244 | (type, value, tb) = sys.exc_info() |
| 245 | str = "EXCEPTION: %s\n" % reason |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 246 | str += ''.join(traceback.format_exception(type, value, tb)) |
| 247 | |
| 248 | sys.stderr.write("\n%s\n" % str) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 249 | send_notify_email("monitor_db exception", str) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 250 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 251 | |
| 252 | def get_proc_poll_fn(pid): |
| 253 | proc_path = os.path.join('/proc', str(pid)) |
| 254 | def poll_fn(): |
| 255 | if os.path.exists(proc_path): |
| 256 | return None |
| 257 | return 0 # we can't get a real exit code |
| 258 | return poll_fn |
| 259 | |
| 260 | |
| 261 | def kill_autoserv(pid, poll_fn=None): |
| 262 | print 'killing', pid |
| 263 | if poll_fn is None: |
| 264 | poll_fn = get_proc_poll_fn(pid) |
| 265 | if poll_fn() == None: |
| 266 | os.kill(pid, signal.SIGCONT) |
| 267 | os.kill(pid, signal.SIGTERM) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | class Dispatcher: |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 271 | autoserv_procs_cache = None |
| 272 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 273 | def __init__(self): |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 274 | self._agents = [] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 275 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 276 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 277 | def do_initial_recovery(self, recover_hosts=True): |
| 278 | # always recover processes |
| 279 | self._recover_processes() |
| 280 | |
| 281 | if recover_hosts: |
| 282 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 283 | |
| 284 | |
| 285 | def tick(self): |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 286 | Dispatcher.autoserv_procs_cache = None |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 287 | self._find_aborting() |
| 288 | self._find_more_work() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 289 | self._handle_agents() |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame^] | 290 | self._clear_inactive_blocks() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 291 | |
| 292 | |
| 293 | def add_agent(self, agent): |
| 294 | self._agents.append(agent) |
| 295 | agent.dispatcher = self |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 296 | |
| 297 | # Find agent corresponding to the specified queue_entry |
| 298 | def get_agents(self, queue_entry): |
| 299 | res_agents = [] |
| 300 | for agent in self._agents: |
| 301 | if queue_entry.id in agent.queue_entry_ids: |
| 302 | res_agents.append(agent) |
| 303 | return res_agents |
| 304 | |
| 305 | |
| 306 | def remove_agent(self, agent): |
| 307 | self._agents.remove(agent) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 308 | |
| 309 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 310 | @classmethod |
| 311 | def find_autoservs(cls, orphans_only=False): |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 312 | """\ |
| 313 | Returns a dict mapping pids to command lines for root autoserv |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 314 | processes. If orphans_only=True, return only processes that |
| 315 | have been orphaned (i.e. parent pid = 1). |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 316 | """ |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 317 | if cls.autoserv_procs_cache is not None: |
| 318 | return cls.autoserv_procs_cache |
| 319 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 320 | proc = subprocess.Popen( |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 321 | ['/bin/ps', 'x', '-o', 'pid,pgid,ppid,comm,args'], |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 322 | stdout=subprocess.PIPE) |
| 323 | # split each line into the four columns output by ps |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 324 | procs = [line.split(None, 4) for line in |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 325 | proc.communicate()[0].splitlines()] |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 326 | autoserv_procs = {} |
| 327 | for proc in procs: |
| 328 | # check ppid == 1 for orphans |
| 329 | if orphans_only and proc[2] != 1: |
| 330 | continue |
| 331 | # only root autoserv processes have pgid == pid |
| 332 | if (proc[3] == 'autoserv' and # comm |
| 333 | proc[1] == proc[0]): # pgid == pid |
| 334 | # map pid to args |
| 335 | autoserv_procs[int(proc[0])] = proc[4] |
| 336 | cls.autoserv_procs_cache = autoserv_procs |
| 337 | return autoserv_procs |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 338 | |
| 339 | |
| 340 | def recover_queue_entry(self, queue_entry, run_monitor): |
| 341 | job = queue_entry.job |
| 342 | if job.is_synchronous(): |
| 343 | all_queue_entries = job.get_host_queue_entries() |
| 344 | else: |
| 345 | all_queue_entries = [queue_entry] |
| 346 | all_queue_entry_ids = [queue_entry.id for queue_entry |
| 347 | in all_queue_entries] |
| 348 | queue_task = RecoveryQueueTask( |
| 349 | job=queue_entry.job, |
| 350 | queue_entries=all_queue_entries, |
| 351 | run_monitor=run_monitor) |
| 352 | self.add_agent(Agent(tasks=[queue_task], |
| 353 | queue_entry_ids=all_queue_entry_ids)) |
| 354 | |
| 355 | |
| 356 | def _recover_processes(self): |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 357 | orphans = self.find_autoservs(orphans_only=True) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 358 | |
| 359 | # first, recover running queue entries |
| 360 | rows = _db.execute("""SELECT * FROM host_queue_entries |
| 361 | WHERE status = 'Running'""") |
| 362 | queue_entries = [HostQueueEntry(row=i) for i in rows] |
| 363 | requeue_entries = [] |
| 364 | recovered_entry_ids = set() |
| 365 | for queue_entry in queue_entries: |
| 366 | run_monitor = PidfileRunMonitor( |
| 367 | queue_entry.results_dir()) |
| 368 | pid, exit_code = run_monitor.get_pidfile_info() |
| 369 | if pid is None: |
| 370 | # autoserv apparently never got run, so requeue |
| 371 | requeue_entries.append(queue_entry) |
| 372 | continue |
| 373 | if queue_entry.id in recovered_entry_ids: |
| 374 | # synchronous job we've already recovered |
| 375 | continue |
| 376 | print 'Recovering queue entry %d (pid %d)' % ( |
| 377 | queue_entry.id, pid) |
| 378 | job = queue_entry.job |
| 379 | if job.is_synchronous(): |
| 380 | for entry in job.get_host_queue_entries(): |
| 381 | assert entry.active |
| 382 | recovered_entry_ids.add(entry.id) |
| 383 | self.recover_queue_entry(queue_entry, |
| 384 | run_monitor) |
| 385 | orphans.pop(pid, None) |
| 386 | |
| 387 | # and requeue other active queue entries |
| 388 | rows = _db.execute("""SELECT * FROM host_queue_entries |
| 389 | WHERE active AND NOT complete |
| 390 | AND status != 'Running' |
| 391 | AND status != 'Pending' |
| 392 | AND status != 'Abort' |
| 393 | AND status != 'Aborting'""") |
| 394 | queue_entries = [HostQueueEntry(row=i) for i in rows] |
| 395 | for queue_entry in queue_entries + requeue_entries: |
| 396 | print 'Requeuing running QE %d' % queue_entry.id |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 397 | queue_entry.clear_results_dir(dont_delete_files=True) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 398 | queue_entry.requeue() |
| 399 | |
| 400 | |
| 401 | # now kill any remaining autoserv processes |
| 402 | for pid in orphans.keys(): |
| 403 | print 'Killing orphan %d (%s)' % (pid, orphans[pid]) |
| 404 | kill_autoserv(pid) |
| 405 | |
| 406 | # recover aborting tasks |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 407 | rebooting_host_ids = set() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 408 | rows = _db.execute("""SELECT * FROM host_queue_entries |
| 409 | WHERE status='Abort' or status='Aborting'""") |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 410 | queue_entries = [HostQueueEntry(row=i) for i in rows] |
| 411 | for queue_entry in queue_entries: |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 412 | print 'Recovering aborting QE %d' % queue_entry.id |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 413 | queue_host = queue_entry.get_host() |
| 414 | reboot_task = RebootTask(queue_host) |
| 415 | verify_task = VerifyTask(host = queue_host) |
| 416 | self.add_agent(Agent(tasks=[reboot_task, |
| 417 | verify_task], |
| 418 | queue_entry_ids=[queue_entry.id])) |
| 419 | queue_entry.set_status('Aborted') |
| 420 | # Secure the host from being picked up |
| 421 | queue_host.set_status('Rebooting') |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 422 | rebooting_host_ids.add(queue_host.id) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 423 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 424 | # reverify hosts that were in the middle of verify, repair or |
| 425 | # reboot |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 426 | self._reverify_hosts_where("""(status = 'Repairing' OR |
| 427 | status = 'Verifying' OR |
| 428 | status = 'Rebooting')""", |
| 429 | exclude_ids=rebooting_host_ids) |
| 430 | |
| 431 | # finally, recover "Running" hosts with no active queue entries, |
| 432 | # although this should never happen |
| 433 | message = ('Recovering running host %s - this probably ' |
| 434 | 'indicates a scheduler bug') |
| 435 | self._reverify_hosts_where("""status = 'Running' AND |
| 436 | id NOT IN (SELECT host_id |
| 437 | FROM host_queue_entries |
| 438 | WHERE active)""", |
| 439 | print_message=message) |
| 440 | |
| 441 | |
| 442 | def _reverify_hosts_where(self, where, |
| 443 | print_message='Reverifying host %s', |
| 444 | exclude_ids=set()): |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 445 | rows = _db.execute('SELECT * FROM hosts WHERE locked = 0 AND ' |
| 446 | 'invalid = 0 AND ' + where) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 447 | hosts = [Host(row=i) for i in rows] |
| 448 | for host in hosts: |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 449 | if host.id in exclude_ids: |
| 450 | continue |
| 451 | if print_message is not None: |
| 452 | print print_message % host.hostname |
| 453 | verify_task = VerifyTask(host = host) |
| 454 | self.add_agent(Agent(tasks = [verify_task])) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | def _recover_hosts(self): |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 458 | # recover "Repair Failed" hosts |
| 459 | message = 'Reverifying dead host %s' |
| 460 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 461 | print_message=message) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 462 | |
| 463 | |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame^] | 464 | def _clear_inactive_blocks(self): |
| 465 | """ |
| 466 | Clear out blocks for all completed jobs. |
| 467 | """ |
| 468 | # this would be simpler using NOT IN (subquery), but MySQL |
| 469 | # treats all IN subqueries as dependent, so this optimizes much |
| 470 | # better |
| 471 | _db.execute(""" |
| 472 | DELETE ihq FROM ineligible_host_queues ihq |
| 473 | LEFT JOIN (SELECT job_id FROM host_queue_entries |
| 474 | WHERE NOT complete) hqe |
| 475 | USING (job_id) WHERE hqe.job_id IS NULL""") |
| 476 | |
| 477 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 478 | def _find_more_work(self): |
| 479 | print "finding work" |
| 480 | |
| 481 | num_started = 0 |
| 482 | for host in idle_hosts(): |
| 483 | tasks = host.next_queue_entries() |
| 484 | if tasks: |
| 485 | for next in tasks: |
| 486 | try: |
| 487 | agent = next.run(assigned_host=host) |
| 488 | if agent: |
| 489 | self.add_agent(agent) |
| 490 | |
| 491 | num_started += 1 |
| 492 | if num_started>=100: |
| 493 | return |
| 494 | break |
| 495 | except: |
| 496 | next.set_status('Failed') |
| 497 | |
| 498 | # if next.host: |
| 499 | # next.host.set_status('Ready') |
| 500 | |
| 501 | log_stacktrace("task_id = %d" % next.id) |
| 502 | |
| 503 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 504 | def _find_aborting(self): |
| 505 | num_aborted = 0 |
| 506 | # Find jobs that are aborting |
| 507 | for entry in queue_entries_to_abort(): |
| 508 | agents_to_abort = self.get_agents(entry) |
| 509 | entry_host = entry.get_host() |
| 510 | reboot_task = RebootTask(entry_host) |
| 511 | verify_task = VerifyTask(host = entry_host) |
| 512 | tasks = [reboot_task, verify_task] |
| 513 | if agents_to_abort: |
| 514 | abort_task = AbortTask(entry, agents_to_abort) |
| 515 | tasks.insert(0, abort_task) |
| 516 | else: |
| 517 | entry.set_status('Aborted') |
| 518 | # just to make sure this host does not get |
| 519 | # taken away |
| 520 | entry_host.set_status('Rebooting') |
| 521 | self.add_agent(Agent(tasks=tasks, |
| 522 | queue_entry_ids = [entry.id])) |
| 523 | num_aborted += 1 |
| 524 | if num_aborted >= 50: |
| 525 | break |
| 526 | |
| 527 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 528 | def _handle_agents(self): |
| 529 | still_running = [] |
| 530 | for agent in self._agents: |
| 531 | agent.tick() |
| 532 | if not agent.is_done(): |
| 533 | still_running.append(agent) |
| 534 | else: |
| 535 | print "agent finished" |
| 536 | self._agents = still_running |
| 537 | |
| 538 | |
| 539 | class RunMonitor(object): |
| 540 | def __init__(self, cmd, nice_level = None, log_file = None): |
| 541 | self.nice_level = nice_level |
| 542 | self.log_file = log_file |
| 543 | self.proc = self.run(cmd) |
| 544 | |
| 545 | def run(self, cmd): |
| 546 | if self.nice_level: |
| 547 | nice_cmd = ['nice','-n', str(self.nice_level)] |
| 548 | nice_cmd.extend(cmd) |
| 549 | cmd = nice_cmd |
| 550 | |
| 551 | out_file = None |
| 552 | if self.log_file: |
| 553 | try: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 554 | os.makedirs(os.path.dirname(self.log_file)) |
mbligh | cadb353 | 2008-04-15 17:46:26 +0000 | [diff] [blame] | 555 | except OSError, exc: |
| 556 | if exc.errno != errno.EEXIST: |
| 557 | log_stacktrace( |
| 558 | 'Unexpected error creating logfile ' |
| 559 | 'directory for %s' % self.log_file) |
| 560 | try: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 561 | out_file = open(self.log_file, 'a') |
| 562 | out_file.write("\n%s\n" % ('*'*80)) |
mbligh | cadb353 | 2008-04-15 17:46:26 +0000 | [diff] [blame] | 563 | out_file.write("%s> %s\n" % |
| 564 | (time.strftime("%X %x"), cmd)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 565 | out_file.write("%s\n" % ('*'*80)) |
mbligh | cadb353 | 2008-04-15 17:46:26 +0000 | [diff] [blame] | 566 | except (OSError, IOError): |
| 567 | log_stacktrace('Error opening log file %s' % |
| 568 | self.log_file) |
| 569 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 570 | if not out_file: |
| 571 | out_file = open('/dev/null', 'w') |
mbligh | cadb353 | 2008-04-15 17:46:26 +0000 | [diff] [blame] | 572 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 573 | in_devnull = open('/dev/null', 'r') |
| 574 | print "cmd = %s" % cmd |
| 575 | print "path = %s" % os.getcwd() |
| 576 | |
| 577 | proc = subprocess.Popen(cmd, stdout=out_file, |
| 578 | stderr=subprocess.STDOUT, stdin=in_devnull) |
| 579 | out_file.close() |
| 580 | in_devnull.close() |
| 581 | return proc |
| 582 | |
| 583 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 584 | def get_pid(self): |
| 585 | return self.proc.pid |
| 586 | |
| 587 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 588 | def kill(self): |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 589 | kill_autoserv(self.get_pid(), self.exit_code) |
| 590 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 591 | |
| 592 | def exit_code(self): |
| 593 | return self.proc.poll() |
| 594 | |
| 595 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 596 | class PidfileException(Exception): |
| 597 | """\ |
| 598 | Raised when there's some unexpected behavior with the pid file. |
| 599 | """ |
| 600 | |
| 601 | |
| 602 | class PidfileRunMonitor(RunMonitor): |
| 603 | def __init__(self, results_dir, cmd=None, nice_level=None, |
| 604 | log_file=None): |
| 605 | self.results_dir = os.path.abspath(results_dir) |
| 606 | self.pid_file = os.path.join(results_dir, AUTOSERV_PID_FILE) |
| 607 | self.lost_process = False |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 608 | self.start_time = time.time() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 609 | if cmd is None: |
| 610 | # we're reattaching to an existing pid, so don't call |
| 611 | # the superconstructor (we don't want to kick off a new |
| 612 | # process) |
| 613 | pass |
| 614 | else: |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 615 | super(PidfileRunMonitor, self).__init__(cmd, |
| 616 | nice_level, log_file) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 617 | |
| 618 | |
| 619 | def get_pid(self): |
| 620 | pid, exit_status = self.get_pidfile_info() |
| 621 | assert pid is not None |
| 622 | return pid |
| 623 | |
| 624 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 625 | def _check_command_line(self, command_line, spacer=' ', |
| 626 | print_error=False): |
| 627 | results_dir_arg = spacer.join(('', '-r', self.results_dir, '')) |
| 628 | match = results_dir_arg in command_line |
| 629 | if print_error and not match: |
| 630 | print '%s not found in %s' % (repr(results_dir_arg), |
| 631 | repr(command_line)) |
| 632 | return match |
| 633 | |
| 634 | |
| 635 | def _check_proc_fs(self, pid): |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 636 | cmdline_path = os.path.join('/proc', str(pid), 'cmdline') |
| 637 | try: |
| 638 | cmdline_file = open(cmdline_path, 'r') |
| 639 | cmdline = cmdline_file.read().strip() |
| 640 | cmdline_file.close() |
| 641 | except IOError: |
| 642 | return False |
| 643 | # /proc/.../cmdline has \x00 separating args |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 644 | return self._check_command_line(cmdline, spacer='\x00', |
| 645 | print_error=True) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 646 | |
| 647 | |
| 648 | def read_pidfile(self): |
| 649 | if not os.path.exists(self.pid_file): |
| 650 | return None, None |
| 651 | file_obj = open(self.pid_file, 'r') |
| 652 | lines = file_obj.readlines() |
| 653 | file_obj.close() |
| 654 | assert 1 <= len(lines) <= 2 |
| 655 | try: |
| 656 | pid = int(lines[0]) |
| 657 | exit_status = None |
| 658 | if len(lines) == 2: |
| 659 | exit_status = int(lines[1]) |
| 660 | except ValueError, exc: |
| 661 | raise Exception('Corrupt pid file: ' + str(exc.args)) |
| 662 | |
| 663 | return pid, exit_status |
| 664 | |
| 665 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 666 | def _find_autoserv_proc(self): |
| 667 | autoserv_procs = Dispatcher.find_autoservs() |
| 668 | for pid, args in autoserv_procs.iteritems(): |
| 669 | if self._check_command_line(args): |
| 670 | return pid, args |
| 671 | return None, None |
| 672 | |
| 673 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 674 | def get_pidfile_info(self): |
| 675 | """\ |
| 676 | Returns: |
| 677 | None, None if autoserv has not yet run |
| 678 | pid, None if autoserv is running |
| 679 | pid, exit_status if autoserv has completed |
| 680 | """ |
| 681 | if self.lost_process: |
| 682 | return self.pid, self.exit_status |
| 683 | |
| 684 | pid, exit_status = self.read_pidfile() |
| 685 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 686 | if pid is None: |
| 687 | return self._handle_no_pid() |
| 688 | |
| 689 | if exit_status is None: |
| 690 | # double check whether or not autoserv is running |
| 691 | proc_running = self._check_proc_fs(pid) |
| 692 | if proc_running: |
| 693 | return pid, exit_status |
| 694 | |
| 695 | # pid but no process - maybe process *just* exited |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 696 | pid, exit_status = self.read_pidfile() |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 697 | if exit_status is None: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 698 | # autoserv exited without writing an exit code |
| 699 | # to the pidfile |
| 700 | error = ('autoserv died without writing exit ' |
| 701 | 'code') |
| 702 | message = error + '\nPid: %s\nPidfile: %s' % ( |
| 703 | pid, self.pid_file) |
| 704 | print message |
| 705 | send_notify_email(error, message) |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 706 | self.on_lost_process(pid) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 707 | return self.pid, self.exit_status |
| 708 | |
| 709 | return pid, exit_status |
| 710 | |
| 711 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 712 | def _handle_no_pid(self): |
| 713 | """\ |
| 714 | Called when no pidfile is found or no pid is in the pidfile. |
| 715 | """ |
| 716 | # is autoserv running? |
| 717 | pid, args = self._find_autoserv_proc() |
| 718 | if pid is None: |
| 719 | # no autoserv process running |
| 720 | message = 'No pid found at ' + self.pid_file |
| 721 | else: |
| 722 | message = ("Process %d (%s) hasn't written pidfile %s" % |
| 723 | (pid, args, self.pid_file)) |
| 724 | |
| 725 | print message |
| 726 | if time.time() - self.start_time > PIDFILE_TIMEOUT: |
| 727 | send_notify_email('Process has failed to write pidfile', |
| 728 | message) |
| 729 | if pid is not None: |
| 730 | kill_autoserv(pid) |
| 731 | else: |
| 732 | pid = 0 |
| 733 | self.on_lost_process(pid) |
| 734 | return self.pid, self.exit_status |
| 735 | |
| 736 | return None, None |
| 737 | |
| 738 | |
| 739 | def on_lost_process(self, pid): |
| 740 | """\ |
| 741 | Called when autoserv has exited without writing an exit status, |
| 742 | or we've timed out waiting for autoserv to write a pid to the |
| 743 | pidfile. In either case, we just return failure and the caller |
| 744 | should signal some kind of warning. |
| 745 | |
| 746 | pid is unimportant here, as it shouldn't be used by anyone. |
| 747 | """ |
| 748 | self.lost_process = True |
| 749 | self.pid = pid |
| 750 | self.exit_status = 1 |
| 751 | |
| 752 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 753 | def exit_code(self): |
| 754 | pid, exit_code = self.get_pidfile_info() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 755 | return exit_code |
| 756 | |
| 757 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 758 | class Agent(object): |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 759 | def __init__(self, tasks, queue_entry_ids=[]): |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 760 | self.active_task = None |
| 761 | self.queue = Queue.Queue(0) |
| 762 | self.dispatcher = None |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 763 | self.queue_entry_ids = queue_entry_ids |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 764 | |
| 765 | for task in tasks: |
| 766 | self.add_task(task) |
| 767 | |
| 768 | |
| 769 | def add_task(self, task): |
| 770 | self.queue.put_nowait(task) |
| 771 | task.agent = self |
| 772 | |
| 773 | |
| 774 | def tick(self): |
| 775 | print "agent tick" |
| 776 | if self.active_task and not self.active_task.is_done(): |
| 777 | self.active_task.poll() |
| 778 | else: |
| 779 | self._next_task(); |
| 780 | |
| 781 | |
| 782 | def _next_task(self): |
| 783 | print "agent picking task" |
| 784 | if self.active_task: |
| 785 | assert self.active_task.is_done() |
| 786 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 787 | if not self.active_task.success: |
| 788 | self.on_task_failure() |
| 789 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 790 | self.active_task = None |
| 791 | if not self.is_done(): |
| 792 | self.active_task = self.queue.get_nowait() |
| 793 | if self.active_task: |
| 794 | self.active_task.start() |
| 795 | |
| 796 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 797 | def on_task_failure(self): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 798 | self.queue = Queue.Queue(0) |
| 799 | for task in self.active_task.failure_tasks: |
| 800 | self.add_task(task) |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 801 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 802 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 803 | def is_done(self): |
| 804 | return self.active_task == None and self.queue.empty() |
| 805 | |
| 806 | |
| 807 | def start(self): |
| 808 | assert self.dispatcher |
| 809 | |
| 810 | self._next_task() |
| 811 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 812 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 813 | class AgentTask(object): |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 814 | def __init__(self, cmd, failure_tasks = []): |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 815 | self.done = False |
| 816 | self.failure_tasks = failure_tasks |
| 817 | self.started = False |
| 818 | self.cmd = cmd |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 819 | self.task = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 820 | self.agent = None |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 821 | self.monitor = None |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 822 | self.success = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 823 | |
| 824 | |
| 825 | def poll(self): |
| 826 | print "poll" |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 827 | if self.monitor: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 828 | self.tick(self.monitor.exit_code()) |
| 829 | else: |
| 830 | self.finished(False) |
| 831 | |
| 832 | |
| 833 | def tick(self, exit_code): |
| 834 | if exit_code==None: |
| 835 | return |
| 836 | # print "exit_code was %d" % exit_code |
| 837 | if exit_code == 0: |
| 838 | success = True |
| 839 | else: |
| 840 | success = False |
| 841 | |
| 842 | self.finished(success) |
| 843 | |
| 844 | |
| 845 | def is_done(self): |
| 846 | return self.done |
| 847 | |
| 848 | |
| 849 | def finished(self, success): |
| 850 | self.done = True |
| 851 | self.success = success |
| 852 | self.epilog() |
| 853 | |
| 854 | |
| 855 | def prolog(self): |
| 856 | pass |
| 857 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 858 | |
| 859 | def create_temp_resultsdir(self, suffix=''): |
| 860 | self.temp_results_dir = tempfile.mkdtemp(suffix=suffix) |
| 861 | |
| 862 | |
| 863 | def cleanup(self): |
| 864 | if (hasattr(self, 'temp_results_dir') and |
| 865 | os.path.exists(self.temp_results_dir)): |
| 866 | shutil.rmtree(self.temp_results_dir) |
| 867 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 868 | |
| 869 | def epilog(self): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 870 | self.cleanup() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 871 | |
| 872 | |
| 873 | def start(self): |
| 874 | assert self.agent |
| 875 | |
| 876 | if not self.started: |
| 877 | self.prolog() |
| 878 | self.run() |
| 879 | |
| 880 | self.started = True |
| 881 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 882 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 883 | def abort(self): |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 884 | if self.monitor: |
| 885 | self.monitor.kill() |
| 886 | self.done = True |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 887 | self.cleanup() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 888 | |
| 889 | |
| 890 | def run(self): |
| 891 | if self.cmd: |
| 892 | print "agent starting monitor" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 893 | log_file = None |
| 894 | if hasattr(self, 'host'): |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 895 | log_file = os.path.join(RESULTS_DIR, 'hosts', |
| 896 | self.host.hostname) |
| 897 | self.monitor = RunMonitor( |
| 898 | self.cmd, nice_level = AUTOSERV_NICE_LEVEL, |
| 899 | log_file = log_file) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 900 | |
| 901 | |
| 902 | class RepairTask(AgentTask): |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 903 | def __init__(self, host, fail_queue_entry=None): |
| 904 | """\ |
| 905 | fail_queue_entry: queue entry to mark failed if this repair |
| 906 | fails. |
| 907 | """ |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 908 | self.create_temp_resultsdir('.repair') |
| 909 | cmd = [_autoserv_path , '-R', '-m', host.hostname, |
| 910 | '-r', self.temp_results_dir] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 911 | self.host = host |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 912 | self.fail_queue_entry = fail_queue_entry |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 913 | super(RepairTask, self).__init__(cmd) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 914 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 915 | |
| 916 | def prolog(self): |
| 917 | print "repair_task starting" |
| 918 | self.host.set_status('Repairing') |
| 919 | |
| 920 | |
| 921 | def epilog(self): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 922 | super(RepairTask, self).epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 923 | if self.success: |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 924 | self.host.set_status('Ready') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 925 | else: |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 926 | self.host.set_status('Repair Failed') |
| 927 | if self.fail_queue_entry: |
| 928 | self.fail_queue_entry.handle_host_failure() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 929 | |
| 930 | |
| 931 | class VerifyTask(AgentTask): |
| 932 | def __init__(self, queue_entry=None, host=None): |
| 933 | assert bool(queue_entry) != bool(host) |
| 934 | |
| 935 | self.host = host or queue_entry.host |
| 936 | self.queue_entry = queue_entry |
| 937 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 938 | self.create_temp_resultsdir('.verify') |
mbligh | 48c10a5 | 2008-02-29 22:46:38 +0000 | [diff] [blame] | 939 | cmd = [_autoserv_path,'-v','-m',self.host.hostname, |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 940 | '-r', self.temp_results_dir] |
| 941 | |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 942 | fail_queue_entry = None |
| 943 | if queue_entry and not queue_entry.meta_host: |
| 944 | fail_queue_entry = queue_entry |
| 945 | failure_tasks = [RepairTask(self.host, fail_queue_entry)] |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 946 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 947 | super(VerifyTask, self).__init__(cmd, |
| 948 | failure_tasks=failure_tasks) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 949 | |
| 950 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 951 | def prolog(self): |
| 952 | print "starting verify on %s" % (self.host.hostname) |
| 953 | if self.queue_entry: |
| 954 | self.queue_entry.set_status('Verifying') |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 955 | self.queue_entry.clear_results_dir( |
| 956 | self.queue_entry.verify_results_dir()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 957 | self.host.set_status('Verifying') |
| 958 | |
| 959 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 960 | def cleanup(self): |
| 961 | if not os.path.exists(self.temp_results_dir): |
| 962 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 963 | if self.queue_entry and (self.success or |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 964 | not self.queue_entry.meta_host): |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 965 | self.move_results() |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 966 | super(VerifyTask, self).cleanup() |
| 967 | |
| 968 | |
| 969 | def epilog(self): |
| 970 | super(VerifyTask, self).epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 971 | |
| 972 | if self.success: |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 973 | self.host.set_status('Ready') |
| 974 | elif self.queue_entry: |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 975 | self.queue_entry.requeue() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 976 | |
| 977 | |
| 978 | def move_results(self): |
| 979 | assert self.queue_entry is not None |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 980 | target_dir = self.queue_entry.verify_results_dir() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 981 | if not os.path.exists(target_dir): |
| 982 | os.makedirs(target_dir) |
| 983 | files = os.listdir(self.temp_results_dir) |
| 984 | for filename in files: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 985 | if filename == AUTOSERV_PID_FILE: |
| 986 | continue |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 987 | self.force_move(os.path.join(self.temp_results_dir, |
| 988 | filename), |
| 989 | os.path.join(target_dir, filename)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 990 | |
| 991 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 992 | @staticmethod |
| 993 | def force_move(source, dest): |
| 994 | """\ |
| 995 | Replacement for shutil.move() that will delete the destination |
| 996 | if it exists, even if it's a directory. |
| 997 | """ |
| 998 | if os.path.exists(dest): |
| 999 | print ('Warning: removing existing destination file ' + |
| 1000 | dest) |
| 1001 | remove_file_or_dir(dest) |
| 1002 | shutil.move(source, dest) |
| 1003 | |
| 1004 | |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 1005 | class VerifySynchronousTask(VerifyTask): |
| 1006 | def __init__(self, queue_entry): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1007 | super(VerifySynchronousTask, self).__init__( |
| 1008 | queue_entry = queue_entry) |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 1009 | |
| 1010 | |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1011 | def epilog(self): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1012 | super(VerifySynchronousTask, self).epilog() |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1013 | if self.success: |
| 1014 | if self.queue_entry.job.num_complete() > 0: |
| 1015 | # some other entry failed verify, and we've |
| 1016 | # already been marked as stopped |
| 1017 | return |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 1018 | |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1019 | self.queue_entry.set_status('Pending') |
| 1020 | job = self.queue_entry.job |
| 1021 | if job.is_ready(): |
| 1022 | agent = job.run(self.queue_entry) |
| 1023 | self.agent.dispatcher.add_agent(agent) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1024 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1025 | class QueueTask(AgentTask): |
| 1026 | def __init__(self, job, queue_entries, cmd): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1027 | super(QueueTask, self).__init__(cmd) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1028 | self.job = job |
| 1029 | self.queue_entries = queue_entries |
| 1030 | |
| 1031 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1032 | @staticmethod |
| 1033 | def _write_keyval(results_dir, field, value): |
| 1034 | key_path = os.path.join(results_dir, 'keyval') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1035 | keyval_file = open(key_path, 'a') |
| 1036 | print >> keyval_file, '%s=%d' % (field, value) |
| 1037 | keyval_file.close() |
| 1038 | |
| 1039 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1040 | def results_dir(self): |
| 1041 | return self.queue_entries[0].results_dir() |
| 1042 | |
| 1043 | |
| 1044 | def run(self): |
| 1045 | """\ |
| 1046 | Override AgentTask.run() so we can use a PidfileRunMonitor. |
| 1047 | """ |
| 1048 | self.monitor = PidfileRunMonitor(self.results_dir(), |
| 1049 | cmd=self.cmd, |
| 1050 | nice_level=AUTOSERV_NICE_LEVEL) |
| 1051 | |
| 1052 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1053 | def prolog(self): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1054 | # write some job timestamps into the job keyval file |
| 1055 | queued = time.mktime(self.job.created_on.timetuple()) |
| 1056 | started = time.time() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1057 | self._write_keyval(self.results_dir(), "job_queued", queued) |
| 1058 | self._write_keyval(self.results_dir(), "job_started", started) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1059 | for queue_entry in self.queue_entries: |
| 1060 | print "starting queue_task on %s/%s" % (queue_entry.host.hostname, queue_entry.id) |
| 1061 | queue_entry.set_status('Running') |
| 1062 | queue_entry.host.set_status('Running') |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1063 | if (not self.job.is_synchronous() and |
| 1064 | self.job.num_machines() > 1): |
| 1065 | assert len(self.queue_entries) == 1 |
| 1066 | self.job.write_to_machines_file(self.queue_entries[0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1067 | |
| 1068 | |
| 1069 | def epilog(self): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1070 | super(QueueTask, self).epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1071 | if self.success: |
| 1072 | status = 'Completed' |
| 1073 | else: |
| 1074 | status = 'Failed' |
| 1075 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1076 | # write another timestamp into the job keyval file |
| 1077 | finished = time.time() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1078 | self._write_keyval(self.results_dir(), "job_finished", finished) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1079 | for queue_entry in self.queue_entries: |
| 1080 | queue_entry.set_status(status) |
| 1081 | queue_entry.host.set_status('Ready') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1082 | |
| 1083 | if self.job.is_synchronous() or self.job.num_machines()==1: |
| 1084 | if self.job.is_finished(): |
| 1085 | parse_results(self.job.results_dir()) |
| 1086 | else: |
| 1087 | for queue_entry in self.queue_entries: |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1088 | parse_results(queue_entry.results_dir(), |
| 1089 | flags='-l 2') |
| 1090 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1091 | print "queue_task finished with %s/%s" % (status, self.success) |
| 1092 | |
| 1093 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1094 | class RecoveryQueueTask(QueueTask): |
| 1095 | def __init__(self, job, queue_entries, run_monitor): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1096 | super(RecoveryQueueTask, self).__init__(job, |
| 1097 | queue_entries, cmd=None) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1098 | self.run_monitor = run_monitor |
| 1099 | |
| 1100 | |
| 1101 | def run(self): |
| 1102 | self.monitor = self.run_monitor |
| 1103 | |
| 1104 | |
| 1105 | def prolog(self): |
| 1106 | # recovering an existing process - don't do prolog |
| 1107 | pass |
| 1108 | |
| 1109 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1110 | class RebootTask(AgentTask): |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1111 | def __init__(self, host): |
| 1112 | global _autoserv_path |
| 1113 | |
| 1114 | # Current implementation of autoserv requires control file |
| 1115 | # to be passed on reboot action request. TODO: remove when no |
| 1116 | # longer appropriate. |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1117 | self.create_temp_resultsdir('.reboot') |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1118 | self.cmd = [_autoserv_path, '-b', '-m', host.hostname, |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1119 | '-r', self.temp_results_dir, '/dev/null'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1120 | self.host = host |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1121 | super(RebootTask, self).__init__(self.cmd, |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1122 | failure_tasks=[RepairTask(host)]) |
| 1123 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1124 | |
| 1125 | def prolog(self): |
| 1126 | print "starting reboot task for host: %s" % self.host.hostname |
| 1127 | self.host.set_status("Rebooting") |
| 1128 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1129 | |
| 1130 | class AbortTask(AgentTask): |
| 1131 | def __init__(self, queue_entry, agents_to_abort): |
| 1132 | self.queue_entry = queue_entry |
| 1133 | self.agents_to_abort = agents_to_abort |
| 1134 | for agent in agents_to_abort: |
| 1135 | agent.dispatcher.remove_agent(agent) |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1136 | super(AbortTask, self).__init__('') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1137 | |
| 1138 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1139 | def prolog(self): |
| 1140 | print "starting abort on host %s, job %s" % ( |
| 1141 | self.queue_entry.host_id, self.queue_entry.job_id) |
| 1142 | self.queue_entry.set_status('Aborting') |
| 1143 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1144 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1145 | def epilog(self): |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1146 | super(AbortTask, self).epilog() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1147 | self.queue_entry.set_status('Aborted') |
| 1148 | self.success = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1149 | |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1150 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1151 | def run(self): |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1152 | for agent in self.agents_to_abort: |
| 1153 | if (agent.active_task): |
| 1154 | agent.active_task.abort() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1155 | |
| 1156 | |
| 1157 | class DBObject(object): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1158 | def __init__(self, fields, id=None, row=None, new_record=False): |
| 1159 | assert (bool(id) != bool(row)) and fields |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1160 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1161 | self.__table = self._get_table() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1162 | self.__fields = fields |
| 1163 | |
| 1164 | self.__new_record = new_record |
| 1165 | |
| 1166 | if row is None: |
| 1167 | sql = 'SELECT * FROM %s WHERE ID=%%s' % self.__table |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1168 | rows = _db.execute(sql, (id,)) |
| 1169 | if len(rows) == 0: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1170 | raise "row not found (table=%s, id=%s)" % \ |
| 1171 | (self.__table, id) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1172 | row = rows[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1173 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1174 | assert len(row)==len(fields), ( |
| 1175 | "table = %s, row = %s/%d, fields = %s/%d" % ( |
| 1176 | self.__table, row, len(row), fields, len(fields))) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1177 | |
| 1178 | self.__valid_fields = {} |
| 1179 | for i,value in enumerate(row): |
| 1180 | self.__dict__[fields[i]] = value |
| 1181 | self.__valid_fields[fields[i]] = True |
| 1182 | |
| 1183 | del self.__valid_fields['id'] |
| 1184 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1185 | |
| 1186 | @classmethod |
| 1187 | def _get_table(cls): |
| 1188 | raise NotImplementedError('Subclasses must override this') |
| 1189 | |
| 1190 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1191 | def count(self, where, table = None): |
| 1192 | if not table: |
| 1193 | table = self.__table |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 1194 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1195 | rows = _db.execute(""" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1196 | SELECT count(*) FROM %s |
| 1197 | WHERE %s |
| 1198 | """ % (table, where)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1199 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1200 | assert len(rows) == 1 |
| 1201 | |
| 1202 | return int(rows[0][0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1203 | |
| 1204 | |
| 1205 | def num_cols(self): |
| 1206 | return len(self.__fields) |
| 1207 | |
| 1208 | |
| 1209 | def update_field(self, field, value): |
| 1210 | assert self.__valid_fields[field] |
| 1211 | |
| 1212 | if self.__dict__[field] == value: |
| 1213 | return |
| 1214 | |
| 1215 | query = "UPDATE %s SET %s = %%s WHERE id = %%s" % \ |
| 1216 | (self.__table, field) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1217 | _db.execute(query, (value, self.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1218 | |
| 1219 | self.__dict__[field] = value |
| 1220 | |
| 1221 | |
| 1222 | def save(self): |
| 1223 | if self.__new_record: |
| 1224 | keys = self.__fields[1:] # avoid id |
| 1225 | columns = ','.join([str(key) for key in keys]) |
| 1226 | values = ['"%s"' % self.__dict__[key] for key in keys] |
| 1227 | values = ','.join(values) |
| 1228 | query = """INSERT INTO %s (%s) VALUES (%s)""" % \ |
| 1229 | (self.__table, columns, values) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1230 | _db.execute(query) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1231 | |
| 1232 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1233 | def delete(self): |
| 1234 | query = 'DELETE FROM %s WHERE id=%%s' % self.__table |
| 1235 | _db.execute(query, (self.id,)) |
| 1236 | |
| 1237 | |
| 1238 | @classmethod |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame^] | 1239 | def fetch(cls, where, params=()): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1240 | rows = _db.execute( |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame^] | 1241 | 'SELECT * FROM %s WHERE %s' % (cls._get_table(), where), |
| 1242 | params) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1243 | for row in rows: |
| 1244 | yield cls(row=row) |
| 1245 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1246 | |
| 1247 | class IneligibleHostQueue(DBObject): |
| 1248 | def __init__(self, id=None, row=None, new_record=None): |
| 1249 | fields = ['id', 'job_id', 'host_id'] |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1250 | super(IneligibleHostQueue, self).__init__(fields, id=id, |
| 1251 | row=row, new_record=new_record) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
| 1254 | @classmethod |
| 1255 | def _get_table(cls): |
| 1256 | return 'ineligible_host_queues' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1257 | |
| 1258 | |
| 1259 | class Host(DBObject): |
| 1260 | def __init__(self, id=None, row=None): |
mbligh | 5244cbb | 2008-04-24 20:39:52 +0000 | [diff] [blame] | 1261 | fields = ['id', 'hostname', 'locked', 'synch_id','status', |
| 1262 | 'invalid'] |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1263 | super(Host, self).__init__(fields, id=id, row=row) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1264 | |
| 1265 | |
| 1266 | @classmethod |
| 1267 | def _get_table(cls): |
| 1268 | return 'hosts' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1269 | |
| 1270 | |
| 1271 | def current_task(self): |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1272 | rows = _db.execute(""" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1273 | SELECT * FROM host_queue_entries WHERE host_id=%s AND NOT complete AND active |
| 1274 | """, (self.id,)) |
| 1275 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1276 | if len(rows) == 0: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1277 | return None |
| 1278 | else: |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1279 | assert len(rows) == 1 |
| 1280 | results = rows[0]; |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1281 | # print "current = %s" % results |
| 1282 | return HostQueueEntry(row=results) |
| 1283 | |
| 1284 | |
| 1285 | def next_queue_entries(self): |
| 1286 | if self.locked: |
| 1287 | print "%s locked, not queuing" % self.hostname |
| 1288 | return None |
| 1289 | # print "%s/%s looking for work" % (self.hostname, self.platform_id) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1290 | rows = _db.execute(""" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1291 | SELECT * FROM host_queue_entries |
| 1292 | WHERE ((host_id=%s) OR (meta_host IS NOT null AND |
| 1293 | (meta_host IN ( |
| 1294 | SELECT label_id FROM hosts_labels WHERE host_id=%s |
| 1295 | ) |
| 1296 | ) |
| 1297 | AND job_id NOT IN ( |
| 1298 | SELECT job_id FROM ineligible_host_queues |
| 1299 | WHERE host_id=%s |
| 1300 | ))) |
| 1301 | AND NOT complete AND NOT active |
| 1302 | ORDER BY priority DESC, meta_host, id |
| 1303 | LIMIT 1 |
| 1304 | """, (self.id,self.id, self.id)) |
| 1305 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1306 | if len(rows) == 0: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1307 | return None |
| 1308 | else: |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1309 | return [HostQueueEntry(row=i) for i in rows] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1310 | |
| 1311 | def yield_work(self): |
| 1312 | print "%s yielding work" % self.hostname |
| 1313 | if self.current_task(): |
| 1314 | self.current_task().requeue() |
| 1315 | |
| 1316 | def set_status(self,status): |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1317 | print '%s -> %s' % (self.hostname, status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1318 | self.update_field('status',status) |
| 1319 | |
| 1320 | |
| 1321 | class HostQueueEntry(DBObject): |
| 1322 | def __init__(self, id=None, row=None): |
| 1323 | assert id or row |
| 1324 | fields = ['id', 'job_id', 'host_id', 'priority', 'status', |
| 1325 | 'meta_host', 'active', 'complete'] |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1326 | super(HostQueueEntry, self).__init__(fields, id=id, row=row) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1327 | self.job = Job(self.job_id) |
| 1328 | |
| 1329 | if self.host_id: |
| 1330 | self.host = Host(self.host_id) |
| 1331 | else: |
| 1332 | self.host = None |
| 1333 | |
| 1334 | self.queue_log_path = os.path.join(self.job.results_dir(), |
| 1335 | 'queue.log.' + str(self.id)) |
| 1336 | |
| 1337 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1338 | @classmethod |
| 1339 | def _get_table(cls): |
| 1340 | return 'host_queue_entries' |
| 1341 | |
| 1342 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1343 | def set_host(self, host): |
| 1344 | if host: |
| 1345 | self.queue_log_record('Assigning host ' + host.hostname) |
| 1346 | self.update_field('host_id', host.id) |
| 1347 | self.update_field('active', True) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1348 | self.block_host(host.id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1349 | else: |
| 1350 | self.queue_log_record('Releasing host') |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1351 | self.unblock_host(self.host.id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1352 | self.update_field('host_id', None) |
| 1353 | |
| 1354 | self.host = host |
| 1355 | |
| 1356 | |
| 1357 | def get_host(self): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1358 | return self.host |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1359 | |
| 1360 | |
| 1361 | def queue_log_record(self, log_line): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1362 | now = str(datetime.datetime.now()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1363 | queue_log = open(self.queue_log_path, 'a', 0) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1364 | queue_log.write(now + ' ' + log_line + '\n') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1365 | queue_log.close() |
| 1366 | |
| 1367 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1368 | def block_host(self, host_id): |
| 1369 | print "creating block %s/%s" % (self.job.id, host_id) |
| 1370 | row = [0, self.job.id, host_id] |
| 1371 | block = IneligibleHostQueue(row=row, new_record=True) |
| 1372 | block.save() |
| 1373 | |
| 1374 | |
| 1375 | def unblock_host(self, host_id): |
| 1376 | print "removing block %s/%s" % (self.job.id, host_id) |
| 1377 | blocks = list(IneligibleHostQueue.fetch( |
| 1378 | 'job_id=%d and host_id=%d' % (self.job.id, host_id))) |
| 1379 | assert len(blocks) == 1 |
| 1380 | blocks[0].delete() |
| 1381 | |
| 1382 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1383 | def results_dir(self): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1384 | if self.job.is_synchronous() or self.job.num_machines() == 1: |
| 1385 | return self.job.job_dir |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1386 | else: |
| 1387 | assert self.host |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1388 | return os.path.join(self.job.job_dir, |
| 1389 | self.host.hostname) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1390 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1391 | |
| 1392 | def verify_results_dir(self): |
| 1393 | if self.job.is_synchronous() or self.job.num_machines() > 1: |
| 1394 | assert self.host |
| 1395 | return os.path.join(self.job.job_dir, |
| 1396 | self.host.hostname) |
| 1397 | else: |
| 1398 | return self.job.job_dir |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1399 | |
| 1400 | |
| 1401 | def set_status(self, status): |
| 1402 | self.update_field('status', status) |
| 1403 | if self.host: |
| 1404 | hostname = self.host.hostname |
| 1405 | else: |
| 1406 | hostname = 'no host' |
| 1407 | print "%s/%d status -> %s" % (hostname, self.id, self.status) |
| 1408 | if status in ['Queued']: |
| 1409 | self.update_field('complete', False) |
| 1410 | self.update_field('active', False) |
| 1411 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1412 | if status in ['Pending', 'Running', 'Verifying', 'Starting', |
| 1413 | 'Abort', 'Aborting']: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1414 | self.update_field('complete', False) |
| 1415 | self.update_field('active', True) |
| 1416 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1417 | if status in ['Failed', 'Completed', 'Stopped', 'Aborted']: |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1418 | self.update_field('complete', True) |
| 1419 | self.update_field('active', False) |
| 1420 | |
| 1421 | |
| 1422 | def run(self,assigned_host=None): |
| 1423 | if self.meta_host: |
| 1424 | assert assigned_host |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1425 | # ensure results dir exists for the queue log |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1426 | self.job.create_results_dir() |
| 1427 | self.set_host(assigned_host) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1428 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1429 | print "%s/%s scheduled on %s, status=%s" % (self.job.name, |
| 1430 | self.meta_host, self.host.hostname, self.status) |
| 1431 | |
| 1432 | return self.job.run(queue_entry=self) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1433 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1434 | def requeue(self): |
| 1435 | self.set_status('Queued') |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1436 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1437 | if self.meta_host: |
| 1438 | self.set_host(None) |
| 1439 | |
| 1440 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1441 | def handle_host_failure(self): |
| 1442 | """\ |
| 1443 | Called when this queue entry's host has failed verification and |
| 1444 | repair. |
| 1445 | """ |
mbligh | dffd637 | 2008-02-29 22:47:33 +0000 | [diff] [blame] | 1446 | assert not self.meta_host |
| 1447 | self.set_status('Failed') |
| 1448 | if self.job.is_synchronous(): |
| 1449 | self.job.stop_all_entries() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1450 | |
| 1451 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1452 | def clear_results_dir(self, results_dir=None, dont_delete_files=False): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1453 | results_dir = results_dir or self.results_dir() |
| 1454 | if not os.path.exists(results_dir): |
| 1455 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1456 | if dont_delete_files: |
| 1457 | temp_dir = tempfile.mkdtemp(suffix='.clear_results') |
| 1458 | print 'Moving results from %s to %s' % (results_dir, |
| 1459 | temp_dir) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1460 | for filename in os.listdir(results_dir): |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1461 | path = os.path.join(results_dir, filename) |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1462 | if dont_delete_files: |
| 1463 | shutil.move(path, |
| 1464 | os.path.join(temp_dir, filename)) |
| 1465 | else: |
| 1466 | remove_file_or_dir(path) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1467 | |
| 1468 | |
| 1469 | class Job(DBObject): |
| 1470 | def __init__(self, id=None, row=None): |
| 1471 | assert id or row |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1472 | super(Job, self).__init__( |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1473 | ['id','owner','name','priority', |
| 1474 | 'control_file','control_type','created_on', |
| 1475 | 'synch_type', 'synch_count','synchronizing'], |
| 1476 | id=id, row=row) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1477 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1478 | self.job_dir = os.path.join(RESULTS_DIR, "%s-%s" % (self.id, |
| 1479 | self.owner)) |
| 1480 | |
| 1481 | |
| 1482 | @classmethod |
| 1483 | def _get_table(cls): |
| 1484 | return 'jobs' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1485 | |
| 1486 | |
| 1487 | def is_server_job(self): |
| 1488 | return self.control_type != 2 |
| 1489 | |
| 1490 | |
| 1491 | def get_host_queue_entries(self): |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1492 | rows = _db.execute(""" |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1493 | SELECT * FROM host_queue_entries |
| 1494 | WHERE job_id= %s |
| 1495 | """, (self.id,)) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 1496 | entries = [HostQueueEntry(row=i) for i in rows] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1497 | |
| 1498 | assert len(entries)>0 |
| 1499 | |
| 1500 | return entries |
| 1501 | |
| 1502 | |
| 1503 | def set_status(self, status, update_queues=False): |
| 1504 | self.update_field('status',status) |
| 1505 | |
| 1506 | if update_queues: |
| 1507 | for queue_entry in self.get_host_queue_entries(): |
| 1508 | queue_entry.set_status(status) |
| 1509 | |
| 1510 | |
| 1511 | def is_synchronous(self): |
| 1512 | return self.synch_type == 2 |
| 1513 | |
| 1514 | |
| 1515 | def is_ready(self): |
| 1516 | if not self.is_synchronous(): |
| 1517 | return True |
| 1518 | sql = "job_id=%s AND status='Pending'" % self.id |
| 1519 | count = self.count(sql, table='host_queue_entries') |
| 1520 | return (count == self.synch_count) |
| 1521 | |
| 1522 | |
| 1523 | def ready_to_synchronize(self): |
| 1524 | # heuristic |
| 1525 | queue_entries = self.get_host_queue_entries() |
| 1526 | count = 0 |
| 1527 | for queue_entry in queue_entries: |
| 1528 | if queue_entry.status == 'Pending': |
| 1529 | count += 1 |
| 1530 | |
| 1531 | return (count/self.synch_count >= 0.5) |
| 1532 | |
| 1533 | |
| 1534 | def start_synchronizing(self): |
| 1535 | self.update_field('synchronizing', True) |
| 1536 | |
| 1537 | |
| 1538 | def results_dir(self): |
| 1539 | return self.job_dir |
| 1540 | |
| 1541 | def num_machines(self, clause = None): |
| 1542 | sql = "job_id=%s" % self.id |
| 1543 | if clause: |
| 1544 | sql += " AND (%s)" % clause |
| 1545 | return self.count(sql, table='host_queue_entries') |
| 1546 | |
| 1547 | |
| 1548 | def num_queued(self): |
| 1549 | return self.num_machines('not complete') |
| 1550 | |
| 1551 | |
| 1552 | def num_active(self): |
| 1553 | return self.num_machines('active') |
| 1554 | |
| 1555 | |
| 1556 | def num_complete(self): |
| 1557 | return self.num_machines('complete') |
| 1558 | |
| 1559 | |
| 1560 | def is_finished(self): |
| 1561 | left = self.num_queued() |
| 1562 | print "%s: %s machines left" % (self.name, left) |
| 1563 | return left==0 |
| 1564 | |
| 1565 | def stop_synchronizing(self): |
| 1566 | self.update_field('synchronizing', False) |
| 1567 | self.set_status('Queued', update_queues = False) |
| 1568 | |
| 1569 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1570 | def stop_all_entries(self): |
| 1571 | for child_entry in self.get_host_queue_entries(): |
| 1572 | if not child_entry.complete: |
| 1573 | child_entry.set_status('Stopped') |
| 1574 | |
| 1575 | |
| 1576 | def write_to_machines_file(self, queue_entry): |
| 1577 | hostname = queue_entry.get_host().hostname |
| 1578 | print "writing %s to job %s machines file" % (hostname, self.id) |
| 1579 | file_path = os.path.join(self.job_dir, '.machines') |
| 1580 | mf = open(file_path, 'a') |
| 1581 | mf.write("%s\n" % queue_entry.get_host().hostname) |
| 1582 | mf.close() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1583 | |
| 1584 | |
| 1585 | def create_results_dir(self, queue_entry=None): |
| 1586 | print "create: active: %s complete %s" % (self.num_active(), |
| 1587 | self.num_complete()) |
| 1588 | |
| 1589 | if not os.path.exists(self.job_dir): |
| 1590 | os.makedirs(self.job_dir) |
| 1591 | |
| 1592 | if queue_entry: |
| 1593 | return queue_entry.results_dir() |
| 1594 | return self.job_dir |
| 1595 | |
| 1596 | |
| 1597 | def run(self, queue_entry): |
| 1598 | results_dir = self.create_results_dir(queue_entry) |
| 1599 | |
| 1600 | if self.is_synchronous(): |
| 1601 | if not self.is_ready(): |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1602 | return Agent([VerifySynchronousTask( |
| 1603 | queue_entry = queue_entry)], |
| 1604 | [queue_entry.id]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1605 | |
| 1606 | queue_entry.set_status('Starting') |
| 1607 | |
| 1608 | ctrl = open(os.tmpnam(), 'w') |
| 1609 | if self.control_file: |
| 1610 | ctrl.write(self.control_file) |
| 1611 | else: |
| 1612 | ctrl.write("") |
| 1613 | ctrl.flush() |
| 1614 | |
| 1615 | if self.is_synchronous(): |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1616 | queue_entries = self.get_host_queue_entries() |
| 1617 | else: |
| 1618 | assert queue_entry |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1619 | queue_entries = [queue_entry] |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1620 | hostnames = ','.join([entry.get_host().hostname |
| 1621 | for entry in queue_entries]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1622 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 1623 | # determine the job tag |
| 1624 | if self.is_synchronous() or self.num_machines() == 1: |
| 1625 | job_name = "%s-%s" % (self.id, self.owner) |
| 1626 | else: |
| 1627 | job_name = "%s-%s/%s" % (self.id, self.owner, |
| 1628 | hostnames) |
| 1629 | |
| 1630 | params = [_autoserv_path, '-P', job_name, '-p', '-n', |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1631 | '-r', os.path.abspath(results_dir), |
| 1632 | '-b', '-u', self.owner, '-l', self.name, |
| 1633 | '-m', hostnames, ctrl.name] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1634 | |
| 1635 | if not self.is_server_job(): |
| 1636 | params.append('-c') |
| 1637 | |
| 1638 | tasks = [] |
| 1639 | if not self.is_synchronous(): |
| 1640 | tasks.append(VerifyTask(queue_entry)) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1641 | |
| 1642 | tasks.append(QueueTask(job = self, |
| 1643 | queue_entries = queue_entries, |
| 1644 | cmd = params)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1645 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1646 | ids = [] |
| 1647 | for entry in queue_entries: |
| 1648 | ids.append(entry.id) |
| 1649 | |
| 1650 | agent = Agent(tasks, ids) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1651 | |
| 1652 | return agent |
| 1653 | |
| 1654 | |
| 1655 | if __name__ == '__main__': |
| 1656 | main() |