James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 3 | # Copyright 2013-2014 OpenStack Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import argparse |
| 18 | import daemon |
| 19 | import extras |
| 20 | |
| 21 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 22 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 23 | pid_file_module = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
| 24 | |
James E. Blair | 119acf3 | 2016-04-18 15:34:36 -0700 | [diff] [blame] | 25 | import logging |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 26 | import os |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 27 | import socket |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 28 | import sys |
| 29 | import signal |
| 30 | |
| 31 | import zuul.cmd |
James E. Blair | a6a5004 | 2016-06-01 15:33:54 -0700 | [diff] [blame] | 32 | import zuul.launcher.ansiblelaunchserver |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 33 | |
James E. Blair | a6a5004 | 2016-06-01 15:33:54 -0700 | [diff] [blame] | 34 | # No zuul imports that pull in paramiko here; it must not be |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 35 | # imported until after the daemonization. |
| 36 | # https://github.com/paramiko/paramiko/issues/59 |
| 37 | # Similar situation with gear and statsd. |
| 38 | |
| 39 | |
| 40 | class Launcher(zuul.cmd.ZuulApp): |
| 41 | |
| 42 | def parse_arguments(self): |
| 43 | parser = argparse.ArgumentParser(description='Zuul launch worker.') |
| 44 | parser.add_argument('-c', dest='config', |
| 45 | help='specify the config file') |
| 46 | parser.add_argument('-d', dest='nodaemon', action='store_true', |
| 47 | help='do not run as a daemon') |
| 48 | parser.add_argument('--version', dest='version', action='version', |
| 49 | version=self._get_version(), |
| 50 | help='show zuul version') |
James E. Blair | f87c5ce | 2016-05-25 08:43:37 -0700 | [diff] [blame] | 51 | parser.add_argument('--keep-jobdir', dest='keep_jobdir', |
| 52 | action='store_true', |
| 53 | help='keep local jobdirs after run completes') |
James E. Blair | a6a5004 | 2016-06-01 15:33:54 -0700 | [diff] [blame] | 54 | parser.add_argument('command', |
| 55 | choices=zuul.launcher.ansiblelaunchserver.COMMANDS, |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 56 | nargs='?') |
| 57 | |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 58 | self.args = parser.parse_args() |
| 59 | |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 60 | def send_command(self, cmd): |
| 61 | if self.config.has_option('zuul', 'state_dir'): |
| 62 | state_dir = os.path.expanduser( |
| 63 | self.config.get('zuul', 'state_dir')) |
| 64 | else: |
| 65 | state_dir = '/var/lib/zuul' |
| 66 | path = os.path.join(state_dir, 'launcher.socket') |
| 67 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 68 | s.connect(path) |
| 69 | s.sendall('%s\n' % cmd) |
James E. Blair | 119acf3 | 2016-04-18 15:34:36 -0700 | [diff] [blame] | 70 | |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 71 | def exit_handler(self): |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 72 | self.launcher.stop() |
| 73 | self.launcher.join() |
| 74 | |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 75 | def main(self, daemon=True): |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 76 | # See comment at top of file about zuul imports |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 77 | |
| 78 | self.setup_logging('launcher', 'log_config') |
| 79 | |
James E. Blair | 119acf3 | 2016-04-18 15:34:36 -0700 | [diff] [blame] | 80 | self.log = logging.getLogger("zuul.Launcher") |
| 81 | |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 82 | LaunchServer = zuul.launcher.ansiblelaunchserver.LaunchServer |
James E. Blair | f87c5ce | 2016-05-25 08:43:37 -0700 | [diff] [blame] | 83 | self.launcher = LaunchServer(self.config, |
| 84 | keep_jobdir=self.args.keep_jobdir) |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 85 | self.launcher.start() |
| 86 | |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 87 | signal.signal(signal.SIGUSR2, zuul.cmd.stack_dump_handler) |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 88 | if daemon: |
| 89 | self.launcher.join() |
| 90 | else: |
| 91 | while True: |
| 92 | try: |
| 93 | signal.pause() |
| 94 | except KeyboardInterrupt: |
Morgan Fainberg | 4c6a774 | 2016-05-27 08:42:17 -0700 | [diff] [blame] | 95 | print("Ctrl + C: asking launcher to exit nicely...\n") |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 96 | self.exit_handler() |
| 97 | sys.exit(0) |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def main(): |
| 101 | server = Launcher() |
| 102 | server.parse_arguments() |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 103 | server.read_config() |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 104 | |
James E. Blair | a6a5004 | 2016-06-01 15:33:54 -0700 | [diff] [blame] | 105 | if server.args.command in zuul.launcher.ansiblelaunchserver.COMMANDS: |
| 106 | server.send_command(server.args.command) |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 107 | sys.exit(0) |
| 108 | |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 109 | server.configure_connections() |
| 110 | |
| 111 | if server.config.has_option('launcher', 'pidfile'): |
| 112 | pid_fn = os.path.expanduser(server.config.get('launcher', 'pidfile')) |
| 113 | else: |
| 114 | pid_fn = '/var/run/zuul-launcher/zuul-launcher.pid' |
| 115 | pid = pid_file_module.TimeoutPIDLockFile(pid_fn, 10) |
| 116 | |
| 117 | if server.args.nodaemon: |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 118 | server.main(False) |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 119 | else: |
| 120 | with daemon.DaemonContext(pidfile=pid): |
James E. Blair | c4b2041 | 2016-05-27 16:45:26 -0700 | [diff] [blame] | 121 | server.main(True) |
James E. Blair | 9f36519 | 2016-04-18 10:34:48 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | if __name__ == "__main__": |
| 125 | sys.path.insert(0, '.') |
| 126 | main() |