From 0dd1bc6f5c6c4f8905636fda4c6db66ee5c520c3 Mon Sep 17 00:00:00 2001 From: Ofir Petrushka Date: Mon, 2 May 2016 16:52:52 +1000 Subject: [PATCH] add docker auto-restart options --- config/GantryConfig.py | 20 ++++++++++++++++++-- runtime/component.py | 21 ++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/config/GantryConfig.py b/config/GantryConfig.py index 92244a1..b7d13e8 100644 --- a/config/GantryConfig.py +++ b/config/GantryConfig.py @@ -97,6 +97,14 @@ class _EnvironmentVariable(CFObject): def __init__(self): super(_EnvironmentVariable, self).__init__('Environment Variable') +class _RestartPolicy(CFObject): + """ A network link required by a component. """ + name = CFField('name').name_field() + alias = CFField('maximumRetryCount').value_field() + + def __init__(self): + super(_RestartPolicy, self).__init__('Restart Policy') + class _Component(CFObject): """ A single gantry component. """ @@ -116,6 +124,7 @@ class _Component(CFObject): defined_component_links = CFField('defineComponentLinks').list_of(_DefinedComponentLink).default([]) required_component_links = CFField('requireComponentLinks').list_of(_RequiredComponentLink).default([]) environment_variables = CFField('environmentVariables').list_of(_EnvironmentVariable).default([]) + restart_policy = CFField('restartPolicy').kind(dict).default({}) connection_check = _HealthCheck().build({'kind': 'connection'}) termination_checks = CFField('terminationChecks').list_of(_HealthCheck).default([connection_check]) @@ -134,6 +143,13 @@ def getUser(self): return self.user + def getRestartPolicy(self): + """ Returns the host config. """ + if not self.restart_policy: + return None + + return self.restart_policy + def getCommand(self): """ Returns the command string to run on component startup or None if none. """ if not self.command: @@ -167,7 +183,7 @@ def getDefinedComponentLinks(self): def getComponentLinks(self): """ Returns a dict of aliases for component links required, with the values being the links' names. """ return {l.alias: l.name for l in self.required_component_links} - + def getEnvironmentVariables(self): """ Returns a dict of the defined environments variables and their values. """ return {v.name: v.value for v in self.environment_variables} @@ -186,4 +202,4 @@ def lookupComponent(self, name): if component.name == name: return component - return None \ No newline at end of file + return None diff --git a/runtime/component.py b/runtime/component.py index 7322c05..585f971 100644 --- a/runtime/component.py +++ b/runtime/component.py @@ -21,15 +21,15 @@ def __init__(self, manager, config): # The underlying config for the component. self.config = config - + def applyConfigOverrides(self, config_overrides): """ Applies the list of configuration overrides to this component's config. - + Format: ['Name=Value', 'Name.SubName=Value'] """ for override in config_overrides: self.config.applyOverride(override) - + def getName(self): """ Returns the name of the component. """ return self.config.name @@ -223,9 +223,7 @@ def start(self): if self.config.privileged: report('Container will be run in privileged mode', component=self) - client.start(container, binds=self.config.getBindings(container['Id']), - volumes_from=self.config.volumes_from, - privileged=self.config.privileged) + client.start(container) # Health check until the instance is ready. report('Waiting for health checks...', component=self) @@ -309,12 +307,17 @@ def createContainer(self, client): self.logger.debug('Starting container for component %s with command %s', self.getName(), command) - + container = client.create_container(self.config.getFullImage(), command, user=self.config.getUser(), volumes=self.config.getVolumes(), - ports=[str(p) for p in self.config.getContainerPorts()], - environment=self.calculateEnvForComponent()) + environment=self.calculateEnvForComponent(), + host_config=client.create_host_config(restart_policy=self.config.getRestartPolicy(), + volumes_from=self.config.volumes_from, + binds={ b.external: { "bind": b.volume } for b in self.config.bindings }, + privileged=self.config.privileged + ) + ) return container