2014-11-11 23:23:15 +00:00
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
2014-11-25 21:14:44 +00:00
|
|
|
from buildman.component.basecomponent import BaseComponent
|
|
|
|
from buildman.component.buildcomponent import BuildComponent
|
2014-11-11 23:23:15 +00:00
|
|
|
from buildman.manager.basemanager import BaseManager
|
|
|
|
|
2015-01-29 15:56:18 +00:00
|
|
|
from trollius import From, Return, coroutine
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
REGISTRATION_REALM = 'registration'
|
2014-11-30 22:48:02 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
class DynamicRegistrationComponent(BaseComponent):
|
|
|
|
""" Component session that handles dynamic registration of the builder components. """
|
|
|
|
|
|
|
|
def onConnect(self):
|
|
|
|
self.join(REGISTRATION_REALM)
|
|
|
|
|
|
|
|
def onJoin(self, details):
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Registering registration method')
|
2014-11-11 23:23:15 +00:00
|
|
|
yield From(self.register(self._worker_register, u'io.quay.buildworker.register'))
|
|
|
|
|
|
|
|
def _worker_register(self):
|
|
|
|
realm = self.parent_manager.add_build_component()
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Registering new build component+worker with realm %s', realm)
|
2014-11-11 23:23:15 +00:00
|
|
|
return realm
|
|
|
|
|
2015-02-18 00:15:54 +00:00
|
|
|
def kind(self):
|
|
|
|
return 'registration'
|
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
class EnterpriseManager(BaseManager):
|
|
|
|
""" Build manager implementation for the Enterprise Registry. """
|
|
|
|
|
2014-12-16 18:41:30 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.ready_components = set()
|
2015-01-29 15:56:18 +00:00
|
|
|
self.all_components = set()
|
2014-12-16 18:41:30 +00:00
|
|
|
self.shutting_down = False
|
|
|
|
|
2015-01-29 15:56:18 +00:00
|
|
|
super(EnterpriseManager, self).__init__(*args, **kwargs)
|
|
|
|
|
2014-12-16 18:41:30 +00:00
|
|
|
def initialize(self, manager_config):
|
2014-11-11 23:23:15 +00:00
|
|
|
# Add a component which is used by build workers for dynamic registration. Unlike
|
|
|
|
# production, build workers in enterprise are long-lived and register dynamically.
|
|
|
|
self.register_component(REGISTRATION_REALM, DynamicRegistrationComponent)
|
2014-11-18 20:45:56 +00:00
|
|
|
|
2014-11-21 19:27:06 +00:00
|
|
|
def setup_time(self):
|
|
|
|
# Builders are already registered, so the setup time should be essentially instant. We therefore
|
|
|
|
# only return a minute here.
|
|
|
|
return 60
|
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
def add_build_component(self):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Adds a new build component for an Enterprise Registry. """
|
2014-11-11 23:23:15 +00:00
|
|
|
# Generate a new unique realm ID for the build worker.
|
|
|
|
realm = str(uuid.uuid4())
|
2015-01-29 15:56:18 +00:00
|
|
|
new_component = self.register_component(realm, BuildComponent, token="")
|
|
|
|
self.all_components.add(new_component)
|
2014-11-11 23:23:15 +00:00
|
|
|
return realm
|
|
|
|
|
2014-12-22 17:14:16 +00:00
|
|
|
@coroutine
|
2014-12-31 16:33:56 +00:00
|
|
|
def schedule(self, build_job):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Schedules a build for an Enterprise Registry. """
|
2014-12-16 18:41:30 +00:00
|
|
|
if self.shutting_down or not self.ready_components:
|
2014-12-22 17:14:16 +00:00
|
|
|
raise Return(False)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-16 18:41:30 +00:00
|
|
|
component = self.ready_components.pop()
|
2014-12-31 16:33:56 +00:00
|
|
|
|
|
|
|
yield From(component.start_build(build_job))
|
|
|
|
|
2014-12-22 17:14:16 +00:00
|
|
|
raise Return(True)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-31 16:33:56 +00:00
|
|
|
@coroutine
|
|
|
|
def build_component_ready(self, build_component):
|
2014-12-16 18:41:30 +00:00
|
|
|
self.ready_components.add(build_component)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.shutting_down = True
|
|
|
|
|
2014-12-22 17:14:16 +00:00
|
|
|
@coroutine
|
2014-11-12 19:03:07 +00:00
|
|
|
def job_completed(self, build_job, job_status, build_component):
|
|
|
|
self.job_complete_callback(build_job, job_status)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
def build_component_disposed(self, build_component, timed_out):
|
2015-01-29 15:56:18 +00:00
|
|
|
self.all_components.remove(build_component)
|
2014-12-16 18:41:30 +00:00
|
|
|
if build_component in self.ready_components:
|
|
|
|
self.ready_components.remove(build_component)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-18 00:15:54 +00:00
|
|
|
self.unregister_component(build_component)
|
|
|
|
|
2014-12-16 18:37:40 +00:00
|
|
|
def num_workers(self):
|
2015-01-29 15:56:18 +00:00
|
|
|
return len(self.all_components)
|