跳转到主要内容

从Jinja模板渲染配置文件

项目描述

CI Codecov Code Style: black License: ISC Debian package PyPI project Ubuntu package

ionit

ionit是一个简单小巧的配置模板工具。它收集上下文并在指定目录中渲染Jinja模板。上下文可以是静态的JSON或YAML文件或动态的Python文件。Python文件还可以定义传递给渲染器的函数。

上下文文件名需要以.json结尾(用于JSON),以.py结尾(用于Python),以.yaml结尾(用于YAML)。上下文文件将按字母顺序读取。如果多个上下文文件定义了相同的键,则读取较晚的文件具有优先权。如果顺序相关,建议在文件名前加数字。

ionit附带一个早期启动的单次服务,该服务在网络服务之前执行,允许在启动之前生成网络和其他服务的配置文件。在这方面,ionit可以充当cloud-init的小型兄弟。

Python模块

Python模块可以定义一个collect_context函数。该函数由ionit调用,并将当前上下文作为参数传递。可以使用当前上下文推导出更多上下文信息,但不应修改此变量。collect_context必须返回一个字典(可以为空)或抛出异常,该异常将由ionit捕获。

Python模块还可以定义可以在渲染时从Jinja模板调用的函数。使用ionit_plugin.function装饰器标记要导出的函数。

请注意,函数名称不应与其他上下文中的键冲突。如果一个Python模块定义了一个与上下文中同名的函数和值,上下文中的值将具有优先级。

一个示例Python模块可能看起来像这样

import ionit_plugin


@ionit_plugin.function
def double(value):
    return 2 * value


@ionit_plugin.function
def example_function():
    return "Lorem ipsum"


def collect_context(current_context):
    return {"key": "value"}

先决条件

  • Python >= 3.6
  • Python模块
    • jinja2
    • PyYAML或ruamel.yaml
  • pandoc(用于从ionit.1.md生成ionit.1手册页)

测试用例有额外的需求

  • black
  • flake8
  • isort
  • pylint

示例

静态上下文

此示例来自一个测试用例,演示了ionit如何从一个JSON文件和一个YAML文件中收集上下文,并渲染一个模板

user@host:~/ionit$ cat tests/config/static/first.json
{"first": 1}
user@host:~/ionit$ cat tests/config/static/second.yaml
---
second: 2
user@host:~/ionit$ cat tests/template/static/counting.jinja
Counting:
* {{ first }}
* {{ second }}
* 3
user@host:~/ionit$ ./ionit -c tests/config/static -t tests/template/static
2018-08-08 17:39:06,956 ionit INFO: Reading configuration file 'tests/config/static/first.json'...
2018-08-08 17:39:06,956 ionit INFO: Reading configuration file 'tests/config/static/second.yaml'...
2018-08-08 17:39:06,960 ionit INFO: Rendered 'tests/template/static/counting.jinja' to 'tests/template/static/counting'.
user@host:~/ionit$ cat tests/template/static/counting
Counting:
* 1
* 2
* 3

Python函数

此示例来自一个测试用例,演示了如何在渲染时定义Python函数

user@host:~/ionit$ cat tests/config/function/function.py
import ionit_plugin


@ionit_plugin.function
def answer_to_all_questions():
    return 42
user@host:~/ionit$ cat tests/template/function/Document.jinja
The answer to the Ultimate Question of Life, The Universe, and Everything is {{ answer_to_all_questions() }}.
user@host:~/ionit$ ./ionit -c tests/config/function -t tests/template/function
2018-08-13 11:58:16,905 ionit INFO: Loading Python module 'function' from 'tests/config/function/function.py'...
2018-08-13 11:58:16,909 ionit INFO: Rendered 'tests/template/function/Document.jinja' to 'tests/template/function/Document'.
user@host:~/ionit$ cat tests/template/function/Document
The answer to the Ultimate Question of Life, The Universe, and Everything is 42.

贡献

欢迎贡献。源代码具有100%的测试覆盖率,应予以保留。因此,请为每个错误修复提供一个测试用例,并为每个新功能提供一个或多个测试用例。请遵循如何编写Git提交信息以编写良好的提交信息。

创建发行版

此项目使用语义版本控制。要创建发行版,请增加setup.py中的版本号,并在NEWS.md中记录值得注意的变化。然后提交更改并标记发行版

git commit -sm "Release ionit $(./setup.py --version)" NEWS.md setup.py
git tag v$(./setup.py --version)

可以通过运行以下命令生成xz压缩的发布tar包

name="ionit-$(./setup.py --version)"
git archive --prefix="$name/" HEAD | xz -c9 > "../$name.tar.xz"
gpg --output "../$name.tar.xz.asc" --armor --detach-sign "../$name.tar.xz"

可以通过运行以下命令构建和上传用于PyPI的包

pyproject-build --no-isolation
twine upload --repository pypi dist/*

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面