Cfengine Goodies

To content | To menu | To search

Tag - lastnode

Entries feed - Comments feed

Friday 1 February 2013

CFEngine 3 :: dynamic bundle parameters #2

instead of using string type #1 to define parameters, you also can use list @() as parameters :

body common control
{
      bundlesequence => {
                          "test_vars",
                          test(@(test_vars.params)),
                        };
}

bundle common test_vars
{

  vars:

   # declare
   "types"
     slist  => { "show", "action" };
   "params"
     slist  => maplist("$(this.bundle).$(this)", "types");

   # define
   "$(types)[version]"
     string => "$(types)_0.1";
   "show[other]"
     string => "other_value";
}

bundle agent test(params)
{

  vars:

   "bundle[$(params)]"
     string => lastnode("$(params)", "\.");

  services:

    "function_$(bundle[$(params)])"
      service_policy => "start",
      service_method => bundle_handler("$(params)");
  reports:

    linux::
      "params : $(params)";
}

body service_method bundle_handler(s)
{
    service_bundle            => $(this.promiser)("$(s)");
}
bundle agent function_show(a)
{

  vars:

    "index"
      slist => getindices("$(a)");

  reports:

    cfengine_3::
      "$(this.bundle) :: $(index) = $($(a)[$(index)])";
}

bundle agent function_action(a)
{

  reports:

    cfengine_3::
      "$(this.bundle) :: version = $($(a)[version])";
}

In action :

# cf-agent -f ./test_listdynparam.cf
R: function_show :: other = other_value
R: function_show :: version = show_0.1
R: function_action :: version = action_0.1
R: params : test_vars.show
R: params : test_vars.action

Wednesday 30 January 2013

CFEngine 3 :: dynamic bundle parameters

Edit : use list __@()__ as parameters

body common control
{
      bundlesequence => {
                          "test_vars",
                          test("$(test_vars.params)"),
                        };
}

bundle common test_vars
{

  vars:

   # declare
   "conflist"
     slist  => { "show", "action" };
   "paramlist"
     slist  => maplist("$(this.bundle).$(this)", "conflist");
   "params"
     string => join(",", "paramlist");

   # define
   "$(conflist)[version]"
     string => "$(conflist)_0.1";
   "show[other]"
     string => "other_value";
}

bundle agent test(params)
{

  vars:

   "str"
     string => "$(params)";
   "paramlist"
     slist  => splitstring("$(str)",",", "10");
   "bundle[$(paramlist)]"
     string => lastnode("$(paramlist)", "\.");

  services:

    "function_$(bundle[$(paramlist)])"
      service_policy => "start",
      service_method => bundle_handler("$(paramlist)");
}
body service_method bundle_handler(s)
{
    service_bundle            => $(this.promiser)("$(s)");
}

bundle agent function_show(a)
{

  vars:

    "index"
      slist => getindices("$(a)");

  reports:

    cfengine_3::
      "$(this.bundle) :: $(index) = $($(a)[$(index)])";
}

bundle agent function_action(a)
{

  reports:

    cfengine_3::
      "$(this.bundle) :: version = $($(a)[version])";
}

In this example, the bundle test_vars declare and define test parameters and test bundle use services promise type to call function bundle with the function specific parameters

# cf-agent -f ./test_dynparam.cf
R: function_show :: other = other_value
R: function_show :: version = show_0.1
R: function_action :: version = action_0.1

Tuesday 29 January 2013

CFEngine 3 :: name of array passed as a parameter

body common control
{
      bundlesequence => {
                          "test_vars", test("test_vars.conf"),
                        };
}

bundle common test_vars
{

  vars:

   "conf[version]"
     string => "0.1";
}

bundle agent test(params)
{

  vars:

   "array_name"
     string  => lastnode("$(params)", "\.");
   "$(array_name)[version]"
     string  => "0.2";

  reports:

    Yr2013::
      "In bundle $(this.bundle) array_name is $(this.array_name) and version is $($(array_name)[version])";

}

In this example, the test bundle receives as argument a variable called params, which (must) contains the name test_vars.conf ( <bundle_name>.<array_name>) lastnode("$(params)", "\.") return the name of array.

You can use this name to define a modified test.conf array.

# cf-agent -f ./unit_lastnode.cf
R: In bundle test array_name is conf and version is 0.2