Merge pull request #1960 from dnephin/fix_smart_recreate_when_service_is_removed

Fix config_hash context to properly represent the service
This commit is contained in:
Aanand Prasad 2015-09-03 11:41:30 -07:00
commit 6a47fa066e
9 changed files with 230 additions and 71 deletions

View file

@ -326,7 +326,7 @@ class TopLevelCommand(Command):
log.warn(INSECURE_SSL_WARNING)
if not options['--no-deps']:
deps = service.get_linked_names()
deps = service.get_linked_service_names()
if len(deps) > 0:
project.up(

View file

@ -22,9 +22,9 @@ from compose.cli.utils import find_candidates_in_parent_dirs
DOCKER_CONFIG_KEYS = [
'cap_add',
'cap_drop',
'command',
'cpu_shares',
'cpuset',
'command',
'detach',
'devices',
'dns',
@ -38,12 +38,12 @@ DOCKER_CONFIG_KEYS = [
'image',
'labels',
'links',
'log_driver',
'log_opt',
'mac_address',
'mem_limit',
'memswap_limit',
'net',
'log_driver',
'log_opt',
'pid',
'ports',
'privileged',

View file

@ -14,7 +14,10 @@ from .const import LABEL_PROJECT
from .const import LABEL_SERVICE
from .container import Container
from .legacy import check_for_legacy_containers
from .service import ContainerNet
from .service import Net
from .service import Service
from .service import ServiceNet
from .utils import parallel_execute
@ -87,8 +90,14 @@ class Project(object):
volumes_from = project.get_volumes_from(service_dict)
net = project.get_net(service_dict)
project.services.append(Service(client=client, project=name, links=links, net=net,
volumes_from=volumes_from, **service_dict))
project.services.append(
Service(
client=client,
project=name,
links=links,
net=net,
volumes_from=volumes_from,
**service_dict))
return project
@property
@ -184,30 +193,26 @@ class Project(object):
return volumes_from
def get_net(self, service_dict):
if 'net' in service_dict:
net_name = get_service_name_from_net(service_dict.get('net'))
net = service_dict.pop('net', None)
if not net:
return Net(None)
if net_name:
try:
net = self.get_service(net_name)
except NoSuchService:
try:
net = Container.from_id(self.client, net_name)
except APIError:
raise ConfigurationError(
'Service "%s" is trying to use the network of "%s", '
'which is not the name of a service or container.' % (
service_dict['name'],
net_name))
else:
net = service_dict['net']
net_name = get_service_name_from_net(net)
if not net_name:
return Net(net)
del service_dict['net']
else:
net = None
return net
try:
return ServiceNet(self.get_service(net_name))
except NoSuchService:
pass
try:
return ContainerNet(Container.from_id(self.client, net_name))
except APIError:
raise ConfigurationError(
'Service "%s" is trying to use the network of "%s", '
'which is not the name of a service or container.' % (
service_dict['name'],
net_name))
def start(self, service_names=None, **options):
for service in self.get_services(service_names):

View file

@ -87,7 +87,16 @@ ConvergencePlan = namedtuple('ConvergencePlan', 'action containers')
class Service(object):
def __init__(self, name, client=None, project='default', links=None, external_links=None, volumes_from=None, net=None, **options):
def __init__(
self,
name,
client=None,
project='default',
links=None,
volumes_from=None,
net=None,
**options
):
if not re.match('^%s+$' % VALID_NAME_CHARS, project):
raise ConfigError('Invalid project name "%s" - only %s are allowed' % (project, VALID_NAME_CHARS))
@ -95,9 +104,8 @@ class Service(object):
self.client = client
self.project = project
self.links = links or []
self.external_links = external_links or []
self.volumes_from = volumes_from or []
self.net = net or None
self.net = net or Net(None)
self.options = options
def containers(self, stopped=False, one_off=False, filters={}):
@ -480,26 +488,26 @@ class Service(object):
return {
'options': self.options,
'image_id': self.image()['Id'],
'links': self.get_link_names(),
'net': self.net.id,
'volumes_from': self.get_volumes_from_names(),
}
def get_dependency_names(self):
net_name = self.get_net_name()
return (self.get_linked_names() +
net_name = self.net.service_name
return (self.get_linked_service_names() +
self.get_volumes_from_names() +
([net_name] if net_name else []))
def get_linked_names(self):
return [s.name for (s, _) in self.links]
def get_linked_service_names(self):
return [service.name for (service, _) in self.links]
def get_link_names(self):
return [(service.name, alias) for service, alias in self.links]
def get_volumes_from_names(self):
return [s.name for s in self.volumes_from if isinstance(s, Service)]
def get_net_name(self):
if isinstance(self.net, Service):
return self.net.name
else:
return
def get_container_name(self, number, one_off=False):
# TODO: Implement issue #652 here
return build_container_name(self.project, self.name, number, one_off)
@ -528,7 +536,7 @@ class Service(object):
links.append((container.name, self.name))
links.append((container.name, container.name))
links.append((container.name, container.name_without_project))
for external_link in self.external_links:
for external_link in self.options.get('external_links') or []:
if ':' not in external_link:
link_name = external_link
else:
@ -551,25 +559,6 @@ class Service(object):
return volumes_from
def _get_net(self):
if not self.net:
return None
if isinstance(self.net, Service):
containers = self.net.containers()
if len(containers) > 0:
net = 'container:' + containers[0].id
else:
log.warning("Warning: Service %s is trying to use reuse the network stack "
"of another service that is not running." % (self.net.name))
net = None
elif isinstance(self.net, Container):
net = 'container:' + self.net.id
else:
net = self.net
return net
def _get_container_create_options(
self,
override_options,
@ -683,7 +672,7 @@ class Service(object):
binds=options.get('binds'),
volumes_from=self._get_volumes_from(),
privileged=privileged,
network_mode=self._get_net(),
network_mode=self.net.mode,
devices=devices,
dns=dns,
dns_search=dns_search,
@ -782,6 +771,61 @@ class Service(object):
stream_output(output, sys.stdout)
class Net(object):
"""A `standard` network mode (ex: host, bridge)"""
service_name = None
def __init__(self, net):
self.net = net
@property
def id(self):
return self.net
mode = id
class ContainerNet(object):
"""A network mode that uses a container's network stack."""
service_name = None
def __init__(self, container):
self.container = container
@property
def id(self):
return self.container.id
@property
def mode(self):
return 'container:' + self.container.id
class ServiceNet(object):
"""A network mode that uses a service's network stack."""
def __init__(self, service):
self.service = service
@property
def id(self):
return self.service.name
service_name = id
@property
def mode(self):
containers = self.service.containers()
if containers:
return 'container:' + containers[0].id
log.warn("Warning: Service %s is trying to use reuse the network stack "
"of another service that is not running." % (self.id))
return None
# Names