2
0
mirror of https://github.com/cmehay/pyentrypoint synced 2024-11-10 13:10:37 +00:00

links almost okay

This commit is contained in:
Christophe Mehay 2015-12-07 03:19:46 +01:00
parent d46a273a12
commit fc23fbf874
7 changed files with 71 additions and 37 deletions

View File

@ -2,10 +2,11 @@
FROM python:2
RUN pip install pytest
RUN pip install pytest twiggy six pyyaml
ADD . /opt/docker-tools/
ADD python-entrypoint /opt/python-entrypoint/
ADD tests/entrypoint-config.yml /opt/python-entrypoint/
WORKDIR /opt/docker-tools/
WORKDIR /opt/python-entrypoint/
CMD ["py.test", "."]
CMD ["py.test", "-s", "."]

View File

@ -2,12 +2,11 @@
FROM python:3
#RUN pip3 install pytest
RUN pip3 install pytest twiggy six pyyaml
ADD python-entrypoint /opt/docker-entrypoint/
ADD python-entrypoint /opt/python-entrypoint/
ADD tests/entrypoint-config.yml /opt/python-entrypoint/
WORKDIR /opt/docker-entrypoint/
WORKDIR /opt/python-entrypoint/
ENTRYPOINT ["./entrypoint.py"]
CMD ["echo", "ok"]
CMD ["py.test", "-s", "."]

View File

@ -27,7 +27,6 @@ test1:
environment:
FOO: bar
# this is an uid compatible string
test2:
image: busybox
command: sleep 30

View File

@ -40,6 +40,9 @@ links:
port: 22
name: 'ssh*'
protocol: tcp
# env can be list, dict or string
env:
FOO: bar
# Commands to run before applying configuration
pre_conf_commands:

View File

@ -8,10 +8,10 @@ from grp import getgrnam
from io import open
from pwd import getpwnam
from six import string_types
from six import string_types, viewitems
from docker_links import DockerLinks
from pyyaml import Loader, load
from yaml import Loader, load
class Link(object):
@ -21,7 +21,7 @@ class Link(object):
def __init__(self, ip, env, port, protocol, names):
self.ip = ip
self.env = env
self.port = port
self.port = int(port)
self.protocol = protocol
self.uri = '{protocol}://{ip}:{port}'.format(
protocol=protocol,
@ -31,6 +31,31 @@ class Link(object):
self.names = tuple(names)
class Filter(object):
"""Filtering links"""
@staticmethod
def name(link, name):
return bool(fnmatch.filter(link.names, name))
@staticmethod
def port(link, port):
return int(port) == link.port
@staticmethod
def protocol(link, protocol):
return protocol == link.protocol
@staticmethod
def env(link, env):
if isinstance(env, dict):
return viewitems(env) <= viewitems(link.env)
if isinstance(env, list):
return bool([key for key in env if key in link.env])
return str(env) in link.env
class Links(object):
"""Links embeder"""
@ -38,33 +63,26 @@ class Links(object):
_links = []
_conf = None
def __init__(self, links={}, conf=None):
if len(links) is 0:
def __init__(self, links=None, config=None):
if not links or len(links.links()) is 0:
pass
for ip, link in links.links.items():
for ip, link in links.links().items():
for port, protocol in link["ports"].items():
self._links.append(Link(ip=ip,
env=link['environment'],
port=port,
port=int(port),
protocol=protocol['protocol'],
names=link['names']))
self._conf = conf
self._conf = config
def _get_link(self, name):
conf = self._conf[name]
links = self._links
for link in links:
if 'port' in conf and link.port is not conf['port']:
links.pop(link)
continue
if 'name' in conf \
and len(fnmatch.filter(link.names, conf['name'])) is 0:
links.pop(link)
continue
if 'protocol' in conf and link.protocol is not conf['protocol']:
links.pop(link)
config = self._conf[name]
print(config)
links = list(self._links)
for key, val in config.items():
links = [link for link in links if getattr(Filter, key)(link, val)]
return tuple(links)

View File

@ -25,9 +25,3 @@ class Entrypoint(object):
self.log.error(err)
if self.config.debug:
quickSetup(min_level=levels.DEBUG)
print(vars(self.config))
print(vars(self.config.links))
Entrypoint()

View File

@ -1,6 +1,7 @@
# Tests using pytest
from docker_links import DockerLinks
from entrypoint import Entrypoint
LINKS = [
'test1',
@ -57,3 +58,22 @@ def test_ports():
else:
assert item["ports"]["800"]['protocol'] == 'udp'
assert item["ports"]["8001"]['protocol'] == 'tcp'
def test_entrypoint():
entry = Entrypoint()
print(vars(entry.config))
print('all')
for link in entry.config.links.all:
print(vars(link))
print('test1')
for link in entry.config.links.test1:
print(vars(link))
print('test2_800')
for link in entry.config.links.test2_800:
print(vars(link))
print('test1')
for link in entry.config.links.test2:
print(vars(link))