Cfengine Goodies

To content | To menu | To search

Tag - edit_line

Entries feed - Comments feed

Monday 19 August 2013

CFEngine 3 :: Exclude local files (type) from edit_line notification

From "$(sys.workdir)/inputs/.*" CoreBase files promise example

files:

  # Warn about rules relating to cfengine 2 in inputs - could conflict

  "$(sys.workdir)/inputs/.*"

       comment     => "Check if there are still promises about cfengine 2 that need removing",
       edit_line   => delete_lines_matching(".*$(cf2bits).*"),
       file_select => OldCf2Files,
       action      => warn_only;

illustrate how to exclude edit_line notification with ISA file_select OldCf2Files body instance :

body file_select OldCf2Files
{
leaf_name => {
             "promises.cf",
             "site.cf",
             "library.cf",
             "failsafe.cf",
             ".*.txt",
             ".*.html",
             ".*~",
             "#.*"
             };

file_result => "!leaf_name";
}

warn_only ISA action body instance with warm action_policy attribute and 60 ifelapsed attribute

Wednesday 15 May 2013

CFEngine 3 :: prefix musician name in csv sheet music

Ensure (first field) musician name definition in a csv sheet music

body common control
{
  any::
    bundlesequence  => { prepare("musician_name") };
    inputs          => { "/var/cfengine/inputs/libraries/cfengine_stdlib.cf" };
}

bundle agent prepare(musician) 
{
  vars:
    "sheet" slist => { "/tmp/sheet" };

  files:
    "$(sheet)"
      create => "true",
      copy_from => no_backup_cp("$(sheet).orig"),
      edit_line => prefix_lines_matching("^(?!$(musician);|(\s*#)).*", "$(musician);");
}

bundle edit_line prefix_lines_matching(regex,prefix)

 # Prefix lines of a file matching a regex

{
replace_patterns:

 "^($(regex))$"

     replace_with => prefix("$(prefix)"),
     comment => "Search and replace string";
}

body replace_with prefix(p)
{
replace_value => "$(p)$(match.1)";
occurrences => "all";
}

Example :

# cat /tmp/sheet.orig
instrument1;file1;k1=v11,k2=v21
instrument2;file1;k1=v11

# cf-agent -I -f ./test.cf
 -> Updated /tmp/sheet from source /tmp/sheet.orig on localhost
 -> Edited file /tmp/sheet

# cat /tmp/sheet
musician_name;instrument1;file1;k1=v11,k2=v21
musician_name;instrument2;file1;k1=v11

# cf-agent -I -f ./test.cf

Friday 1 March 2013

CFEngine 3 :: satellite mail system with postfix

email_send.jpgBased on the work found at http://www.danstraw.com/ Simply install and configure a ghost postfix without dns MX, only with a external email account.

Continue reading...

Tuesday 26 February 2013

CFEngine 3 :: From csv to service_catalog

engine.jpg When you have service catalog datas from #1, we can organize then to feed service with this bundle (first field is the name of the array in accordance with meta service definition constraints):

CFEngine code :

Continue reading...

Monday 25 February 2013

CFEngine 3 :: sys_name_resolution service & convergence #2

Instead of using persistence classes like #1 to avoid dangerous deletion and preserve convergence, it is easier to use inheritance of classes.

CFEngine code:

Continue reading...

Friday 15 February 2013

CFEngine 3 :: convergent set of files in a directory

The important parts are :


1) '.*' anchored regexp in the files promise stops at the first '/' encountered, excluding '.' and '..'
2) ex_list body take @() syntax
3) we maintain the files we want in a convergent way with edit_line, and flush the others at the same FS level.

Further reading @cfengine.com


 bundle agent example(prefix,params)
{
vars:
"dest_dir" string => "/tmp";
"filenames" slist => { "file1", "fileN" };

files:
"$(dest_dir)/.*"
file_select => ex_list(@(filenames)),
delete => tidy;

"$(dest_dir)/$(filenames)"
create => "true",
edit_line => edit_whatever_body("$(prefix)","$(catalog)", "$(whatever_params)"),
edit_defaults => empty;

}

bundle edit_line edit_whatever_body(prefix,catalog,name)
{
insert_lines:
"# File Managed by CFEengine $(sys.cf_version)";
"$($(catalog)[$(name)][type]) $($(catalog)[$(name)][url]) $($(catalog)[$(name)][release]) $($(catalog)[$(name)][components])";
}

CFEngine 3 :: csv concatenation

If your service catalog datas are stored in csv files and depend on architecture contexts, you can use this concat function.

bundle agent function_meta(prefix,params)
{

  vars:

    any::
      # variables
      "$(prefix)confidx"       slist   => getindices("$(prefix)_vars.conf");
      "$(prefix)mainfile"      string  => "$(globals.dir_cache)/main/$(prefix)/config.csv";
      "$(prefix)archfile"      string  => "$(globals.dir_cache)/$(architectures_vars.architecture_name)/$(prefix)/config.csv";
      "$(prefix)concatfile"    string  => "$(globals.dir_cache)/main/$(prefix)/concat.csv";
      # readfile
      "$(prefix)len"           int     => readstringarrayidx("$(prefix)meta","$($(prefix)concatfile)","\s*#[^\n]*",";",99,999999);

    architecture_defined::
      "concatlist"    slist   => { "function_meta.$(prefix)mainfile", "function_meta.$(prefix)archfile" };

    !architecture_defined::
      "concatlist"    slist   => { "function_meta.$(prefix)mainfile" };

  files:

        "$($(prefix)mainfile)"
            create      => "true",
            copy_from => u_dcp("$(globals.resources_location)/main/$(prefix)/config.csv");

        "$($(prefix)archfile)"
            create      => "true",
            copy_from   => u_dcp("$(globals.resources_location)/$(architectures_vars.architecture_name)/$(prefix)/config.csv"),
            ifvarclass  => "architecture_defined";

        "$($(prefix)concatfile)"
            create        => "true",
            edit_defaults => empty,
            edit_line     => concatWith(@(this.concatlist));

  reports:

    verbose::
      "$(prefix)::$(this.bundle) csv file :: $(function_meta.$(prefix)concatfile)";
}

bundle edit_line concatWith(file)
{

  insert_lines:

    "$($(file))"
      insert_type    => "file_preserve_block",
      expand_scalars => "false";
}

Wednesday 13 February 2013

CFEngine 3 :: sys_name_resolution service & convergence

You can avoid dangerous deletion by using persistence classes definition and preserve convergence.

Example with comma-separated strings

Continue reading...

Monday 4 February 2013

CFEngine 3 :: delete non managed values

In etc_host sketch, a (regex) list of ips passed to delete_nonmanaged bundle to delete lines do not match ips or are not comments.

caller :

edit_line   => cfdc_etc_hosts:delete_nonmanaged("@(configure.ip)"),

delete_nonmanaged bundle:

bundle edit_line delete_nonmanaged(ips)
{
  vars:
      "regex" string => join ("|", "ips"),
      comment => "Or the escaped ips together so we can delete all others.";

  delete_lines:
      # This is the negative look ahead, and what would need tweaked
      # if you don't like the behavior. Specifically it's identifying
      # our list of ips followed by a space, as well as # comment or
      # localhost and deleting everything else

      "^(?!(\#|127\.0\.0\.1|\:\:1|$(regex))\s.*).*$"
      comment => "Delete lines do not match our ips or are not comments";
}