Skip to content

Commit

Permalink
Merge pull request #2 from lindt/master
Browse files Browse the repository at this point in the history
added notes to services
  • Loading branch information
lindt committed Mar 26, 2016
2 parents 54bad56 + 4517c45 commit 642675a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
9 changes: 7 additions & 2 deletions bin/compose_plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ if __name__ == '__main__':
help='group similar properties together',
default=False,
)
parser.add_argument(
'--notes', action='store_const', const=True,
help='utilize notes for displaying additional information',
default=False,
)
parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args()
plantuml = ComposePlantuml()
Expand All @@ -33,9 +38,9 @@ if __name__ == '__main__':
parsed = plantuml.parse(data)

if args.link_graph:
print(plantuml.link_graph(parsed))
print(plantuml.link_graph(parsed, notes=args.notes))
if args.boundaries:
print(plantuml.boundaries(parsed, args.group))
print(plantuml.boundaries(parsed, group=args.group, notes=args.notes))

if len(args.files) == 0:
execute(sys.stdin.read())
Expand Down
43 changes: 40 additions & 3 deletions compose_plantuml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self):
def parse(self, data):
return load(data)

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

for component in sorted(self.components(compose)):
Expand All @@ -19,9 +19,17 @@ def link_graph(self, compose):
result += '[{0}] --> [{1}]\n'.format(source, destination)
for source, destination in sorted(self.dependencies(compose)):
result += '[{0}] ..> [{1}] : depends on\n'.format(source, destination)
if notes:
for component_name in sorted(self.components(compose)):
component = self.component(compose, component_name)
if 'labels' in component:
labels = []
for key, value in component['labels'].items():
labels.append('{0}={1}'.format(key, value))
result += 'note top of [{0}]\n {1}\nend note\n'.format(component_name, '\n '.join(labels))
return result.strip()

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

result += 'cloud system {\n'
Expand Down Expand Up @@ -62,8 +70,30 @@ def boundaries(self, compose, group=False):
if '{0}.{1}'.format(volume, volume_path) in volume_registry:
name = volume_registry['{0}.{1}'.format(volume, volume_path)]
result += '[{0}] --> {1}\n'.format(service, name)

if notes:
for component_name in sorted(self.components(compose)):
if not (self.has_service_external_ports(compose, component_name) or self.has_service_volumes(compose, component_name)):
continue
if not self.labels(compose, component_name):
continue
labels = []
for key, value in self.labels(compose, component_name).items():
labels.append('{0}={1}'.format(key, value))
result += 'note top of [{0}]\n {1}\nend note\n'.format(component_name, '\n '.join(labels))

return result.strip()

@staticmethod
def labels(compose, service):
service = ComposePlantuml.component(compose, service)
if 'labels' not in service:
return None
if type(service['labels']) is str:
key, value = service['labels'].split(':')
return {key: value}
return service['labels']

@staticmethod
def group(name, content):
if len(content) == 0:
Expand Down Expand Up @@ -129,7 +159,14 @@ def volume_identifier(volume, path):
def components(compose):
if 'version' not in compose:
return [component for component in compose]
return [component for component in compose.get('services', [])]
return [component for component in compose.get('services', {})]

@staticmethod
def component(compose, name):
root = compose if 'version' not in compose else compose['services']

assert name in root
return root[name]

@staticmethod
def links(compose):
Expand Down
29 changes: 29 additions & 0 deletions features/boundaries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ Feature: Boundaries
"""

Scenario: Notes
Given a file named "compose.yml" with:
"""
version: "2"
services:
service:
volumes:
- service_log:/log
labels:
key:value
volumes:
service_log: {}
"""
When I run `bin/compose_plantuml --boundaries --notes compose.yml`
Then it should pass with exactly:
"""
skinparam componentStyle uml2
cloud system {
[service]
}
database service_log {
[/log] as volume_1
}
[service] --> volume_1
note top of [service]
key=value
end note
"""

Scenario: Suppport for legacy docker-compose format
Given a file named "compose.yml" with:
"""
Expand Down
20 changes: 20 additions & 0 deletions features/link_graph.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ Feature: Link Graph
"""

Scenario: Notes
Given a file named "compose.yml" with:
"""
version: "2"
services:
first:
labels:
key: value
"""
When I run `bin/compose_plantuml --link-graph --notes compose.yml`
Then it should pass with exactly:
"""
skinparam componentStyle uml2
[first]
note top of [first]
key=value
end note
"""

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.10',
version='0.0.11',
description='converts docker-compose into plantuml',
long_description=readme(),
url='http://github.com/funkwerk/compose_plantuml',
Expand Down

0 comments on commit 642675a

Please sign in to comment.