Commit 8b073098 authored by Benjamin Tissoires's avatar Benjamin Tissoires
Browse files

ci-fairy: add the config nodes in the 'ci-fairy' jinja2 context



Let's say you have a settings.yaml file with the following:
```yaml
foo:
  data: banana

bar:
  data: apple
```

This allows to have a template with the following:
```jinja2
{% for item in ci_fairy.nodes.values() %}
{{ item.data }}
{% endfor %}
```
Signed-off-by: Benjamin Tissoires's avatarBenjamin Tissoires <benjamin.tissoires@gmail.com>
parent 7ca500a4
......@@ -166,7 +166,7 @@ ci-fairy images:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-00c33dfcd5406e9924516d4104cd1e0ad08c8f15ef82a2f5ed622135cb603439
FDO_DISTRIBUTION_TAG: sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
.ci-fairy-local-image:
extends:
......@@ -355,7 +355,7 @@ test published images:
stage: test published images
script:
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:buildah-2021-03-18.0
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-00c33dfcd5406e9924516d4104cd1e0ad08c8f15ef82a2f5ed622135cb603439
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-base-2021-03-18.0
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-mkosi-base-2021-03-18.0
rules:
......
......@@ -23,7 +23,7 @@ ci-fairy-base-image:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-00c33dfcd5406e9924516d4104cd1e0ad08c8f15ef82a2f5ed622135cb603439
FDO_DISTRIBUTION_TAG: sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
# The actual ci-fairy image with ci-fairy installed
# This image uses the sha of the ci-fairy script itself as tag.
......
......@@ -246,6 +246,13 @@ allows for the selection of the root node. For example: ::
variables:
CHECKSUM: {{ci_fairy.sha256sum('tarball.tar.xz')}}
* ``ci_fairy.nodes``: gives the full config nodes as a dictionary.
This allows to iterate over all root nodes. Example: ::
{% for key in ci_fairy.nodes %}
- root node in config: { key }
{% endfor %}
.. note:: Root nodes in the config files must not be named ``ci_fairy``
to avoid namespace clashes.
......
......@@ -31,6 +31,6 @@
# Variables provided by this template should be considered read-only.
#
.fdo.ci-fairy:
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-00c33dfcd5406e9924516d4104cd1e0ad08c8f15ef82a2f5ed622135cb603439
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
variables:
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-00c33dfcd5406e9924516d4104cd1e0ad08c8f15ef82a2f5ed622135cb603439
\ No newline at end of file
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
\ No newline at end of file
......@@ -26,6 +26,8 @@ from pathlib import Path
class _env_ci_fairy:
context = {}
@staticmethod
def hashfiles(*argv):
'''
......@@ -965,6 +967,8 @@ def generate_template(ctx, config, root, template, output_file, verify):
if 'ci_fairy' in data:
raise click.BadParameter('"ci_fairy" is a reserved keyword for node names')
_env_ci_fairy.nodes = dict(data)
templatedir = templatefile.parent
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.fspath(templatedir)),
trim_blocks=True, lstrip_blocks=True,
......
......@@ -622,6 +622,53 @@ def test_template_ci_fairy_sha256sum():
f'file4: {sha}'
def test_template_ci_fairy_nodes():
runner = CliRunner()
with runner.isolated_filesystem():
with open('test.yml', 'w') as fd:
fd.write(DEFAULT_YAML)
with open('test.tmpl', 'w') as fd:
fd.write('{% for key, item in ci_fairy.nodes.items() %}\n')
fd.write('{{ key }}:\n')
fd.write(' {% for node in item %}\n')
fd.write(' {{ node }}\n')
fd.write(' {% endfor %}\n')
fd.write('{% endfor %}\n')
args = ['generate-template', '--config', 'test.yml',
'test.tmpl']
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert result.stdout == 'numbers:\n' + \
' one\n' + \
' two\n' + \
' three\n' + \
'fruit:\n' + \
' apple\n' + \
' banana\n' + \
' orange\n'
def test_template_ci_fairy_root_nodes():
runner = CliRunner()
with runner.isolated_filesystem():
with open('test.yml', 'w') as fd:
fd.write(DEFAULT_YAML)
with open('test.tmpl', 'w') as fd:
fd.write('{% for key, item in ci_fairy.nodes.items() %}\n')
fd.write('{{ key }}\n')
fd.write('{% endfor %}\n')
args = ['generate-template', '--config', 'test.yml',
'--root', '/fruit/orange', 'test.tmpl']
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert result.stdout == 'color\n' + \
'health\n'
def test_template_no_ci_fairy():
runner = CliRunner()
with runner.isolated_filesystem():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment