2019-05-10 23:07:18 +02:00
|
|
|
# nginx.servers_config
|
2016-10-19 23:35:38 +02:00
|
|
|
#
|
|
|
|
# Manages the configuration of virtual host files.
|
|
|
|
|
2019-05-16 08:27:21 +02:00
|
|
|
{#- Get the `tplroot` from `tpldir` #}
|
|
|
|
{%- set tplroot = tpldir.split('/')[0] %}
|
|
|
|
{%- from tplroot ~ '/map.jinja' import nginx, sls_block with context %}
|
|
|
|
{%- from tplroot ~ '/libtofs.jinja' import files_switch with context %}
|
|
|
|
|
2016-10-19 23:35:38 +02:00
|
|
|
{% set server_states = [] %}
|
2020-11-09 00:30:45 +01:00
|
|
|
{#- _nginx is a lightened copy of nginx map intended to passed in templates #}
|
|
|
|
{%- set _nginx = nginx.copy() %}
|
|
|
|
{%- do _nginx.pop('snippets') %}
|
|
|
|
{%- do _nginx.pop('servers') %}
|
2016-10-19 23:35:38 +02:00
|
|
|
|
|
|
|
# Simple path concatenation.
|
|
|
|
# Needs work to make this function on windows.
|
|
|
|
{% macro path_join(file, root) -%}
|
|
|
|
{{ root ~ '/' ~ file }}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
# Retrieves the disabled name of a particular server
|
|
|
|
{% macro disabled_name(server) -%}
|
|
|
|
{%- if nginx.lookup.server_use_symlink -%}
|
|
|
|
{{ nginx.servers.managed.get(server).get('disabled_name', server) }}
|
|
|
|
{%- else -%}
|
|
|
|
{{ nginx.servers.managed.get(server).get('disabled_name', server ~ nginx.servers.disabled_postfix) }}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
# Gets the path of a particular server
|
|
|
|
{% macro server_path(server, state) -%}
|
|
|
|
{%- if state == True -%}
|
|
|
|
{{ path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) }}
|
|
|
|
{%- elif state == False -%}
|
|
|
|
{{ path_join(disabled_name(server), nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
|
|
|
|
{%- else -%}
|
|
|
|
{{ path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) }}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
# Gets the current canonical name of a server
|
|
|
|
{% macro server_curpath(server) -%}
|
2019-01-25 20:20:58 +01:00
|
|
|
{{ server_path(server, nginx.servers.managed.get(server).get('available_dir')) }}
|
2016-10-19 23:35:38 +02:00
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
# Creates the sls block that manages symlinking / renaming servers
|
2017-09-01 11:23:15 +02:00
|
|
|
{% macro manage_status(server, state, deleted) -%}
|
2016-10-19 23:35:38 +02:00
|
|
|
{%- set anti_state = {True:False, False:True}.get(state) -%}
|
|
|
|
{% if state == True %}
|
|
|
|
{%- if nginx.lookup.server_use_symlink %}
|
|
|
|
file.symlink:
|
|
|
|
{{ sls_block(nginx.servers.symlink_opts) }}
|
|
|
|
- name: {{ server_path(server, state) }}
|
2021-03-13 21:54:07 +01:00
|
|
|
- makedirs: True
|
2016-10-19 23:35:38 +02:00
|
|
|
- target: {{ server_path(server, anti_state) }}
|
|
|
|
{%- else %}
|
2017-09-01 11:23:15 +02:00
|
|
|
{%- if deleted == True %}
|
|
|
|
file.absent:
|
|
|
|
- name: {{ server_path(server, state) }}
|
|
|
|
{%- else %}
|
2016-10-19 23:35:38 +02:00
|
|
|
file.rename:
|
|
|
|
{{ sls_block(nginx.servers.rename_opts) }}
|
|
|
|
- name: {{ server_path(server, state) }}
|
|
|
|
- source: {{ server_path(server, anti_state) }}
|
2017-09-01 11:23:15 +02:00
|
|
|
{%- endif %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{%- endif %}
|
|
|
|
{%- elif state == False %}
|
|
|
|
{%- if nginx.lookup.server_use_symlink %}
|
|
|
|
file.absent:
|
|
|
|
- name: {{ server_path(server, anti_state) }}
|
|
|
|
{%- else %}
|
2017-09-01 11:23:15 +02:00
|
|
|
{%- if deleted == True %}
|
|
|
|
file.absent:
|
|
|
|
- name: {{ server_path(server, state) }}
|
|
|
|
{%- else %}
|
2016-10-19 23:35:38 +02:00
|
|
|
file.rename:
|
|
|
|
{{ sls_block(nginx.servers.rename_opts) }}
|
|
|
|
- name: {{ server_path(server, state) }}
|
|
|
|
- source: {{ server_path(server, anti_state) }}
|
2017-09-01 11:23:15 +02:00
|
|
|
{%- endif %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{%- endif -%}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
# Makes sure the enabled directory exists
|
|
|
|
nginx_server_enabled_dir:
|
|
|
|
file.directory:
|
|
|
|
{{ sls_block(nginx.servers.dir_opts) }}
|
|
|
|
- name: {{ nginx.lookup.server_enabled }}
|
2020-03-25 17:32:47 +01:00
|
|
|
- clean: {{ nginx.servers.purge_servers_config }}
|
2016-10-19 23:35:38 +02:00
|
|
|
|
|
|
|
# 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 }}
|
2020-03-25 17:32:47 +01:00
|
|
|
- clean: {{ nginx.servers.purge_servers_config }}
|
2016-10-19 23:35:38 +02:00
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
# Managed enabled/disabled state for servers
|
|
|
|
{% for server, settings in nginx.servers.managed.items() %}
|
|
|
|
{% set conf_state_id = 'server_conf_' ~ loop.index0 %}
|
2017-09-01 11:23:15 +02:00
|
|
|
{% if 'deleted' in settings and settings.deleted %}
|
|
|
|
{{ conf_state_id }}:
|
|
|
|
file.absent:
|
|
|
|
- name: {{ server_curpath(server) }}
|
|
|
|
{% else %}
|
2019-05-16 08:27:21 +02:00
|
|
|
{% if settings.enabled == True %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{{ conf_state_id }}:
|
|
|
|
file.managed:
|
|
|
|
{{ sls_block(nginx.servers.managed_opts) }}
|
|
|
|
- name: {{ server_curpath(server) }}
|
2019-05-16 08:27:21 +02:00
|
|
|
- source:
|
|
|
|
{%- if 'source_path' in settings.config %}
|
|
|
|
- {{ settings.config.source_path }}
|
|
|
|
{%- endif %}
|
|
|
|
{{ files_switch([server, 'server.conf'],
|
|
|
|
'server_conf_file_managed'
|
|
|
|
)
|
|
|
|
}}
|
2019-01-23 23:56:22 +01:00
|
|
|
- makedirs: True
|
2016-10-19 23:35:38 +02:00
|
|
|
- template: jinja
|
2017-08-31 13:19:46 +02:00
|
|
|
{% if 'source_path' not in settings.config %}
|
2016-10-19 23:35:38 +02:00
|
|
|
- context:
|
2017-03-23 18:54:16 +01:00
|
|
|
config: {{ settings.config|json(sort_keys=False) }}
|
2020-11-09 00:30:45 +01:00
|
|
|
nginx: {{ _nginx|json() }}
|
2017-08-30 19:48:44 +02:00
|
|
|
{% endif %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{% if 'overwrite' in settings and settings.overwrite == False %}
|
|
|
|
- unless:
|
|
|
|
- test -e {{ server_curpath(server) }}
|
|
|
|
{% endif %}
|
|
|
|
{% endif %}
|
2017-09-01 11:23:15 +02:00
|
|
|
{% endif %}
|
2021-03-13 21:54:07 +01:00
|
|
|
{% do server_states.append(conf_state_id) %}
|
2016-10-19 23:35:38 +02:00
|
|
|
|
|
|
|
{% if settings.enabled != None %}
|
|
|
|
{% set status_state_id = 'server_state_' ~ loop.index0 %}
|
2017-12-21 20:48:00 +01:00
|
|
|
{%- set enabled_dir = path_join(server, nginx.servers.managed.get(server).get('enabled_dir', nginx.lookup.server_enabled)) -%}
|
|
|
|
{%- set available_dir = path_join(server, nginx.servers.managed.get(server).get('available_dir', nginx.lookup.server_available)) -%}
|
|
|
|
{%- if enabled_dir != available_dir %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{{ status_state_id }}:
|
2017-09-01 11:23:15 +02:00
|
|
|
{% if 'deleted' in settings and settings.deleted %}
|
|
|
|
{{ manage_status(server, False, True) }}
|
|
|
|
{% else %}
|
|
|
|
{{ manage_status(server, settings.enabled, False) }}
|
|
|
|
{% endif %}
|
2019-05-16 08:27:21 +02:00
|
|
|
{% if settings.enabled == True %}
|
2016-10-19 23:35:38 +02:00
|
|
|
- require:
|
|
|
|
- file: {{ conf_state_id }}
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% do server_states.append(status_state_id) %}
|
2017-12-21 20:48:00 +01:00
|
|
|
{%- endif %} {# enabled != available_dir #}
|
2017-09-01 11:23:15 +02:00
|
|
|
{% endif %}
|
2016-10-19 23:35:38 +02:00
|
|
|
{% endfor %}
|