Skip to content

Commit 40ffb58

Browse files
authored
Merge pull request #70 from baby-gnu/feature/configurable-map.jinja
feat(map.jinja): load configuration values from configurables sources
2 parents 2011d6a + 365f711 commit 40ffb58

File tree

13 files changed

+252
-121
lines changed

13 files changed

+252
-121
lines changed

libvirt/map.jinja

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,118 @@
33

44
{#- Get the `tplroot` from `tpldir` #}
55
{%- set tplroot = tpldir.split('/')[0] %}
6-
{#- Start imports as #}
7-
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %}
8-
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %}
9-
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
10-
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %}
11-
12-
{#- Retrieve the config dict only once #}
13-
{%- set _config = salt['config.get'](tplroot, default={}) %}
14-
15-
{%- set defaults = salt['grains.filter_by'](
16-
default_settings,
17-
default=tplroot,
18-
merge=salt['grains.filter_by'](
19-
osfamilymap,
20-
grain='os_family',
21-
merge=salt['grains.filter_by'](
22-
osmap,
23-
grain='os',
24-
merge=salt['grains.filter_by'](
25-
osfingermap,
26-
grain='osfinger',
27-
merge=salt['grains.filter_by'](
28-
_config,
29-
default='lookup'
30-
)
31-
)
32-
)
33-
)
34-
)
35-
%}
36-
37-
{%- set config = salt['grains.filter_by'](
38-
{'defaults': defaults},
39-
default='defaults',
40-
merge=_config
41-
)
42-
%}
43-
44-
{%- set libvirt_settings = config %}
6+
7+
{#- Where to lookup parameters source files #}
8+
{%- set map_sources_dir = tplroot | path_join('parameters') %}
9+
10+
{#- Load defaults first to allow per formula default map.jinja configuration #}
11+
{%- set _defaults_filename = map_sources_dir | path_join('defaults.yaml') %}
12+
{%- do salt['log.debug']('map.jinja: initialise parameters from ' ~ _defaults_filename ) %}
13+
{%- import_yaml _defaults_filename as default_settings %}
14+
15+
{#- List of sources to lookup for parameters #}
16+
{%- do salt['log.debug']("map.jinja: lookup 'map_jinja' configuration sources") %}
17+
{#- Fallback to previously used grains plus minion `id` #}
18+
{%- set map_sources = ['osarch', 'os_family', 'os', 'osfinger', 'config_get_lookup', 'config_get', 'id'] %}
19+
{#- Configure map.jinja from defaults.yaml #}
20+
{%- set map_sources = default_settings | traverse('values:map_jinja:sources', map_sources) %}
21+
22+
{#- Lookup global sources #}
23+
{%- set map_sources = salt['config.get']('map_jinja:sources', map_sources) %}
24+
{#- Lookup per formula sources #}
25+
{%- set map_sources = salt['config.get'](tplroot ~ ':map_jinja:sources', map_sources) %}
26+
27+
{%- do salt['log.debug']('map.jinja: load parameters with sources from ' ~ map_sources) %}
28+
29+
30+
{#- Work around assignment inside for loop #}
31+
{#- load configuration values used in `config.get` merging strategies #}
32+
{%- set _config = {'stack': default_settings.get('values', {}),
33+
'merge_strategy': salt['config.get'](tplroot ~ ':strategy', None),
34+
'merge_lists': salt['config.get'](tplroot ~ ':merge_lists', False),
35+
'cli': salt['config.get']('__cli'),
36+
'root_dir': salt['config.get']('root_dir')
37+
} %}
38+
39+
{#- `config.get` merge option works only for `minion` or local `salt-call` #}
40+
{%- if _config['cli'] == 'salt-minion'
41+
or (_config['cli'] == 'salt-call' and not _config['root_dir'].endswith('/running_data')) %}
42+
{%- do _config.update({'merge_opt': {'merge': _config['merge_strategy']},
43+
'merge_msg': ", merge: strategy='" ~ _config['merge_strategy'] ~ "'"}) %}
44+
{%- else %}
45+
{#- no `config.get` merge option for `salt-ssh` or `unknown` cli #}
46+
{%- if _config['merge_strategy'] %}
47+
{%- do salt['log.error']("map.jinja: the 'merge' option of 'config.get' is skipped with salt cli '"
48+
~ _config['cli'] | replace('call', 'ssh') | default('unknown', True) ~ "'")
49+
%}
50+
{%- endif %}
51+
{%- do _config.update({'merge_opt': {},
52+
'merge_msg': ''}) %}
53+
{%- endif %}
54+
55+
56+
{#- process each `map.jinja` source #}
57+
{%- for map_source in map_sources %}
58+
{%- if map_source in ['config_get', 'config_get_lookup'] %}
59+
{%- set _config_key = {'config_get': tplroot,
60+
'config_get_lookup': tplroot ~ ':lookup'}.get(map_source) %}
61+
{%- set _config_type = {'config_get': 'configuration',
62+
'config_get_lookup': 'lookup'}.get(map_source) %}
63+
64+
{%- do salt['log.debug']("map.jinja: retrieve formula " ~ _config_type
65+
~ " with 'config.get'"
66+
~ _config['merge_msg']
67+
) %}
68+
{%- set _config_get = salt['config.get'](_config_key, default={}, **_config['merge_opt']) %}
69+
70+
{#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
71+
{%- set _strategy = _config['merge_strategy'] | default('smart', boolean=True) %}
72+
{%- do salt['log.debug']("map.jinja: merge formula " ~ _config_type
73+
~ " retrieved with 'config.get'"
74+
~ ", merge: strategy='" ~ _strategy
75+
~ "', lists='" ~ _config['merge_lists'] ~ "'"
76+
) %}
77+
{%- do _config.update({'stack': salt['slsutil.merge'](_config['stack'],
78+
_config_get,
79+
strategy=_strategy,
80+
merge_lists=_config['merge_lists'])})
81+
%}
82+
{%- else %}
83+
{#- Lookup the grain/pillar/... #}
84+
{#- Fallback to use the source name as a direct filename #}
85+
{%- set map_values = salt['config.get'](map_source, []) %}
86+
87+
{#- Mangle `map_source` to use it as literal path #}
88+
{%- if map_values|length == 0 %}
89+
{%- set map_source_parts = map_source.split('/') %}
90+
{%- set map_source = map_source_parts[0:-1] | join('/') %}
91+
{%- set map_values = map_source_parts[-1].rstrip('.yaml') %}
92+
{%- endif %}
93+
94+
{#- Some configuration return list #}
95+
{%- if map_values is string %}
96+
{%- set map_values = [map_values] %}
97+
{%- endif %}
98+
99+
{%- for map_value in map_values %}
100+
{%- set yamlfile = map_sources_dir | path_join(map_source, map_value ~ '.yaml') %}
101+
{%- do salt['log.debug']('map.jinja: load parameters from file ' ~ yamlfile) %}
102+
{%- load_yaml as loaded_values %}
103+
{%- include yamlfile ignore missing %}
104+
{%- endload %}
105+
106+
{%- if loaded_values %}
107+
{#- Merge loaded values on the stack #}
108+
{%- do salt['log.debug']('map.jinja: merge parameters from ' ~ yamlfile) %}
109+
{%- do _config.update({'stack': salt['slsutil.merge'](_config['stack'],
110+
loaded_values.get('values', {}),
111+
strategy=loaded_values.get('strategy', 'smart'),
112+
merge_lists=loaded_values.get('merge_lists', False) | to_bool)})
113+
%}
114+
{%- endif %}
115+
{%- endfor %}
116+
{%- endif %}
117+
{%- endfor %}
118+
119+
{%- do salt['log.debug']("map.jinja: save parameters in variable 'libvirt_settings'") %}
120+
{%- set libvirt_settings = _config['stack'] %}

libvirt/osfamilymap.yaml

Lines changed: 0 additions & 40 deletions
This file was deleted.

libvirt/osfingermap.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

libvirt/osmap.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
# vim: ft=yaml
33
---
4-
libvirt:
4+
values:
55
libvirt_pkg: libvirt
66
qemu_pkg: qemu
77
python2_pkg: libvirt-python
@@ -10,3 +10,4 @@ libvirt:
1010
libvirtd_config: /etc/libvirt/libvirtd.conf
1111
daemon_config_path: {}
1212
extra_pkgs: []
13+
...

libvirt/parameters/os/CentOS.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
#
4+
# Setup variables specific to salt['config.get']('os') == CentOS.
5+
# You just need to add the key:values for this `os` that differ
6+
# from `defaults.yaml` + `<osarch>.yaml` + `<os_family>.yaml`.
7+
#
8+
# If you do not need to provide defaults via the `os` config,
9+
# you can remove this file or provide at least an empty dict, e.g.
10+
# values: {}
11+
---
12+
values:
13+
python2_pkg: ~
14+
python3_pkg: python3-libvirt
15+
...

libvirt/parameters/os/Fedora.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
#
4+
# Setup variables specific to salt['config.get']('os') == Fedora.
5+
# You just need to add the key:values for this `os` that differ
6+
# from `defaults.yaml` + `<osarch>.yaml` + `<os_family>.yaml`.
7+
#
8+
# If you do not need to provide defaults via the `os` config,
9+
# you can remove this file or provide at least an empty dict, e.g.
10+
# values: {}
11+
---
12+
values:
13+
python2_pkg: python2-libvirt
14+
python3_pkg: python3-libvirt
15+
...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
#
4+
# Setup variables specific to salt['config.get']('os_family') == Arch.
5+
# You just need to add the key:values for this `os_family` that differ
6+
# from `defaults.yaml` + `<osarch>.yaml`.
7+
#
8+
# If you do not need to provide defaults via the `os_family` config,
9+
# you can remove this file or provide at least an empty dict, e.g.
10+
# values: {}
11+
---
12+
values:
13+
daemon_config_path: /etc/conf.d
14+
...
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
#
4+
# Setup variables specific to salt['config.get']('os_family') == Debian.
5+
# You just need to add the key:values for this `os_family` that differ
6+
# from `defaults.yaml` + `<osarch>.yaml`.
7+
#
8+
# If you do not need to provide defaults via the `os_family` config,
9+
# you can remove this file or provide at least an empty dict, e.g.
10+
# values: {}
11+
---
12+
values:
13+
libvirt_pkg: libvirt-daemon-system
14+
libvirt_service: libvirtd
15+
qemu_pkg: qemu-kvm
16+
python2_pkg: python-libvirt
17+
python3_pkg: python3-libvirt
18+
extra_pkgs:
19+
- libguestfs0
20+
- libguestfs-tools
21+
- gnutls-bin
22+
- virt-top
23+
daemon_config_path: /etc/default
24+
...
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
#
4+
# Setup variables specific to salt['config.get']('os_family') == RedHat.
5+
# You just need to add the key:values for this `os_family` that differ
6+
# from `defaults.yaml` + `<osarch>.yaml`.
7+
#
8+
# If you do not need to provide defaults via the `os_family` config,
9+
# you can remove this file or provide at least an empty dict, e.g.
10+
# values: {}
11+
---
12+
values:
13+
qemu_pkg: qemu-kvm
14+
extra_pkgs:
15+
- libguestfs
16+
daemon_config_path: /etc/sysconfig
17+
...

0 commit comments

Comments
 (0)