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

> 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:
>
> bundle edit_line config_option_space_separated(name,value)
> {
>  classes:
>          "not_there" not => regline("$(name)\s+$(value)(\s+.*)$",
> "$(edit.filename)");
>
>      not_there::
>          "wrong_value" expression => regline("$(name)\s+.*",
> "$(edit.filename)");
>
>  delete_lines:
>      wrong_value::
>          "$(name)\s+" comment => "remove any incorrect values";
>
>  insert_lines:
>      not_there::
>           "$(name) $(value)";
> }
>
> Now you can call it like
>
>  files:
>     "/tmp/main.cf"
>                 edit_line =>
> config_option_space_separated("smtp_host", "smtp.example.net");

I test it and found a strange behaviour:

#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:~$ cat /tmp/main.cf
smtp_host smtp.example.org

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

m...@home:~$ cat /tmp/main.cf
smtp_host smtp.example.org
smtp_host smtp.example.net
#v-

With the following (a cut and past of your bundle)

#v+
body common control
{

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

bundle agent filetest
{

    vars:

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

    files:

        "/tmp/main.cf"
            create => "true",
            edit_line => config_option_space_separated("smtp_host", 
"smtp.example.net"),
            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 config_option_space_separated(name,value)
{
 classes:
         "not_there" not => regline("$(name)\s+$(value)(\s+.*)$",
"$(edit.filename)");

     not_there::
         "wrong_value" expression => regline("$(name)\s+.*",
"$(edit.filename)");

 delete_lines:
     wrong_value::
         "$(name)\s+" comment => "remove any incorrect values",
                      classes => if_repaired("SomeSMTPHostDeleted");

 insert_lines:
     not_there::
          "$(name) $(value)",
              classes => if_repaired("SMTPHostAdded");
}

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

I fix your body like this:

#v+
bundle edit_line config_option_space_separated(name,value)
{
 classes:
         "not_there" not => regline("^\s*$(name)\s+$(value)\s*$",
"$(edit.filename)");

     not_there::
         "wrong_value" expression => regline("^\s*$(name)\s+.*",
"$(edit.filename)");

 delete_lines:
     wrong_value::
         "^$(name)\s+.*" comment => "remove any incorrect values",
                      classes => if_repaired("SomeSMTPHostDeleted");

 insert_lines:
     not_there::
          "$(name) $(value)",
              classes => if_repaired("SMTPHostAdded");

 reports:

     not_there::
         "Not there";

     wrong_value::
        "wrong value";
}
#v-

The only problem I found during my test is that, after the first test, I
had two lines in my /tmp/main.cf, a good one and a bad one.

The 'config_option_space_separated' body do not remove any
misconfigured lines, as the 'regline' matches one of the lines.

With the following body, it behave as expected, it removes any wrongly
valued lines and add a good one if none:

#v+
bundle edit_line config_option_space_separated(name,value)
{
 delete_lines:
     any::
         "$(name)\s+(?!$(value)).*" comment => "remove any incorrect values",
                      classes => if_repaired("SomeSMTPHostDeleted");

 insert_lines:
     any::
          "$(name) $(value)",
              classes => if_repaired("SMTPHostAdded");
}
#v-

Regards.

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

Attachment: pgpH8K4FvEXIk.pgp
Description: PGP signature

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

Reply via email to