Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tofs): test new structure that works with old and new pillars #17

Merged
merged 1 commit into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions systemd/macros.jinja
Original file line number Diff line number Diff line change
@@ -1,58 +1,83 @@
{%- macro files_switch(prefix,
files,
{%- macro files_switch(files,
default_files_switch=['id', 'os_family'],
indent_width=6) %}
{#
{#-
Returns a valid value for the "source" parameter of a "file.managed"
state function. This makes easier the usage of the Template Override and
Files Switch (TOFS) pattern.

Params:
* prefix: pillar prefix to custom ':files_switch'. Colons ':'
are replaced by '/' to be used as directory prefix
* files: ordered list of files to look for, with full path
* default_files_switch: if there's no pillar 'prefix:files_switch'
this is the ordered list of grains to use as selector switch of the
directories under "prefix/files"
are replaced by '/' to be used as directory prefix (<path_prefix>)
* files: ordered list of files to look for
* default_files_switch: if there's no pillar
'<prefix>:files_switch' this is the ordered list of grains to
use as selector switch of the directories under
"<path_prefix>/files"
* indent_witdh: indentation of the result value to conform to YAML

Example:

If we have a state:

/etc/xxx/xxx.conf:
file:
- managed
- source: {{ files_switch('xxx', ['/etc/xxx/xxx.conf',
'/etc/xxx/xxx.conf.jinja']) }}
Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source: {{ files_switch('xxx', ['/etc/yyy/zzz.conf',
'/etc/yyy/zzz.conf.jinja']) }}
- template: jinja

In a minion with id=theminion and os_family=RedHat, it's going to be
rendered as:

/etc/xxx/xxx.conf:
file:
- managed
Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source:
- salt://xxx/files/theminion/etc/xxx/xxx.conf
- salt://xxx/files/theminion/etc/xxx/xxx.conf.jinja
- salt://xxx/files/RedHat/etc/xxx/xxx.conf
- salt://xxx/files/RedHat/etc/xxx/xxx.conf.jinja
- salt://xxx/files/default/etc/xxx/xxx.conf
- salt://xxx/files/default/etc/xxx/xxx.conf.jinja
- salt://xxx/files/theminion/etc/yyy/zzz.conf
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
- salt://xxx/files/default/etc/yyy/zzz.conf
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
- template: jinja
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these comment changes have come from template-formula. These still have to be updated everywhere.

#}
{%- set path_prefix = prefix|replace(':', '/') %}
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch',
default_files_switch) %}
{%- for grain in files_switch_list if grains.get(grain) is defined %}
{%- for file in files %}
{%- set url = '- salt://' ~ path_prefix ~ '/files/' ~
grains.get(grain) ~ file %}
{#- Get the `topdir` from `tpldir` #}
{%- set topdir = tpldir.split('/')[0] %}
{%- set path_prefix = salt['config.get'](topdir ~ ':tofs:path_prefix', topdir) %}
{%- set files_dir = salt['config.get'](topdir ~ ':tofs:dirs:files', 'files') %}
{%- set files_switch_list = salt['config.get'](
topdir ~ ':tofs:files_switch',
default_files_switch
) %}
{#- Only change this from '' when supporting older TOFS implementations #}
{%- for path_prefix_ext in ['', '/networkd', '/resolved', '/timesyncd'] %}
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
{#- For older TOFS implementation, use `files_switch` from the pillar #}
{#- Use the default, new method otherwise #}
{%- set fsl = salt['pillar.get'](
topdir ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
files_switch_list
) %}
{#- Append an empty value to evaluate as `default` in the loop below #}
{%- if '' not in fsl %}
{%- do fsl.append('') %}
{%- endif %}
{%- for fs in fsl %}
{%- for file in files %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
{%- else %}
{%- set fs_dir = salt['config.get'](topdir ~ ':tofs:dirs:default', 'default') %}
{%- endif %}
{%- set url = '- salt://' ~ '/'.join([
path_prefix_inc_ext,
files_dir,
fs_dir,
file.lstrip('/')
]) %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- for file in files %}
{%- set url = '- salt://' ~ path_prefix ~ '/files/default' ~ file %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endmacro %}
7 changes: 6 additions & 1 deletion systemd/networkd/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ networkd:
- user: root
- group: root
- template: jinja
- source: {{ files_switch('systemd:networkd', ['/network']) }}
- source: {{ files_switch(
salt['config.get'](
tpldir ~ ':tofs:files:networkd',
['network']
)
) }}
- clean: True
- dir_mode: 755
- file_mode: 644
Expand Down
7 changes: 6 additions & 1 deletion systemd/resolved/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ resolved:
- group: root
- mode: 644
- template: jinja
- source: {{ files_switch('systemd:resolved', ['/resolved.conf']) }}
- source: {{ files_switch(
salt['config.get'](
tpldir ~ ':tofs:files:resolved',
['resolved.conf']
)
) }}
- listen_in:
- service: resolved
service.running:
Expand Down
7 changes: 6 additions & 1 deletion systemd/timesyncd/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ include:
timesyncd:
file.managed:
- name: /etc/systemd/timesyncd.conf
- source: {{ files_switch('systemd:timesyncd', ['/timesyncd.conf']) }}
- source: {{ files_switch(
salt['config.get'](
tpldir ~ ':tofs:files:timesyncd',
['timesyncd.conf']
)
) }}
cmd.wait:
- name: timedatectl set-ntp true
- runas: root
Expand Down