Skip to content

Commit

Permalink
filter internal services
Browse files Browse the repository at this point in the history
  • Loading branch information
lindt committed Mar 24, 2016
1 parent 259892e commit 8983b9e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
56 changes: 55 additions & 1 deletion compose_plantuml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def boundaries(self, compose):

result += 'cloud system {\n'
for component in sorted(self.components(compose)):
result += ' [{0}]\n'.format(component)
if self.has_service_external_ports(compose, component) or self.has_service_volumes(compose, component):
result += ' [{0}]\n'.format(component)
result += '}\n'
volume_registry = {}

for volume in sorted(self.volumes(compose)):
if not self.is_volume_used(compose, volume):
continue
result += 'database {0}'.format(volume) + ' {\n'
for path in sorted(self.volume_usage(compose, volume)):
id = self.volume_identifier(volume, path)
Expand All @@ -56,6 +59,57 @@ def boundaries(self, compose):
result += '[{0}] --> {1}\n'.format(service, name)
return result.strip()

@staticmethod
def is_volume_used(compose, volume):
components = compose if 'version' not in compose else compose.get('services', {})

for _, component in components.items():
for volume_name in component.get('volumes', {}):
if volume_name.startswith('{0}:'.format(volume)):
return True
return False

@staticmethod
def is_service_used(compose, service):
components = compose if 'version' not in compose else compose.get('services', {})

for _, component in components.items():
for link in component.get('links', []):
link = link if ':' not in link else link.split(':')[0]
if link == service:
return True

for dependency in component.get('depends_on', []):
if dependency == service:
return True
return False

@staticmethod
def has_service_external_ports(compose, service):
components = compose if 'version' not in compose else compose.get('services', {})

for name, component in components.items():
if service != name:
continue
return 'ports' in component
return False

@staticmethod
def has_service_volumes(compose, service):
components = compose if 'version' not in compose else compose.get('services', {})

for name, component in components.items():
if service != name:
continue
if 'volumes' not in component:
return False
for volume in component['volumes']:
if volume.startswith('/'):
continue
if ':' in volume:
return True
return False

@staticmethod
def volume_identifier(volume, path):
return '{0}.{1}'.format(volume, path)
Expand Down
27 changes: 27 additions & 0 deletions features/boundaries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ Feature: Boundaries
"""

Scenario: Filter internal services
Given a file named "compose.yml" with:
"""
version: "2"
services:
service:
volumes:
- service_log:/log
unused_service: {}
volumes:
service_log: {}
unused_volume: {}
"""
When I run `bin/compose_plantuml --boundaries compose.yml`
Then it should pass with exactly:
"""
skinparam componentStyle uml2
cloud system {
[service]
}
database service_log {
[/log] as volume_1
}
[service] --> volume_1
"""

Scenario: Suppport for legacy docker-compose format
Given a file named "compose.yml" with:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def readme():

setup(
name='compose_plantuml',
version='0.0.7',
version='0.0.8',
description='converts docker-compose into plantuml',
long_description=readme(),
url='http://github.com/funkwerk/compose_plantuml',
Expand Down

0 comments on commit 8983b9e

Please sign in to comment.