Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dependencies): Remove docker-compose dependency (#140)
Close #140
- Loading branch information
Showing
7 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class ConfigurationError(Exception): | ||
def __init__(self, msg): | ||
self.msg = msg | ||
|
||
def __str__(self): | ||
return self.msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ''), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters