From c709251f211113d7729e4ed8e3cc8060c2dbd958 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Sun, 2 Mar 2014 00:17:19 +0800 Subject: [PATCH 1/3] Add custom link names (Closes #72) --- fig/project.py | 14 ++++++++++---- fig/service.py | 4 +++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/fig/project.py b/fig/project.py index 235826d28..1b4d2725c 100644 --- a/fig/project.py +++ b/fig/project.py @@ -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 diff --git a/fig/service.py b/fig/service.py index e98441f84..c4ebd85e8 100644 --- a/fig/service.py +++ b/fig/service.py @@ -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(): From e38e8666268b89cf12c0a947be81bb367bdb88eb Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Sun, 2 Mar 2014 00:30:33 +0800 Subject: [PATCH 2/3] Fix links related test --- tests/service_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/service_test.py b/tests/service_test.py index c2f42510d..258d92619 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -154,7 +154,7 @@ 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()) From 193558a4bce1980676d9fd3d9d95dfb020f3c415 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Mon, 3 Mar 2014 08:54:47 +0800 Subject: [PATCH 3/3] Add link names test --- tests/service_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/service_test.py b/tests/service_test.py index 258d92619..01c1ecabb 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -160,6 +160,13 @@ class ServiceTest(DockerClientTestCase): 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')