Skip to content

Commit

Permalink
group ports and volumes if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
lindt committed Mar 25, 2016
1 parent 8983b9e commit 621c7dc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
7 changes: 6 additions & 1 deletion bin/compose_plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ if __name__ == '__main__':
help='prints the system boundaries',
default=False,
)
parser.add_argument(
'--group', action='store_const', const=True,
help='group similar properties together',
default=False,
)
parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args()
plantuml = ComposePlantuml()
Expand All @@ -30,7 +35,7 @@ if __name__ == '__main__':
if args.link_graph:
print(plantuml.link_graph(parsed))
if args.boundaries:
print(plantuml.boundaries(parsed))
print(plantuml.boundaries(parsed, args.group))

if len(args.files) == 0:
execute(sys.stdin.read())
Expand Down
23 changes: 18 additions & 5 deletions compose_plantuml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def link_graph(self, compose):
result += '[{0}] ..> [{1}] : depends on\n'.format(source, destination)
return result.strip()

def boundaries(self, compose):
def boundaries(self, compose, group=False):
result = 'skinparam componentStyle uml2\n'

result += 'cloud system {\n'
Expand All @@ -31,25 +31,32 @@ def boundaries(self, compose):
result += '}\n'
volume_registry = {}

volume_uml = ''
for volume in sorted(self.volumes(compose)):
if not self.is_volume_used(compose, volume):
continue
result += 'database {0}'.format(volume) + ' {\n'
volume_uml += 'database {0}'.format(volume) + ' {\n'
for path in sorted(self.volume_usage(compose, volume)):
id = self.volume_identifier(volume, path)

if id in volume_registry:
continue
volume_registry[id] = 'volume_{0}'.format(len(volume_registry.keys()) + 1)
result += ' [{0}] as {1}\n'.format(path, volume_registry[id])
volume_uml += ' [{0}] as {1}\n'.format(path, volume_registry[id])

result += '}\n'
volume_uml += '}\n'
result += self.group('volumes', volume_uml) if group else volume_uml

port_uml = ''
port_links = ''
for service, host, container in sorted(self.ports(compose)):
port = host
if container is not None:
port = '{0} : {1}'.format(host, container)
result += '[{0}] --> {1}\n'.format(service, port)
port_links += '[{0}] --> {1}\n'.format(service, port)
port_uml += 'interface {0}\n'.format(port)
result += self.group('ports', port_uml) if group else ''
result += port_links

for volume in sorted(self.volumes(compose)):
for service, volume_path in sorted(self.service_using_path(compose, volume)):
Expand All @@ -59,6 +66,12 @@ def boundaries(self, compose):
result += '[{0}] --> {1}\n'.format(service, name)
return result.strip()

@staticmethod
def group(name, content):
if len(content) == 0:
return ''
return 'package {0} '.format(name) + '{\n ' + '\n '.join(content.split('\n')).strip() + '\n}\n'

@staticmethod
def is_volume_used(compose, volume):
components = compose if 'version' not in compose else compose.get('services', {})
Expand Down
35 changes: 35 additions & 0 deletions features/boundaries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,41 @@ Feature: Boundaries
"""

Scenario: Group Volumes and Ports
Given a file named "compose.yml" with:
"""
version: "2"
services:
service:
volumes:
- service_log:/log
ports:
- 8080
unused_service: {}
volumes:
service_log: {}
unused_volume: {}
"""
When I run `bin/compose_plantuml --boundaries --group compose.yml`
Then it should pass with exactly:
"""
skinparam componentStyle uml2
cloud system {
[service]
}
package volumes {
database service_log {
[/log] as volume_1
}
}
package ports {
interface 8080
}
[service] --> 8080
[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.8',
version='0.0.9',
description='converts docker-compose into plantuml',
long_description=readme(),
url='http://github.com/funkwerk/compose_plantuml',
Expand Down

0 comments on commit 621c7dc

Please sign in to comment.