Merge pull request #120 from marksteve/link-name

Add custom link names (Closes #72)
This commit is contained in:
Aanand Prasad 2014-03-03 11:22:57 +00:00
commit c07e96cf2b
3 changed files with 21 additions and 6 deletions

View File

@ -12,15 +12,17 @@ def sort_service_dicts(services):
temporary_marked = set()
sorted_services = []
get_service_names = lambda links: [link.split(':')[0] for link in links]
def visit(n):
if n['name'] in temporary_marked:
if n['name'] in n.get('links', []):
if n['name'] in get_service_names(n.get('links', [])):
raise DependencyError('A service can not link to itself: %s' % n['name'])
else:
raise DependencyError('Circular import between %s' % ' and '.join(temporary_marked))
if n in unmarked:
temporary_marked.add(n['name'])
dependents = [m for m in services if n['name'] in m.get('links', [])]
dependents = [m for m in services if n['name'] in get_service_names(m.get('links', []))]
for m in dependents:
visit(m)
temporary_marked.remove(n['name'])
@ -51,8 +53,12 @@ class Project(object):
# Reference links by object
links = []
if 'links' in service_dict:
for service_name in service_dict.get('links', []):
links.append(project.get_service(service_name))
for link in service_dict.get('links', []):
if ':' in link:
service_name, link_name = link.split(':', 1)
else:
service_name, link_name = link, None
links.append((project.get_service(service_name), link_name))
del service_dict['links']
project.services.append(Service(client=client, project=name, links=links, **service_dict))
return project

View File

@ -229,8 +229,10 @@ class Service(object):
def _get_links(self):
links = []
for service in self.links:
for service, link_name in self.links:
for container in service.containers():
if link_name:
links.append((container.name, link_name))
links.append((container.name, container.name))
links.append((container.name, container.name_without_project))
for container in self.containers():

View File

@ -154,12 +154,19 @@ class ServiceTest(DockerClientTestCase):
def test_start_container_creates_links(self):
db = self.create_service('db')
web = self.create_service('web', links=[db])
web = self.create_service('web', links=[(db, None)])
db.start_container()
web.start_container()
self.assertIn('figtest_db_1', web.containers()[0].links())
self.assertIn('db_1', web.containers()[0].links())
def test_start_container_creates_links_with_names(self):
db = self.create_service('db')
web = self.create_service('web', links=[(db, 'custom_link_name')])
db.start_container()
web.start_container()
self.assertIn('custom_link_name', web.containers()[0].links())
def test_start_container_creates_links_to_its_own_service(self):
db1 = self.create_service('db')
db2 = self.create_service('db')