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

Improve TOFS implementation for increased configurability #28

Merged
merged 1 commit into from
Feb 23, 2019

Conversation

myii
Copy link
Member

@myii myii commented Feb 15, 2019


I'll explain this inline and in further comments.


  • prefix removed -- ultimately replaced by tpldir to make files_switch macro fully-portable.
    • Potential bug: Confirm that this works properly in 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).
  • Duplication removed (single for loop in files_switch).
  • Update pillar.example -- current scheme (working with existing formulas using TOFS).
  • Update pillar.example -- proposed scheme (improved layout with minor change required to existing formulas using TOFS).
  • Pillar structure resolved (discussion required) -- see comment below.
    • PR is based on the proposed pillar.example scheme.
  • Update all documentation and inline comments as required.
  • Check and update tests to ensure they are passing.


include:
- template.install

template-config:
file.managed:
- name: {{ template.config }}
- source: {{ files_switch('template', ['example.tmpl'] }}
- source: {{ files_switch(['example.tmpl', 'example.tmpl.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.

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
Copy link
Member Author

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 }}>.
Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member

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 }}>.
Copy link
Member Author

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,
Copy link
Member Author

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.

Copy link
Member

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) %}
{#
{#-
Copy link
Member Author

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

{%- set path_prefix = prefix|replace(':', '/') %}
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch',
default_files_switch) %}
{%- set formula = 'template' %}
Copy link
Member Author

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.

Copy link
Member Author

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just remembered: tpldir.

{%- 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) %}
Copy link
Member Author

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'](
Copy link
Member Author

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.

file.lstrip('/')]) %}
{%- set url = '- salt://' ~ '/'.join([
path_prefix,
salt['config.get'](formula ~ ':tofs:dirs:files', 'files'),
Copy link
Member Author

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.

{%- set url = '- salt://' ~ '/'.join([
path_prefix,
salt['config.get'](formula ~ ':tofs:dirs:files', 'files'),
salt['config.get'](formula ~ ':tofs:dirs:default', 'default'),
Copy link
Member Author

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.

Copy link
Member

@daks daks Feb 15, 2019

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

Copy link
Member

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?

Copy link
Member Author

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.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

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

@myii
Copy link
Member Author

myii commented Feb 15, 2019

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

@myii
Copy link
Member Author

myii commented Feb 15, 2019

Haven't done this yet. The final (really) crazy suggestion but for ultimate configurability:

    - source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja']) }}
  • Make the file list configurable via. config.get as well(!)

Meaning something like:

    - source: {{ files_switch(
                    salt['config.get'](
                        formula ~ ':tofs:files:template-config',
                        ['example.tmpl', 'example.tmpl.jinja']
                    )
              ) }}
  • So that's <formula>:tofs:files:<state-id>.
  • We'd need to define formula at a higher level so that it could be used both here and in the files_switch macro.

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.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

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

@@ -3,14 +3,20 @@

{%- from "template/map.jinja" import template with context %}
{%- from "template/macros.jinja" import files_switch with context %}
{%- set formula = 'template' %}
Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just remembered: tpldir.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

Obviously, this is only a WIP. If going forwards with any of the changes, the documentation and pillar.example would also need to be updated.

@daks
Copy link
Member

daks commented Feb 15, 2019

May be advantageous to merge these under one key.

I think merging all keys under the tofs key makes more sense

@myii
Copy link
Member Author

myii commented Feb 15, 2019

May be advantageous to merge these under one key.

I think merging all keys under the tofs key makes more sense

@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.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

@baby-gnu Based on your comment, added commit dfbdb3e, which has allowed the files_switch macro to be simplified to use just the one loop rather than two. Removes more duplication.

grains.get(grain),
file.lstrip('/')]) %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
Copy link
Member Author

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:

  1. Allows for additional path matching from the pillar.
  2. Easier to debug when not working, since the unmatched value will be shown directly in the URL.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

@baby-gnu Using tpldir makes the files_switch macro fully-portable -- drop-in to any formula without any modifications required. So the last remaining task is to decide about the pillar layout as mentioned above. While it will be a hassle in the existing formulas (zabbix, systemd, etc.), going forward I believe it is more appropriate to merge it all under one key, as mentioned in my comment above -- @daks concurred with that suggestion.

@myii myii self-assigned this Feb 15, 2019
@myii
Copy link
Member Author

myii commented Feb 15, 2019

Example of dfbdb3e in action, in regards to using any custom path (any/path/can/be/used/here in the example below). With the pillar:

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

@myii
Copy link
Member Author

myii commented Feb 15, 2019

Pushed two more commits re: pillar.example.

  1. The first commit adds the whole tofs key alongside files_switch from before.
  2. The second commits shows the proposal of files_switch being demoted into the tofs key, including the minor change to macros.jinja.

@myii
Copy link
Member Author

myii commented Feb 15, 2019

Added 7ea0f5a to fix the bug reported in the discussion from here.

For each additional state in the sls, additional default lines were rendered before this fix:

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
    ...

@aboe76
Copy link
Member

aboe76 commented Feb 18, 2019

I think this needs a rebase..

@myii
Copy link
Member Author

myii commented Feb 18, 2019

@aboe76 Rebasing is not a problem. We still need to finalise what we want to do about the remaining tasks, particularly:

  • Pillar structure resolved (discussion required) -- see comment below.
    • PR is based on the proposed pillar.example scheme.

@baby-gnu What are your thoughts about this?

* 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
@myii
Copy link
Member Author

myii commented Feb 23, 2019

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:

  • Update all documentation and inline comments as required.

@myii myii changed the title WIP: Improve TOFS implementation for increased configurability Improve TOFS implementation for increased configurability Feb 23, 2019
@myii myii removed the WIP label Feb 23, 2019
@aboe76 aboe76 merged commit 249d6a7 into saltstack-formulas:master Feb 23, 2019
@aboe76
Copy link
Member

aboe76 commented Feb 23, 2019

merged it!

@myii
Copy link
Member Author

myii commented Feb 23, 2019

@aboe76 Thanks!

@myii myii deleted the PR_improve-TOFS branch February 23, 2019 23:41
@saltstack-formulas-travis

🎉 This PR is included in version 0.7.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants