Re: regex quirks

Bas van der Vlies <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Hi Xander,

 I knnow that is not the answer to your problem. But I use mostly mustaache templates with json data to generate the file. For root cron file generation. I prefer filesi in /etc/cron.d. 
 In the cf_surfsara_lib this is generated with inline mustach:
  * https://github.com/basvandervlies/cf_surfsara_lib/blob/master/doc/library/files.md#sara_make_cron_filename-data (doc)
  * https://github.com/basvandervlies/cf_surfsara_lib/blob/affc1d6cf9e7f2cb14876b65cef3384bf4d5276a/masterfiles/lib/surfsara/files.cf#L173

Maybe it is useful for you.

> On 28 Jul 2021, at 16:37, Xander Cage <[email protected]> wrote:
> 
> i  also tried to compare the cron line with the file content using strcmp and regline to check if strings are equal or not with unfortunatly was not successfull.
> 
> On Wednesday, July 28, 2021 at 4:29:43 PM UTC+2 Xander Cage wrote:
> 
> hey,
> 
> i'm toying  around in making a bundle for crontab editing (add, modify, remove) and regex is
> beginning to bite me.
> 
> i got an "informational" message that my pattern matching skills are downlevel.
> 
> /var/cfengine/bin/cf-agent -KI -f ./nmon_cron_test.cf
>     info: Promised replacement '0 0 * * * /usr/bin/nmon -F /var/log/nmon/`hostname`_`date +'\%y\%m\%d_\%H\%M'`.nmon -s 300 -c 288 1>/dev/null 2>/dev/null' for pattern '^.*/usr/bin/nmon -F.*$' is not properly convergent while editing '/var/spool/cron/crontabs/root'
>     info: Because the regular expression '^.*/usr/bin/nmon -F.*$' still matches the end-state replacement string '0 0 * * * /usr/bin/nmon -F /var/log/nmon/`hostname`_`date +'\%y\%m\%d_\%H\%M'`.nmon -s 300 -c 288 1>/dev/null 2>/dev/null'
>     info: Promise belongs to bundle 'itsv_modify_cron_line' in file './nmon_cron_test.cf' near line 99
> R: Time: Wed Jul 28 16:19:05 2021 -  Bundle: do_what_i_say - Message: cron line found
> 
> can i ignore this? not sure.
> 
> heres the policy code, maybe someone can look over it....
> 
> body file control
> {
>   inputs => { "$(sys.libdir)/stdlib.cf"
>             };
> }
> 
> bundle agent do_what_i_say
> {
> 
>   vars:
> 
> "cron_line" string => "0 0 * * * /usr/bin/nmon -F /var/log/nmon/`hostname`_`date +'\%y\%m\%d_\%H\%M'`.nmon -s 300 -c 288 1>/dev/null 2>/dev/null";
> 
> "root_cron_file"     string  => "/var/spool/cron/crontabs/root";
> 
> "find_anchor"  string => "^.*/usr/bin/nmon -F.*$";
> 
> "file_content"
>           string => readfile( "$(root_cron_file)" , inf ),
>           if => not( isvariable( $(this.promiser) ) );
> 
>   classes:
> 
>         "cron_line_found"
>           expression => regcmp( $(find_anchor),
>                                 $(file_content));
> 
>         "cron_line_not_found"
>           expression => not( regcmp( $(find_anchor),
>                                 $(file_content)));
> 
>   files:
> 
>         "$(root_cron_file)"
>             comment   => "Promise root cron table entries",
>             create    => "true",
>             classes => results( "bundle", "itsv_modify_cron_line" ),
>             if      => cron_line_found,
>             perms     => mog("0600","root","cron"),
>             edit_line => itsv_modify_cron_line("$(find_anchor)", $(cron_line));
> 
>         "$(root_cron_file)"
>             comment   => "Promise root cron table entries",
>             create    => "true",
>             classes => results( "bundle", "itsv_add_cron_line" ),
>             if => cron_line_not_found,
>             perms     => mog("0600","root","cron"),
>             edit_line => itsv_add_cron_line($(cron_line));
> 
>   commands:
> 
>     itsv_modify_cron_line_repaired|itsv_add_cron_line_repaired::
> 
>         "/usr/bin/crontab"
>             args      => "$(root_cron_file)",
>             comment   => "Reread cron tables if it was edited.";
> 
>   reports:
> 
> 
>     cron_line_found::
> 
>           "Time: $(sys.date) -  Bundle: $(this.bundle) - Message: cron line found";
> 
>     cron_line_not_found::
> 
>            "Time: $(sys.date) -  Bundle: $(this.bundle) - Message: cron line not found";
> 
> 
> 
>     itsv_modify_cron_line_repaired|itsv_add_cron_line_repaired::
> 
>       "Time: $(sys.date) -  Bundle: $(this.bundle) - Message: reloaded cron with file $(root_cron_file)";
> 
> }
> 
> 
> bundle edit_line itsv_add_cron_line ( line )
> # @brief append lines if they don't exist
> # @param line The regular expression that the lines need to match
> {
> 
>   insert_lines:
> 
>    "$(line)"
>      comment => "Append lines if they don't exist";
> 
> }
> 
> 
> bundle edit_line itsv_modify_cron_line ( _find, _replace )
> # @brief modify lines matching a regular expression eg. "^.*/usr/bin/nmon -F.*$"
> # @param _find The regular expression that the lines need to match
> # @param _replace String to substitute `_find` with
> {
> 
>   replace_patterns:
> 
>    "$(_find)"
>      replace_with => value("$(_replace)");
> 
> }
> 
> bundle edit_line itsv_delete_cron_line ( regex )
> # @brief delete lines matching a regular expression eg. "^.*/usr/bin/nmon -F.*$"
> # @param regex The regular expression that the lines need to match
> {
> 
>   delete_lines:
> 
>    "$(regex)"
>      comment => "Delete lines matching the marker";
> 
> }
> 
> 
> bundle agent __main__
> {
>   methods: "do_what_i_say";
> }
> 
> wbr
> 
> chris
> 
> 
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups "help-cfengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/4eb43471-b704-4fbb-8066-a8ee2dfb40bdn%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/CB6046B6-4EA6-4BCF-AB8C-41A42BC2AF67%40surf.nl.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.