From e07b5582dc7895af9a81c4716d5da929f193aa6f Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 23 Jul 2019 12:02:42 +0100 Subject: [PATCH 1/2] feat(passenger): inc config, snippets, servers, etc - currently you're forced to define extra states if you opt to install nginx with passenger, if you want the same outcome for passenger and nginx installs with equivalent config - passenger is an extra module on top of nginx, makes no sense to end up with less configuration by default --- nginx/passenger.sls | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nginx/passenger.sls b/nginx/passenger.sls index 1f962904..91808c0a 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: From 1ceb3f72a5f281465e07f863ff885a2424bdbd91 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 23 Jul 2019 14:11:50 +0100 Subject: [PATCH 2/2] feat(config): use separate defaults for all states - In the case that you want to use a custom template for one state, it's _unlikely_ that you want to use the same custom template for another - Although this results in duplicate files, it makes the behaviour of the formula easier to understand, e.g - I specify a custom hard-coded nginx.conf file that I use on all servers. I don't expect it to suddenly be impossible to configure passenger. - Similarly, if I do something custom with server.conf, I don't expect that snippers are affected --- nginx/files/default/passenger.conf | 45 ++++++++++++++++++++++++++++++ nginx/files/default/snippet.conf | 29 +++++++++++++++++++ nginx/passenger.sls | 2 +- nginx/snippets.sls | 2 +- pillar.example | 6 ++-- 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 nginx/files/default/passenger.conf create mode 100644 nginx/files/default/snippet.conf 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 91808c0a..e3643d62 100644 --- a/nginx/passenger.sls +++ b/nginx/passenger.sls @@ -35,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