Skip to content

Commit

Permalink
Merge pull request #16 from ambitioninc/develop
Browse files Browse the repository at this point in the history
0.5.0 Release
  • Loading branch information
Micah Hausler committed Jun 22, 2015
2 parents 6f62146 + 7338c18 commit 168f899
Show file tree
Hide file tree
Showing 21 changed files with 887 additions and 423 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ python:
- '3.4'
install:
- pip install coveralls flake8 nose>=1.3.0
- python setup.py install
- pip install -r requirements/docs.txt
- pip install -e .[all]
script:
- flake8 .
- python setup.py nosetests
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include README.rst
include CONTRIBUTORS.txt
include LICENSE
prune container_transform/tests
89 changes: 50 additions & 39 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

.. image:: https://coveralls.io/repos/ambitioninc/container-transform/badge.png?branch=develop
:target: https://coveralls.io/r/ambitioninc/container-transform?branch=develop
.. image:: https://pypip.in/v/container-transform/badge.png
:target: https://pypi.python.org/pypi/container-transform/
:alt: Latest PyPI version

.. image:: https://pypip.in/d/container-transform/badge.png
:target: https://pypi.python.org/pypi/container-transform/
:alt: Number of PyPI downloads


container-transform
Expand All @@ -26,40 +20,57 @@ Quickstart
::

$ cat docker-compose.yml | container-transform -v
[
{
"memory": 1024,
"image": "postgres:9.3",
"name": "db",
"essential": true
},
{
"memory": 128,
"image": "redis:latest",
"name": "redis",
"essential": true
},
{
"name": "web",
"memory": 64,
"command": [
"uwsgi",
"--json",
"uwsgi.json"
],
"environment": [
{
"name": "AWS_ACCESS_KEY_ID",
"value": "AAAAAAAAAAAAAAAAAAAA"
},
{
"name": "AWS_SECRET_ACCESS_KEY",
"value": "1111111111111111111111111111111111111111"
{
"family": "python-app",
"volumes": [
{
"name": "host_logs",
"host": {
"sourcePath": "/var/log/myapp"
}
],
"essential": true
}
]
}
],
"containerDefinitions": [
{
"memory": 1024,
"image": "postgres:9.3",
"name": "db",
"essential": true
},
{
"memory": 128,
"image": "redis:latest",
"name": "redis",
"essential": true
},
{
"name": "web",
"memory": 64,
"command": [
"uwsgi",
"--json",
"uwsgi.json"
],
"mountPoints": [
{
"sourceVolume": "host_logs",
"containerPath": "/var/log/uwsgi/"
}
],
"environment": [
{
"name": "AWS_ACCESS_KEY_ID",
"value": "AAAAAAAAAAAAAAAAAAAA"
},
{
"name": "AWS_SECRET_ACCESS_KEY",
"value": "1111111111111111111111111111111111111111"
}
],
"essential": true
}
]
}
Container db is missing required parameter "cpu".
Container redis is missing required parameter "cpu".
Container web is missing required parameter "image".
Expand Down
101 changes: 69 additions & 32 deletions container_transform/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def ingest_containers(self, containers=None):

return output_containers

@staticmethod
def emit_containers(containers, verbose=True):
def emit_containers(self, containers, verbose=True):

output = {}
for container in containers:
Expand Down Expand Up @@ -95,8 +94,7 @@ def _parse_port_mapping(mapping):
'container_port': int(parts[3])
}

@staticmethod
def ingest_port_mappings(port_mappings):
def ingest_port_mappings(self, port_mappings):
"""
Transform the docker-compose port mappings to base schema port_mappings
Expand All @@ -105,7 +103,7 @@ def ingest_port_mappings(port_mappings):
:return: the base schema port_mappings
:rtype: list of dict
"""
return [ComposeTransformer._parse_port_mapping(mapping) for mapping in port_mappings]
return [self._parse_port_mapping(mapping) for mapping in port_mappings]

@staticmethod
def _emit_mapping(mapping):
Expand All @@ -120,18 +118,16 @@ def _emit_mapping(mapping):
parts.append(str(mapping['container_port']))
return ':'.join(parts)

@staticmethod
def emit_port_mappings(port_mappings):
def emit_port_mappings(self, port_mappings):
"""
:param port_mappings: the base schema port_mappings
:type port_mappings: list of dict
:return:
:rtype: list of str
"""
return [str(ComposeTransformer._emit_mapping(mapping)) for mapping in port_mappings]
return [str(self._emit_mapping(mapping)) for mapping in port_mappings]

@staticmethod
def ingest_memory(memory):
def ingest_memory(self, memory):
"""
Transform the memory into bytes
Expand All @@ -156,20 +152,16 @@ def rshift(num, shift):
number = int(memory[:-1])
return bit_shift[unit]['func'](number, bit_shift[unit]['shift'])

@staticmethod
def emit_memory(memory):
def emit_memory(self, memory):
return '{}b'.format(memory)

@staticmethod
def ingest_cpu(cpu):
def ingest_cpu(self, cpu):
return cpu

@staticmethod
def emit_cpu(cpu):
def emit_cpu(self, cpu):
return cpu

@staticmethod
def ingest_environment(environment):
def ingest_environment(self, environment):
output = {}
if type(environment) is list:
for kv in environment:
Expand All @@ -180,30 +172,75 @@ def ingest_environment(environment):
output[str(key)] = str(value)
return output

@staticmethod
def emit_environment(environment):
def emit_environment(self, environment):
return environment

@staticmethod
def ingest_command(command):
def ingest_command(self, command):
return command

@staticmethod
def emit_command(command):
def emit_command(self, command):
return command

@staticmethod
def ingest_entrypoint(entrypoint):
def ingest_entrypoint(self, entrypoint):
return entrypoint

@staticmethod
def emit_entrypoint(entrypoint):
def emit_entrypoint(self, entrypoint):
return entrypoint

@staticmethod
def ingest_volumes_from(volumes_from):
def ingest_volumes_from(self, volumes_from):
return volumes_from

@staticmethod
def emit_volumes_from(volumes_from):
def emit_volumes_from(self, volumes_from):
return volumes_from

@staticmethod
def _ingest_volume(volume):
parts = volume.split(':')

if len(parts) == 1:
return {
'host': parts[0],
'container': parts[0]
}
if len(parts) == 2 and parts[1] != 'ro':
return {
'host': parts[0],
'container': parts[1]
}
if len(parts) == 2 and parts[1] == 'ro':
return {
'host': parts[0],
'container': parts[0],
'readonly': True
}
if len(parts) == 3 and parts[-1] == 'ro':
return {
'host': parts[0],
'container': parts[1],
'readonly': True
}

def ingest_volumes(self, volumes):
return [
self._ingest_volume(volume)
for volume
in volumes
if self._ingest_volume(volume) is not None
]

@staticmethod
def _emit_volume(volume):
volume_str = volume.get('host') + ':' + volume.get('container', ':')
volume_str = volume_str.strip(':')

if volume.get('readonly') and len(volume_str):
volume_str += ':ro'
return volume_str

def emit_volumes(self, volumes):
return [
self._emit_volume(volume)
for volume
in volumes
if len(self._emit_volume(volume))
]
16 changes: 10 additions & 6 deletions container_transform/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ def convert(self, verbose=True):
for container in containers:
validated = output_transformer.validate(container)

converted_container = self._convert_container(validated)
converted_container = self._convert_container(
validated,
input_transformer,
output_transformer
)

validated = output_transformer.validate(converted_container)

output_containers.append(validated)

return output_transformer.emit_containers(output_containers, verbose)

def _convert_container(self, container):
def _convert_container(self, container, input_transformer, output_transformer):
"""
Converts a given dictionary to an output container definition
Expand Down Expand Up @@ -84,11 +88,11 @@ def _convert_container(self, container):
)

if container.get(input_name) and \
hasattr(self._input_class, 'ingest_{}'.format(parameter)) and \
output_name and hasattr(self._output_class, 'emit_{}'.format(parameter)):
hasattr(input_transformer, 'ingest_{}'.format(parameter)) and \
output_name and hasattr(output_transformer, 'emit_{}'.format(parameter)):
# call transform_{}
ingest_func = getattr(self._input_class, 'ingest_{}'.format(parameter))
emit_func = getattr(self._output_class, 'emit_{}'.format(parameter))
ingest_func = getattr(input_transformer, 'ingest_{}'.format(parameter))
emit_func = getattr(output_transformer, 'emit_{}'.format(parameter))

output[output_name] = emit_func(ingest_func(container.get(input_name)))

Expand Down
Loading

0 comments on commit 168f899

Please sign in to comment.