Skip to content

Commit 71b59fd

Browse files
author
nb
committed
feat(tofs): impl. for ini cfgs and fpm-pools
1 parent 1943ab5 commit 71b59fd

File tree

10 files changed

+253
-72
lines changed

10 files changed

+253
-72
lines changed

php/ng/apache2/ini.sls

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Manages the main Apache2 ini file
2-
{% from "php/ng/map.jinja" import php with context %}
3-
{% from "php/ng/ini.jinja" import php_ini %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/ini.jinja" import php_ini %}
45

5-
{% set settings = php.ini.defaults %}
6-
{% do settings.update(php.apache2.ini.settings) %}
6+
{%- set settings = php.ini.defaults %}
7+
{%- do settings.update(php.apache2.ini.settings) %}
78

89
php_apache2_ini:
9-
{{ php_ini(php.lookup.apache2.ini, php.apache2.ini.opts, settings) }}
10+
{{ php_ini(php.lookup.apache2.ini,
11+
'php_apache2_ini',
12+
php.apache2.ini.opts,
13+
settings
14+
) }}

php/ng/cli/ini.sls

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Manages the php cli main ini file
2-
{% from "php/ng/map.jinja" import php with context %}
3-
{% from "php/ng/ini.jinja" import php_ini %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/ini.jinja" import php_ini %}
45
5-
{% set settings = php.ini.defaults %}
6-
{% for key, value in php.cli.ini.settings.items() %}
7-
{% if settings[key] is defined %}
8-
{% do settings[key].update(value) %}
9-
{% else %}
10-
{% do settings.update({key: value}) %}
11-
{% endif %}
12-
{% endfor %}
6+
{%- set settings = php.ini.defaults %}
7+
{%- for key, value in php.cli.ini.settings.items() %}
8+
{%- if settings[key] is defined %}
9+
{%- do settings[key].update(value) %}
10+
{%- else %}
11+
{%- do settings.update({key: value}) %}
12+
{%- endif %}
13+
{%- endfor %}
1314
1415
php_cli_ini:
15-
{{ php_ini(php.lookup.cli.ini, php.cli.ini.opts, settings) }}
16+
{{ php_ini(php.lookup.cli.ini,
17+
'php_cli_ini',
18+
php.cli.ini.opts,
19+
settings
20+
) }}
File renamed without changes.

php/ng/fpm/config.sls

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
# Manages the php-fpm main ini file
2-
{% from 'php/ng/map.jinja' import php with context %}
3-
{% from "php/ng/ini.jinja" import php_ini %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/ini.jinja" import php_ini %}
45
5-
{% set ini_settings = php.ini.defaults %}
6-
{% for key, value in php.fpm.config.ini.settings.items() %}
7-
{% if ini_settings[key] is defined %}
8-
{% do ini_settings[key].update(value) %}
9-
{% else %}
10-
{% do ini_settings.update({key: value}) %}
11-
{% endif %}
12-
{% endfor %}
6+
{%- set ini_settings = php.ini.defaults %}
7+
{%- for key, value in php.fpm.config.ini.settings.items() %}
8+
{%- if ini_settings[key] is defined %}
9+
{%- do ini_settings[key].update(value) %}
10+
{%- else %}
11+
{%- do ini_settings.update({key: value}) %}
12+
{%- endif %}
13+
{%- endfor %}
1314
14-
{% set conf_settings = php.lookup.fpm.defaults %}
15-
{% do conf_settings.update(php.fpm.config.conf.settings) %}
15+
{%- set conf_settings = php.lookup.fpm.defaults %}
16+
{%- do conf_settings.update(php.fpm.config.conf.settings) %}
1617
1718
php_fpm_ini_config:
18-
{{ php_ini(php.lookup.fpm.ini, php.fpm.config.ini.opts, ini_settings) }}
19+
{{ php_ini(php.lookup.fpm.ini,
20+
'php_fpm_ini_config',
21+
php.fpm.config.ini.opts,
22+
ini_settings
23+
) }}
1924
2025
php_fpm_conf_config:
21-
{{ php_ini(php.lookup.fpm.conf, php.fpm.config.conf.opts, conf_settings) }}
26+
{{ php_ini(php.lookup.fpm.conf,
27+
'php_fpm_conf_config',
28+
php.fpm.config.conf.opts,
29+
conf_settings
30+
) }}
2231
2332
{{ php.lookup.fpm.pools }}:
2433
file.directory:

php/ng/fpm/pools_config.sls

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
# Manages the php-fpm pools config files
2-
{% from 'php/ng/map.jinja' import php with context %}
3-
{% from "php/ng/macro.jinja" import sls_block, serialize %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/macro.jinja" import sls_block, serialize %}
5+
{%- from tplroot ~ "/ng/libtofs.jinja" import files_switch with context %}
46
57
# Simple path concatenation.
68
{% macro path_join(file, root) -%}
79
{{ root ~ '/' ~ file }}
810
{%- endmacro %}
911
10-
{% set pool_states = [] %}
12+
{%- set pool_states = [] %}
1113
12-
{% for pool, config in php.fpm.pools.items() %}
13-
{% if pool == 'defaults' %}{% continue %}{% endif %}
14-
{% for pkey, pvalues in config.get('settings', {}).items() %}
15-
{% set pool_defaults = php.fpm.pools.get('defaults', {}).copy() %}
16-
{% do pool_defaults.update(pvalues) %}
17-
{% do pvalues.update(pool_defaults) %}
18-
{% endfor %}
19-
{% set state = 'php_fpm_pool_conf_' ~ loop.index0 %}
20-
{% set fpath = path_join(config.get('filename', pool), php.lookup.fpm.pools) %}
14+
{%- for pool, config in php.fpm.pools.items() %}
15+
{%- if pool == 'defaults' %}{% continue %}{% endif %}
16+
{%- for pkey, pvalues in config.get('settings', {}).items() %}
17+
{%- set pool_defaults = php.fpm.pools.get('defaults', {}).copy() %}
18+
{%- do pool_defaults.update(pvalues) %}
19+
{%- do pvalues.update(pool_defaults) %}
20+
{%- endfor %}
21+
{%- set state = 'php_fpm_pool_conf_' ~ loop.index0 %}
22+
{%- set fpath = path_join(config.get('filename', pool), php.lookup.fpm.pools) %}
2123
2224
{{ state }}:
23-
{% if config.enabled %}
25+
{%- if config.enabled %}
2426
file.managed:
2527
{{ sls_block(config.get('opts', {})) }}
2628
- name: {{ fpath }}
27-
- source: salt://php/ng/files/php.ini
29+
- source: {{ files_switch( [ 'php.ini' ],
30+
'php_fpm_pool_conf',
31+
v1_path_prefix = '/ng'
32+
) }}
2833
- template: jinja
2934
- context:
3035
config: {{ serialize(config.get('settings', {})) }}
31-
{% else %}
36+
{%- else %}
3237
file.absent:
3338
- name: {{ fpath }}
34-
{% endif %}
39+
{%- endif %}
3540
36-
{% do pool_states.append(state) %}
37-
{% endfor %}
41+
{%- do pool_states.append(state) %}
42+
{%- endfor %}

php/ng/hhvm/config.sls

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# Manages the php-hhvm main ini file
2-
{% from "php/ng/map.jinja" import php with context %}
3-
{% from "php/ng/ini.jinja" import php_ini %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/ini.jinja" import php_ini %}
45
5-
{% set server_settings = php.lookup.hhvm.server %}
6-
{% do server_settings.update(php.hhvm.config.server.settings) %}
6+
{%- set server_settings = php.lookup.hhvm.server %}
7+
{%- do server_settings.update(php.hhvm.config.server.settings) %}
78
8-
{% set php_settings = php.lookup.hhvm.php %}
9-
{% do php_settings.update(php.hhvm.config.php.settings) %}
9+
{%- set php_settings = php.lookup.hhvm.php %}
10+
{%- do php_settings.update(php.hhvm.config.php.settings) %}
1011
1112
php_hhvm_ini_config:
12-
{{ php_ini(php.lookup.hhvm.conf, php.hhvm.config.server.opts, server_settings) }}
13+
{{ php_ini(php.lookup.hhvm.conf,
14+
'php_hhvm_ini_config',
15+
php.hhvm.config.server.opts,
16+
server_settings
17+
) }}
1318
1419
php_hhvm_conf_config:
15-
{{ php_ini(php.lookup.hhvm.ini, php.hhvm.config.php.opts, php_settings) }}
20+
{{ php_ini(php.lookup.hhvm.ini,
21+
'php_hhvm_conf_config',
22+
php.hhvm.config.php.opts,
23+
php_settings
24+
) }}
1625

php/ng/ini.jinja

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# -*- coding: utf-8 -*-
22
# vim: ft=jinja
3-
{# php.ini management macro. #}
4-
{% from "php/ng/macro.jinja" import sls_block, serialize %}
3+
{#- php.ini management macro. #}
4+
{%- set tplroot = tpldir.split('/')[0] %}
5+
{%- from tplroot ~ "/ng/macro.jinja" import sls_block, serialize %}
6+
{%- from tplroot ~ "/ng/libtofs.jinja" import files_switch with context %}
57

6-
{% macro php_ini(filename, opts={}, settings={}) %}
8+
{%- macro php_ini(filepath, tofs_lookup, opts={}, settings={}) %}
79
file.managed:
810
{{ sls_block(opts) }}
9-
- name: {{ filename }}
10-
- source: salt://php/ng/files/php.ini
11+
- name: {{ filepath }}
12+
- source: {{ files_switch( [ 'php.ini' ],
13+
tofs_lookup,
14+
v1_path_prefix = '/ng'
15+
) }}
1116
- template: jinja
1217
- context:
1318
config: {{ serialize(settings) }}
19+
1420
{%- endmacro -%}

php/ng/libtofs.jinja

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{%- macro files_switch(source_files,
2+
lookup=None,
3+
default_files_switch=['id', 'os_family'],
4+
indent_width=6,
5+
v1_path_prefix='') %}
6+
{#-
7+
Returns a valid value for the "source" parameter of a "file.managed"
8+
state function. This makes easier the usage of the Template Override and
9+
Files Switch (TOFS) pattern.
10+
11+
Params:
12+
* source_files: ordered list of files to look for
13+
* lookup: key under '<tplroot>:tofs:source_files' to override
14+
list of source files
15+
* default_files_switch: if there's no config (e.g. pillar)
16+
'<tplroot>:tofs:files_switch' this is the ordered list of grains to
17+
use as selector switch of the directories under
18+
"<path_prefix>/files"
19+
* indent_witdh: indentation of the result value to conform to YAML
20+
* v1_path_prefix: (deprecated) only used for injecting a path prefix into
21+
the source, to support older TOFS configs
22+
23+
Example (based on a `tplroot` of `xxx`):
24+
25+
If we have a state:
26+
27+
Deploy configuration:
28+
file.managed:
29+
- name: /etc/yyy/zzz.conf
30+
- source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'],
31+
lookup='Deploy configuration'
32+
) }}
33+
- template: jinja
34+
35+
In a minion with id=theminion and os_family=RedHat, it's going to be
36+
rendered as:
37+
38+
Deploy configuration:
39+
file.managed:
40+
- name: /etc/yyy/zzz.conf
41+
- source:
42+
- salt://xxx/files/theminion/etc/yyy/zzz.conf
43+
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
44+
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
45+
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
46+
- salt://xxx/files/default/etc/yyy/zzz.conf
47+
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
48+
- template: jinja
49+
#}
50+
{#- Get the `tplroot` from `tpldir` #}
51+
{%- set tplroot = tpldir.split('/')[0] %}
52+
{%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %}
53+
{%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %}
54+
{%- set files_switch_list = salt['config.get'](
55+
tplroot ~ ':tofs:files_switch',
56+
default_files_switch
57+
) %}
58+
{#- Lookup source_files (v2), files (v1), or fallback to source_files parameter #}
59+
{%- set src_files = salt['config.get'](
60+
tplroot ~ ':tofs:source_files:' ~ lookup,
61+
salt['config.get'](
62+
tplroot ~ ':tofs:files:' ~ lookup,
63+
source_files
64+
)
65+
) %}
66+
{#- Only add to [''] when supporting older TOFS implementations #}
67+
{%- set path_prefix_exts = [''] %}
68+
{%- if v1_path_prefix != '' %}
69+
{%- do path_prefix_exts.append(v1_path_prefix) %}
70+
{%- endif %}
71+
{%- for path_prefix_ext in path_prefix_exts %}
72+
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
73+
{#- For older TOFS implementation, use `files_switch` from the config #}
74+
{#- Use the default, new method otherwise #}
75+
{%- set fsl = salt['config.get'](
76+
tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
77+
files_switch_list
78+
) %}
79+
{#- Append an empty value to evaluate as `default` in the loop below #}
80+
{%- if '' not in fsl %}
81+
{%- do fsl.append('') %}
82+
{%- endif %}
83+
{%- for fs in fsl %}
84+
{%- for src_file in src_files %}
85+
{%- if fs %}
86+
{%- set fs_dir = salt['config.get'](fs, fs) %}
87+
{%- else %}
88+
{%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
89+
{%- endif %}
90+
{%- set url = [
91+
'- salt:/',
92+
path_prefix_inc_ext.strip('/'),
93+
files_dir.strip('/'),
94+
fs_dir.strip('/'),
95+
src_file.strip('/'),
96+
] | select | join('/') %}
97+
{{ url | indent(indent_width, true) }}
98+
{%- endfor %}
99+
{%- endfor %}
100+
{%- endfor %}
101+
{%- endmacro %}

php/ng/xcache/ini.sls

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Manages the php cli main ini file
2-
{% from "php/ng/map.jinja" import php with context %}
3-
{% from "php/ng/ini.jinja" import php_ini %}
2+
{%- set tplroot = tpldir.split('/')[0] %}
3+
{%- from tplroot ~ "/ng/map.jinja" import php with context %}
4+
{%- from tplroot ~ "/ng/ini.jinja" import php_ini %}
45
5-
{% set settings = php.xcache.ini.defaults %}
6-
{% for key, value in php.xcache.ini.settings.items() %}
7-
{% if settings[key] is defined %}
8-
{% do settings[key].update(value) %}
9-
{% else %}
10-
{% do settings.update({key: value}) %}
11-
{% endif %}
12-
{% endfor %}
6+
{%- set settings = php.xcache.ini.defaults %}
7+
{%- for key, value in php.xcache.ini.settings.items() %}
8+
{%- if settings[key] is defined %}
9+
{%- do settings[key].update(value) %}
10+
{%- else %}
11+
{%- do settings.update({key: value}) %}
12+
{%- endif %}
13+
{%- endfor %}
1314
1415
php_xcache_ini:
15-
{{ php_ini(php.lookup.xcache.ini, php.xcache.ini.opts, settings) }}
16+
{{ php_ini(php.lookup.xcache.ini,
17+
'php_xcache_ini',
18+
php.xcache.ini.opts,
19+
settings
20+
) }}

pillar.example

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,39 @@ php:
190190
# When using php.ng.apache2 on FreeBSD:
191191
# Set this to False if you're not using apache-formula
192192
use_apache_formula: True
193+
tofs:
194+
# The files_switch key serves as a selector for alternative
195+
# directories under the formula files directory. See TOFS pattern
196+
# doc for more info.
197+
# Note: Any value not evaluated by `config.get` will be used literally.
198+
# This can be used to set custom paths, as many levels deep as required.
199+
# files_switch:
200+
# - any/path/can/be/used/here
201+
# - id
202+
# - role
203+
# - osfinger
204+
# - os
205+
# - os_family
206+
# All aspects of path/file resolution are customisable using the options below.
207+
# This is unnecessary in most cases; there are sensible defaults.
208+
# path_prefix: template_alt
209+
# dirs:
210+
# files: files_alt
211+
# default: default_alt
212+
source_files:
213+
php_apache2_ini:
214+
- alt_php.ini
215+
php_cli_ini:
216+
- alt_php.ini
217+
php_fpm_conf_config:
218+
- alt_php.ini
219+
php_fpm_ini_config:
220+
- alt_php.ini
221+
php_fpm_pool_conf:
222+
- alt_php.ini
223+
php_hhvm_conf_config:
224+
- alt_php.ini
225+
php_hhvm_ini_config:
226+
- alt_php.ini
227+
php_xcache_ini:
228+
- alt_php.ini

0 commit comments

Comments
 (0)