Skip to content

Commit 8983b9e

Browse files
committed
filter internal services
1 parent 259892e commit 8983b9e

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

compose_plantuml/__init__.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ def boundaries(self, compose):
2626

2727
result += 'cloud system {\n'
2828
for component in sorted(self.components(compose)):
29-
result += ' [{0}]\n'.format(component)
29+
if self.has_service_external_ports(compose, component) or self.has_service_volumes(compose, component):
30+
result += ' [{0}]\n'.format(component)
3031
result += '}\n'
3132
volume_registry = {}
3233

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

62+
@staticmethod
63+
def is_volume_used(compose, volume):
64+
components = compose if 'version' not in compose else compose.get('services', {})
65+
66+
for _, component in components.items():
67+
for volume_name in component.get('volumes', {}):
68+
if volume_name.startswith('{0}:'.format(volume)):
69+
return True
70+
return False
71+
72+
@staticmethod
73+
def is_service_used(compose, service):
74+
components = compose if 'version' not in compose else compose.get('services', {})
75+
76+
for _, component in components.items():
77+
for link in component.get('links', []):
78+
link = link if ':' not in link else link.split(':')[0]
79+
if link == service:
80+
return True
81+
82+
for dependency in component.get('depends_on', []):
83+
if dependency == service:
84+
return True
85+
return False
86+
87+
@staticmethod
88+
def has_service_external_ports(compose, service):
89+
components = compose if 'version' not in compose else compose.get('services', {})
90+
91+
for name, component in components.items():
92+
if service != name:
93+
continue
94+
return 'ports' in component
95+
return False
96+
97+
@staticmethod
98+
def has_service_volumes(compose, service):
99+
components = compose if 'version' not in compose else compose.get('services', {})
100+
101+
for name, component in components.items():
102+
if service != name:
103+
continue
104+
if 'volumes' not in component:
105+
return False
106+
for volume in component['volumes']:
107+
if volume.startswith('/'):
108+
continue
109+
if ':' in volume:
110+
return True
111+
return False
112+
59113
@staticmethod
60114
def volume_identifier(volume, path):
61115
return '{0}.{1}'.format(volume, path)

features/boundaries.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ Feature: Boundaries
6868
6969
"""
7070

71+
Scenario: Filter internal services
72+
Given a file named "compose.yml" with:
73+
"""
74+
version: "2"
75+
services:
76+
service:
77+
volumes:
78+
- service_log:/log
79+
unused_service: {}
80+
volumes:
81+
service_log: {}
82+
unused_volume: {}
83+
"""
84+
When I run `bin/compose_plantuml --boundaries compose.yml`
85+
Then it should pass with exactly:
86+
"""
87+
skinparam componentStyle uml2
88+
cloud system {
89+
[service]
90+
}
91+
database service_log {
92+
[/log] as volume_1
93+
}
94+
[service] --> volume_1
95+
96+
"""
97+
7198
Scenario: Suppport for legacy docker-compose format
7299
Given a file named "compose.yml" with:
73100
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def readme():
77

88
setup(
99
name='compose_plantuml',
10-
version='0.0.7',
10+
version='0.0.8',
1111
description='converts docker-compose into plantuml',
1212
long_description=readme(),
1313
url='http://github.com/funkwerk/compose_plantuml',

0 commit comments

Comments
 (0)