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
ENV SECRET nothing
CMD ["py.test", "--verbose", "-rw", "."]

@ -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", "."]

@ -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

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

@ -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

@ -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 <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 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):

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

@ -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')

@ -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 %}

Loading…
Cancel
Save