Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
Joshua Hesketh | 39a0fee | 2013-07-31 12:00:53 +1000 | [diff] [blame] | 3 | # Copyright 2013 Rackspace Australia |
| 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. |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 16 | |
Joshua Hesketh | 1377f6f | 2013-07-26 12:02:15 +1000 | [diff] [blame] | 17 | |
| 18 | """ worker_server.py is an executable worker server that loads and runs |
| 19 | task_plugins. """ |
| 20 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 21 | import logging |
| 22 | import os |
| 23 | import signal |
| 24 | import sys |
| 25 | |
| 26 | import worker_manager |
| 27 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 28 | |
| 29 | class Server(object): |
| 30 | |
| 31 | """ This is the worker server object to be daemonized """ |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 32 | log = logging.getLogger("worker_server.Server") |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 33 | |
| 34 | def __init__(self, config): |
Joshua Hesketh | 8326907 | 2013-11-20 12:22:19 +1100 | [diff] [blame] | 35 | self.config = config |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 36 | # Python logging output file. |
| 37 | self.debug_log = self.config['debug_log'] |
| 38 | |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 39 | # Config init |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 40 | self.zuul_manager = None |
| 41 | self.zuul_client = None |
| 42 | self.plugins = [] |
Joshua Hesketh | b39f884 | 2013-11-21 13:12:00 +1100 | [diff] [blame] | 43 | |
| 44 | # TODO: Make me unique (random?) and we should be able to run multiple |
| 45 | # instances of turbo-hipster on the one host |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 46 | self.worker_name = os.uname()[1] |
| 47 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 48 | self.tasks = {} |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 49 | self.load_plugins() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 50 | |
| 51 | def setup_logging(self): |
Michael Still | 89e4c6d | 2014-01-09 17:38:24 +1100 | [diff] [blame^] | 52 | if not self.debug_log: |
| 53 | raise Exception('Debug log not configured') |
| 54 | |
| 55 | # NOTE(mikal): debug logging _must_ be enabled for the log writing |
| 56 | # in lib.utils.execute_to_log to work correctly. |
| 57 | if not os.path.isdir(os.path.dirname(self.debug_log)): |
| 58 | os.makedirs(os.path.dirname(self.debug_log)) |
| 59 | logging.basicConfig(format='%(asctime)s %(name)s %(message)s', |
| 60 | filename=self.debug_log, level=logging.DEBUG) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 61 | |
| 62 | def load_plugins(self): |
| 63 | """ Load the available plugins from task_plugins """ |
Joshua Hesketh | 6eb5fdc | 2013-11-20 12:36:06 +1100 | [diff] [blame] | 64 | self.log.debug('Loading plugins') |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 65 | # Load plugins |
Joshua Hesketh | 6b6700b | 2013-07-26 16:47:32 +1000 | [diff] [blame] | 66 | for plugin in self.config['plugins']: |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 67 | self.plugins.append({ |
| 68 | 'module': __import__('turbo_hipster.task_plugins.' + |
Joshua Hesketh | 3b74fcd | 2013-09-06 16:19:52 +1000 | [diff] [blame] | 69 | plugin['name'] + '.task', |
| 70 | fromlist='turbo_hipster.task_plugins' + |
| 71 | plugin['name']), |
Joshua Hesketh | 4acd716 | 2013-09-06 16:05:37 +1000 | [diff] [blame] | 72 | 'plugin_config': plugin |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 73 | }) |
Joshua Hesketh | 6eb5fdc | 2013-11-20 12:36:06 +1100 | [diff] [blame] | 74 | self.log.debug('Plugin %s loaded' % plugin['name']) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 75 | |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 76 | def start_gearman_workers(self): |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 77 | """ Run the tasks """ |
Joshua Hesketh | 6eb5fdc | 2013-11-20 12:36:06 +1100 | [diff] [blame] | 78 | self.log.debug('Starting gearman workers') |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 79 | self.zuul_client = worker_manager.ZuulClient(self.config, |
| 80 | self.worker_name) |
| 81 | |
| 82 | for task_number, plugin in enumerate(self.plugins): |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 83 | module = plugin['module'] |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 84 | job_name = '%s-%s-%s' % (plugin['plugin_config']['name'], |
| 85 | self.worker_name, task_number) |
| 86 | self.tasks[job_name] = module.Runner( |
Joshua Hesketh | 1217693 | 2013-09-17 13:34:46 +1000 | [diff] [blame] | 87 | self.config, |
Joshua Hesketh | 527966b | 2013-11-19 12:01:06 +1100 | [diff] [blame] | 88 | plugin['plugin_config'], |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 89 | job_name |
Joshua Hesketh | 1217693 | 2013-09-17 13:34:46 +1000 | [diff] [blame] | 90 | ) |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 91 | self.zuul_client.add_function(plugin['plugin_config']['function'], |
| 92 | self.tasks[job_name]) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 93 | |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 94 | self.zuul_client.register_functions() |
| 95 | self.zuul_client.daemon = True |
| 96 | self.zuul_client.start() |
| 97 | |
| 98 | self.zuul_manager = worker_manager.ZuulManager(self.config, self.tasks) |
| 99 | self.zuul_manager.daemon = True |
| 100 | self.zuul_manager.start() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 101 | |
| 102 | def exit_handler(self, signum): |
Joshua Hesketh | 6eb5fdc | 2013-11-20 12:36:06 +1100 | [diff] [blame] | 103 | self.log.debug('Exiting...') |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 104 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 105 | for task_name, task in self.tasks.items(): |
| 106 | task.stop() |
| 107 | self.manager.stop() |
| 108 | sys.exit(0) |
| 109 | |
| 110 | def main(self): |
| 111 | self.setup_logging() |
Joshua Hesketh | 4343b95 | 2013-11-20 12:11:55 +1100 | [diff] [blame] | 112 | self.start_gearman_workers() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 113 | |
| 114 | while True: |
| 115 | try: |
| 116 | signal.pause() |
| 117 | except KeyboardInterrupt: |
| 118 | print "Ctrl + C: asking tasks to exit nicely...\n" |
| 119 | self.exit_handler(signal.SIGINT) |