Connect services to networks with the 'networks' key
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
3f9038aea9
commit
3eafdbb01b
12 changed files with 195 additions and 78 deletions
|
|
@ -704,7 +704,7 @@ def run_one_off_container(container_options, project, service, options):
|
|||
**container_options)
|
||||
|
||||
if options['-d']:
|
||||
container.start()
|
||||
service.start_container(container)
|
||||
print(container.name)
|
||||
return
|
||||
|
||||
|
|
@ -716,6 +716,7 @@ def run_one_off_container(container_options, project, service, options):
|
|||
try:
|
||||
try:
|
||||
dockerpty.start(project.client, container.id, interactive=not options['-T'])
|
||||
service.connect_container_to_networks(container)
|
||||
exit_code = container.wait()
|
||||
except signals.ShutdownException:
|
||||
project.client.stop(container.id)
|
||||
|
|
|
|||
|
|
@ -89,6 +89,13 @@
|
|||
"mac_address": {"type": "string"},
|
||||
"mem_limit": {"type": ["number", "string"]},
|
||||
"memswap_limit": {"type": ["number", "string"]},
|
||||
|
||||
"networks": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"uniqueItems": true
|
||||
},
|
||||
|
||||
"pid": {"type": ["string", "null"]},
|
||||
|
||||
"ports": {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ from .config import ConfigurationError
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# TODO: support external networks
|
||||
class Network(object):
|
||||
def __init__(self, client, project, name, driver=None, driver_opts=None):
|
||||
self.client = client
|
||||
|
|
|
|||
|
|
@ -58,7 +58,21 @@ class Project(object):
|
|||
use_networking = (config_data.version and config_data.version >= 2)
|
||||
project = cls(name, [], client, use_networking=use_networking)
|
||||
|
||||
custom_networks = []
|
||||
if config_data.networks:
|
||||
for network_name, data in config_data.networks.items():
|
||||
custom_networks.append(
|
||||
Network(
|
||||
client=client, project=name, name=network_name,
|
||||
driver=data.get('driver'), driver_opts=data.get('driver_opts')
|
||||
)
|
||||
)
|
||||
|
||||
for service_dict in config_data.services:
|
||||
networks = project.get_networks(
|
||||
service_dict,
|
||||
custom_networks + [project.default_network])
|
||||
|
||||
links = project.get_links(service_dict)
|
||||
volumes_from = get_volumes_from(project, service_dict)
|
||||
net = project.get_net(service_dict)
|
||||
|
|
@ -68,19 +82,15 @@ class Project(object):
|
|||
client=client,
|
||||
project=name,
|
||||
use_networking=use_networking,
|
||||
networks=networks,
|
||||
links=links,
|
||||
net=net,
|
||||
volumes_from=volumes_from,
|
||||
**service_dict))
|
||||
|
||||
if config_data.networks:
|
||||
for network_name, data in config_data.networks.items():
|
||||
project.networks.append(
|
||||
Network(
|
||||
client=client, project=name, name=network_name,
|
||||
driver=data.get('driver'), driver_opts=data.get('driver_opts')
|
||||
)
|
||||
)
|
||||
project.networks += custom_networks
|
||||
if project.uses_default_network():
|
||||
project.networks.append(project.default_network)
|
||||
|
||||
if config_data.volumes:
|
||||
for vol_name, data in config_data.volumes.items():
|
||||
|
|
@ -154,6 +164,18 @@ class Project(object):
|
|||
service.remove_duplicate_containers()
|
||||
return services
|
||||
|
||||
def get_networks(self, service_dict, network_definitions):
|
||||
networks = []
|
||||
for name in service_dict.pop('networks', ['default']):
|
||||
matches = [n for n in network_definitions if n.name == name]
|
||||
if matches:
|
||||
networks.append(matches[0].full_name)
|
||||
else:
|
||||
raise ConfigurationError(
|
||||
'Service "{}" uses an undefined network "{}"'
|
||||
.format(service_dict['name'], name))
|
||||
return networks
|
||||
|
||||
def get_links(self, service_dict):
|
||||
links = []
|
||||
if 'links' in service_dict:
|
||||
|
|
@ -172,10 +194,11 @@ class Project(object):
|
|||
return links
|
||||
|
||||
def get_net(self, service_dict):
|
||||
if self.use_networking:
|
||||
return Net(None)
|
||||
|
||||
net = service_dict.pop('net', None)
|
||||
if not net:
|
||||
if self.use_networking:
|
||||
return Net(self.default_network.full_name)
|
||||
return Net(None)
|
||||
|
||||
net_name = get_service_name_from_net(net)
|
||||
|
|
@ -282,6 +305,9 @@ class Project(object):
|
|||
volume.remove()
|
||||
|
||||
def initialize_networks(self):
|
||||
if not self.use_networking:
|
||||
return
|
||||
|
||||
networks = self.networks
|
||||
if self.uses_default_network():
|
||||
networks.append(self.default_network)
|
||||
|
|
@ -291,7 +317,7 @@ class Project(object):
|
|||
|
||||
def uses_default_network(self):
|
||||
return any(
|
||||
service.net.mode == self.default_network.full_name
|
||||
self.default_network.full_name in service.networks
|
||||
for service in self.services
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ class Service(object):
|
|||
links=None,
|
||||
volumes_from=None,
|
||||
net=None,
|
||||
networks=None,
|
||||
**options
|
||||
):
|
||||
self.name = name
|
||||
|
|
@ -125,6 +126,7 @@ class Service(object):
|
|||
self.links = links or []
|
||||
self.volumes_from = volumes_from or []
|
||||
self.net = net or Net(None)
|
||||
self.networks = networks or []
|
||||
self.options = options
|
||||
|
||||
def containers(self, stopped=False, one_off=False, filters={}):
|
||||
|
|
@ -175,7 +177,7 @@ class Service(object):
|
|||
|
||||
def create_and_start(service, number):
|
||||
container = service.create_container(number=number, quiet=True)
|
||||
container.start()
|
||||
service.start_container(container)
|
||||
return container
|
||||
|
||||
running_containers = self.containers(stopped=False)
|
||||
|
|
@ -348,7 +350,7 @@ class Service(object):
|
|||
container.attach_log_stream()
|
||||
|
||||
if start:
|
||||
container.start()
|
||||
self.start_container(container)
|
||||
|
||||
return [container]
|
||||
|
||||
|
|
@ -406,7 +408,7 @@ class Service(object):
|
|||
if attach_logs:
|
||||
new_container.attach_log_stream()
|
||||
if start_new_container:
|
||||
new_container.start()
|
||||
self.start_container(new_container)
|
||||
container.remove()
|
||||
return new_container
|
||||
|
||||
|
|
@ -415,9 +417,18 @@ class Service(object):
|
|||
log.info("Starting %s" % container.name)
|
||||
if attach_logs:
|
||||
container.attach_log_stream()
|
||||
container.start()
|
||||
return self.start_container(container)
|
||||
|
||||
def start_container(self, container):
|
||||
container.start()
|
||||
self.connect_container_to_networks(container)
|
||||
return container
|
||||
|
||||
def connect_container_to_networks(self, container):
|
||||
for network in self.networks:
|
||||
log.debug('Connecting "{}" to "{}"'.format(container.name, network))
|
||||
self.client.connect_container_to_network(container.id, network)
|
||||
|
||||
def remove_duplicate_containers(self, timeout=DEFAULT_TIMEOUT):
|
||||
for c in self.duplicate_containers():
|
||||
log.info('Removing %s' % c.name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue