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

refactor(tofs): avoid using “salt['config.get']” for formula writers #77

Merged

Conversation

baby-gnu
Copy link
Contributor

We can hide the call to “salt['config.get']” in the macro by only
asking for a namespace where to lookup the “source_files”.

  • docs/TOFS_pattern.rst (Example): document the use of the new
    “namespace” parameter.

  • template/macros.jinja: add a new “namespace” parameter.
    Lookup files override under
    “:tofs:sources_files:” and fallback to
    “source_files” parameter.

  • template/config/file.sls (template-config-file-file-managed): use
    the new “namespace” parameter

@baby-gnu baby-gnu force-pushed the feature/simplify-files_switch-call branch from 041d261 to 8003492 Compare March 11, 2019 14:36
@baby-gnu
Copy link
Contributor Author

Hello.

I think I can rewrite the commit message since it actually breaks things. Maybe there is a better way of doing it, it's open to suggestion.

Regards

@myii
Copy link
Member

myii commented Mar 11, 2019

@baby-gnu Just had a brief glance -- this looks like a very good idea. What break things -- just the commit message or something else?

@myii myii changed the title refactor(tofs): avoid using “salt['config.get']” for forumla writers refactor(tofs): avoid using “salt['config.get']” for formula writers Mar 12, 2019
template/macros.jinja Outdated Show resolved Hide resolved
template/macros.jinja Outdated Show resolved Hide resolved
template/macros.jinja Outdated Show resolved Hide resolved
@myii
Copy link
Member

myii commented Mar 12, 2019

@baby-gnu I've had a chance to look at this and test it out and I definitely agree that this is the right way to go forwards -- thanks for the contribution! However, I'd just like a couple of things cleaned up, as I've mentioned in my comments above. I also want to ask @aboe76 to have a look, so that we can be confident that we're heading in the right direction.

I've tested before and after with the following in my pillar (includes some extra test states for files_switch):

  tofs:
    files_switch:
      - any/path/can/be/used/here
      - id
      - osfinger
      - os
      - os_family
    path_prefix: template_alt
    dirs:
      files: files_alt
      default: default_alt
    source_files:
      template-config-file-file-managed:
        - 'example_alt_source.tmpl'
        - 'example_alt.tmpl.jinja'
      formula.autofs.config.master:
        - 'auto_alt.master'
      formula.autofs.config.home:
        - 'auto_alt.homeLdap'
      formula.autofs.config.away:
        - 'auto_alt.awayLdap'

I've got the same results before and after:

    - source:
      - salt://template_alt/files_alt/any/path/can/be/used/here/example_alt_source.tmpl
      - salt://template_alt/files_alt/any/path/can/be/used/here/example_alt.tmpl.jinja
      - salt://template_alt/files_alt/ABC/example_alt_source.tmpl
      - salt://template_alt/files_alt/ABC/example_alt.tmpl.jinja
      - salt://template_alt/files_alt/Ubuntu-16.04/example_alt_source.tmpl
      - salt://template_alt/files_alt/Ubuntu-16.04/example_alt.tmpl.jinja
      - salt://template_alt/files_alt/Ubuntu/example_alt_source.tmpl
      - salt://template_alt/files_alt/Ubuntu/example_alt.tmpl.jinja
      - salt://template_alt/files_alt/Debian/example_alt_source.tmpl
      - salt://template_alt/files_alt/Debian/example_alt.tmpl.jinja
      - salt://template_alt/files_alt/default_alt/example_alt_source.tmpl
      - salt://template_alt/files_alt/default_alt/example_alt.tmpl.jinja

    - source:
      - salt://template_alt/files_alt/any/path/can/be/used/here/auto_alt.master
      - salt://template_alt/files_alt/ABC/auto_alt.master
      - salt://template_alt/files_alt/Ubuntu-16.04/auto_alt.master
      - salt://template_alt/files_alt/Ubuntu/auto_alt.master
      - salt://template_alt/files_alt/Debian/auto_alt.master
      - salt://template_alt/files_alt/default_alt/auto_alt.master

    - source:
      - salt://template_alt/files_alt/any/path/can/be/used/here/auto_alt.homeLdap
      - salt://template_alt/files_alt/ABC/auto_alt.homeLdap
      - salt://template_alt/files_alt/Ubuntu-16.04/auto_alt.homeLdap
      - salt://template_alt/files_alt/Ubuntu/auto_alt.homeLdap
      - salt://template_alt/files_alt/Debian/auto_alt.homeLdap
      - salt://template_alt/files_alt/default_alt/auto_alt.homeLdap

    - source:
      - salt://template_alt/files_alt/any/path/can/be/used/here/auto_alt.awayLdap
      - salt://template_alt/files_alt/ABC/auto_alt.awayLdap
      - salt://template_alt/files_alt/Ubuntu-16.04/auto_alt.awayLdap
      - salt://template_alt/files_alt/Ubuntu/auto_alt.awayLdap
      - salt://template_alt/files_alt/Debian/auto_alt.awayLdap
      - salt://template_alt/files_alt/default_alt/auto_alt.awayLdap

So I'm happy with this implementation.

Copy link
Member

@myii myii left a comment

Choose a reason for hiding this comment

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

Two changes:

  1. {#-
  2. Avoid using files, which can be mixed up with the files in tofs:dir:files.
    1. I've made the suggestions inline to use src_file and src_files for these variables.

Another change to be discussed:

  1. This comment about whether it is worth keeping namespace= and source_files= or not.

You could also fix the typo in the commit message:

  • forumla => formula.

@myii myii requested a review from aboe76 March 12, 2019 02:19
)
) }}
- source: {{ files_switch(namespace='template-config-file-file-managed',
source_files=['example.tmpl', 'example.tmpl.jinja']
Copy link
Member

Choose a reason for hiding this comment

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

Using namespace= and source_files= seems to make sense at this stage. However, I think it just makes this longer than in needs to be. Not pushing this issue, just opening it for discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe lookup= is much more meanful than namespace=.

@baby-gnu
Copy link
Contributor Author

@baby-gnu Just had a brief glance -- this looks like a very good idea. What break things -- just the commit message or something else?

The files_switch API is broken, I introduced a new first parameter.

To make it compatible with users calling directly salt['config.get'] we should invert the two parameters like:

{%- macro files_switch(source_files,
                       namespace,
                       default_files_switch=['id', 'os_family'],
                       indent_width=6) %}

This way it can be used as

    - source: {{ files_switch(
                    salt['config.get'](
                        tplroot ~ ':tofs:source_files:template-config-file-file-managed',
                        ['example.tmpl', 'example.tmpl.jinja']
                    )
              ) }}

or

    - source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja'],
                              namespace='template-config-file-file-managed',
                 )
              }}

It could even be missused (doing the lookup two times) with:

    - source: {{ files_switch(
                    salt['config.get'](
                        tplroot ~ ':tofs:source_files:template-config-file-file-managed',
                        ['example.tmpl', 'example.tmpl.jinja']
                    ),
                    namespace='template-config-file-file-managed',

              ) }}

So, feature or refactor? ;-)

@baby-gnu baby-gnu force-pushed the feature/simplify-files_switch-call branch 3 times, most recently from d8292e2 to 62d1d24 Compare March 13, 2019 13:02
@baby-gnu
Copy link
Contributor Author

baby-gnu commented Mar 13, 2019

I rebased my work, integrating the different comments made.

The default-debian-8-backports test fail seems not related to my work.

I changed the new variable name, position and optionality.

People using the old format are not forced to change, they can still call salt['config.get'] by themself:

    - source: {{ files_switch(
                    salt['config.get'](
                        tplroot ~ ':tofs:source_files:template-config-file-file-managed',
                        ['example.tmpl', 'example.tmpl.jinja']
                    )
                 )
              }}

or they can change and use the new optional lookup argument:

    - source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja'],
                              lookup='template-config-file-file-managed'
                 )
              }}

There is no error if the formula writer mistakely added the lookup argument without changing the list of files:

    - source: {{ files_switch(
                    salt['config.get'](
                        tplroot ~ ':tofs:source_files:template-config-file-file-managed',
                        ['example.tmpl', 'example.tmpl.jinja']
                    ),
                    lookup='template-config-file-file-managed'
                 )
              }}

Regards.

@myii
Copy link
Member

myii commented Mar 13, 2019

Restarted the failing tests and all OK from that end.

template/macros.jinja Outdated Show resolved Hide resolved
template/macros.jinja Outdated Show resolved Hide resolved
@myii
Copy link
Member

myii commented Mar 13, 2019

@baby-gnu I think this updated method is definitely an improvement -- thanks for the extra effort you've put in. Please consider my new comments and let's wait for @aboe76 to review before merging.

We can hide the call to “salt['config.get']” in the macro by only
asking for a “lookup key” where to lookup the list of “source_files”.

* docs/TOFS_pattern.rst (Example): document the use of the new
  optional “lookup” parameter.

* template/macros.jinja: add a new optional “lookup” parameter.
  Lookup “files override” under the
  “<tplroot>:tofs:sources_files:<lookup key>” and fallback to
  “source_files” parameter.

* template/config/file.sls (template-config-file-file-managed): use
  the new “lookup” parameter.
@baby-gnu baby-gnu force-pushed the feature/simplify-files_switch-call branch from 62d1d24 to 60d43e7 Compare March 14, 2019 09:14
@myii
Copy link
Member

myii commented Mar 14, 2019

@baby-gnu I'm really happy with this now -- an excellent refactor. Thanks for your work and your patience. Would you be willing to submit a follow-up PR to update docs/TOFS_pattern.rst?

@aboe76 I'm happy with this to be merged, if you are OK with the changes as well.

@baby-gnu
Copy link
Contributor Author

Would you be willing to submit a follow-up PR to update docs/TOFS_pattern.rst?

I should have missed something @myii because I updated the example in docs/TOFS_pattern.rst.

@myii
Copy link
Member

myii commented Mar 14, 2019

@baby-gnu You're right, it has been updated! I didn't look at the whole file, so I thought there may be more changes needed.

@aboe76
Copy link
Member

aboe76 commented Mar 14, 2019

@baby-gnu and @myii, you are both right, this looks like an improvement and I like the lookup part, this is a better pattern to use. LGTM

@myii
Copy link
Member

myii commented Mar 15, 2019

UPDATE: Please leave this table for the time being, until the libtofs.jinja idea is finalised. The table can then be reproduced in that PR when it is submitted.


@baby-gnu As discussed on IRC/Matrix, list of formulas to be updated:

Formula Assignees Status Done
systemd-formula @baby-gnu Small update -
lighttpd-formula Small update -
stack-formula @myii Small update -
zabbix-formula From v1 -
varnish-formula From v1 -

@babilen5 Sorry for the false ping, somehow got you instead of @baby-gnu!

@alxwr
Copy link
Member

alxwr commented Mar 19, 2019

So macros.jinja now becomes a library which can be just dropped inside any formula? :-) I'm all for that. Maybe we want a libtofs.jinja? I really like the idea of being able to update standard functionality by copy-pasting the macro files I need into specific formulas. Copy-paste keeps the workload low while avoiding inter-formula dependencies.

@baby-gnu
Copy link
Contributor Author

Hello.

So macros.jinja now becomes a library which can be just dropped inside any formula? :-) I'm all for that. Maybe we want a libtofs.jinja?

I like the idea, but for systemd-formula it looks like there are some specific things to lookup files under the sub-directories for each component.

I did not took enough time to look at it but I think a libtofs.jinja is a great idea since it meaningful.

Regards.

@myii myii merged commit 6d2140f into saltstack-formulas:master Mar 21, 2019
@myii
Copy link
Member

myii commented Mar 21, 2019

@alxwr I really like the idea of a libtofs.jinja, keeping macros in separate files for ease of reuse. @baby-gnu Your concerns for the systemd-formula are well-founded -- I've got some progress to share, so I'm going to merge this PR in the meantime. I'll put something up and ping you all soon.

To all, thanks for your work, reviews and ideas.

@saltstack-formulas-travis

🎉 This PR is included in version 1.2.4 🎉

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
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants