Add json and yaml modules in Jinja templates

pull/10/head
Christophe Mehay 8 years ago
parent 5494d50fb1
commit 218a80c690

@ -14,6 +14,4 @@ ADD tests/test_template.yml.tpl /tmp/test_template2.yml.tpl
WORKDIR /opt/tests WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "--verbose", "-rw", "."] CMD ["py.test", "--verbose", "-rw", "."]

@ -14,6 +14,4 @@ ADD tests/test_template.yml.tpl /tmp/test_template2.yml.tpl
WORKDIR /opt/tests WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "--verbose", "-rw", "."] CMD ["py.test", "--verbose", "-rw", "."]

@ -147,7 +147,7 @@ You can generate configuration for your service with jinga2 template.
Here is an example for an hypothetical ssh config file: Here is an example for an hypothetical ssh config file:
```jinga ```jinja
host server: host server:
hostname {{links.ssh.ip}} hostname {{links.ssh.ip}}
port {{links.ssh.port}} 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. 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 %} {% for link in links.all %}
host {{link.names[0]}} host {{link.names[0]}}
hostname {{link.ip}} 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. 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 %} {% for link in links.ssh %}
host {{link.names[0]}} host {{link.names[0]}}
hostname {{link.ip}} hostname {{link.ip}}
@ -175,7 +175,7 @@ host {{link.names[0]}}
Accessing environment in template. Accessing environment in template.
```jinga ```jinja
{% if 'SSHKEY in env' %} {% if 'SSHKEY in env' %}
{{env['SSHKEY']}} {{env['SSHKEY']}}
{% endfor %} {% endfor %}
@ -189,6 +189,8 @@ You have 4 available objects in your templates.
- `links` - `links`
- `containers` - `containers`
- `environ` - `environ`
- `yaml`
- `json`
#### config #### config
@ -237,6 +239,21 @@ You have 4 available objects in your templates.
`env` is an alias to `environ`. `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 ## Setup

@ -0,0 +1,7 @@
environ:
environment:
SECRET: nothing
JSON: |
{"json": "ok"}
YAML: |
yaml: ok

@ -1,52 +1,64 @@
testpython3: testpython3:
build: . build: .
dockerfile: Dockerfile.py3 dockerfile: Dockerfile.py3
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
links: links:
- test1 - test1
- test2 - test2
- test3 - test3
- test4 - test4
volumes: extends:
- ./pyentrypoint:/opt/pyentrypoint:ro file: common.yml
- ./tests:/opt/tests service: environ
testpython2: testpython2:
build: . build: .
dockerfile: Dockerfile.py2 dockerfile: Dockerfile.py2
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
links: links:
- test1 - test1
- test2 - test2
- test3 - test3
- test4 - test4
volumes: extends:
- ./pyentrypoint:/opt/pyentrypoint:ro file: common.yml
- ./tests:/opt/tests service: environ
testpython3_debug: testpython3_debug:
build: . build: .
dockerfile: Dockerfile.py3 dockerfile: Dockerfile.py3
command: ["py.test", "--verbose", "-s", "-rw", "."] volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
links: links:
- test1 - test1
- test2 - test2
- test3 - test3
- test4 - test4
volumes: command: ["py.test", "--verbose", "-s", "-rw", "."]
- ./pyentrypoint:/opt/pyentrypoint:ro extends:
- ./tests:/opt/tests file: common.yml
service: environ
testpython2_debug: testpython2_debug:
build: . build: .
dockerfile: Dockerfile.py2 dockerfile: Dockerfile.py2
command: ["py.test", "--verbose", "-s", "-rw", "."] volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
links: links:
- test1 - test1
- test2 - test2
- test3 - test3
- test4 - test4
volumes: command: ["py.test", "--verbose", "-s", "-rw", "."]
- ./pyentrypoint:/opt/pyentrypoint:ro extends:
- ./tests:/opt/tests file: common.yml
service: environ
test1: test1:
image: busybox image: busybox

@ -92,3 +92,19 @@ environ
``environ`` is the environment of the container (os.environ). ``environ`` is the environment of the container (os.environ).
``env`` is an alias to ``environ``. ``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.
.. 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']}}

@ -6,6 +6,7 @@ from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import json
import os import os
from subprocess import PIPE from subprocess import PIPE
from subprocess import Popen from subprocess import Popen
@ -13,6 +14,7 @@ from sys import argv
from sys import exit from sys import exit
from sys import stdout from sys import stdout
import yaml
from jinja2 import Environment from jinja2 import Environment
from jinja2 import FileSystemLoader from jinja2 import FileSystemLoader
@ -88,6 +90,8 @@ class Entrypoint(object):
links=self.config.links, links=self.config.links,
env=os.environ, env=os.environ,
environ=os.environ, environ=os.environ,
json=json,
yaml=yaml,
containers=DockerLinks().to_containers())) containers=DockerLinks().to_containers()))
def run_conf_cmd(self, cmd): def run_conf_cmd(self, cmd):

@ -5,7 +5,7 @@ from setuptools import setup
# Thanks Sam and Max # Thanks Sam and Max
__version__ = '0.4.4' __version__ = '0.4.5'
if __name__ == '__main__': if __name__ == '__main__':
setup( setup(

@ -145,6 +145,12 @@ def test_templates():
assert test['ENV']['SECRET'] == 'nothing' assert test['ENV']['SECRET'] == 'nothing'
assert test['ENVIRON']['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(): def test_conf_commands():
entry = Entrypoint(conf='configs/base.yml') entry = Entrypoint(conf='configs/base.yml')

@ -45,10 +45,20 @@ ID:
ENV: ENV:
{% for e in env %} {% for e in env %}
{{e}}: {{env[e]}} '{{e}}': '{{env[e]}}'
{% endfor %} {% endfor %}
ENVIRON: ENVIRON:
{% for e in 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 %} {% endfor %}

Loading…
Cancel
Save