blob: e78fa7b8538d0a3534c48447ab9c64da6cd62cb2 [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. Blaird3bbe002012-05-31 13:06:31 -070018import daemon
19import daemon.pidlockfile
James E. Blaire9d45c32012-05-31 09:56:45 -070020import logging.config
James E. Blairee743612012-05-29 14:49:32 -070021import os
James E. Blaire9d45c32012-05-31 09:56:45 -070022import signal
James E. Blairee743612012-05-29 14:49:32 -070023
24import zuul.scheduler
25import zuul.launcher.jenkins
26import zuul.trigger.gerrit
27
James E. Blairee743612012-05-29 14:49:32 -070028
James E. Blaire9d45c32012-05-31 09:56:45 -070029class Server(object):
30 def __init__(self):
31 self.args = None
32 self.config = None
James E. Blair1e8dd892012-05-30 09:15:05 -070033
James E. Blaire9d45c32012-05-31 09:56:45 -070034 def parse_arguments(self):
35 parser = argparse.ArgumentParser(description='Project gating system.')
36 parser.add_argument('-c', dest='config',
37 help='specify the config file')
James E. Blaird3bbe002012-05-31 13:06:31 -070038 parser.add_argument('-d', dest='nodaemon', action='store_true',
39 help='do not run as a daemon')
James E. Blaire9d45c32012-05-31 09:56:45 -070040 self.args = parser.parse_args()
James E. Blairee743612012-05-29 14:49:32 -070041
James E. Blaire9d45c32012-05-31 09:56:45 -070042 def read_config(self):
43 self.config = ConfigParser.ConfigParser()
44 if self.args.config:
45 locations = [self.args.config]
46 else:
47 locations = ['/etc/zuul/zuul.conf',
48 '~/zuul.conf']
49 for fp in locations:
50 if os.path.exists(os.path.expanduser(fp)):
51 self.config.read(os.path.expanduser(fp))
52 return
53 raise Exception("Unable to locate config file in %s" % locations)
James E. Blair1e8dd892012-05-30 09:15:05 -070054
James E. Blaire9d45c32012-05-31 09:56:45 -070055 def setup_logging(self):
56 if self.config.has_option('zuul', 'log_config'):
57 fp = os.path.expanduser(self.config.get('zuul', 'log_config'))
58 if not os.path.exists(fp):
59 raise Exception("Unable to read logging config file at %s" %
60 fp)
61 logging.config.fileConfig(fp)
62 else:
63 logging.basicConfig(level=logging.DEBUG)
James E. Blairee743612012-05-29 14:49:32 -070064
James E. Blaire9d45c32012-05-31 09:56:45 -070065 def reconfigure_handler(self, signum, frame):
66 signal.signal(signal.SIGHUP, signal.SIG_IGN)
67 self.read_config()
68 self.setup_logging()
69 self.sched.reconfigure(self.config)
70 signal.signal(signal.SIGHUP, self.reconfigure_handler)
James E. Blair1e8dd892012-05-30 09:15:05 -070071
James E. Blaire9d45c32012-05-31 09:56:45 -070072 def main(self):
73 self.sched = zuul.scheduler.Scheduler()
James E. Blairee743612012-05-29 14:49:32 -070074
James E. Blaire9d45c32012-05-31 09:56:45 -070075 jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
76 gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched)
James E. Blair1e8dd892012-05-30 09:15:05 -070077
James E. Blaire9d45c32012-05-31 09:56:45 -070078 self.sched.setLauncher(jenkins)
79 self.sched.setTrigger(gerrit)
James E. Blairee743612012-05-29 14:49:32 -070080
James E. Blaire9d45c32012-05-31 09:56:45 -070081 self.sched.start()
82 self.sched.reconfigure(self.config)
83 signal.signal(signal.SIGHUP, self.reconfigure_handler)
84 while True:
85 signal.pause()
James E. Blairee743612012-05-29 14:49:32 -070086
James E. Blairee743612012-05-29 14:49:32 -070087
88if __name__ == '__main__':
James E. Blaire9d45c32012-05-31 09:56:45 -070089 server = Server()
James E. Blaird3bbe002012-05-31 13:06:31 -070090 server.parse_arguments()
91 server.read_config()
92 server.setup_logging()
93
94 if server.config.has_option('zuul', 'pidfile'):
95 pid_fn = os.path.expanduser(server.config.get('zuul', 'pidfile'))
96 else:
97 pid_fn = '/var/run/zuul/zuul.pid'
98 pid = daemon.pidlockfile.TimeoutPIDLockFile(pid_fn, 10)
99
100 if server.args.nodaemon:
101 server.main()
102 else:
103 with daemon.DaemonContext(pidfile=pid):
104 server.main()