Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for compose port range syntax #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion container_transform/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import yaml

from .transformer import BaseTransformer
from .utils import flatten


class ComposeTransformer(BaseTransformer):
Expand Down Expand Up @@ -95,6 +96,55 @@ def _parse_port_mapping(mapping):
}
mapping = str(mapping).rstrip('/udp')
parts = str(mapping).split(':')
is_port_range = '-' in mapping

if is_port_range:
output = []

# 6060-6061
if len(parts) == 1:
single_range = str(mapping).split('-')

for port in single_range:
output.append({
'protocol': protocol,
'host_port': int(port),
'container_port': int(port),
})

return output

elif len(parts) == 2:
# 9090-9091:8080-8081
if '-' in parts[0] and '-' in parts[1]:
first_range = str(parts[0]).split('-')
second_range = str(parts[1]).split('-')

first_range = list(map(int, first_range))
second_range = list(map(int, second_range))

for host, container in zip(first_range, second_range):
output.append({
'protocol': protocol,
'host_port': host,
'container_port': container,
})

return output

# 12400-12500:1240
elif '-' in parts[0]:
single_range = str(parts[0]).split('-')

for port in range(int(single_range[0]), int(single_range[1]) + 1):
output.append({
'protocol': protocol,
'host_port': int(port),
'container_port': int(parts[1]),
})

return output

if len(parts) == 1:
output.update({
'container_port': int(parts[0])
Expand Down Expand Up @@ -135,7 +185,9 @@ def ingest_port_mappings(self, port_mappings):
:return: the base schema port_mappings
:rtype: list of dict
"""
return [self._parse_port_mapping(mapping) for mapping in port_mappings]
mappings = [self._parse_port_mapping(mapping) for mapping in port_mappings]

return flatten(mappings)

@staticmethod
def _emit_mapping(mapping):
Expand Down
3 changes: 3 additions & 0 deletions container_transform/tests/composev2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ services:
ports:
- "5000:5000"
- "53:53/udp"
- "3000-3001"
- "9090-9091:8080-8081"
- "9092-9093:8082"
pid: "host"
env_file: "./web.env"
volumes:
Expand Down
12 changes: 9 additions & 3 deletions container_transform/tests/composev2_output.service
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# web.service #######################################################################
[Unit]
Description=Web
After=docker.service
Requires=docker.service
After=docker.service
Requires=docker.service

[Service]
ExecStartPre=-/usr/bin/docker kill web
Expand All @@ -13,12 +13,18 @@ ExecStart=/usr/bin/docker run \
--pid host \
-p 5000:5000 \
-p 53:53/udp \
-p 3000:3000 \
-p 3001:3001 \
-p 9090:8080 \
-p 9091:8081 \
-p 9092:8082 \
-p 9093:8082 \
-v .:/code \
--log-driver=gelf \
--log-opt gelf-address=udp://127.0.0.1:12900 \
--log-opt tag=web \
--label com.example.department="Finance" \
--label com.example.description="Accounting webapp" \
--label com.example.label-with-empty-value="" \
<image>
<image>
ExecStop=/usr/bin/docker stop web
11 changes: 11 additions & 0 deletions container_transform/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def flatten(lst, final=None):
if final is None:
final = []

for val in lst:
if isinstance(val, list):
flatten(val, final)
else:
final.append(val)

return final