diff --git a/README.rst b/README.rst index c5d7b36..9722a4e 100644 --- a/README.rst +++ b/README.rst @@ -55,7 +55,7 @@ Next-generation, alternate approach =================================== The following states provide an alternate approach to managing Nginx and Nginx -vhosts, as well as code organization. Please provide feedback by filing issues, +servers, as well as code organization. Please provide feedback by filing issues, discussing in ``#salt`` in Freenode and the mailing list as normal. .. contents:: @@ -84,13 +84,13 @@ Manages the nginx main server configuration file. Manages the startup and running state of the nginx service. -``nginx.ng.vhosts_config`` +``nginx.ng.servers_config`` -------------------------- Manages virtual host files. This state only manages the content of the files and does not bind them to service calls. -``nginx.ng.vhosts`` +``nginx.ng.servers`` ------------------- Manages nginx virtual hosts files and binds them to service calls. diff --git a/nginx/ng/files/vhost.conf b/nginx/ng/files/server.conf similarity index 66% rename from nginx/ng/files/vhost.conf rename to nginx/ng/files/server.conf index 50e6296..6f8dfaf 100644 --- a/nginx/ng/files/vhost.conf +++ b/nginx/ng/files/server.conf @@ -1,29 +1,29 @@ {% set ind_increment = 4 %} -{%- macro vhost_config(values, key='', ind=0, lb='\n', delim=';', operator=' ') -%} +{%- macro server_config(values, key='', ind=0, lb='\n', delim=';', operator=' ') -%} {%- for value in values -%} {%- if value is number or value is string -%} {{ lb }}{{ key|indent(ind, True) }}{{ operator }}{{ value }}{{ delim }} {%- elif value is mapping -%} {%- for k, v in value.items() -%} {%- if v is number or v is string -%} -{{ vhost_config([v], k, ind) }} +{{ server_config([v], k, ind) }} {%- elif v|length() > 0 and (v[0] is number or v[0] is string) -%} -{{ lb }}{{ k|indent(ind,True) }}{{ vhost_config(v,'', 0, '', '')}}{{ delim }} +{{ lb }}{{ k|indent(ind,True) }}{{ server_config(v,'', 0, '', '')}}{{ delim }} {%- else %} {{ lb }}{{ k|indent(ind, True) }} {{ '{' }} -{{- vhost_config(v, '', ind + ind_increment) }} +{{- server_config(v, '', ind + ind_increment) }} {{ '}'|indent(ind, True) }} {%- endif -%} {%- endfor -%} {%- elif value is iterable -%} -{{ vhost_config(value, ind + ind_increment, delim, operator) }} +{{ server_config(value, ind + ind_increment, delim, operator) }} {%- endif -%} {%- endfor -%} {%- endmacro -%} -# Nginx vhost configuration +# Nginx server configuration # # **** DO NOT EDIT THIS FILE **** # # This file is managed by Salt. -{{ vhost_config(config) }} +{{ server_config(config) }} diff --git a/nginx/ng/init.sls b/nginx/ng/init.sls index 3473b44..4c64626 100644 --- a/nginx/ng/init.sls +++ b/nginx/ng/init.sls @@ -7,7 +7,7 @@ include: - nginx.ng.config - nginx.ng.service - - nginx.ng.vhosts + - nginx.ng.servers - nginx.ng.certificates extend: diff --git a/nginx/ng/map.jinja b/nginx/ng/map.jinja index 423987f..8bf0e3e 100644 --- a/nginx/ng/map.jinja +++ b/nginx/ng/map.jinja @@ -11,9 +11,9 @@ 'service': 'nginx', 'webuser': 'www-data', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/sites-available', - 'vhost_enabled': '/etc/nginx/sites-enabled', - 'vhost_use_symlink': True, + 'server_available': '/etc/nginx/sites-available', + 'server_enabled': '/etc/nginx/sites-enabled', + 'server_use_symlink': True, 'pid_file': '/run/nginx.pid', }, 'CentOS': { @@ -21,9 +21,9 @@ 'service': 'nginx', 'webuser': 'nginx', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/conf.d', - 'vhost_enabled': '/etc/nginx/conf.d', - 'vhost_use_symlink': False, + 'server_available': '/etc/nginx/conf.d', + 'server_enabled': '/etc/nginx/conf.d', + 'server_use_symlink': False, 'pid_file': '/run/nginx.pid', 'rh_os_releasever': '$releasever', 'gpg_check': False, @@ -34,9 +34,9 @@ 'service': 'nginx', 'webuser': 'nginx', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/conf.d', - 'vhost_enabled': '/etc/nginx/conf.d', - 'vhost_use_symlink': False, + 'server_available': '/etc/nginx/conf.d', + 'server_enabled': '/etc/nginx/conf.d', + 'server_use_symlink': False, 'pid_file': '/run/nginx.pid', 'rh_os_releasever': '$releasever', 'gpg_check': False, @@ -47,9 +47,9 @@ 'service': 'nginx', 'webuser': 'nginx', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/conf.d', - 'vhost_enabled': '/etc/nginx/conf.d', - 'vhost_use_symlink': False, + 'server_available': '/etc/nginx/conf.d', + 'server_enabled': '/etc/nginx/conf.d', + 'server_use_symlink': False, 'pid_file': '/run/nginx.pid', 'gpg_check': True, 'gpg_key': 'http://download.opensuse.org/repositories/server:/http/openSUSE_13.2/repodata/repomd.xml.key' @@ -59,18 +59,18 @@ 'service': 'nginx', 'webuser': 'http', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/sites-available', - 'vhost_enabled': '/etc/nginx/sites-enabled', - 'vhost_use_symlink': True, + 'server_available': '/etc/nginx/sites-available', + 'server_enabled': '/etc/nginx/sites-enabled', + 'server_use_symlink': True, }, 'Gentoo': { 'package': 'www-servers/nginx', 'service': 'nginx', 'webuser': 'nginx', 'conf_file': '/etc/nginx/nginx.conf', - 'vhost_available': '/etc/nginx/sites-available', - 'vhost_enabled': '/etc/nginx/sites-enabled', - 'vhost_use_symlink': True, + 'server_available': '/etc/nginx/sites-available', + 'server_enabled': '/etc/nginx/sites-enabled', + 'server_use_symlink': True, }, }, default='Debian' ), 'install_from_source': False, @@ -115,7 +115,7 @@ }, }, }, - 'vhosts': { + 'servers': { 'disabled_postfix': '.disabled', 'symlink_opts': {}, 'rename_opts': {}, diff --git a/nginx/ng/vhosts.sls b/nginx/ng/servers.sls similarity index 71% rename from nginx/ng/vhosts.sls rename to nginx/ng/servers.sls index c9ec514..d468549 100644 --- a/nginx/ng/vhosts.sls +++ b/nginx/ng/servers.sls @@ -1,9 +1,9 @@ -# nginx.ng.vhosts +# nginx.ng.servers # # Manages virtual hosts and their relationship to the nginx service. {% from 'nginx/ng/map.jinja' import nginx, sls_block with context %} -{% from 'nginx/ng/vhosts_config.sls' import vhost_states with context %} +{% from 'nginx/ng/servers_config.sls' import server_states with context %} {% from 'nginx/ng/service.sls' import service_function with context %} {% macro file_requisites(states) %} @@ -14,9 +14,9 @@ include: - nginx.ng.service - - nginx.ng.vhosts_config + - nginx.ng.servers_config -{% if vhost_states|length() > 0 %} +{% if server_states|length() > 0 %} nginx_service_reload: service.{{ service_function }}: - name: {{ nginx.lookup.service }} @@ -24,8 +24,8 @@ nginx_service_reload: - use: - service: nginx_service - watch: - {{ file_requisites(vhost_states) }} + {{ file_requisites(server_states) }} - require: - {{ file_requisites(vhost_states) }} + {{ file_requisites(server_states) }} - service: nginx_service {% endif %} diff --git a/nginx/ng/servers_config.sls b/nginx/ng/servers_config.sls new file mode 100644 index 0000000..75cf529 --- /dev/null +++ b/nginx/ng/servers_config.sls @@ -0,0 +1,115 @@ +# nginx.ng.servers_config +# +# Manages the configuration of virtual host files. + +{% from 'nginx/ng/map.jinja' import nginx, sls_block with context %} +{% set server_states = [] %} + +# 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) -%} + {{ server_path(server, nginx.servers.managed.get(server).get('available')) }} +{%- endmacro %} + +# Creates the sls block that manages symlinking / renaming servers +{% macro manage_status(server, state) -%} + {%- 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) }} + - target: {{ server_path(server, anti_state) }} + {%- else %} + file.rename: + {{ sls_block(nginx.servers.rename_opts) }} + - name: {{ server_path(server, state) }} + - source: {{ server_path(server, anti_state) }} + {%- endif %} + {%- elif state == False %} + {%- if nginx.lookup.server_use_symlink %} + file.absent: + - name: {{ server_path(server, anti_state) }} + {%- else %} + file.rename: + {{ sls_block(nginx.servers.rename_opts) }} + - name: {{ server_path(server, state) }} + - source: {{ server_path(server, anti_state) }} + {%- 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 }} + +# 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 }} +{%- endif %} + +# Manage the actual server files +{% for server, settings in nginx.servers.managed.items() %} +{% endfor %} + +# 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 %} +{{ conf_state_id }}: + file.managed: + {{ sls_block(nginx.servers.managed_opts) }} + - name: {{ server_curpath(server) }} + - source: salt://nginx/ng/files/server.conf + - template: jinja + - context: + config: {{ settings.config|json() }} + {% if 'overwrite' in settings and settings.overwrite == False %} + - unless: + - test -e {{ server_curpath(server) }} + {% endif %} +{% do server_states.append(conf_state_id) %} +{% 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 %} + - require: + - file: {{ conf_state_id }} +{% endif %} + +{% do server_states.append(status_state_id) %} +{% endif %} +{% endfor %} diff --git a/nginx/ng/vhosts_config.sls b/nginx/ng/vhosts_config.sls deleted file mode 100644 index 48921cd..0000000 --- a/nginx/ng/vhosts_config.sls +++ /dev/null @@ -1,115 +0,0 @@ -# nginx.ng.vhosts_config -# -# Manages the configuration of virtual host files. - -{% from 'nginx/ng/map.jinja' import nginx, sls_block with context %} -{% set vhost_states = [] %} - -# 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 vhost -{% macro disabled_name(vhost) -%} - {%- if nginx.lookup.vhost_use_symlink -%} - {{ nginx.vhosts.managed.get(vhost).get('disabled_name', vhost) }} - {%- else -%} - {{ nginx.vhosts.managed.get(vhost).get('disabled_name', vhost ~ nginx.vhosts.disabled_postfix) }} - {%- endif -%} -{%- endmacro %} - -# Gets the path of a particular vhost -{% macro vhost_path(vhost, state) -%} - {%- if state == True -%} - {{ path_join(vhost, nginx.vhosts.managed.get(vhost).get('enabled_dir', nginx.lookup.vhost_enabled)) }} - {%- elif state == False -%} - {{ path_join(disabled_name(vhost), nginx.vhosts.managed.get(vhost).get('available_dir', nginx.lookup.vhost_available)) }} - {%- else -%} - {{ path_join(vhost, nginx.vhosts.managed.get(vhost).get('available_dir', nginx.lookup.vhost_available)) }} - {%- endif -%} -{%- endmacro %} - -# Gets the current canonical name of a vhost -{% macro vhost_curpath(vhost) -%} - {{ vhost_path(vhost, nginx.vhosts.managed.get(vhost).get('available')) }} -{%- endmacro %} - -# Creates the sls block that manages symlinking / renaming vhosts -{% macro manage_status(vhost, state) -%} - {%- set anti_state = {True:False, False:True}.get(state) -%} - {% if state == True %} - {%- if nginx.lookup.vhost_use_symlink %} - file.symlink: - {{ sls_block(nginx.vhosts.symlink_opts) }} - - name: {{ vhost_path(vhost, state) }} - - target: {{ vhost_path(vhost, anti_state) }} - {%- else %} - file.rename: - {{ sls_block(nginx.vhosts.rename_opts) }} - - name: {{ vhost_path(vhost, state) }} - - source: {{ vhost_path(vhost, anti_state) }} - {%- endif %} - {%- elif state == False %} - {%- if nginx.lookup.vhost_use_symlink %} - file.absent: - - name: {{ vhost_path(vhost, anti_state) }} - {%- else %} - file.rename: - {{ sls_block(nginx.vhosts.rename_opts) }} - - name: {{ vhost_path(vhost, state) }} - - source: {{ vhost_path(vhost, anti_state) }} - {%- endif -%} - {%- endif -%} -{%- endmacro %} - -# Makes sure the enabled directory exists -nginx_vhost_enabled_dir: - file.directory: - {{ sls_block(nginx.vhosts.dir_opts) }} - - name: {{ nginx.lookup.vhost_enabled }} - -# If enabled and available are not the same, create available -{% if nginx.lookup.vhost_enabled != nginx.lookup.vhost_available -%} -nginx_vhost_available_dir: - file.directory: - {{ sls_block(nginx.vhosts.dir_opts) }} - - name: {{ nginx.lookup.vhost_available }} -{%- endif %} - -# Manage the actual vhost files -{% for vhost, settings in nginx.vhosts.managed.items() %} -{% endfor %} - -# Managed enabled/disabled state for vhosts -{% for vhost, settings in nginx.vhosts.managed.items() %} -{% if settings.config != None %} -{% set conf_state_id = 'vhost_conf_' ~ loop.index0 %} -{{ conf_state_id }}: - file.managed: - {{ sls_block(nginx.vhosts.managed_opts) }} - - name: {{ vhost_curpath(vhost) }} - - source: salt://nginx/ng/files/vhost.conf - - template: jinja - - context: - config: {{ settings.config|json() }} - {% if 'overwrite' in settings and settings.overwrite == False %} - - unless: - - test -e {{ vhost_curpath(vhost) }} - {% endif %} -{% do vhost_states.append(conf_state_id) %} -{% endif %} - -{% if settings.enabled != None %} -{% set status_state_id = 'vhost_state_' ~ loop.index0 %} -{{ status_state_id }}: -{{ manage_status(vhost, settings.enabled) }} -{% if settings.config != None %} - - require: - - file: {{ conf_state_id }} -{% endif %} - -{% do vhost_states.append(status_state_id) %} -{% endif %} -{% endfor %} diff --git a/pillar.example b/pillar.example index bfdc737..4399586 100644 --- a/pillar.example +++ b/pillar.example @@ -36,9 +36,9 @@ nginx: service: nginx webuser: www-data conf_file: /etc/nginx/nginx.conf - vhost_available: /etc/nginx/sites-available - vhost_enabled: /etc/nginx/sites-enabled - vhost_use_symlink: True + server_available: /etc/nginx/sites-available + server_enabled: /etc/nginx/sites-enabled + server_use_symlink: True # This is required for RedHat like distros (Amazon Linux) that don't follow semantic versioning for $releasever rh_os_releasever: '6' # Currently it can be used on rhel/centos/suse when installing from repo @@ -74,25 +74,25 @@ nginx: - /etc/nginx/conf.d/*.conf - /etc/nginx/sites-enabled/* - vhosts: + servers: disabled_postfix: .disabled # a postfix appended to files when doing non-symlink disabling symlink_opts: {} # partially exposes file.symlink params when symlinking enabled sites rename_opts: {} # partially exposes file.rename params when not symlinking disabled/enabled sites - managed_opts: {} # partially exposes file.managed params for managed vhost files + managed_opts: {} # partially exposes file.managed params for managed server files dir_opts: {} # partially exposes file.directory params for site available/enabled dirs - # vhost declarations - # vhosts will default to being placed in vhost_available + # server declarations + # servers will default to being placed in server_available managed: - mysite: # relative pathname of the vhost 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 - available_dir: /tmp/sites-available # an alternate directory (not sites-available) where this vhost may be found - enabled_dir: /tmp/sites-enabled # an alternate directory (not sites-enabled) where this vhost may be found + available_dir: /tmp/sites-available # an alternate directory (not sites-available) where this server may be found + enabled_dir: /tmp/sites-enabled # an alternate directory (not sites-enabled) where this server may be found disabled_name: mysite.aint_on # an alternative disabled name to be use when not symlinking enabled: True - overwrite: True # overwrite an existing vhost file or not + overwrite: True # overwrite an existing server file or not - # May be a list of config options or None, if None, no vhost file will be managed/templated + # May be a list of config options or None, if None, no server file will be managed/templated # Take server directives as lists of dictionaries. If the dictionary value is another list of # dictionaries a block {} will be started with the dictionary key name config: