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