James E. Blair | 1ce97ad | 2012-05-29 15:38:19 -0700 | [diff] [blame] | 1 | #!/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. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 16 | import argparse |
| 17 | import ConfigParser |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 18 | import logging.config |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 19 | import os |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 20 | import signal |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 21 | |
| 22 | import zuul.scheduler |
| 23 | import zuul.launcher.jenkins |
| 24 | import zuul.trigger.gerrit |
| 25 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 26 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 27 | class Server(object): |
| 28 | def __init__(self): |
| 29 | self.args = None |
| 30 | self.config = None |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 31 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 32 | 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. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 37 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 38 | 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. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 50 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 51 | 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. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 60 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 61 | 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. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 67 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 68 | def main(self): |
| 69 | self.sched = zuul.scheduler.Scheduler() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 70 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 71 | jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched) |
| 72 | gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 73 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 74 | self.sched.setLauncher(jenkins) |
| 75 | self.sched.setTrigger(gerrit) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 76 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 77 | 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. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 82 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 83 | def start(self): |
| 84 | self.parse_arguments() |
| 85 | self.read_config() |
| 86 | self.setup_logging() |
| 87 | self.main() |
| 88 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 89 | |
| 90 | if __name__ == '__main__': |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 91 | server = Server() |
| 92 | server.start() |