Skip to content

Commit ee8c891

Browse files
author
nb
committed
feat(TOFS): TOFS Impl. for fpm and fpm-pools
1 parent 27ec474 commit ee8c891

File tree

5 files changed

+150
-7
lines changed

5 files changed

+150
-7
lines changed
File renamed without changes.

php/ng/fpm/pools_config.sls

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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) -%}
@@ -24,7 +26,10 @@
2426
file.managed:
2527
{{ sls_block(config.get('opts', {})) }}
2628
- name: {{ fpath }}
27-
- source: salt://php/ng/files/php.ini
29+
- source: {{ files_switch( [ pool, 'php.ini' ],
30+
'pool_config_file_managed',
31+
v1_path_prefix = '/ng'
32+
) }}
2833
- template: jinja
2934
- context:
3035
config: {{ serialize(config.get('settings', {})) }}

php/ng/ini.jinja

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# -*- coding: utf-8 -*-
22
# vim: ft=jinja
33
{# php.ini management macro. #}
4-
{% from "php/ng/macro.jinja" import sls_block, serialize %}
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, opts={}, settings={}) %}
9+
10+
{% set filename = filepath.split('/')[-1] %}
711
file.managed:
812
{{ sls_block(opts) }}
9-
- name: {{ filename }}
10-
- source: salt://php/ng/files/php.ini
13+
- name: {{ filepath }}
14+
- source: {{ files_switch( [ filename, 'php.ini' ],
15+
'fpm_config_file_managed',
16+
v1_path_prefix = '/ng'
17+
) }}
1118
- template: jinja
1219
- context:
1320
config: {{ serialize(settings) }}
21+
1422
{%- 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 %}

pillar.example

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ php:
6969
reload: True
7070

7171
# settings for the relevant php-fpm configuration files
72+
# These files or templates can be retrieved by TOFS with
73+
# php.ng.lookup.fpm.[conf,ini] filenames values respectively
74+
# Fallback to the files/default/php.ini template
7275
config:
7376

7477
# options to manage the php.ini file used by php-fpm
@@ -107,6 +110,8 @@ php:
107110

108111
# name of the pool file to be managed, this will be appended
109112
# to the path specified in php.ng.lookup.fpm.pools
113+
# This file or template can be retrieved by TOFS with its name,
114+
# it fallbacks to the files/default/php.ini template
110115
'mypool.conf':
111116
# If true, the pool file will be managed, if False it will be
112117
# absent
@@ -190,3 +195,27 @@ php:
190195
# When using php.ng.apache2 on FreeBSD:
191196
# Set this to False if you're not using apache-formula
192197
use_apache_formula: True
198+
tofs:
199+
# The files_switch key serves as a selector for alternative
200+
# directories under the formula files directory. See TOFS pattern
201+
# doc for more info.
202+
# Note: Any value not evaluated by `config.get` will be used literally.
203+
# This can be used to set custom paths, as many levels deep as required.
204+
# files_switch:
205+
# - any/path/can/be/used/here
206+
# - id
207+
# - role
208+
# - osfinger
209+
# - os
210+
# - os_family
211+
# All aspects of path/file resolution are customisable using the options below.
212+
# This is unnecessary in most cases; there are sensible defaults.
213+
# path_prefix: template_alt
214+
# dirs:
215+
# files: files_alt
216+
# default: default_alt
217+
source_files:
218+
fpm_config_file_managed:
219+
- alt_fpm_config.ini
220+
pool_config_file_managed:
221+
- alt_pool.conf

0 commit comments

Comments
 (0)