Skip to main content

e2j2 is a commandline utility to render text/configuration files from jinja2 templates

Project description

e2j2

What is e2j2?

e2j2 (environment to jinja2 variables) is a commandline tool which will render jinja2 templates to textfiles. all environment variables can be used in the jinja2 templates, within the environment variables you can use special tags which give you the option to insert json, json file paths, base64 hashes, consul kv keys.

e2j2 is intended to be used within docker containers, you can simply add the j2 extention to a configuration file and then run e2j2 before you’re starting the actual executable.

Example:

lets assume we want to render the following server block in nginx, if we place the server configuration in a nginx include directory for example /etc/nginx/conf.d

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500 502 503 504 /50x.html;

  location / {
    index {{ NGINX.index_page }};
    root {{ NGINX.web_root }};
  }

  location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass unix:{{ NGINX.fpm_socket }};
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include {{ NGINX.fcgi_params }};
    root {{ NGINX.web_root }};
    try_files $uri =404;
  }
}

if you then set the NGINX environment variable, running e2j2 will render the jinja2 template and place it in the same folder:

~> export NGINX='json:
{
"server_name": "www.myweb.com",
"index_page": "index.php",
"web_root": "/usr/local/www/myweb",
"fcgi_params": "/usr/local/etc/nginx/myweb-fcgi-params",
"fpm_socket": "/var/run/php-fpm/myweb.socket"
}'
~> e2j2

In: .
    rendering: nginx_vhost_config.conf.j2=>done => writing: nginx_vhost_config.conf=>done

~> cat nginx_vhost_config.conf
server {
  server_name www.myweb.com;
  listen 80;
  listen [::]:80;
  error_page 500 502 503 504 /50x.html;

  location / {
    index index.php;
    root /usr/local/www/myweb;
  }

  location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php-fpm/myweb.socket;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include /usr/local/etc/nginx/myweb-fcgi-params;
    root /usr/local/www/roundcube;
    try_files $uri =404;
  }
}

Environment variable examples:

Plain environment variable:

Example:

setting:

MYENVVAR='plain environment variable'

will render envvar-example.j2 to:

This is a plain environment variable

Tag json:

Example:

setting:

MYJSONVAR='json:{"key": "json-example"}'

will render json-example.j2 to:

This is a json-example

Tag jsonfile:

Example:

setting:

MYJSONFILEVAR='jsonfile:jsonfile-example.json'

will render jsonfile-example.j2 to:

This is a jsonfile example with subkey

Tag base64:

Example:

Setting:

export MYBASE64VAR='base64:YmFzZTY0IGV4YW1wbGU='

will render base64-example.j2 to:

This is a base64 example

Tag consul:

Configuration:

You can configure the consul tag by setting the CONSUL_CONFIG environment variable. The following config items are supported:

Item

Explanation

Default

scheme

url scheme http or https

http

host

consul host

localhost

port

consul http(s) port

8500

token

consul token

none

Config example:

$ read -d '' CONSUL_CONFIG << EOF
> {
>   "scheme": "https",
>   "host": "consul.foobar.tld",
>   "port": 443,
>   "token": "abcdef01-0123-abcd-1234-0123456789ab"
> }
> EOF

Example:

Setting:

key: consulvar in consul to value: consul example

and

export MYCONSULVAR='consul:consulvar'

will render consul-example.j2 to:

This is a consul example

Changelog

0.1.6 (2018-05-17)

Changed

  • Improved error messages

  • Handle json decode errors

0.1.5 (2018-05-11)

Added

  • add no-color option

0.1.4 (2017-08-21)

Added

  • add version option

  • add file list option

Fixed

  • Fix bug with empty consul key values

0.1.3 (2017-07-11)

Breaking change

  • the consul tags will now use default jinja2 objects, this breaks the previous behaviour of separation by underscores

Changed

  • remove dependency click, use argparse instead.

Fixed

  • make sure that bytes are casted to strings (for base64 and consul tags)

0.1.2 (2017-05-17)

Fixed

  • additional fix for install issue on python2 (added MANIFEST.in)

0.1.1 (2017-05-17)

Fixed

  • add README.rst and CHANGELOG.rst as package_data this fixes install issue with python 2.x

Removed

  • remove dependency colorama

Changed

  • move methods to separate helper files

0.1.0 (2017-05-16)

Added

  • add short options for extention (-e) searchlist (-s) and noop (-N)

  • add (MIT) license

Changed

  • e2j2 is now packaged as pip package

  • split script and module, script will be installed in /usr/bin or /usr/local/bin

0.0.2 (2017-05-16)

Added

  • Add recurse flag

Changed

  • Searchlist is no longer a required option e2j2 will use the current directory as default

  • Recursion is no longer on by default

  • Improve error handling, e2j2 will now report failures and render *.err files which will make debugging errors much easier

0.0.1 (2017-05-01)

Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

e2j2-0.1.6.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

e2j2-0.1.6-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file e2j2-0.1.6.tar.gz.

File metadata

  • Download URL: e2j2-0.1.6.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for e2j2-0.1.6.tar.gz
Algorithm Hash digest
SHA256 44ab41ef86416314ff52afdd0f62e408e403e8c898330a9be2d74d979ae840d5
MD5 2c18b57709f6c03adf5720ccefe70dab
BLAKE2b-256 2ed09db5d0acd6a9589bd607c8df1c5574a749fd7b4ceef1e6d79549bda7c606

See more details on using hashes here.

File details

Details for the file e2j2-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: e2j2-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for e2j2-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3ffcd77cef9c60190994d6057e0e5cdd6214f07797762990734c876b6780e91d
MD5 215123fdc37ba012a32be7347006f1c3
BLAKE2b-256 3cbc2481c10fb3c26efeeae8f53e0be036b1669f78b79114ece758495f5eded6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page