From aa9c76a6226d277e76963532ac618ac50784839c Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 7 Oct 2024 23:58:56 +0200 Subject: [PATCH] Add auto-cleaning for caddy sites --- playbooks/filter_plugins/utils.py | 8 +++++++ playbooks/tasks/reload-caddy.yml | 23 +++++++++++++++++++ .../tasks/steps/template-site-config.yml | 6 +---- playbooks/templates/caddy_site.conf.j2 | 2 ++ roles/always/handlers/main.yml | 6 +---- roles/extra_services/tasks/main.yml | 5 ++-- roles/extra_services/vars/main.yml | 4 +++- 7 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 playbooks/tasks/reload-caddy.yml diff --git a/playbooks/filter_plugins/utils.py b/playbooks/filter_plugins/utils.py index 25f9354..8e88f4b 100644 --- a/playbooks/filter_plugins/utils.py +++ b/playbooks/filter_plugins/utils.py @@ -2,6 +2,7 @@ class FilterModule(object): def filters(self): return { 'list_prefix_suffix': self.list_prefix_suffix, + 'list_prefix_path_suffix': self.list_prefix_path_suffix, } def list_prefix_suffix(self, values, prefix, suffix): @@ -9,3 +10,10 @@ class FilterModule(object): for value in values: result.append(f"{prefix}{value}{suffix}") return result + + def list_prefix_path_suffix(self, values, prefix, suffix): + prefix = prefix if prefix.endswith('/') else f"{prefix}/" + result = [] + for value in values: + result.append(f"{prefix}{value}{suffix}") + return result diff --git a/playbooks/tasks/reload-caddy.yml b/playbooks/tasks/reload-caddy.yml new file mode 100644 index 0000000..ea20885 --- /dev/null +++ b/playbooks/tasks/reload-caddy.yml @@ -0,0 +1,23 @@ +- name: Find existing caddy site configs + ansible.builtin.find: + paths: "{{ caddy_config_path }}" + recurse: no + register: find_result +- name: Map exisiting/wanted caddy site configs + ansible.builtin.set_fact: + caddy_site_configs_have: "{{ find_result.files | map(attribute='path') }}" + caddy_site_configs_want: "{{ all_services | my_service_attributes(inventory_hostname) | list_prefix_path_suffix(caddy_config_path, '.conf') }}" + +- name: Remove unwanted caddy site configs + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: "{{ caddy_site_configs_have | difference(caddy_site_configs_want) }}" + become: true + +- name: Reload caddy container + ansible.builtin.command: + cmd: docker compose exec app sh -c "caddy validate --config /etc/caddy/Caddyfile && caddy reload --config /etc/caddy/Caddyfile" + chdir: "{{ caddy_path }}" + when: "'local-dev' != inventory_hostname" + changed_when: true diff --git a/playbooks/tasks/steps/template-site-config.yml b/playbooks/tasks/steps/template-site-config.yml index 2788a00..50ccb5d 100644 --- a/playbooks/tasks/steps/template-site-config.yml +++ b/playbooks/tasks/steps/template-site-config.yml @@ -2,11 +2,7 @@ - name: Template caddy site ansible.builtin.template: src: caddy_site.conf.j2 - dest: "{{ (caddy_config_path, svc.domain + '.conf') | path_join }}" + dest: "{{ (caddy_config_path, role_name + '.conf') | path_join }}" mode: "0644" notify: - Reload caddy - -- name: Register caddy site - ansible.builtin.set_fact: - managed_sites: "{{ managed_sites + [svc.domain + '.conf'] }}" diff --git a/playbooks/templates/caddy_site.conf.j2 b/playbooks/templates/caddy_site.conf.j2 index 967ba7a..9a8ef9b 100644 --- a/playbooks/templates/caddy_site.conf.j2 +++ b/playbooks/templates/caddy_site.conf.j2 @@ -36,7 +36,9 @@ {% endif %} {% endmacro -%} +{% if svc.domain|default(false) %} {{ caddy_site(svc) }} +{% endif %} {%- for extra_svc in svc.extra_svcs|default([]) %} {{ caddy_site(extra_svc) }} diff --git a/roles/always/handlers/main.yml b/roles/always/handlers/main.yml index 517225f..f406513 100644 --- a/roles/always/handlers/main.yml +++ b/roles/always/handlers/main.yml @@ -1,7 +1,3 @@ --- - name: Reload caddy - ansible.builtin.command: - cmd: docker compose exec app sh -c "caddy validate --config /etc/caddy/Caddyfile && caddy reload --config /etc/caddy/Caddyfile" - chdir: "{{ caddy_path }}" - when: "'local-dev' != inventory_hostname" - changed_when: true + ansible.builtin.include_tasks: tasks/reload-caddy.yml diff --git a/roles/extra_services/tasks/main.yml b/roles/extra_services/tasks/main.yml index 9c1c71f..22d1c5f 100644 --- a/roles/extra_services/tasks/main.yml +++ b/roles/extra_services/tasks/main.yml @@ -3,9 +3,8 @@ ansible.builtin.import_tasks: tasks/set-default-facts.yml - name: Deploy extra services + vars: + svc: "{{ extra_services_svc }}" block: - name: Import tasks to template the site and functions for the reverse proxy ansible.builtin.include_tasks: tasks/steps/template-site-config.yml - loop: "{{ extra_services_all }}" - loop_control: - loop_var: svc diff --git a/roles/extra_services/vars/main.yml b/roles/extra_services/vars/main.yml index fa35e04..72f238c 100644 --- a/roles/extra_services/vars/main.yml +++ b/roles/extra_services/vars/main.yml @@ -1,2 +1,4 @@ --- -extra_services_all: "{{ vault_extra_services }}" +extra_services_svc: + name: extra_services + extra_svcs: "{{ vault_extra_services }}"