Done - before package
parent
a1926eb153
commit
36dc9ba11c
@ -0,0 +1,26 @@
|
||||
"""
|
||||
Container object handle a single container link
|
||||
"""
|
||||
|
||||
|
||||
class Container(object):
|
||||
|
||||
"""Container handles a single container link"""
|
||||
|
||||
ip = None
|
||||
environ = None
|
||||
names = None
|
||||
links = None
|
||||
|
||||
def __init__(self, ip, env, names, links=None):
|
||||
self.ip = ip
|
||||
self.environ = env
|
||||
self.names = names
|
||||
self.links = self._set_links(links)
|
||||
|
||||
def _set_links(self, links):
|
||||
lst = []
|
||||
for link in links:
|
||||
if link.ip == self.ip:
|
||||
lst.append(link)
|
||||
self.links = tuple(lst)
|
@ -0,0 +1,97 @@
|
||||
"""
|
||||
Link handle a single link to another container, determined by his port
|
||||
"""
|
||||
import fnmatch
|
||||
|
||||
from six import viewitems
|
||||
|
||||
|
||||
class Link(object):
|
||||
|
||||
"""Link object"""
|
||||
|
||||
def __init__(self, ip, env, port, protocol, names):
|
||||
self.ip = ip
|
||||
self.env = env
|
||||
self.port = int(port)
|
||||
self.protocol = protocol
|
||||
self.uri = '{protocol}://{ip}:{port}'.format(
|
||||
protocol=protocol,
|
||||
ip=ip,
|
||||
port=port,
|
||||
)
|
||||
self.names = tuple(names)
|
||||
|
||||
def _filter_name(self, name):
|
||||
"return true if name match"
|
||||
return bool(fnmatch.filter(self.names, name))
|
||||
|
||||
def _filter_port(self, port):
|
||||
"return true if port match"
|
||||
return int(port) == self.port
|
||||
|
||||
def _filter_protocol(self, protocol):
|
||||
"return true if protocol match"
|
||||
return protocol == self.protocol
|
||||
|
||||
def _filter_env(self, env):
|
||||
"return true if env match"
|
||||
if isinstance(env, dict):
|
||||
return viewitems(env) <= viewitems(self.env)
|
||||
if isinstance(env, list):
|
||||
return bool([key for key in env if key in self.env])
|
||||
return str(env) in self.env
|
||||
|
||||
|
||||
class Links(object):
|
||||
|
||||
"""Links embeder"""
|
||||
|
||||
_conf = None
|
||||
_def_options = {'single': False,
|
||||
'required': True}
|
||||
|
||||
def __init__(self, config=None, links=None):
|
||||
if not links or len(links.links()) is 0:
|
||||
pass
|
||||
|
||||
self._links = []
|
||||
for ip, link in links.links().items():
|
||||
for port, protocol in link["ports"].items():
|
||||
self._links.append(Link(ip=ip,
|
||||
env=link['environment'],
|
||||
port=int(port),
|
||||
protocol=protocol['protocol'],
|
||||
names=link['names']))
|
||||
|
||||
self._conf = config
|
||||
|
||||
def _get_link(self, name):
|
||||
config = self._conf[name]
|
||||
links = self._links
|
||||
options = dict(self._def_options)
|
||||
for key, val in config.items():
|
||||
if key in options:
|
||||
options[key] = val
|
||||
continue
|
||||
links = [link for link in links
|
||||
if getattr(link, "_filter_{}".format(key))(val)]
|
||||
|
||||
if options['required'] and len(links) is 0:
|
||||
raise Exception("No links was found for {name}".format(name=name))
|
||||
if options['single'] and len(links) > 1:
|
||||
raise Exception("Only one link should be provided for {name}"
|
||||
.format(name=name))
|
||||
if options['single']:
|
||||
return links[0]
|
||||
return tuple(links)
|
||||
|
||||
@classmethod
|
||||
def _add_name(cls, name):
|
||||
"Add method attribute name"
|
||||
setattr(cls, name, property(lambda self: self._get_link(name)))
|
||||
|
||||
@property
|
||||
def all(self):
|
||||
"""all returns tuple of all Link objects"""
|
||||
return tuple(self._links)
|
@ -0,0 +1,22 @@
|
||||
PyYAML==3.11
|
||||
Pygments==2.0.2
|
||||
argparse==1.4.0
|
||||
aspy.yaml==0.2.1
|
||||
blessings==1.6
|
||||
bpython==0.14.2
|
||||
cached-property==1.3.0
|
||||
curtsies==0.1.23
|
||||
docker-compose==1.5.2
|
||||
docker-py==1.6.0
|
||||
dockerpty==0.3.4
|
||||
docopt==0.6.2
|
||||
greenlet==0.4.9
|
||||
jsonschema==2.5.1
|
||||
nodeenv==0.13.6
|
||||
ordereddict==1.1
|
||||
pre-commit==0.7.5
|
||||
requests==2.7.0
|
||||
six==1.10.0
|
||||
texttable==0.8.4
|
||||
virtualenv==13.1.2
|
||||
websocket-client==0.35.0
|
@ -1,17 +0,0 @@
|
||||
All links:
|
||||
{% for link in links.all %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
||||
|
||||
All links 1:
|
||||
{% for link in links.test1 %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
||||
|
||||
Links 2 800:
|
||||
- {{links.test2_800.uri}}
|
||||
|
||||
All links 2:
|
||||
{% for link in links.test2 %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
@ -0,0 +1,39 @@
|
||||
All links:
|
||||
{% for link in links.all %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
||||
|
||||
All links 1:
|
||||
{% for link in links.test1 %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
Links 2 800:
|
||||
- {{links.test2_800.uri}}
|
||||
|
||||
All links 2:
|
||||
{% for link in links.test2 %}
|
||||
- {{link.uri}}
|
||||
{% endfor %}
|
||||
|
||||
All environ:
|
||||
{% for link in links.all %}
|
||||
{% for key in link.env %}
|
||||
{{key}}: {{link.env[key]}}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
All links 2 environ:
|
||||
{% for link in links.test2 %}
|
||||
{% for key in link.env %}
|
||||
{{key}}: {{link.env[key]}}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
All names:
|
||||
{% for container in containers %}
|
||||
{% for name in container.names %}
|
||||
- {{name}}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
Loading…
Reference in New Issue