Skip to content

Commit

Permalink
fix(dependencies): Remove docker-compose dependency (#140)
Browse files Browse the repository at this point in the history
Close #140
  • Loading branch information
Toilal committed Dec 22, 2020
1 parent 1025018 commit ac12c8a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ddb/feature/docker/actions.py
Expand Up @@ -5,7 +5,7 @@
from typing import Union, Iterable, List, Dict

import yaml
from compose.config.types import ServicePort
from .lib.compose.config.types import ServicePort
from ddb.feature import features
from ddb.feature.traefik import TraefikExtraServicesAction
from dotty_dict import Dotty
Expand Down
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions ddb/feature/docker/lib/compose/config/errors.py
@@ -0,0 +1,6 @@
class ConfigurationError(Exception):
def __init__(self, msg):
self.msg = msg

def __str__(self):
return self.msg
91 changes: 91 additions & 0 deletions ddb/feature/docker/lib/compose/config/types.py
@@ -0,0 +1,91 @@
from collections import namedtuple

from .errors import ConfigurationError
from docker.utils.ports import build_port_bindings


class ServicePort(namedtuple('_ServicePort', 'target published protocol mode external_ip')):
def __new__(cls, target, published, *args, **kwargs):
try:
if target:
target = int(target)
except ValueError:
raise ConfigurationError('Invalid target port: {}'.format(target))

if published:
if isinstance(published, str) and '-' in published: # "x-y:z" format
a, b = published.split('-', 1)
if not a.isdigit() or not b.isdigit():
raise ConfigurationError('Invalid published port: {}'.format(published))
else:
try:
published = int(published)
except ValueError:
raise ConfigurationError('Invalid published port: {}'.format(published))

return super().__new__(
cls, target, published, *args, **kwargs
)

@classmethod
def parse(cls, spec):
if isinstance(spec, cls):
# When extending a service with ports, the port definitions have already been parsed
return [spec]

if not isinstance(spec, dict):
result = []
try:
for k, v in build_port_bindings([spec]).items():
if '/' in k:
target, proto = k.split('/', 1)
else:
target, proto = (k, None)
for pub in v:
if pub is None:
result.append(
cls(target, None, proto, None, None)
)
elif isinstance(pub, tuple):
result.append(
cls(target, pub[1], proto, None, pub[0])
)
else:
result.append(
cls(target, pub, proto, None, None)
)
except ValueError as e:
raise ConfigurationError(str(e))

return result

return [cls(
spec.get('target'),
spec.get('published'),
spec.get('protocol'),
spec.get('mode'),
None
)]

@property
def merge_field(self):
return (self.target, self.published, self.external_ip, self.protocol)

def repr(self):
return {
k: v for k, v in zip(self._fields, self) if v is not None
}

def legacy_repr(self):
return normalize_port_dict(self.repr())


def normalize_port_dict(port):
return '{external_ip}{has_ext_ip}{published}{is_pub}{target}/{protocol}'.format(
published=port.get('published', ''),
is_pub=(':' if port.get('published') is not None or port.get('external_ip') else ''),
target=port.get('target'),
protocol=port.get('protocol', 'tcp'),
external_ip=port.get('external_ip', ''),
has_ext_ip=(':' if port.get('external_ip') else ''),
)
2 changes: 1 addition & 1 deletion requirements-dev.txt
@@ -1,11 +1,11 @@
python-semantic-release
commitizen
pre-commit
githubrelease
tox
pylint
pytest
coverage
docker-compose
https://github.com/Toilal/pytest-docker-compose/archive/configuration-inside-tests.zip
waiting
pytest-mock
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -27,7 +27,6 @@ cryptography
watchdog
gitpython
chmod-monkey>=1.1.1
docker-compose
paramiko
wcmatch
marshmallow-union
Expand Down

0 comments on commit ac12c8a

Please sign in to comment.