diff --git a/php/ng/files/php.ini b/php/ng/files/php.ini index 20ae67e..d3f1a77 100644 --- a/php/ng/files/php.ini +++ b/php/ng/files/php.ini @@ -1,21 +1,26 @@ -{%- macro php_block(section, settings) %} - - {%- if settings is number or settings is string %} +{%- macro php_block(config) %} + {% for sections in config %} + {%- for section, settings in sections.items() -%} + {%- if settings is number or settings is string %} {{ section }} = {{ settings }} - {%- else %} + {%- else %} [{{ section }}] - {%- for setting, value in settings.items() -%} - {%- if value is number or value is string %} - {{ setting }} = {{ value }} - {%- elif value is iterable -%} - {%- if setting == 'error_reporting' %} - {{ setting }} = {{ value|join(" & ") }} - {%- else %} - {{ setting }} = {{ value|join(",") }} - {%- endif -%} - {%- endif %} + {%- for setting in settings -%} + {%- for key, value in setting.items() %} + {%- if value is number or value is string %} + {{ key }} = {{ value }} + {%- elif value is iterable -%} + {%- if key == 'error_reporting' %} + {{ key }} = {{ value|join(" & ") }} + {%- else %} + {{ key }} = {{ value|join(",") }} + {%- endif -%} + {%- endif -%} + {%- endfor -%} + {%- endfor -%} + {%- endif -%} {%- endfor -%} - {%- endif -%} + {% endfor %} {%- endmacro -%} ; PHP configuration file. @@ -23,6 +28,4 @@ ; **** DO NOT EDIT THIS FILE **** ; ; This file is managed by Salt -{%- for section, settings in config.items() -%} -{{ php_block(section, settings) }} -{%- endfor -%} +{{ php_block(config) }} diff --git a/php/ng/fpm/config.sls b/php/ng/fpm/config.sls index fd49961..1f24472 100644 --- a/php/ng/fpm/config.sls +++ b/php/ng/fpm/config.sls @@ -1,5 +1,6 @@ # Manages the php-fpm main ini file -{% from 'php/ng/map.jinja' import php, sls_block with context %} +{% from 'php/ng/map.jinja' import php with context %} +{% from "php/ng/macro.jinja" import sls_block, serialize %} {% set ini_settings = php.ini.defaults %} {% do ini_settings.update(php.fpm.config.ini.settings) %} @@ -14,7 +15,7 @@ php_fpm_ini_config: - source: salt://php/ng/files/php.ini - template: jinja - context: - config: {{ ini_settings }} + config: {{ serialize(ini_settings) }} php_fpm_conf_config: file.managed: @@ -23,4 +24,4 @@ php_fpm_conf_config: - source: salt://php/ng/files/php.ini - template: jinja - context: - config: {{ conf_settings }} + config: {{ serialize(conf_settings) }} diff --git a/php/ng/fpm/pools.sls b/php/ng/fpm/pools.sls index 459f4c8..79bb0b4 100644 --- a/php/ng/fpm/pools.sls +++ b/php/ng/fpm/pools.sls @@ -1,6 +1,7 @@ # Manages the php-fpm pools config files -{% from 'php/ng/map.jinja' import php, sls_block with context %} -{% from 'php/ng/fpm/pools_config.sls' import pool_states with context %} +{% from "php/ng/map.jinja" import php with context %} +{% from "php/ng/macro.jinja" import sls_block %} +{% from "php/ng/fpm/pools_config.sls" import pool_states with context %} {% macro file_requisites(states) %} {%- for state in states %} diff --git a/php/ng/fpm/pools_config.sls b/php/ng/fpm/pools_config.sls index 61f0ba4..924b461 100644 --- a/php/ng/fpm/pools_config.sls +++ b/php/ng/fpm/pools_config.sls @@ -1,5 +1,6 @@ # Manages the php-fpm pools config files -{% from 'php/ng/map.jinja' import php, sls_block with context %} +{% from 'php/ng/map.jinja' import php with context %} +{% from "php/ng/macro.jinja" import sls_block, serialize %} # Simple path concatenation. {% macro path_join(file, root) -%} @@ -20,7 +21,7 @@ - source: salt://php/ng/files/php.ini - template: jinja - context: - config: {{ config.settings }} + config: {{ serialize(config.settings) }} {% else %} file.absent: - name: {{ fpath }} diff --git a/php/ng/fpm/service.sls b/php/ng/fpm/service.sls index 45cb1f5..aa8cd2c 100644 --- a/php/ng/fpm/service.sls +++ b/php/ng/fpm/service.sls @@ -1,5 +1,7 @@ # Manages the php-fpm service. -{% from 'php/ng/map.jinja' import php, sls_block with context %} +{% from "php/ng/map.jinja" import php with context %} +{% from "php/ng/macro.jinja" import sls_block %} + {% set service_function = {True:'running', False:'dead'}.get(php.fpm.service.enabled) %} include: diff --git a/php/ng/ini.sls b/php/ng/ini.sls index 2730a70..cae024f 100644 --- a/php/ng/ini.sls +++ b/php/ng/ini.sls @@ -1,7 +1,8 @@ # php.ng.ini.sls # # Generic php.ini management state. -{% from "php/ng/map.jinja" import php, sls_block with context %} +{% from "php/ng/map.jinja" import php with context %} +{% from "php/ng/macro.jinja" import sls_block, serialize %} php_ini: file.managed: @@ -9,4 +10,4 @@ php_ini: - source: salt://php/ng/files/php.ini - template: jinja - context: - config: {{ php.ini.settings }} + config: {{ serialize(php.ini.settings) }} diff --git a/php/ng/installed.jinja b/php/ng/installed.jinja index 3717475..0f9f360 100644 --- a/php/ng/installed.jinja +++ b/php/ng/installed.jinja @@ -1,5 +1,7 @@ # Template for installing packages. -{% from "php/ng/map.jinja" import php, sls_block with context %} +{% from "php/ng/map.jinja" import php with context %} +{% from "php/ng/macro.jinja" import sls_block %} + {% set opts = php.installed.get(state, {}) %} php_install_{{ state }}: diff --git a/php/ng/macro.jinja b/php/ng/macro.jinja new file mode 100644 index 0000000..4dfb717 --- /dev/null +++ b/php/ng/macro.jinja @@ -0,0 +1,68 @@ +# Returns a generic block of values suitable for inclusion in most states. +{% macro sls_block(dict, ind=4) %} + {% for key, value in dict.items() %} +{{ '-'|indent(ind, True) }} {{ key }}: {{ value|json() }} + {% endfor %} +{% endmacro %} + +# Serializes dicts into sequenced data +{%- macro serialize(data) -%} + {%- if data is mapping -%} + {%- set ret = [] -%} + {%- for key, value in data.items() -%} + {%- set value = serialize(value)|load_json() -%} + {%- do ret.append({key: value}) -%} + {%- endfor -%} + {%- elif data is iterable and data is not string -%} + {%- set ret = [] -%} + {%- for value in data -%} + {%- set value = serialize(value)|load_json() -%} + {%- do ret.append(value) -%} + {%- endfor -%} + {%- else -%} + {% set ret = data %} + {%- endif -%} + + {{ ret|json() }} +{%- endmacro -%} + +{%- macro deserialize(data) -%} + {%- if data is mapping -%} + {%- set ret = odict([]) -%} + {%- for key, value in data.items() -%} + {%- do ret.update({key: deserialize(value)}) -%} + {%- endfor -%} + {%- elif data is iterable and data is not string -%} + {%- if is_list_skd(data)|int() == 1 -%} + {%- set ret = odict([]) -%} + {%- for item in data -%} + {%- for key, value in item.items() -%} + {% do ret.update({key: deserialize(value)}) %} + {%- endfor -%} + {%- endfor -%} + {%- else -%} + {%- set ret = [] -%} + {%- for item in data -%} + {%- do ret.append(deserialize(item)) -%} + {%- endfor -%} + {%- endif -%} + {%- else -%} + {% set ret = data %} + {%- endif -%} + + {{ ret }} +{%- endmacro -%} + +# and is not number and is mapping and item|length() == 1 +{%- macro is_list_skd(list) -%} + {% set ret = 0 %} + {%- set skds = {'counter': 0} -%} + {%- for item in list if item is mapping and item|length() == 1 -%} + {%- do skds.update({'counter': (skds.counter + 1)}) -%} + {%- endfor -%} + {%- if skds.counter == list|length() -%} + {% set ret = 1 %} + {%- endif -%} + + {{ ret }} +{%- endmacro -%} diff --git a/php/ng/map.jinja b/php/ng/map.jinja index e91c1dd..48b5e6e 100644 --- a/php/ng/map.jinja +++ b/php/ng/map.jinja @@ -1,9 +1,3 @@ -{% macro sls_block(dict) %} - {% for key, value in dict.items() %} - - {{ key }}: {{ value|json() }} - {% endfor %} -{% endmacro %} - {% set php = salt['pillar.get']('php:ng', { 'lookup': salt['grains.filter_by']({ 'Debian': {