e2j2是一个命令行实用程序,用于从shell环境变量中呈现jinja2模板中的文本/配置文件

e2j2的Python项目详细描述


e2j2[ 构建状态coverage status

什么是E2J2?

e2j2(jinja2变量的环境)是一个命令行工具,它将 将jinja2模板呈现为文本文件。所有环境变量都可以是 在jinja2模板中使用,在环境变量中可以 使用特殊标记,可以选择插入json、json文件 路径,base64哈希,consul千伏密钥。

E2J2可以在Docker容器中使用,也可以用于其他简单的配置模板任务。在Docker容器中,您可以简单地将j2扩展添加到配置文件中,或者添加到一个或多个文件夹结构中的多个文件中,然后作为Docker入口点脚本的一部分运行e2j2。

示例:

假设我们想在nginx中呈现以下服务器块,如果我们 将服务器配置放在nginx include目录中,例如 /etc/nginx/conf.d

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}

如果设置nginx环境变量,运行e2j2将呈现 Jinja2模板并将其放在同一文件夹中:

~> exportNGINX='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 500502503504 /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;}}

环境变量示例:

普通环境变量:

示例:

设置:

MYENVVAR='plain environment variable'

将envvar-example.j2渲染为:

This is a plain environment variable

标记文件:

示例:

设置:

MYFILEVAR=file:./file-example.txt

将file-example.j2渲染为:

This is a file example

标记json:

示例:

设置:

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

将json-example.j2呈现为:

This is a json-example

标记jsonfile:

示例:

设置:

MYJSONFILEVAR='jsonfile:jsonfile-example.json'

将jsonfile-example.j2呈现为:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
0

标记基64:

示例:

设置:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
1

将base64-example.j2渲染为:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
2

标签领事:

配置:

您可以通过设置consu配置来配置consu标记 环境变量。支持以下配置项:

<表> < COLGROUP > < COL/> < COL/> < COL/> <广告> 项 说明 默认值 < /广告> <正文> 网址 领事馆网址 http://127.0.0.1:8500 方案 consul url scheme http或https 来自url的方案 主机 领事馆主人 来自url的主机名 端口 领事http(s)端口 来自url的端口 令牌 领事令牌 无 < > <表>

全局配置示例:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
3

acl令牌可以通过上述配置或设置consu_token变量进行配置。

作为全局配置的另一种选择,也可以通过在使用consun标记时简单地包括配置来配置/调整每个consun标记的全局配置。

标记配置示例:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
4

执政官示例:

设置:

key:consulvar in consul to value:consun示例

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
5

将consun-example.j2呈现为:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
6

标记列表:

示例:

设置:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
7

将list-example.j2渲染为:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
8

两次渲染

从0.1.12版开始E2J2支持在 环境变量。

示例:

设置以下两个环境变量:

server {
  server_name {{ NGINX.server_name }};
  listen 80;
  listen [::]:80;
  error_page 500502503504 /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;}}
9

将(通过运行: e2j2 -f twopass example.j2 -2 )呈现为:

~> exportNGINX='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 500502503504 /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;}}
0

标记库:

配置:

您可以通过设置保险库配置来配置保险库标记 环境变量。支持以下配置项:

<表> < COLGROUP > < COL/> < COL/> < COL/> <广告> 项 说明 默认值 < /广告> <正文> 网址 保险库URL http://127.0.0.1:8200 方案 保险库URL方案http或https 来自url的方案 主机 保险库主机 来自url的主机名 端口 保险库http(s)端口 来自url的端口 后端 金库机密后端 <原始的< > > 令牌 金库代币 无 < > <表>

支持以下后端:

<表> < COLGROUP > < COL/> < COL/> <广告> 后端 说明 < /广告> <正文> 未加工 使用对机密存储api的普通get请求 kv1 键/值版本1 kv2 键/值版本2 < > <表>

全局配置示例:

~> exportNGINX='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 500502503504 /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;}}
1

身份验证令牌可以通过上述配置或设置保险存储令牌变量进行配置。

作为全局配置的另一种选择,也可以通过在使用保险库标记时简单地包括配置来配置/调整每个保险库标记的全局配置。

标记配置示例:

~> exportNGINX='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 500502503504 /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;}}
2

保险库示例:

设置:

~> exportNGINX='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 500502503504 /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;}}
3

将把vault-kv1-example.j2(通过运行: e2j2 -f vault example.j2)呈现为:

~> exportNGINX='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 500502503504 /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;}}
4

更改日志

0.2.0(2019-07-23)

添加

  • 保险库机密支持
  • 每个标签配置(用于领事馆和保险库标签)
  • 添加对令牌变量的支持(用于consun和vault标记)

更改

  • 为consul和vault config添加json模式验证

0.1.21(2019-07-09)

已修复

  • 修复其他目录中包含的模板的问题

添加

  • 将选项添加到"复制"文件所有权和文件权限中

0.1.20(2019-06-12)

已修复

  • 修复"仅渲染一个文件"问题

0.1.19(2019-06-10)

添加

  • 向白名单/黑名单环境变量添加选项

已修复

  • 设置正确的退出代码(0=成功/1=失败)

更改

  • 添加单元测试
  • 添加功能测试

0.1.18(2019-05-17)

已修复

  • 修复consul parser中的错误

0.1.17(2019-05-17)

已修复

  • 用jinja2模板中的include语句修复错误

0.1.16(2019-05-16)

已修复
  • 用默认的jinja标记修复错误

0.1.15(2019-05-16)

更改

  • 添加选项以覆盖默认jinja2标记(块开始/结束, 变量开始/结束和注释开始/结束)

断裂变化

  • 删除了双通道渲染的"hack",您可以使用新的标记 处理模板中的冲突字符

0.1.14(2019-04-30)

已修复
  • 在文件结尾保留换行符
  • CVE-2019-10906,模块现在需要jinja2>;=2.10.1

0.1.13(2019-03-29)

已修复
  • 通过两次渲染修复忽略的原始标记

0.1.12(2019-03-14)

已添加

  • 添加对两次渲染的支持

0.1.11(2019-01-30)

已更改
  • 更改了带有consun标记的嵌套键的行为。

示例:在consul中,我们有一个key1/key2/123 envvar key=consun:key1/key2=>;{{key}}现在将呈现为123,不再 到{"key2":123}

0.1.10(2018-09-24)

已添加

  • 添加文件标记,此标记将文件内容放入 变量

0.1.9(2018-09-21)

已修复
  • 修复Python2.7上的导入问题

0.1.8(2018-09-21)

已更改
  • 处理json标记中的hashrocket字符(由hiera生成)

0.1.7(2018-09-20)

已添加

  • 添加将解析逗号分隔列表的列表标记

0.1.6(2018-05-17)

已更改
  • 改进的错误消息
  • 处理JSON解码错误

0.1.5(2018-05-11)

已添加

  • 不添加颜色选项

0.1.4(2017-08-21)

已添加

  • 添加版本选项
  • 添加文件列表选项

固定的
  • 用空的consun键值修复错误

0.1.3(2017-07-11)

中断改变

  • consun标记现在将使用默认的jinja2对象,这将打破 以前用下划线分隔的行为

已更改

  • 删除依赖项单击,改用argparse。

固定的
  • 确保字节被强制转换为字符串(对于base64和consul 标签)

0.1.2(2017-05-17)

已修复
  • python2上安装问题的附加修复(添加了manifest.in)

0.1.1(2017-05-17)

固定的
  • 将readme.rst和changelog.rst添加为此修复程序安装的包数据 python 2.x的问题

已删除

  • 删除依赖项colorama

已更改

  • 将方法移动到单独的帮助程序文件

0.1.0(2017-05-16)

已添加

  • 为扩展(-e)searchlist(-s)和noop(-n)添加短选项
  • 添加(MIT)许可证

已更改

  • E2J2现在打包为PIP包
  • 拆分脚本和模块,脚本将安装在/usr/bin或 /usr/本地/bin

0.0.2(2017-05-16)

已添加

  • 添加递归标志

已更改

  • searchlist不再是必需的选项e2j2将使用当前 默认目录
  • 默认情况下,递归不再启用
  • 改进错误处理,E2J2现在将报告故障并呈现 *.err文件,这将使调试错误更加容易

0.0.1(2017-05-01)

初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
基于Java的遗传算法确定最优交易行为   java改型2.0无法解析Json嵌套对象   java在数组中查找最大额定值(数字),我们不能跳过数组中的一个或多个连续数字   java在spring boot中从命令行设置活动概要文件和配置位置   JavaAxis2:传输错误:404错误:未找到帮助理解其真正含义   java使用Play2WAR和Play2.2.1   java理解函数运算符:Lambda   在代理java后面读取https网页数据   java应用程序。Android单元测试中的类mock   java为什么onClick布局XML引用的方法需要是公共的?   从SMTLIB2文件解析的java显示声明   java重写给定的类以使用组合而不是继承   HTMLUnit和Java:NoSuchMethodException:createDefaultSSLContext()   java如何使用Spring和ThymeLeaf从前端正确更新后端中的对象?   来自init()Java的方法调用   使用cellrendering从数据库向JTable动态添加数据后,java无法使用JTable执行排序操作   java Android Studio 1.5.1。渲染错误(浮动操作按钮)   web服务如何使用UsenameToken和PasswordDigest为JAVA中的SOAP客户端附加wsse安全头   java为什么要在局部变量和myApplicationClass中同时删除“ArrayList.remove”?