From f50c7e27e1767e3e5755311ac458e81987101de7 Mon Sep 17 00:00:00 2001 From: David Seira Date: Fri, 1 Sep 2017 11:23:15 +0200 Subject: [PATCH] New Feature - Added deleted option to be able to remove site files (clean up sites folder) Improvement - Disable reload of nginx when enabled=False (previously the nginx was always reloaded if settings.config != None; even with enabled=False) --- nginx/ng/servers_config.sls | 32 +++++++++++++++++++++++++++----- pillar.example | 4 ++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/nginx/ng/servers_config.sls b/nginx/ng/servers_config.sls index 041e1db..c22761c 100644 --- a/nginx/ng/servers_config.sls +++ b/nginx/ng/servers_config.sls @@ -37,7 +37,7 @@ {%- endmacro %} # Creates the sls block that manages symlinking / renaming servers -{% macro manage_status(server, state) -%} +{% macro manage_status(server, state, deleted) -%} {%- set anti_state = {True:False, False:True}.get(state) -%} {% if state == True %} {%- if nginx.lookup.server_use_symlink %} @@ -46,20 +46,30 @@ - name: {{ server_path(server, state) }} - target: {{ server_path(server, anti_state) }} {%- else %} + {%- if deleted == True %} + file.absent: + - name: {{ server_path(server, state) }} + {%- else %} file.rename: {{ sls_block(nginx.servers.rename_opts) }} - name: {{ server_path(server, state) }} - source: {{ server_path(server, anti_state) }} + {%- endif %} {%- endif %} {%- elif state == False %} {%- if nginx.lookup.server_use_symlink %} file.absent: - name: {{ server_path(server, anti_state) }} {%- else %} + {%- if deleted == True %} + file.absent: + - name: {{ server_path(server, state) }} + {%- else %} file.rename: {{ sls_block(nginx.servers.rename_opts) }} - name: {{ server_path(server, state) }} - source: {{ server_path(server, anti_state) }} + {%- endif %} {%- endif -%} {%- endif -%} {%- endmacro %} @@ -84,13 +94,18 @@ nginx_server_available_dir: # Managed enabled/disabled state for servers {% for server, settings in nginx.servers.managed.items() %} -{% if settings.config != None %} +{% set conf_state_id = 'server_conf_' ~ loop.index0 %} +{% if 'deleted' in settings and settings.deleted %} +{{ conf_state_id }}: + file.absent: + - name: {{ server_curpath(server) }} +{% else %} +{% if settings.config != None and settings.enabled == True %} {% if 'source_path' in settings.config %} {% set source_path = settings.config.source_path %} {% else %} {% set source_path = 'salt://nginx/ng/files/server.conf' %} {% endif %} -{% set conf_state_id = 'server_conf_' ~ loop.index0 %} {{ conf_state_id }}: file.managed: {{ sls_block(nginx.servers.managed_opts) }} @@ -107,16 +122,23 @@ nginx_server_available_dir: {% endif %} {% do server_states.append(conf_state_id) %} {% endif %} +{% endif %} {% if settings.enabled != None %} {% set status_state_id = 'server_state_' ~ loop.index0 %} {{ status_state_id }}: -{{ manage_status(server, settings.enabled) }} -{% if settings.config != None %} +{% if 'deleted' in settings and settings.deleted %} +{{ manage_status(server, False, True) }} +{% else %} +{{ manage_status(server, settings.enabled, False) }} +{% endif %} +{% if settings.config != None and settings.enabled == True %} - require: - file: {{ conf_state_id }} {% endif %} +{% if 'deleted' not in settings or ( 'deleted' in settings and settings.deleted == False ) %} {% do server_states.append(status_state_id) %} {% endif %} +{% endif %} {% endfor %} diff --git a/pillar.example b/pillar.example index 3debc2a..7786849 100644 --- a/pillar.example +++ b/pillar.example @@ -105,6 +105,10 @@ nginx: mysite: # relative pathname of the server file # may be True, False, or None where True is enabled, False, disabled, and None indicates no action enabled: True + # Remove the site config file. Nice to clean up the conf.d (or sites-available). + # It also remove the symlink (if it is exists). + # The site MUST be disabled before delete it (if not the nginx is not reloaded). + deleted: True ########### ## Modify 'available_dir' AND 'enabled_dir' '/etc/nginx' location to alternative value. ###########