Refactor network_mode logic out of Service.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2015-09-03 13:02:46 -04:00
commit 187ad4ce26
4 changed files with 116 additions and 37 deletions

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
@ -192,18 +195,18 @@ class Project(object):
def get_net(self, service_dict):
net = service_dict.pop('net', None)
if not net:
return
return Net(None)
net_name = get_service_name_from_net(net)
if not net_name:
return net
return Net(net)
try:
return self.get_service(net_name)
return ServiceNet(self.get_service(net_name))
except NoSuchService:
pass
try:
return Container.from_id(self.client, net_name)
return ContainerNet(Container.from_id(self.client, net_name))
except APIError:
raise ConfigurationError(
'Service "%s" is trying to use the network of "%s", '

View file

@ -105,7 +105,7 @@ class Service(object):
self.project = project
self.links = 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={}):
@ -489,12 +489,12 @@ class Service(object):
'options': self.options,
'image_id': self.image()['Id'],
'links': [(service.name, alias) for service, alias in self.links],
'net': self.get_net_name() or getattr(self.net, 'id', self.net),
'net': self.net.id,
'volumes_from': self.get_volumes_from_names(),
}
def get_dependency_names(self):
net_name = self.get_net_name()
net_name = self.net.service_name
return (self.get_linked_names() +
self.get_volumes_from_names() +
([net_name] if net_name else []))
@ -505,12 +505,6 @@ class Service(object):
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)
@ -562,25 +556,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,
@ -694,7 +669,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,
@ -793,6 +768,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 containers 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