Merge pull request #168 from dseira/master

New Feature/Improvement - Added deleted option to be able to remove site files
This commit is contained in:
David Seira 2017-09-01 17:41:28 +02:00 committed by GitHub
commit 69e9b47baf
2 changed files with 31 additions and 5 deletions

View File

@ -37,7 +37,7 @@
{%- endmacro %} {%- endmacro %}
# Creates the sls block that manages symlinking / renaming servers # 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) -%} {%- set anti_state = {True:False, False:True}.get(state) -%}
{% if state == True %} {% if state == True %}
{%- if nginx.lookup.server_use_symlink %} {%- if nginx.lookup.server_use_symlink %}
@ -46,20 +46,30 @@
- name: {{ server_path(server, state) }} - name: {{ server_path(server, state) }}
- target: {{ server_path(server, anti_state) }} - target: {{ server_path(server, anti_state) }}
{%- else %} {%- else %}
{%- if deleted == True %}
file.absent:
- name: {{ server_path(server, state) }}
{%- else %}
file.rename: file.rename:
{{ sls_block(nginx.servers.rename_opts) }} {{ sls_block(nginx.servers.rename_opts) }}
- name: {{ server_path(server, state) }} - name: {{ server_path(server, state) }}
- source: {{ server_path(server, anti_state) }} - source: {{ server_path(server, anti_state) }}
{%- endif %} {%- endif %}
{%- endif %}
{%- elif state == False %} {%- elif state == False %}
{%- if nginx.lookup.server_use_symlink %} {%- if nginx.lookup.server_use_symlink %}
file.absent: file.absent:
- name: {{ server_path(server, anti_state) }} - name: {{ server_path(server, anti_state) }}
{%- else %} {%- else %}
{%- if deleted == True %}
file.absent:
- name: {{ server_path(server, state) }}
{%- else %}
file.rename: file.rename:
{{ sls_block(nginx.servers.rename_opts) }} {{ sls_block(nginx.servers.rename_opts) }}
- name: {{ server_path(server, state) }} - name: {{ server_path(server, state) }}
- source: {{ server_path(server, anti_state) }} - source: {{ server_path(server, anti_state) }}
{%- endif %}
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
{%- endmacro %} {%- endmacro %}
@ -84,13 +94,18 @@ nginx_server_available_dir:
# Managed enabled/disabled state for servers # Managed enabled/disabled state for servers
{% for server, settings in nginx.servers.managed.items() %} {% 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 %} {% if 'source_path' in settings.config %}
{% set source_path = settings.config.source_path %} {% set source_path = settings.config.source_path %}
{% else %} {% else %}
{% set source_path = 'salt://nginx/ng/files/server.conf' %} {% set source_path = 'salt://nginx/ng/files/server.conf' %}
{% endif %} {% endif %}
{% set conf_state_id = 'server_conf_' ~ loop.index0 %}
{{ conf_state_id }}: {{ conf_state_id }}:
file.managed: file.managed:
{{ sls_block(nginx.servers.managed_opts) }} {{ sls_block(nginx.servers.managed_opts) }}
@ -107,16 +122,23 @@ nginx_server_available_dir:
{% endif %} {% endif %}
{% do server_states.append(conf_state_id) %} {% do server_states.append(conf_state_id) %}
{% endif %} {% endif %}
{% endif %}
{% if settings.enabled != None %} {% if settings.enabled != None %}
{% set status_state_id = 'server_state_' ~ loop.index0 %} {% set status_state_id = 'server_state_' ~ loop.index0 %}
{{ status_state_id }}: {{ status_state_id }}:
{{ manage_status(server, settings.enabled) }} {% if 'deleted' in settings and settings.deleted %}
{% if settings.config != None %} {{ manage_status(server, False, True) }}
{% else %}
{{ manage_status(server, settings.enabled, False) }}
{% endif %}
{% if settings.config != None and settings.enabled == True %}
- require: - require:
- file: {{ conf_state_id }} - file: {{ conf_state_id }}
{% endif %} {% endif %}
{% if 'deleted' not in settings or ( 'deleted' in settings and settings.deleted == False ) %}
{% do server_states.append(status_state_id) %} {% do server_states.append(status_state_id) %}
{% endif %} {% endif %}
{% endif %}
{% endfor %} {% endfor %}

View File

@ -105,6 +105,10 @@ nginx:
mysite: # relative pathname of the server file mysite: # relative pathname of the server file
# may be True, False, or None where True is enabled, False, disabled, and None indicates no action # may be True, False, or None where True is enabled, False, disabled, and None indicates no action
enabled: True 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. ## Modify 'available_dir' AND 'enabled_dir' '/etc/nginx' location to alternative value.
########### ###########