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 | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 18 | import daemon |
| 19 | import daemon.pidlockfile |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 20 | import logging.config |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 21 | import os |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 22 | import signal |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 23 | |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 24 | # No zuul imports here because they pull in paramiko which must not be |
| 25 | # imported until after the daemonization. |
| 26 | # https://github.com/paramiko/paramiko/issues/59 |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 27 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 28 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 29 | class Server(object): |
| 30 | def __init__(self): |
| 31 | self.args = None |
| 32 | self.config = None |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 33 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 34 | 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. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 38 | parser.add_argument('-d', dest='nodaemon', action='store_true', |
| 39 | help='do not run as a daemon') |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 40 | self.args = parser.parse_args() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 41 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 42 | 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. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 54 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 55 | 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. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 64 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 65 | 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. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 71 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 72 | def main(self): |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 73 | # See comment at top of file about zuul imports |
| 74 | import zuul.scheduler |
| 75 | import zuul.launcher.jenkins |
| 76 | import zuul.trigger.gerrit |
| 77 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 78 | self.sched = zuul.scheduler.Scheduler() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 79 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 80 | jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched) |
| 81 | gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 82 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 83 | self.sched.setLauncher(jenkins) |
| 84 | self.sched.setTrigger(gerrit) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 85 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 86 | self.sched.start() |
| 87 | self.sched.reconfigure(self.config) |
| 88 | signal.signal(signal.SIGHUP, self.reconfigure_handler) |
| 89 | while True: |
| 90 | signal.pause() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 91 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 92 | |
| 93 | if __name__ == '__main__': |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 94 | server = Server() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 95 | server.parse_arguments() |
| 96 | server.read_config() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 97 | |
| 98 | if server.config.has_option('zuul', 'pidfile'): |
| 99 | pid_fn = os.path.expanduser(server.config.get('zuul', 'pidfile')) |
| 100 | else: |
| 101 | pid_fn = '/var/run/zuul/zuul.pid' |
| 102 | pid = daemon.pidlockfile.TimeoutPIDLockFile(pid_fn, 10) |
| 103 | |
| 104 | if server.args.nodaemon: |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 105 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 106 | server.main() |
| 107 | else: |
| 108 | with daemon.DaemonContext(pidfile=pid): |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 109 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 110 | server.main() |