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 |
Antoine Musso | 80925f5 | 2012-09-22 21:37:45 +0200 | [diff] [blame] | 19 | |
| 20 | try: |
| 21 | import daemon.pidlockfile as pid_file_module |
| 22 | pid_file_module # workaround for pyflakes issue #13 |
| 23 | except: |
| 24 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 25 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 26 | import daemon.pidfile as pid_file_module |
| 27 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 28 | import logging.config |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 29 | import os |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 30 | import signal |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 31 | |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 32 | # No zuul imports here because they pull in paramiko which must not be |
| 33 | # imported until after the daemonization. |
| 34 | # https://github.com/paramiko/paramiko/issues/59 |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 35 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 36 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 37 | class Server(object): |
| 38 | def __init__(self): |
| 39 | self.args = None |
| 40 | self.config = None |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 41 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 42 | def parse_arguments(self): |
| 43 | parser = argparse.ArgumentParser(description='Project gating system.') |
| 44 | parser.add_argument('-c', dest='config', |
| 45 | help='specify the config file') |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 46 | parser.add_argument('-d', dest='nodaemon', action='store_true', |
| 47 | help='do not run as a daemon') |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 48 | self.args = parser.parse_args() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 49 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 50 | def read_config(self): |
| 51 | self.config = ConfigParser.ConfigParser() |
| 52 | if self.args.config: |
| 53 | locations = [self.args.config] |
| 54 | else: |
| 55 | locations = ['/etc/zuul/zuul.conf', |
| 56 | '~/zuul.conf'] |
| 57 | for fp in locations: |
| 58 | if os.path.exists(os.path.expanduser(fp)): |
| 59 | self.config.read(os.path.expanduser(fp)) |
| 60 | return |
| 61 | raise Exception("Unable to locate config file in %s" % locations) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 62 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 63 | def setup_logging(self): |
| 64 | if self.config.has_option('zuul', 'log_config'): |
| 65 | fp = os.path.expanduser(self.config.get('zuul', 'log_config')) |
| 66 | if not os.path.exists(fp): |
| 67 | raise Exception("Unable to read logging config file at %s" % |
| 68 | fp) |
| 69 | logging.config.fileConfig(fp) |
| 70 | else: |
| 71 | logging.basicConfig(level=logging.DEBUG) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 72 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 73 | def reconfigure_handler(self, signum, frame): |
| 74 | signal.signal(signal.SIGHUP, signal.SIG_IGN) |
| 75 | self.read_config() |
| 76 | self.setup_logging() |
| 77 | self.sched.reconfigure(self.config) |
| 78 | signal.signal(signal.SIGHUP, self.reconfigure_handler) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 79 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 80 | def exit_handler(self, signum, frame): |
| 81 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 82 | self.sched.exit() |
| 83 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 84 | def main(self): |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 85 | # See comment at top of file about zuul imports |
| 86 | import zuul.scheduler |
| 87 | import zuul.launcher.jenkins |
| 88 | import zuul.trigger.gerrit |
| 89 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 90 | self.sched = zuul.scheduler.Scheduler() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 91 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 92 | jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched) |
| 93 | gerrit = zuul.trigger.gerrit.Gerrit(self.config, self.sched) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 94 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 95 | self.sched.setLauncher(jenkins) |
| 96 | self.sched.setTrigger(gerrit) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 97 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 98 | self.sched.start() |
| 99 | self.sched.reconfigure(self.config) |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 100 | self.sched.resume() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 101 | signal.signal(signal.SIGHUP, self.reconfigure_handler) |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 102 | signal.signal(signal.SIGUSR1, self.exit_handler) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 103 | while True: |
Antoine Musso | 6ad5d5b | 2012-09-24 13:25:28 +0200 | [diff] [blame^] | 104 | try: |
| 105 | signal.pause() |
| 106 | except KeyboardInterrupt: |
| 107 | print "Ctrl + C: asking scheduler to exit nicely...\n" |
| 108 | self.exit_handler( signal.SIGINT, None ) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 109 | |
| 110 | if __name__ == '__main__': |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 111 | server = Server() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 112 | server.parse_arguments() |
| 113 | server.read_config() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 114 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 115 | if server.config.has_option('zuul', 'state_dir'): |
| 116 | state_dir = os.path.expanduser(server.config.get('zuul', 'state_dir')) |
| 117 | else: |
| 118 | state_dir = '/var/lib/zuul' |
| 119 | test_fn = os.path.join(state_dir, 'test') |
| 120 | try: |
| 121 | f = open(test_fn, 'w') |
| 122 | f.close() |
| 123 | os.unlink(test_fn) |
| 124 | except: |
| 125 | print |
| 126 | print "Unable to write to state directory: %s" % state_dir |
| 127 | print |
| 128 | raise |
| 129 | |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 130 | if server.config.has_option('zuul', 'pidfile'): |
| 131 | pid_fn = os.path.expanduser(server.config.get('zuul', 'pidfile')) |
| 132 | else: |
| 133 | pid_fn = '/var/run/zuul/zuul.pid' |
Antoine Musso | 80925f5 | 2012-09-22 21:37:45 +0200 | [diff] [blame] | 134 | pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10) |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 135 | |
| 136 | if server.args.nodaemon: |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 137 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 138 | server.main() |
| 139 | else: |
| 140 | with daemon.DaemonContext(pidfile=pid): |
James E. Blair | 4c58c8f | 2012-05-31 13:38:09 -0700 | [diff] [blame] | 141 | server.setup_logging() |
James E. Blair | d3bbe00 | 2012-05-31 13:06:31 -0700 | [diff] [blame] | 142 | server.main() |