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 | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 72 | def exit_handler(self, signum, frame): |
| 73 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 74 | self.sched.exit() |
| 75 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 76 | def main(self): |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 77 | # See comment at top of file about zuul imports |
| 78 | import zuul.scheduler |
| 79 | import zuul.launcher.jenkins |
| 80 | import zuul.trigger.gerrit |
| 81 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 82 | self.sched = zuul.scheduler.Scheduler() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 83 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 84 | jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched) |
| 85 | gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 86 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 87 | self.sched.setLauncher(jenkins) |
| 88 | self.sched.setTrigger(gerrit) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 89 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 90 | self.sched.start() |
| 91 | self.sched.reconfigure(self.config) |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 92 | self.sched.resume() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 93 | signal.signal(signal.SIGHUP, self.reconfigure_handler) |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 94 | signal.signal(signal.SIGUSR1, self.exit_handler) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 95 | while True: |
| 96 | signal.pause() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 97 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 98 | |
| 99 | if __name__ == '__main__': |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 100 | server = Server() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 101 | server.parse_arguments() |
| 102 | server.read_config() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 103 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 104 | if server.config.has_option('zuul', 'state_dir'): |
| 105 | state_dir = os.path.expanduser(server.config.get('zuul', 'state_dir')) |
| 106 | else: |
| 107 | state_dir = '/var/lib/zuul' |
| 108 | test_fn = os.path.join(state_dir, 'test') |
| 109 | try: |
| 110 | f = open(test_fn, 'w') |
| 111 | f.close() |
| 112 | os.unlink(test_fn) |
| 113 | except: |
| 114 | print |
| 115 | print "Unable to write to state directory: %s" % state_dir |
| 116 | print |
| 117 | raise |
| 118 | |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 119 | if server.config.has_option('zuul', 'pidfile'): |
| 120 | pid_fn = os.path.expanduser(server.config.get('zuul', 'pidfile')) |
| 121 | else: |
| 122 | pid_fn = '/var/run/zuul/zuul.pid' |
| 123 | pid = daemon.pidlockfile.TimeoutPIDLockFile(pid_fn, 10) |
| 124 | |
| 125 | if server.args.nodaemon: |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 126 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 127 | server.main() |
| 128 | else: |
| 129 | with daemon.DaemonContext(pidfile=pid): |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 130 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 131 | server.main() |