stoneridge/srworker.py

213 строки
7.0 KiB
Python
Исходник Обычный вид История

2013-01-10 02:48:20 +04:00
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
import logging
import os
import subprocess
import sys
import tempfile
import time
2013-01-10 02:48:20 +04:00
import stoneridge
class StoneRidgeTestRunner(object):
pass
class StoneRidgeWorker(stoneridge.RpcHandler):
def setup(self):
self.srconffile = stoneridge.get_config_file()
2013-01-10 02:48:20 +04:00
self.srlogdir = stoneridge.get_config('stoneridge', 'logs')
self.unittest = stoneridge.get_config_bool('stoneridge', 'unittest')
2013-01-10 02:48:20 +04:00
logging.debug('srconffile: %s' % (self.srconffile,))
logging.debug('srlogdir: %s' % (self.srlogdir,))
logging.debug('unittest: %s' % (self.unittest,))
2013-01-10 02:48:20 +04:00
self.runconfig = None # Needs to be here so reset doesn't barf
2013-01-10 02:48:20 +04:00
self.reset()
def handle(self, srid, netconfig):
# Have a logger just for this run
2013-02-01 23:29:01 +04:00
logdir = 'stoneridge_%s_%s' % (srid, netconfig)
2013-01-10 02:48:20 +04:00
self.logdir = os.path.join(self.srlogdir, logdir)
if os.path.exists(self.logdir):
# Don't blow away the old logs, just make a new directory for this
# run of the srid
self.logdir = '%s_%s' % (self.logdir, int(time.time()))
2013-01-10 02:48:20 +04:00
os.makedirs(self.logdir)
logging.debug('Running test with logs in %s' % (self.logdir,))
logfile = os.path.join(self.logdir, '00_worker.log')
2013-01-10 02:48:20 +04:00
handler = logging.FileHandler(logfile)
2013-01-11 00:54:41 +04:00
formatter = logging.Formatter(fmt=stoneridge.LOG_FMT)
2013-01-10 02:48:20 +04:00
handler.setFormatter(formatter)
self.logger = logging.getLogger(logdir)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(handler)
# Create a working space for this run
2013-01-11 03:58:12 +04:00
srwork = tempfile.mkdtemp()
srdownload = os.path.join(srwork, 'download')
os.mkdir(srdownload)
firefox_path = stoneridge.get_config('machine', 'firefox_path')
srbindir = os.path.join(srwork, firefox_path)
srout = os.path.join(srwork, 'out')
os.mkdir(srout)
2013-01-11 04:08:24 +04:00
metadata = os.path.join(srout, 'metadata.zip')
info = os.path.join(srout, 'info.json')
2013-01-10 02:48:20 +04:00
# Make a name for output from xpcshell (can't make the actual directory
# yet, because we don't know what directory it'll live in)
2013-01-11 03:58:12 +04:00
srxpcout = os.path.basename(tempfile.mktemp())
2013-01-10 02:48:20 +04:00
self.srnetconfig = netconfig
self.archive_on_failure = False
self.cleaner_called = False
self.procno = 1
self.childlog = None
self.runconfig = os.path.join(srout, 'run.ini')
2013-01-11 03:58:12 +04:00
with file(self.runconfig, 'w') as f:
f.write('[run]\n')
f.write('netconfig = %s\n' % (netconfig,))
f.write('work = %s\n' % (srwork,))
f.write('download = %s\n' % (srdownload,))
f.write('bin = %s\n' % (srbindir,))
f.write('out = %s\n' % (srout,))
2013-01-11 04:08:24 +04:00
f.write('metadata = %s\n' % (metadata,))
f.write('info = %s\n' % (info,))
2013-01-11 03:58:12 +04:00
f.write('xpcoutleaf = %s\n' % (srxpcout,))
f.write('srid = %s\n' % (srid,))
2013-01-10 02:48:20 +04:00
self.logger.debug('srnetconfig: %s' % (self.srnetconfig,))
self.logger.debug('archive on failure: %s' % (self.archive_on_failure,))
self.logger.debug('cleaner called: %s' % (self.cleaner_called,))
self.logger.debug('procno: %s' % (self.procno,))
self.logger.debug('childlog: %s' % (self.childlog,))
self.logger.debug('logdir: %s' % (self.logdir,))
2013-01-11 03:58:12 +04:00
self.logger.debug('runconfig: %s' % (self.runconfig,))
2013-01-10 02:48:20 +04:00
res = {'ok': True}
2013-01-10 02:48:20 +04:00
try:
self.run_test()
except StoneRidgeException as e:
self.logger.exception(e)
res['ok'] = False
res['msg'] = str(e)
2013-01-10 02:48:20 +04:00
self.reset()
return res
2013-01-10 02:48:20 +04:00
def reset(self):
self.srnetconfig = None
self.archive_on_failure = True
self.cleaner_called = True
self.procno = -1
self.childlog = None
self.logdir = None
2013-01-10 03:03:50 +04:00
self.logger = None
2013-01-11 03:58:12 +04:00
if self.runconfig and os.path.exists(self.runconfig):
os.unlink(self.runconfig)
self.runconfig = None
2013-01-10 02:48:20 +04:00
def do_error(self, stage):
"""Print an error and raise an exception that will be handled by the
top level
"""
self.logger.error('Error exit during %s' % (stage,))
raise StoneRidgeException('Error running %s: see %s\n' % (stage,
self.childlog))
def run_process(self, stage, *args):
"""Run a particular subprocess with the default arguments, as well as
any arguments requested by the caller
"""
script = 'sr%s.py' % (stage,)
2013-01-10 02:48:20 +04:00
logfile = os.path.join(self.logdir, '%02d_%s_%s.log' %
(self.procno, stage, self.srnetconfig))
self.procno += 1
command = [script,
'--config', self.srconffile,
2013-01-11 03:58:12 +04:00
'--runconfig', self.runconfig,
2013-01-10 02:48:20 +04:00
'--log', logfile]
command.extend(args)
if self.unittest:
# This code path is used for unit testing the worker
self.logger.debug('Would run %s' % (command,))
return
2013-01-10 02:48:20 +04:00
try:
stoneridge.run_process(*command, logger=self.logger)
except subprocess.CalledProcessError as e:
# The process failed to run correctly, we need to say so
self.childlog = logfile
if self.archive_on_failure:
# We've reached the point in our run where we have something to
# save off for usage. Archive it, but don't try to archive again
# if for some reason the archival process fails :)
self.archive_on_failure = False
try:
self.run_process('archiver')
2013-02-02 03:00:56 +04:00
except StoneRidgeException as e:
2013-01-10 02:48:20 +04:00
pass
if not self.cleaner_called:
# Let's be nice and clean up after ourselves
self.cleaner_called = True
try:
self.run_process('cleaner')
2013-02-02 03:00:56 +04:00
except StoneRidgeException as e:
2013-01-10 02:48:20 +04:00
pass
# Finally, bubble the error up to the top level
self.do_error(stage)
def run_test(self):
self.run_process('downloader')
self.run_process('unpacker')
self.run_process('infogatherer')
self.archive_on_failure = True
self.run_process('dnsupdater')
self.run_process('runner')
self.run_process('dnsupdater', '--restore')
self.run_process('collator')
self.run_process('uploader')
self.archive_on_failure = False
self.run_process('archiver')
self.cleaner_called = True
self.run_process('cleaner')
def daemon():
2013-01-11 20:48:18 +04:00
osname = stoneridge.get_config('machine', 'os')
queue = stoneridge.CLIENT_QUEUES[osname]
2013-01-10 02:48:20 +04:00
worker = StoneRidgeWorker(queue)
2013-01-10 02:48:20 +04:00
worker.run()
2013-01-11 23:36:55 +04:00
@stoneridge.main
def main():
parser = stoneridge.DaemonArgumentParser()
args = parser.parse_args()
parser.start_daemon(daemon)