forked from envtpl/envtpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
126 lines (84 loc) · 3.48 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sys
from setuptools import setup
long_description = '''
envtpl
======
_Render jinja2 templates on the command line with shell environment variables_
Installation
------------
pip install envtpl
How-to
------
Say you have a configuration file called whatever.conf that looks like this
foo = 123
bar = "abc"
You can use envtpl to set `foo` and `bar` from the command line by creating a file called whatever.conf.tpl
foo = {{ FOO }}
bar = "{{ BAR }}"
If you run
FOO=123 BAR=abc envtpl < whatever.conf.tpl > whatever.conf
you'll get back the original whatever.conf.
You can also specify default values
foo = {{ FOO | default(123) }}
bar = "{{ BAR | default("abc") }}"
Running
FOO=456 envtpl < whatever.conf.tpl > whatever.conf
will generate
foo = 456
bar = "abc"
This is all standard [Jinja2 syntax](http://jinja.pocoo.org/docs/templates/), so you can do things like
{% if BAZ is defined %}
foo = 123
{% else %}
foo = 456
{% endif %}
bar = "abc"
If an environment variable is missing, envtpl will throw an error
$ echo '{{ FOO }} {{ BAR }}' | FOO=123 envtpl
Error: 'BAR' is undefined
You can change this behaviour to insert empty strings instead by passing the `--allow-missing` flag.
Instead of reading from stdin and writing to stdout, you can pass the input filename as an optional positional argument,
and set the output filename with the `--output-file` (`-o`) argument.
envtpl -o whatever.conf whatever.conf.tpl
As a convenience, if you don't specify an output filename and the input filename ends with `.tpl`, the output filename will be the input filename without the `.tpl` extension, i.e.
envtpl whatever.conf.tpl
# is equivalent to
envtpl -o whatever.conf whatever.conf.tpl
By default, envtpl will **delete** the input template file. You can keep it by passing the `--keep-template` flag.
There's a special `environment(prefix='')` function that you can use as a kind of wildcard variable. If you have `hello.tpl`
hello = {{ FOO }}
{% for key, value in environment('MY_') %}{{ key }} = {{ value }}
{% endfor %}
and compile it using
FOO=world MY_baz=qux MY_foo=bar envtpl hello.tpl
You end up with
hello = world
baz = qux
foo = bar
What's the point?
-----------------
I use this script quite a lot in Docker images. Usually I'll have the CMD execute some file, like /bin/start_container, that sets up the runtime configuration for the container by inserting environment variables into config files before starting the main process. A redis example could look like this
#!/bin/bash
# start_container
envtpl /etc/redis.conf.tpl
redis-server
This is the use case I've optimised for, so that's why envtpl by default will delete the original template file.
''' # noqa
install_requires = ['Jinja2>=2.7', 'binaryornot>=0.4.4']
if sys.version_info < (2, 7):
install_requires.append('argparse>=1.0')
setup(
name='envtpl',
version='0.5.3',
py_modules=['envtpl', 'renvtpl'],
entry_points={
'console_scripts': ['envtpl = envtpl:main', 'renvtpl = renvtpl:main']
},
install_requires=install_requires,
author='Andreas Jansson, Fabien Marty',
description=('Render jinja2 templates on the command line using shell environment variables'),
license='GPL v3',
keywords='template environment variables parameter substitution shell jinja2 docker',
long_description=long_description,
url='https://github.com/metwork-framework/envtpl',
)