-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
#} | ||
{%- 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.