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

Add file.comment ignore_missing #62045

Merged

Conversation

nicholasmhughes
Copy link
Collaborator

@nicholasmhughes nicholasmhughes commented May 10, 2022

What does this PR do?

This PR adds the ability to ignore missing regex matches in a file when using the file.comment state. This allows for a "comment only if a match is found" workflow and prevents unnecessary state failures or requisite use. This PR also fixes a small bug where changes would be reported in test mode when the line has already been commented.

What issues does this PR fix or reference?

Fixes: #62044
Fixes: #61662

Previous Behavior

See relevant issues for details.

New Behavior

The ignore_missing parameter allows the state to succeed if the line to be commented is not found. Additionally, changes are no longer reported in test mode when the line is already commented.

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

@nicholasmhughes nicholasmhughes requested a review from a team as a code owner May 10, 2022 18:42
@nicholasmhughes nicholasmhughes requested review from garethgreenaway and removed request for a team May 10, 2022 18:42
@nicholasmhughes
Copy link
Collaborator Author

re-run pr-debian-11-arm64-py3-pytest

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-freebsd-130-amd64-py3-pytest

1 similar comment
@nicholasmhughes
Copy link
Collaborator Author

re-run pr-freebsd-130-amd64-py3-pytest

@nicholasmhughes nicholasmhughes added the Phosphorus v3005.0 Release code name and version label May 11, 2022
@Ch3LL Ch3LL merged commit 391abc9 into saltstack:master May 18, 2022
@nicholasmhughes nicholasmhughes deleted the add-file-comment-ignore-missing branch May 18, 2022 20:25
@myii
Copy link
Contributor

myii commented May 30, 2022

This PR has resulted in a regression, when the line matched by the provided regex already has a comment at the end of the line.

Identified 764c769 as the offending commit using git bisect:

764c7699eb98c9b5f4dac43b1b2cba17e4bce34c is the first bad commit
commit 764c7699eb98c9b5f4dac43b1b2cba17e4bce34c
...
...

    fixes saltstack/salt#61662 fix file.comment reports changes in test mode

 salt/states/file.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Here's a minimal reproducible example:

regression_file_comment/init/file.managed:
  file.managed:
    - name: /tmp/regression_file_comment
    - contents: |
        port = 5432                             # (change requires restart)

regression_file_comment/init/file.comment:
  file.comment:
    - name: /tmp/regression_file_comment
    - regex: ^port\s*=.+
    - require:
      - file: regression_file_comment/init/file.managed

See further details below that may be useful.


Encountered these failures during this week's master testing:

In this case, the default postgresql.conf (on Debian and Ubuntu) has a port line which is commented like so:

port = 5432                             # (change requires restart)

This line is supposed to be commented out if a (different) port has been provided in the config.

https://github.com/saltstack-formulas/postgres-formula/blob/6eecf7f0688744d8cfbdf4b76d938c0a81efdca2/postgres/server/init.sls#L115-L125

{%- set db_port = salt['config.option']('postgres.port') %}
{%- if db_port %}
postgresql-conf-comment-port:
  file.comment:
    - name: {{ postgres.conf_dir }}/postgresql.conf
    - regex: ^port\s*=.+
    - require:
      - file: postgresql-config-dir
{%- endif %}

With the regression being:

                 ID: postgresql-conf-comment-port
           Function: file.comment
               Name: /etc/postgresql/13/main/postgresql.conf
             Result: False
            Comment: port\s*=.+: Pattern not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Phosphorus v3005.0 Release code name and version
Projects
None yet
3 participants