blob: 8d99e105dcc6cbe65947609d1f0af3ff9ad504de [file] [log] [blame]
James E. Blair1ce97ad2012-05-29 15:38:19 -07001#!/usr/bin/env python
2# Copyright 2012 Hewlett-Packard Development Company, L.P.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
James E. Blairee743612012-05-29 14:49:32 -070016import argparse
17import ConfigParser
James E. Blaire9d45c32012-05-31 09:56:45 -070018import logging.config
James E. Blairee743612012-05-29 14:49:32 -070019import os
James E. Blaire9d45c32012-05-31 09:56:45 -070020import signal
James E. Blairee743612012-05-29 14:49:32 -070021
22import zuul.scheduler
23import zuul.launcher.jenkins
24import zuul.trigger.gerrit
25
James E. Blairee743612012-05-29 14:49:32 -070026
James E. Blaire9d45c32012-05-31 09:56:45 -070027class Server(object):
28 def __init__(self):
29 self.args = None
30 self.config = None
James E. Blair1e8dd892012-05-30 09:15:05 -070031
James E. Blaire9d45c32012-05-31 09:56:45 -070032 def parse_arguments(self):
33 parser = argparse.ArgumentParser(description='Project gating system.')
34 parser.add_argument('-c', dest='config',
35 help='specify the config file')
36 self.args = parser.parse_args()
James E. Blairee743612012-05-29 14:49:32 -070037
James E. Blaire9d45c32012-05-31 09:56:45 -070038 def read_config(self):
39 self.config = ConfigParser.ConfigParser()
40 if self.args.config:
41 locations = [self.args.config]
42 else:
43 locations = ['/etc/zuul/zuul.conf',
44 '~/zuul.conf']
45 for fp in locations:
46 if os.path.exists(os.path.expanduser(fp)):
47 self.config.read(os.path.expanduser(fp))
48 return
49 raise Exception("Unable to locate config file in %s" % locations)
James E. Blair1e8dd892012-05-30 09:15:05 -070050
James E. Blaire9d45c32012-05-31 09:56:45 -070051 def setup_logging(self):
52 if self.config.has_option('zuul', 'log_config'):
53 fp = os.path.expanduser(self.config.get('zuul', 'log_config'))
54 if not os.path.exists(fp):
55 raise Exception("Unable to read logging config file at %s" %
56 fp)
57 logging.config.fileConfig(fp)
58 else:
59 logging.basicConfig(level=logging.DEBUG)
James E. Blairee743612012-05-29 14:49:32 -070060
James E. Blaire9d45c32012-05-31 09:56:45 -070061 def reconfigure_handler(self, signum, frame):
62 signal.signal(signal.SIGHUP, signal.SIG_IGN)
63 self.read_config()
64 self.setup_logging()
65 self.sched.reconfigure(self.config)
66 signal.signal(signal.SIGHUP, self.reconfigure_handler)
James E. Blair1e8dd892012-05-30 09:15:05 -070067
James E. Blaire9d45c32012-05-31 09:56:45 -070068 def main(self):
69 self.sched = zuul.scheduler.Scheduler()
James E. Blairee743612012-05-29 14:49:32 -070070
James E. Blaire9d45c32012-05-31 09:56:45 -070071 jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
72 gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched)
James E. Blair1e8dd892012-05-30 09:15:05 -070073
James E. Blaire9d45c32012-05-31 09:56:45 -070074 self.sched.setLauncher(jenkins)
75 self.sched.setTrigger(gerrit)
James E. Blairee743612012-05-29 14:49:32 -070076
James E. Blaire9d45c32012-05-31 09:56:45 -070077 self.sched.start()
78 self.sched.reconfigure(self.config)
79 signal.signal(signal.SIGHUP, self.reconfigure_handler)
80 while True:
81 signal.pause()
James E. Blairee743612012-05-29 14:49:32 -070082
James E. Blaire9d45c32012-05-31 09:56:45 -070083 def start(self):
84 self.parse_arguments()
85 self.read_config()
86 self.setup_logging()
87 self.main()
88
James E. Blairee743612012-05-29 14:49:32 -070089
90if __name__ == '__main__':
James E. Blaire9d45c32012-05-31 09:56:45 -070091 server = Server()
92 server.start()