no-re...@cfengine.com writes:

> I don't think that PCRE exactly matches Perl regular expressions so
> beware when testing with Perl.  I tend to use pcregrep to test
> matches.  However, that is not exactly like Cfengine's use of PCRE
> since CF tends to need extra escapes here and there.  So I also test
> using the regex tool at cfengine.com.

Here is my pcretest session:
#v+
pcretest 
PCRE version 8.02 2010-03-19

  re> /^(smtp_host)\s+(?!smtp.example.net$).*/
data> smtp_host smtp.example.net
No match
data> smtp_host smtp.example.org
 0: smtp_host smtp.example.org
 1: smtp_host
#v-

Testing the different regexp on the pcre regexp tester gives me the
following (note that '\s+' in input is changed to 's+' in output):

- in the case the line must be replaced:

#v+
Summary:

Regular expression: '^(smtp_host)s+(?!smtp.example.net$).*'
Line to be matched: 'smtp_host smtp.example.org'

Compilation status:

Success: Regular expression OK.

Editfiles:

Error: Undefined error

Processes:

Error: Undefined error
#v-

- in the case the line must not be replaced:

#v+
Summary:

Regular expression: '^(smtp_host)s+(?!smtp.example.net$).*'
Line to be matched: 'smtp_host smtp.example.net'

Compilation status:

Success: Regular expression OK.

Editfiles:

Error: Undefined error

Processes:

Error: Undefined error
#v-

> I had a client who was using the if_repaired or if_ok bodies from the
> standard library.  I don't know if the error was in the library or he
> introduced the error during his learning process but there was a logic
> error in one of the bodies that caused unexpected results.  You might
> double check yours.

The replace_patterns is always repaired:

#v+
body common control
{

    bundlesequence => { "filetest" };
    version => "0.0.2";
}

bundle agent filetest
{

    vars:

        "smtp" string => "smtp.example.net";

    files:

        "/tmp/main.cf"
            create => "true",
            edit_line => fix_smtp_host("$(smtp)"),
            comment => "smpt_host must be $(smtp)";


    reports:

        SMTPHostFixed::
            "I add the 'smtp_host $(smtp)' line";

        SomeSMTPHostReplaced::
            "I replace some missconfigured smtp_host line";
}

bundle edit_line fix_smtp_host(smtp)
{
    classes:

        "HasSMTPhost" expression => regline("^smtp_host.*", "$(edit.filename)");

    insert_lines:

        !HasSMTPhost::
            "smtp_host $(smtp)",
                classes => if_repaired("SMTPHostFixed"),
                comment => "Add smtp_host if none";

    replace_patterns:

        HasSMTPhost::
            "^(smtp_host)\s+(?!$(smtp)$).*" replace_with => value("$(match.1) 
$(smtp)"),
                classes => if_repaired("SomeSMTPHostReplaced"),
                comment => "Replace missconfigured smtp_host";

}

body replace_with value(v)
{
    replace_value => "$(v)";
    occurrences => "all";
}

body classes if_repaired(x)
{
    promise_repaired => { "$(x)" };
}
#v-

Regards.
--
Daniel Dehennin
Récupérer ma clef GPG:
gpg --keyserver pgp.mit.edu --recv-keys 0x6A2540D1

Attachment: pgpLx5hM45JLI.pgp
Description: PGP signature

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to