Skip to content

Commit

Permalink
feat(RHTAPREL-392): add jinja2 and templating to the image
Browse files Browse the repository at this point in the history
As part of RHTAPREL-392, we need to create advisories. The easiest way
to do this is from a template using jinja2, so this commit adds jinja2
and a template to the repo. Some ansible filters are not present in
vanilla jinja2, so a module providing those is added too.

Signed-off-by: Johnny Bieren <[email protected]>
  • Loading branch information
johnbieren committed Oct 5, 2023
1 parent 48a1cd6 commit 2cb0056
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ RUN dnf -y --setopt=tsflags=nodocs install \
jq \
python39-devel \
diffutils \
python39-pip \
python39-requests \
skopeo \
krb5-workstation \
&& dnf clean all

RUN pip3 install Jinja2 \
jinja2-ansible-filters

ADD data/certs/2015-IT-Root-CA.pem data/certs/2022-IT-Root-CA.pem /etc/pki/ca-trust/source/anchors/
RUN update-ca-trust

COPY pyxis /home/pyxis
COPY utils /home/utils
COPY templates /home/templates

# Set HOME variable to something else than `/` to avoid 'permission denied' problems when writing files.
ENV HOME=/tekton/home
Expand Down
23 changes: 23 additions & 0 deletions templates/advisory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: rhtap.redhat.com/v1alpha1

Check warning on line 1 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

missing document start "---"
kind: Advisory
metadata:
name: {{ advisory_name }}

Check failure on line 4 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces

Check failure on line 4 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces
spec:
product_id: {{ advisory.spec.product_id }}

Check failure on line 6 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces

Check failure on line 6 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces
cpe: {{ advisory.spec.cpe }}

Check failure on line 7 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces

Check failure on line 7 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces
type: {{ advisory_type }}

Check failure on line 8 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces

Check failure on line 8 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

too many spaces inside braces
{%- if 'issues' in advisory.spec %}

Check failure on line 9 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

syntax error: found character '%' that cannot start any token
issues:
{{ advisory.spec.issues | to_nice_yaml(indent=2) | comment(decoration=' ') | trim }}
{%- endif %}
content:
{{ advisory.spec.content | to_nice_yaml(indent=2) | comment(decoration=' ') | trim }}

Check failure on line 14 in templates/advisory.yaml

View workflow job for this annotation

GitHub Actions / yamllint

line too long
synopsis: {{ advisory.spec.synopsis }}
topic: >-
{{ advisory.spec.topic | wordwrap(76) | replace('\n', '\n ') }}
description: >-
{{ advisory.spec.description | wordwrap(76) | replace('\n', '\n ') }}
solution: >-
{{ advisory.spec.solution | wordwrap(76) | replace('\n', '\n ') }}
references:
{{ advisory.spec.references | to_nice_yaml | indent(4) | trim }}
53 changes: 53 additions & 0 deletions utils/apply-template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
from jinja2 import Template
from jinja2_ansible_filters import AnsibleCoreFiltersExtension
import argparse
import json
from typing import Any


def setup_argparser() -> Any: # pragma: no cover
"""Setup argument parser
:return: Initialized argument parser
"""

parser = argparse.ArgumentParser(description="Applies a template.")

parser.add_argument(
"--data",
help="JSON string containing data to use in the template.",
required=True,
)
parser.add_argument(
"--template",
help="Path to the template file to use.",
required=True,
)
parser.add_argument(
"-o",
"--output",
help="The desired filename of the result.",
required=True,
)
return parser


def main(): # pragma: no cover
"""Main func"""

parser = setup_argparser()
args = parser.parse_args()

with open(args.template) as t:
template = Template(t.read(), extensions=[AnsibleCoreFiltersExtension])
content = template.render(json.loads(args.data))

filename = args.output
with open(filename, mode="w", encoding="utf-8") as advisory:
advisory.write(content)
print(f"Wrote {filename}")


if __name__ == "__main__": # pragma: no cover
main()

0 comments on commit 2cb0056

Please sign in to comment.