Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow links to containers outside of the project #544

Merged
merged 1 commit into from
Jan 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ An entry with the alias' name will be created in `/etc/hosts` inside containers

Environment variables will also be created - see the [environment variable reference](env.html) for details.

### external_links

Link to containers started outside this `fig.yml` or even outside of fig, especially for containers that provide shared or common services.
`external_links` follow semantics similar to `links` when specifying both the container name and the link alias (`CONTAINER:ALIAS`).

```
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
```

### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container port (a random host port will be chosen).
Expand Down
12 changes: 10 additions & 2 deletions fig/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ class ConfigError(ValueError):


class Service(object):
def __init__(self, name, client=None, project='default', links=None, volumes_from=None, **options):
def __init__(self, name, client=None, project='default', links=None, external_links=None, volumes_from=None, **options):
if not re.match('^%s+$' % VALID_NAME_CHARS, name):
raise ConfigError('Invalid service name "%s" - only %s are allowed' % (name, VALID_NAME_CHARS))
if not re.match('^%s+$' % VALID_NAME_CHARS, project):
raise ConfigError('Invalid project name "%s" - only %s are allowed' % (project, VALID_NAME_CHARS))
if 'image' in options and 'build' in options:
raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name)

supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose']
supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose',
'external_links']

for k in options:
if k not in supported_options:
Expand All @@ -95,6 +96,7 @@ def __init__(self, name, client=None, project='default', links=None, volumes_fro
self.client = client
self.project = project
self.links = links or []
self.external_links = external_links or []
self.volumes_from = volumes_from or []
self.options = options

Expand Down Expand Up @@ -345,6 +347,12 @@ def _get_links(self, link_to_self):
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:
if ':' not in external_link:
link_name = external_link
else:
external_link, link_name = external_link.split(':')
links.append((external_link, link_name))
return links

def _get_volumes_from(self, intermediate_container=None):
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ def test_start_container_creates_links_with_names(self):
]),
)

def test_start_container_with_external_links(self):
db = self.create_service('db')
web = self.create_service('web', external_links=['figtest_db_1',
'figtest_db_2',
'figtest_db_3:db_3'])

db.start_container()
db.start_container()
db.start_container()
web.start_container()

self.assertEqual(
set(web.containers()[0].links()),
set([
'figtest_db_1',
'figtest_db_2',
'db_3',
]),
)

def test_start_normal_container_does_not_create_links_to_its_own_service(self):
db = self.create_service('db')

Expand Down