From 562fc0215e4d7d4d30d421dd84425937924eacc0 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Sat, 9 Dec 2023 18:36:25 +0100 Subject: [PATCH] feat(template): move flag formatting to filter_plugin --- filter_plugins/docker_filters.py | 26 +++++++++++++++++++++ molecule/default_vagrant/group_vars/all.yml | 3 +++ templates/unit.j2 | 26 ++------------------- 3 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 filter_plugins/docker_filters.py diff --git a/filter_plugins/docker_filters.py b/filter_plugins/docker_filters.py new file mode 100644 index 0000000..6c163a8 --- /dev/null +++ b/filter_plugins/docker_filters.py @@ -0,0 +1,26 @@ +# filter_plugins/docker_filters.py + + +def create_docker_flags(flags): + return "\n".join([create_docker_flag(item) for item in flags]) + + +def create_docker_flag(item): + if isinstance(item, dict): + key = list(item.keys())[0] + value = item[key] + if isinstance(value, list): + return "\n".join(['--{} "{}" \\'.format(key, val) for val in value]) + else: + return '--{} "{}" \\'.format(key, value) + elif isinstance(item, str): + return "--{} \\".format(item) + else: + return "" + + +class FilterModule(object): + def filters(self): + return { + "create_docker_flags": create_docker_flags, + } diff --git a/molecule/default_vagrant/group_vars/all.yml b/molecule/default_vagrant/group_vars/all.yml index d797bed..b02a738 100644 --- a/molecule/default_vagrant/group_vars/all.yml +++ b/molecule/default_vagrant/group_vars/all.yml @@ -12,6 +12,9 @@ docker_systemd_service_flags: - /var:/test:ro - health-cmd: curl localhost:80 - health-retries: 10 + - cap-add: + - SYS_ADMIN + - dns: "8.8.8.8" docker_systemd_service_container_cmd: [] #! TO REPLACE diff --git a/templates/unit.j2 b/templates/unit.j2 index f671ce4..f75743a 100644 --- a/templates/unit.j2 +++ b/templates/unit.j2 @@ -1,26 +1,4 @@ # {{ ansible_managed }} -{%- macro create_docker_flags(flags) %} -{% for item in flags %} -{{ create_docker_flag(item) }} -{% endfor %} -{% endmacro -%} - -{%- macro create_docker_flag(item) %} -{%- if item is mapping %} -{% set key = item.keys() | first %} -{% set value = item[key] %} -{%- if value.__class__.__name__ == 'list' %} -{% for val in value %} ---{{ key }} "{{ val }}" \{% if not loop.last +%} -{% endif %}{% endfor %} -{%- else %} ---{{ key }} "{{ value }}" \ -{%- endif %} -{%- elif item is string %} ---{{ item }} \ -{%- endif -%} -{% endmacro %} - {% set service_systemd_options_keys = docker_systemd_service_systemd_options | selectattr("key") | map(attribute="key") | list %} [Unit] {% for key, value in docker_systemd_service_systemd_unit_options | dictsort %} @@ -41,8 +19,8 @@ ExecStartPre=-{{ docker_systemd_service_docker_path }} rm -f {{ docker_systemd_s ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_service_container_name }} \ --rm \ --env-file {{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }} \ -{{ create_docker_flags(docker_systemd_service_flags) -}} -{{- docker_systemd_service_image -}}{{ ' ' if docker_systemd_service_container_cmd else '' }}{% if docker_systemd_service_container_cmd is string %}{{ docker_systemd_service_container_cmd | trim }}{% else %}{{ docker_systemd_service_container_cmd | join(' ') | trim }}{% endif %} +{{ docker_systemd_service_flags|create_docker_flags }} +{{ docker_systemd_service_image -}}{{ ' ' if docker_systemd_service_container_cmd else '' }}{% if docker_systemd_service_container_cmd is string %}{{ docker_systemd_service_container_cmd | trim }}{% else %}{{ docker_systemd_service_container_cmd | join(' ') | trim }}{% endif %} {% endif +%} {% if not 'ExecStop' in service_systemd_options_keys %} ExecStop={{ docker_systemd_service_docker_path }} stop {{ docker_systemd_service_container_name }}