Skip to content

Commit

Permalink
Better cleanup solution
Browse files Browse the repository at this point in the history
Sometimes, the cleanup process happend AFTER vhosts are installed, which
resulted in no vhost declared.

Parsing declared vhosts in pillar and comparing to present files on the
server allows to remove only unwanted files.
Plus, files are kept in sites-available, and removed only in
sites-enabled.
  • Loading branch information
Tristan Charbonneau committed Aug 27, 2020
1 parent aabccad commit 12f5bce
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions nginx/servers_config.sls
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ nginx_server_enabled_dir:
file.directory:
{{ sls_block(nginx.servers.dir_opts) }}
- name: {{ nginx.lookup.server_enabled }}
- clean: {{ nginx.servers.purge_servers_config }}
# If enabled and available are not the same, create available
{% if nginx.lookup.server_enabled != nginx.lookup.server_available -%}
nginx_server_available_dir:
file.directory:
{{ sls_block(nginx.servers.dir_opts) }}
- name: {{ nginx.lookup.server_available }}
- clean: {{ nginx.servers.purge_servers_config }}
{%- endif %}
# Manage the actual server files
Expand Down Expand Up @@ -157,3 +155,23 @@ nginx_server_available_dir:
{%- endif %} {# enabled != available_dir #}
{% endif %}
{% endfor %}
{# Add . and .. to make it easier to not clean those #}
{% set valid_sites = ['.', '..', ] %}
{# Take sites from nginx.servers.managed #}
{% for server, settings in salt['pillar.get']('nginx:servers:managed', {}).items() %}
{% do valid_sites.append(server) %}
{% endfor %}
{% if salt['file.directory_exists'](nginx.lookup.server_enabled) %}
{% for filename in salt['file.readdir'](nginx.lookup.server_enabled) %}
{% if filename not in valid_sites %}
{{ nginx.lookup.server_enabled ~ '/' ~ filename }}:
file.absent
{% endif %}
{% endfor %}
{% endif %}

0 comments on commit 12f5bce

Please sign in to comment.