diff --git a/nginx/files/default/passenger.conf b/nginx/files/default/passenger.conf new file mode 100644 index 00000000..91efad61 --- /dev/null +++ b/nginx/files/default/passenger.conf @@ -0,0 +1,45 @@ +{% set indent_increment = 4 %} + +{%- macro nginx_block(value, key=None, operator=' ', delim=';', ind=0) -%} + {%- if value != None -%} + {%- if value is number or value is string -%} + {{ key|indent(ind, True) }}{{ operator }}{{ value }}{{ delim }} + {%- elif value is mapping -%} + {{ key|indent(ind, True) }}{{ operator }}{{ '{' }} + {%- for k, v in value.items() %} + {%- if k != 'include' %} +{{ nginx_block(v, k, operator, delim, (ind + indent_increment)) }} + {%- endif %} + {%- endfor %} + {%- if 'include' in value.keys() %} +{{ nginx_block(value['include'], 'include', operator, delim, (ind + indent_increment)) }} + {%- endif %} +{{ '}'|indent(ind, True) }} + {%- elif value is iterable -%} + {%- for v in value %} +{{ nginx_block(v, key, operator, delim, ind) }} + {%- endfor -%} + {%- else -%} + {{ key|indent(ind, True) }}{{ operator }}{{ value }}{{ delim }} + {%- endif -%} + {%- else -%} + {%- endif -%} +{%- endmacro -%} + +# Default nginx server configuration +# +# **** DO NOT EDIT THIS FILE **** +# +# This file is managed by Salt. + +{% if 'load_module' in config.keys() %} +{{ nginx_block(config.pop('load_module'), 'load_module') }} +{%- endif -%} + +{% if 'include' in config.keys() %} +{{ nginx_block(config.pop('include'), 'include') }} +{%- endif -%} + +{% for key, value in config.items() %} +{{ nginx_block(value, key) }} +{%- endfor -%} diff --git a/nginx/files/default/snippet.conf b/nginx/files/default/snippet.conf new file mode 100644 index 00000000..5721dd1a --- /dev/null +++ b/nginx/files/default/snippet.conf @@ -0,0 +1,29 @@ +{% set ind_increment = 4 %} +{%- macro server_config(values, key='', ind=0, lb='\n', delim=';', operator=' ') -%} + {%- for value in values -%} + {%- if value is number or value is string -%} +{{ key|indent(ind, True) }}{{ operator }}{{ value }}{{ delim }}{{ lb }} + {%- elif value is mapping -%} + {%- for k, v in value.items() -%} + {%- if v is number or v is string -%} +{{ server_config([v], k, ind) }} + {%- elif v|length() > 0 and (v[0] is number or v[0] is string) -%} +{{ server_config(v, k, ind) }} + {%- else -%} +{{ lb }}{{ k|indent(ind, True) }} {{ '{' }} +{{ server_config(v, '', ind + ind_increment) -}} +{{ '}'|indent(ind, True) }}{{ lb }} + {%- endif -%} + {%- endfor -%} + {%- elif value is iterable -%} +{{ server_config(value, ind + ind_increment, delim, operator) }} + {%- endif -%} + {%- endfor -%} +{%- endmacro -%} + +# Nginx server configuration +# +# **** DO NOT EDIT THIS FILE **** +# +# This file is managed by Salt. +{{ server_config(config) }} diff --git a/nginx/passenger.sls b/nginx/passenger.sls index 1f962904..e3643d62 100644 --- a/nginx/passenger.sls +++ b/nginx/passenger.sls @@ -10,7 +10,13 @@ {% if salt['grains.get']('os_family') in ['Debian', 'RedHat'] %} include: - nginx.pkg + - nginx.config - nginx.service + {%- if nginx.snippets is defined %} + - nginx.snippets + {%- endif %} + - nginx.servers + - nginx.certificates passenger_install: pkg.installed: @@ -29,7 +35,7 @@ passenger_config: file.managed: {{ sls_block(nginx.server.opts) }} - name: {{ nginx.lookup.passenger_config_file }} - - source: {{ files_switch(['nginx.conf'], + - source: {{ files_switch(['passenger.conf'], 'passenger_config_file_managed' ) }} diff --git a/nginx/snippets.sls b/nginx/snippets.sls index bd881a93..5f05c803 100644 --- a/nginx/snippets.sls +++ b/nginx/snippets.sls @@ -15,7 +15,7 @@ nginx_snippets_dir: nginx_snippet_{{ snippet }}: file.managed: - name: {{ nginx.lookup.snippets_dir ~ '/' ~ snippet }} - - source: {{ files_switch([ snippet, 'server.conf' ], + - source: {{ files_switch([ snippet, 'snippet.conf' ], 'nginx_snippet_file_managed' ) }} diff --git a/pillar.example b/pillar.example index de62d4bf..447506ef 100644 --- a/pillar.example +++ b/pillar.example @@ -303,7 +303,7 @@ nginx: # All aspects of path/file resolution are customisable using the options below. # This is unnecessary in most cases; there are sensible defaults. # Path pattern: salt://{{ path_prefix or 'nginx' }}/{{ dirs.files or 'files' }}/{{ dirs.default or 'default' }} - # path_prefix: template_alt + # path_prefix: template_alt # dirs: # files: files_alt # default: default_alt @@ -311,10 +311,10 @@ nginx: nginx_config_file_managed: - alt_nginx.conf passenger_config_file_managed: - - alt_nginx.conf + - alt_passenger.conf server_conf_file_managed: - alt_server.conf nginx_systemd_service_file: - alt_nginx.service nginx_snippet_file_managed: - - alt_server.conf + - alt_snippet.conf