diff --git a/Dockerfile.py2 b/Dockerfile.py2 index 23838ca..9245497 100644 --- a/Dockerfile.py2 +++ b/Dockerfile.py2 @@ -14,6 +14,4 @@ ADD tests/test_template.yml.tpl /tmp/test_template2.yml.tpl WORKDIR /opt/tests -ENV SECRET nothing - CMD ["py.test", "--verbose", "-rw", "."] diff --git a/Dockerfile.py3 b/Dockerfile.py3 index fb87626..129bb91 100644 --- a/Dockerfile.py3 +++ b/Dockerfile.py3 @@ -14,6 +14,4 @@ ADD tests/test_template.yml.tpl /tmp/test_template2.yml.tpl WORKDIR /opt/tests -ENV SECRET nothing - CMD ["py.test", "--verbose", "-rw", "."] diff --git a/README.md b/README.md index 699ee64..c1fd6ac 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ You can generate configuration for your service with jinga2 template. Here is an example for an hypothetical ssh config file: -```jinga +```jinja host server: hostname {{links.ssh.ip}} port {{links.ssh.port}} @@ -155,7 +155,7 @@ host server: Templates will be replaced with ip address and port of the identified link. All links can be accessed from `links.all`, this is a tuple of links you can iterate on it. -```jinga +```jinja {% for link in links.all %} host {{link.names[0]}} hostname {{link.ip}} @@ -165,7 +165,7 @@ host {{link.names[0]}} If you change the option `single` to `false` in the `entrypoint-config.yml`, the identified link `ssh` will become a tuple of links. You must iterate on it in the `jinja` template. -```jinga +```jinja {% for link in links.ssh %} host {{link.names[0]}} hostname {{link.ip}} @@ -175,7 +175,7 @@ host {{link.names[0]}} Accessing environment in template. -```jinga +```jinja {% if 'SSHKEY in env' %} {{env['SSHKEY']}} {% endfor %} @@ -189,6 +189,8 @@ You have 4 available objects in your templates. - `links` - `containers` - `environ` + - `yaml` + - `json` #### config @@ -237,6 +239,21 @@ You have 4 available objects in your templates. `env` is an alias to `environ`. +#### yaml and json + +`yaml` and `json` objects are respectively an import of [`PyYAML`](http://pyyaml.org/) and [`json`](https://docs.python.org/2/library/json.html) modules. + +They are useful to load and dump serialized data from environment. + +```jinja +# Here yaml is present in SETUP_YAML environment variable +{% set data = yaml.load(env['SETUP_YAML'])%} +{{data['param']}} + +# Here json is present in SETUP_JSON environment variable +{% set data = json.loads(env['SETUP_JSON'])%} +{{data['param']}} +``` ## Setup diff --git a/common.yml b/common.yml new file mode 100644 index 0000000..3a25767 --- /dev/null +++ b/common.yml @@ -0,0 +1,7 @@ +environ: + environment: + SECRET: nothing + JSON: | + {"json": "ok"} + YAML: | + yaml: ok diff --git a/docker-compose.yml b/docker-compose.yml index 604743b..4ce1b0a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,52 +1,64 @@ testpython3: build: . dockerfile: Dockerfile.py3 + volumes: + - ./pyentrypoint:/opt/pyentrypoint:ro + - ./tests:/opt/tests links: - test1 - test2 - test3 - test4 - volumes: - - ./pyentrypoint:/opt/pyentrypoint:ro - - ./tests:/opt/tests + extends: + file: common.yml + service: environ testpython2: build: . dockerfile: Dockerfile.py2 + volumes: + - ./pyentrypoint:/opt/pyentrypoint:ro + - ./tests:/opt/tests links: - test1 - test2 - test3 - test4 - volumes: - - ./pyentrypoint:/opt/pyentrypoint:ro - - ./tests:/opt/tests + extends: + file: common.yml + service: environ testpython3_debug: build: . dockerfile: Dockerfile.py3 - command: ["py.test", "--verbose", "-s", "-rw", "."] + volumes: + - ./pyentrypoint:/opt/pyentrypoint:ro + - ./tests:/opt/tests links: - test1 - test2 - test3 - test4 - volumes: - - ./pyentrypoint:/opt/pyentrypoint:ro - - ./tests:/opt/tests + command: ["py.test", "--verbose", "-s", "-rw", "."] + extends: + file: common.yml + service: environ testpython2_debug: build: . dockerfile: Dockerfile.py2 - command: ["py.test", "--verbose", "-s", "-rw", "."] + volumes: + - ./pyentrypoint:/opt/pyentrypoint:ro + - ./tests:/opt/tests links: - test1 - test2 - test3 - test4 - volumes: - - ./pyentrypoint:/opt/pyentrypoint:ro - - ./tests:/opt/tests + command: ["py.test", "--verbose", "-s", "-rw", "."] + extends: + file: common.yml + service: environ test1: image: busybox diff --git a/docs/templates.rst b/docs/templates.rst index 0e2281c..8794eb3 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -92,3 +92,19 @@ environ ``environ`` is the environment of the container (os.environ). ``env`` is an alias to ``environ``. + +``yaml`` and ``json`` +^^^^^^^^^^^^^^^^^^^^^ + +``yaml`` and ``json`` objects are respectively an import of `PyYAML ` and `json modules. + +They are useful to load and dump serialized data from environment. + +.. code:: jinja + # Here yaml is present in SETUP_YAML environment variable + {% set data = yaml.load(env['SETUP_YAML'])%} + {{data['param']}} + + # Here json is present in SETUP_JSON environment variable + {% set data = json.loads(env['SETUP_JSON'])%} + {{data['param']}} diff --git a/pyentrypoint/entrypoint.py b/pyentrypoint/entrypoint.py index 561c6de..accd56c 100644 --- a/pyentrypoint/entrypoint.py +++ b/pyentrypoint/entrypoint.py @@ -6,6 +6,7 @@ from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals +import json import os from subprocess import PIPE from subprocess import Popen @@ -13,6 +14,7 @@ from sys import argv from sys import exit from sys import stdout +import yaml from jinja2 import Environment from jinja2 import FileSystemLoader @@ -88,6 +90,8 @@ class Entrypoint(object): links=self.config.links, env=os.environ, environ=os.environ, + json=json, + yaml=yaml, containers=DockerLinks().to_containers())) def run_conf_cmd(self, cmd): diff --git a/setup.py b/setup.py index 5470af6..eb530e3 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup # Thanks Sam and Max -__version__ = '0.4.4' +__version__ = '0.4.5' if __name__ == '__main__': setup( diff --git a/tests/pyentrypoint_test.py b/tests/pyentrypoint_test.py index db3c8f4..58c4c3c 100644 --- a/tests/pyentrypoint_test.py +++ b/tests/pyentrypoint_test.py @@ -145,6 +145,12 @@ def test_templates(): assert test['ENV']['SECRET'] == 'nothing' assert test['ENVIRON']['SECRET'] == 'nothing' + # test yaml + assert test['YAML']['yaml'] == 'ok' + + # test json + assert test['JSON']['json'] == 'ok' + def test_conf_commands(): entry = Entrypoint(conf='configs/base.yml') diff --git a/tests/test_template.yml.tpl b/tests/test_template.yml.tpl index 0c0be52..76c5e43 100644 --- a/tests/test_template.yml.tpl +++ b/tests/test_template.yml.tpl @@ -45,10 +45,20 @@ ID: ENV: {% for e in env %} - {{e}}: {{env[e]}} + '{{e}}': '{{env[e]}}' {% endfor %} ENVIRON: {% for e in environ %} - {{e}}: {{env[e]}} + '{{e}}': '{{env[e]}}' +{% endfor %} + +JSON: +{% for key, val in json.loads(env['JSON']).items() %} + '{{key}}': '{{val}}' +{% endfor %} + +YAML: +{% for key, val in yaml.load(env['YAML']).items() %} + '{{key}}': '{{val}}' {% endfor %}