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