-
Notifications
You must be signed in to change notification settings - Fork 85
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
Improve TOFS implementation for increased configurability #28
Conversation
template/config.sls
Outdated
|
||
include: | ||
- template.install | ||
|
||
template-config: | ||
file.managed: | ||
- name: {{ template.config }} | ||
- source: {{ files_switch('template', ['example.tmpl'] }} | ||
- source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja']) }} |
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.
No requirement to provide the prefix
here any more. Done just once in the files_switch
macro.
- mode: 644 | ||
- user: root | ||
- group: root | ||
- template: jinja |
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.
Using a jinja
template in order to show the source
of the output file for debugging purposes.
@@ -1,3 +1,6 @@ | |||
# Managed by saltstack | |||
######################################################################## | |||
# File managed by Salt at <{{ source }}>. |
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.
This will show the source
of the file in the header of the managed file.
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.
This header is used in other formulas such as the nfs-formula
.
It's useful enough in normal cases, just to know where the source template resides. In the TOFS system, I believe this is indispensable, since there are so many potential templates a file could have used.
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.
nice
@@ -0,0 +1,6 @@ | |||
######################################################################## | |||
# File managed by Salt at <{{ source }}>. |
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.
Adding this file for further debugging. Using source
again as before.
@@ -1,8 +1,7 @@ | |||
{%- macro files_switch(prefix, |
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.
Removed the need for prefix
.
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.
@myii, I'm looking into this to incorporate at the systemd-formula,
but I use it here to distinguish multiple files:
systemd:networkd (has tofs)
systemd:timesyncd (has tofs)
but both should have a different prefix
otherwise the files will be in the same place....
default_files_switch=['id', 'os_family'], | ||
indent_width=6) %} | ||
{# | ||
{#- |
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.
Without the -
, it leaves an unnecessary blank line in the debug output:
- source:
- salt://template/files/ABC/example.tmpl
template/macros.jinja
Outdated
{%- set path_prefix = prefix|replace(':', '/') %} | ||
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch', | ||
default_files_switch) %} | ||
{%- set formula = 'template' %} |
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.
Define the formula
name once and use it multiple times below.
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.
Isn't there some clever way to get this automatically? Something built in?
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.
Just remembered: tpldir
.
template/macros.jinja
Outdated
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch', | ||
default_files_switch) %} | ||
{%- set formula = 'template' %} | ||
{%- set path_prefix = salt['config.get'](formula ~ ':tofs:path_prefix', formula) %} |
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.
Using config.get
to make the path_prefix
configurable or use formula
name as the default.
default_files_switch) %} | ||
{%- set formula = 'template' %} | ||
{%- set path_prefix = salt['config.get'](formula ~ ':tofs:path_prefix', formula) %} | ||
{%- set files_switch_list = salt['config.get']( |
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.
Now with config.get
instead of pillar.get
.
template/macros.jinja
Outdated
file.lstrip('/')]) %} | ||
{%- set url = '- salt://' ~ '/'.join([ | ||
path_prefix, | ||
salt['config.get'](formula ~ ':tofs:dirs:files', 'files'), |
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.
Using config.get
to make the 'files'
directory configurable or use 'files'
by default.
template/macros.jinja
Outdated
{%- set url = '- salt://' ~ '/'.join([ | ||
path_prefix, | ||
salt['config.get'](formula ~ ':tofs:dirs:files', 'files'), | ||
salt['config.get'](formula ~ ':tofs:dirs:default', 'default'), |
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.
Using config.get
to make the 'default'
directory configurable or use 'default'
by default.
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.
I find the '- salt://'
weird. I would have done salt://
and add the dash in the loop, to have the 'url' really be an url, not an item in a YAML list
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.
maybe the same for salt://' ~ '/'
, why not use salt:///
directly?
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.
That particular section was just direct refactoring, didn't look at it in any detail.
So looking now, this is being used to populate the list under the source
of file.managed
, thus requiring the dash -
prefixed.
So by default, with no pillar, etc. set: template-config:
file.managed:
- name: /etc/template.d/custom-ubuntu.conf
- source:
- salt://template/files/ABC/example.tmpl
- salt://template/files/ABC/example.tmpl.jinja
- salt://template/files/Debian/example.tmpl
- salt://template/files/Debian/example.tmpl.jinja
- salt://template/files/default/example.tmpl
- salt://template/files/default/example.tmpl.jinja
- mode: 644
- user: root
- group: root
- template: jinja With the following pillar set: template:
tofs:
path_prefix: template_alt
dirs:
files: files_alt
default: default_alt
files_switch:
- id
- osfinger
- os
- os_family We get: template-config:
file.managed:
- name: /etc/template.d/custom-ubuntu.conf
- source:
- salt://template_alt/files_alt/ABC/example.tmpl
- salt://template_alt/files_alt/ABC/example.tmpl.jinja
- salt://template_alt/files_alt/Ubuntu-16.04/example.tmpl
- salt://template_alt/files_alt/Ubuntu-16.04/example.tmpl.jinja
- salt://template_alt/files_alt/Ubuntu/example.tmpl
- salt://template_alt/files_alt/Ubuntu/example.tmpl.jinja
- salt://template_alt/files_alt/Debian/example.tmpl
- salt://template_alt/files_alt/Debian/example.tmpl.jinja
- salt://template_alt/files_alt/default_alt/example.tmpl
- salt://template_alt/files_alt/default_alt/example.tmpl.jinja
- mode: 644
- user: root
- group: root
- template: jinja |
Looking at the pillar: template:
tofs:
path_prefix: template_alt
dirs:
files: files_alt
default: default_alt
files_switch:
- id
- osfinger
- os
- os_family May be advantageous to merge these under one key. Maybe something like: template:
tofs:
files_switch:
- id
- osfinger
- os
- os_family
path_prefix: template_alt
dirs:
files: files_alt
default: default_alt |
Haven't done this yet. The final (really) crazy suggestion but for ultimate configurability: - source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja']) }}
Meaning something like: - source: {{ files_switch(
salt['config.get'](
formula ~ ':tofs:files:template-config',
['example.tmpl', 'example.tmpl.jinja']
)
) }}
With an example pillar: template:
tofs:
...
files:
template-config:
- 'example_alt.tmpl'
- 'example_alt.tmpl.jinja' Actually, just tried it and it worked. So I'll push it here as a second commit so that you can try it for yourselves. |
So with the second commit and the additions to the pillar as mentioned in the previous comment, we get: - source:
- salt://template_alt/files_alt/ABC/example_alt.tmpl
- salt://template_alt/files_alt/ABC/example_alt.tmpl.jinja
- salt://template_alt/files_alt/Ubuntu-16.04/example_alt.tmpl
- salt://template_alt/files_alt/Ubuntu-16.04/example_alt.tmpl.jinja
- salt://template_alt/files_alt/Ubuntu/example_alt.tmpl
- salt://template_alt/files_alt/Ubuntu/example_alt.tmpl.jinja
- salt://template_alt/files_alt/Debian/example_alt.tmpl
- salt://template_alt/files_alt/Debian/example_alt.tmpl.jinja
- salt://template_alt/files_alt/default_alt/example_alt.tmpl
- salt://template_alt/files_alt/default_alt/example_alt.tmpl.jinja
- mode: 644
- user: root
- group: root
- template: jinja |
template/config.sls
Outdated
@@ -3,14 +3,20 @@ | |||
|
|||
{%- from "template/map.jinja" import template with context %} | |||
{%- from "template/macros.jinja" import files_switch with context %} | |||
{%- set formula = 'template' %} |
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.
This is ugly but just for testing in this WIP.
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.
Just remembered: tpldir
.
Obviously, this is only a WIP. If going forwards with any of the changes, the documentation and |
I think merging all keys under the |
@daks I'm being cautious here because the TOFS pattern is already being used in other formulas. So this change will cause blowback there. Leaving it open for discussion for now even though I'd prefer if it could be merged going forward. |
@baby-gnu Based on your comment, added commit dfbdb3e, which has allowed the |
template/macros.jinja
Outdated
grains.get(grain), | ||
file.lstrip('/')]) %} | ||
{%- if fs %} | ||
{%- set fs_dir = salt['config.get'](fs, fs) %} |
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.
If the "grain" isn't found, it uses it as a literal value in the path instead. This has two noticeable benefits:
- Allows for additional path matching from the pillar.
- Easier to debug when not working, since the unmatched value will be shown directly in the URL.
@baby-gnu Using |
Example of dfbdb3e in action, in regards to using any custom path ( template:
lookup:
master: template-master
tofs:
...
files_switch:
- any/path/can/be/used/here
- id
- osfinger
- os
- os_family Results in: template-config:
file.managed:
- name: /etc/template.d/custom-ubuntu.conf
- source:
- salt://template/files/any/path/can/be/used/here/example.tmpl
- salt://template/files/any/path/can/be/used/here/example.tmpl.jinja
- salt://template/files/ABC/example.tmpl
- salt://template/files/ABC/example.tmpl.jinja
- salt://template/files/Ubuntu-16.04/example.tmpl
- salt://template/files/Ubuntu-16.04/example.tmpl.jinja
- salt://template/files/Ubuntu/example.tmpl
- salt://template/files/Ubuntu/example.tmpl.jinja
- salt://template/files/Debian/example.tmpl
- salt://template/files/Debian/example.tmpl.jinja
- salt://template/files/default/example.tmpl
- salt://template/files/default/example.tmpl.jinja
- mode: 644
- user: root
- group: root
- template: jinja |
1270a51
to
5afb5c0
Compare
Pushed two more commits re:
|
Added 7ea0f5a to fix the bug reported in the discussion from here. For each additional state in the template-config:
file.managed:
...
- source:
- salt://template/files/ABC/example.tmpl
- salt://template/files/ABC/example.tmpl.jinja
- salt://template/files/Debian/example.tmpl
- salt://template/files/Debian/example.tmpl.jinja
- salt://template/files/default/example.tmpl
- salt://template/files/default/example.tmpl.jinja
...
formula.autofs.config.master:
file.managed:
...
- source:
- salt://template/files/ABC/auto.master
- salt://template/files/Debian/auto.master
- salt://template/files/default/auto.master
- salt://template/files/default/auto.master
...
formula.autofs.config.home:
file.managed:
...
- source:
- salt://template/files/ABC/auto.homeLdap
- salt://template/files/Debian/auto.homeLdap
- salt://template/files/default/auto.homeLdap
- salt://template/files/default/auto.homeLdap
- salt://template/files/default/auto.homeLdap
...
formula.autofs.config.away:
file.managed:
...
- source:
- salt://template/files/ABC/auto.awayLdap
- salt://template/files/Debian/auto.awayLdap
- salt://template/files/default/auto.awayLdap
- salt://template/files/default/auto.awayLdap
- salt://template/files/default/auto.awayLdap
- salt://template/files/default/auto.awayLdap
... |
I think this needs a rebase.. |
OK, consensus on using |
* As discussed between comments: - https://freenode.logbot.info/saltstack-formulas/20190214#c1995273 - https://freenode.logbot.info/saltstack-formulas/20190214#c1995487 * Squashed original PR with these commits present: - Improve TOFS implementation for increased configurability - Improve TOFS to allow custom files selection as well - Simplify `files_switch` macro to use only one loop - Make TOFS pattern fully-portable by using `tpldir` - Update `pillar.example` with current scheme for TOFS - Update `pillar.example` with the proposed scheme for TOFS - Fix bug where duplicate default lines are produced by the loop - feat(tofs): fix to work with old & new pillars and `tpldir` context * Already tested and applied over existing TOFS in `systemd-formula`: - saltstack-formulas/systemd-formula#17
1063ba8
to
068a94d
Compare
After resolving saltstack-formulas/systemd-formula#17, transferred the changes back to this PR and now ready to go. There is one stage left that can be covered by a future PR:
|
merged it! |
@aboe76 Thanks! |
🎉 This PR is included in version 0.7.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
I'll explain this inline and in further comments.
prefix
removed -- ultimately replaced bytpldir
to makefiles_switch
macro fully-portable.macros.jinja
when using the{% from ... %}
directive from an.sls
file in a nested directory (e.g.pkg/install.sls
).path_prefix
configurable.files_dir
configurable.files_switch_list
configurable.default
configurable.files_switch
pillar values extended beyond searching for grains only -- allowing any number of custom paths to be defined (evaluated as literal paths).for
loop infiles_switch
).pillar.example
-- current scheme (working with existing formulas using TOFS).pillar.example
-- proposed scheme (improved layout with minor change required to existing formulas using TOFS).pillar.example
scheme.