Seva Gluschenko <seva.glusche...@gmail.com> writes:

> Dan,
>
> the useful policy to keep in mind all the time while creating either
> cfengine promises or any other piece of code is KISS (keep it safe and
> simple).
>
> I've proposed a generic edit_line bundle for /etc/fstab adjustment
> recently which can be easily adapted to handle almost any space
> separated file:

Interestingly, the following always work as expected, without
circumlocutions (maybe a simplification of your proposal?):

Remove everything that do not match, insert the good line.

#v+
m...@home: ~/$ rm -f /tmp/main.cf
removed `/tmp/main.cf'

m...@home: ~/$ /usr/sbin/cf-agent -K
R: I add the 'smtp_host smtp.example.net' line

m...@home: ~/$ /usr/sbin/cf-agent -K

m...@home: ~/$ perl -pi -e 's/\.net/.org/' /tmp/main.cf

m...@home: ~/$ /usr/sbin/cf-agent -K
R: I delete some missconfigured smtp_host line
R: I add the 'smtp_host smtp.example.net' line
#v-

Delete everything that do match 'line with the wrong smtp server', this
works as expected (second cf-agent repairs nothing).

Add 'line with the good smtp server', this is repaired only if the line
is not already present.

#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:

        SomeSMTPHostDeleted::
            "I delete some missconfigured smtp_host line";

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

}

bundle edit_line fix_smtp_host(smtp)
{
    delete_lines:
        any::
            "^(smtp_host)\s+(?!$(smtp)).*",
                classes => if_repaired("SomeSMTPHostDeleted"),
                comment => "Delete missconfigured smtp_host";

    insert_lines:
        any::
            "smtp_host $(smtp)",
                classes => if_repaired("SMTPHostAdded"),
                comment => "Add smtp_host if none";
}

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: pgp3cKsJ2r0o0.pgp
Description: PGP signature

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

Reply via email to