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

file.comment issue when multiple lines with some already commented #41818

Closed
chris-be opened this issue Jun 17, 2017 · 7 comments · Fixed by #51988
Closed

file.comment issue when multiple lines with some already commented #41818

chris-be opened this issue Jun 17, 2017 · 7 comments · Fixed by #51988
Labels
Bug broken, incorrect, or confusing behavior P2 Priority 2 severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around State-Module
Milestone

Comments

@chris-be
Copy link

chris-be commented Jun 17, 2017

Description of Issue/Question

I try to comment some lines in a FreeBSD configuration file. This file contains some comment containing itself the pattern I want to find. Result is that nothing is done.
According to documentation "comment"/"uncomment" do not have a "flags" argument, would it be relevant to add it ?

Setup

Master(less) : Debian Stretch / virtualenv Python 2.7.13 (pip install salt-ssh) / salt-ssh 2016.11.5 (Carbon)
Minion (agentless) : FreeBSD 11.0 in virtualbox / Python 2.7.13

Steps to Reproduce Issue

First

On minion : create a file /home/test_user/test.conf :

# Test sample
*.whatever;and_also 	 /bad/news

# Don't forget to configure /bad/news before enabling it .. 

*.error /bad/news

On master : create a test.sls :

test-comment:
  file.comment:
    - name: /home/test_user/test.conf
    - regex: ^.*/bad/news\s*
    - backup: False

And add it to top.sls

base:
  'the_minion':
    - test

Than

salt-ssh 'the_minion' state.apply

What I get

salt-ssh 'the_minion' state.apply 
the_minion:
----------
          ID: test-comment
    Function: file.comment
        Name: /home/test_user/test.conf
      Result: True
     Comment: Pattern already commented
     Started: 20:11:39.976112
    Duration: 10.888 ms
     Changes:   

Summary for the_minion
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:  10.888 ms

But if I replace "/bad/news" by "bAd/news" in line 4 of test.conf, I get :

salt-ssh 'the_minion' state.apply 
the_minion:
----------
          ID: test-comment
    Function: file.comment
        Name: /home/test_user/test.conf
      Result: True
     Comment: Commented lines successfully
     Started: 20:17:45.291284
    Duration: 14.342 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,7 +1,7 @@
                   # Test sample
                  -*.whatever;and_also 	 /bad/news
                  +#*.whatever;and_also 	 /bad/news
                   
                   # Don't forget to configure /bAd/news before enabling it .. 
                   
                  -*.error /bad/news
                  +#*.error /bad/news
                   

Summary for the_minion
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  14.342 ms
@garethgreenaway garethgreenaway added this to the Approved milestone Jun 19, 2017
@garethgreenaway
Copy link
Contributor

Looks like we need the ability to search and comment all instances of a particular expression. Marking this as approved. Thanks for the report!

@garethgreenaway garethgreenaway added Bug broken, incorrect, or confusing behavior severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around P2 Priority 2 State-Module labels Jun 19, 2017
@Arabus
Copy link
Contributor

Arabus commented Jun 19, 2017

I think someone really went overboard with the error checking in this module.
"Let's run the regex over the file three times to see if we need to run the regex over the file to actually do something..." There ought to be a better way to do this.

@mbunkus
Copy link
Contributor

mbunkus commented Feb 17, 2018

I've just stumbled across this as well and agree that the checks done in file.py are completely overboard. The easiest way I can see is to iterate over all occurrences where the "to comment" regex matches, to comment them, and to set a flag according if at least one match is found. If the flag is still false, emit that "already commented" message.

Dito for file.uncomment.

Just get rid of that first "already commented?" regex search…

@mikesoule
Copy link

Any update on this one? Seems to still be an issue.

@diepes
Copy link
Contributor

diepes commented Mar 5, 2019

I got here searching for file.commet bugs.
In my case I don't have any match and get an error back, is that the expected result?
Comment: xyz: Pattern not found

It would make sense to me that un-comment throws an error if no match found, but the comment should not, or it should be made clear in the documentation.

Also, the regex seems to be anchored with an implicit "^" even if not supplied in the state regex.

@mbunkus
Copy link
Contributor

mbunkus commented Mar 5, 2019

@diepes I'm not a Salt developer myself, so take what I'm saying with a grain of salt (no pun intended). The behavior if the pattern isn't found at all matches the file.(un)comment module functions. They return False in that case, too. A discussion about whether the pattern not being present should be considered an error seems way out of scope of this particular issue — I'd recommend opening a new one instead.

@twangboy
Copy link
Contributor

twangboy commented Mar 6, 2019

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior P2 Priority 2 severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around State-Module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants