https://docs.cfengine.com/docs/3.5/manuals-language-concepts-classes.html

17 views
Skip to first unread message

Christian Linden

unread,
Sep 12, 2016, 6:55:10 AM9/12/16
to help-cfengine
Hi,

how can I test in bundle local_two on the "one" class in `bundle local_one, pls?

Thanks,
Chris

Nick Anderson

unread,
Sep 12, 2016, 4:56:08 PM9/12/16
to Christian Linden, help-cfengine
On 09/12/2016 05:55 AM, Christian Linden wrote:
> Hi,
>
> how can I test in bundle local_two on the "one" class in `bundle
> local_one, pls?

I am not certain that I understand what your asking. I think you are
probably referring to this policy based on the URL in the subject.

,----
| body common control
| {
| bundlesequence => { "global","local_one", "local_two" };
| }
|
| #################################
|
| bundle common global
| {
| classes:
| # The soft class "zero" is always satisfied,
| # and is global in scope
| "zero" expression => "any";
| }
|
| #################################
|
| bundle agent local_one
| {
| classes:
| # The soft class "one" is always satisfied,
| # and is local in scope to local_one
| "one" expression => "any";
| }
|
| #################################
|
| bundle agent local_two
| {
| classes:
| # The soft class "two" is always satisfied,
| # and is local in scope to ls_2
| "two" expression => "any";
|
| reports:
| zero.!one.two::
| # This report will be generated
| "Success";
| }
`----

And I think you might be asking how to check if the class `one'
defined in `bundle agent local_one' can be seen from `bundle agent
local_two'. If so, note that `Success' is only emitted if the class
`zero' is defined, the class `one' is not defined, and the class `two'
is defined.

You could add some additional reports:

,----
| bundle agent local_two
| {
| classes:
| # The soft class "two" is always satisfied,
| # and is local in scope to ls_2
| "two" expression => "any";
|
| reports:
| zero.!one.two::
| # This report will be generated
| "Success";
|
| !one::
| "The class =one= is NOT defined (or not available based on
its scope).";
| }
`----

If you are wondering how you cold make the class `one' that is defined
in `bundle agent local_one' available for use in `bundle agent
local_two' you can do so by adjusting the scope of the class when it
is defined.

For example you can explicitly set the scope:

,----
| bundle agent local_one
| {
| classes:
| # The soft class "one" is always satisfied,
| # and is global in scope
| "one" expression => "any", scope => "namespace";
| }
`----

Classes defined as the result of a classes type promise in agent
bundles are by default bundle scoped. Classes defined as the result of
a classes type promise in common bundles are by default namespace
scoped.

Hope this helps.



signature.asc

Christian Linden

unread,
Sep 13, 2016, 6:42:38 AM9/13/16
to help-cfengine, lindo...@gmail.com
Yes, Nick, this is exactly what I meant.

Using:
   one::
in agent bundle local_two works, great, this is what I want.

But pls look at my mount promise, I use the same construction, on my system the class sdb_found is set;
the report is printed if uncommented in the detect_disks bundle but it's not seen from the part_format_disk bundle.

bundle agent mount_main
{
  methods:
       "any" usebundle => detect_disks, inherit => "true";
       "any" usebundle => part_format_disk, inherit => "true";
       #usebundle => lvm_on_disk;
       #usebundle => mount_disk;
}

bundle agent detect_disks
{
  vars:
    #"alphabet" slist => string_split("b:c:d:e:f:g:h:i:j:k:l:m:n:o:p", ":", "25");
    "alphabet"  string => "b";

  classes:
    "sd$(alphabet)_found" expression => fileexists("/dev/sd$(alphabet)"), scope => "namespace";

#  reports:
#    "sd$(alphabet)_found"::
#      "sd$(alphabet) is found on the system by CFEngine.";
}

bundle agent part_format_disk
{
  commands:
    "sd$(alphabet)_found"::
# o=clear in mem partition table, n=new partition, p=primary p, 1=part.number, w=write p=table, q=done (ohne?)
      "echo -e \"o\nn\np\n1\n\n\nw\" | fdisk /dev/sd$(alphabet)"
        classes => default:results("namespace", "disk_format_sd$(alphabet)");

  reports:
    "sd$(alphabet)_found"::
      "sd$(alphabet) is found on the system by CFEngine AND seen from 2nd bundle.";
    "disk_format_sd$(alphabet)"::
      "sd$(alphabet)1 is created on sd$(alphabet).";
}


There might be a bug if vars are used in the class name?

Thanks!
Chris

Nick Anderson

unread,
Sep 13, 2016, 9:34:30 AM9/13/16
to help-c...@googlegroups.com
On 09/13/2016 05:42 AM, Christian Linden wrote:
> "sd$(alphabet) is found on the system by CFEngine AND seen from
> 2nd bundle.";

Looks like you don't have `alphabet' defined in `part_format_disk'.

,----
| bundle agent main
| {
| methods:
| "any" usebundle => detect_disks, inherit => "true";
| "any" usebundle => part_format_disk, inherit => "true";
| }
|
| bundle agent detect_disks
| {
| vars:
| #"alphabet" slist =>
string_split("b:c:d:e:f:g:h:i:j:k:l:m:n:o:p", ":", "25");
| "alphabet" string => "a";
|
| classes:
| "sd$(alphabet)_found" expression =>
fileexists("/dev/sd$(alphabet)"), scope => "namespace";
|
| reports:
| "sd$(alphabet)_found"::
| "in bundle $(this.bundle): sd$(alphabet) is found on the
system by CFEngine.";
| }
|
| bundle agent part_format_disk
| {
|
| # You were referencing this variable but it wasnt defined
within the bundle.
| # So you either need to define it within the bundle or
reference it with a
| # fully qualified name like $(detect_disks.alphabet).
|
| # You could make a copy:
| vars:
| "alphabet" string => "$(detect_disks.alphabet)";
|
| # commands:
| # "sd$(alphabet)_found"::
| # # o=clear in mem partition table, n=new partition, p=primary p,
1=part.number, w=write p=table, q=done (ohne?)
| # "echo -e \"o\nn\np\n1\n\n\nw\" | fdisk /dev/sd$(alphabet)"
| # classes => default:results("namespace",
"disk_format_sd$(alphabet)");
|
| reports:
| "sd$(alphabet)_found"::
| "in bundle $(this.bundle): sd$(alphabet) is found on the
system by CFEngine AND seen from 2nd bundle.";
| "disk_format_sd$(alphabet)"::
| "in bundle $(this.bundle): sd$(alphabet)1 is created on
sd$(alphabet).";
| }
`----

,----
| R: in bundle detect_disks: sda is found on the system by CFEngine.
| R: in bundle part_format_disk: sda is found on the system by
CFEngine AND seen from 2nd bundle.
`----


[Followup] cfengine/deutsche_telekom.org


signature.asc

Christian Linden

unread,
Sep 13, 2016, 10:09:49 AM9/13/16
to help-cfengine
Oh, man.. the var.. totally forgot about it, thought it's global automatically.. =(

Thanks for solving this for me!

c

Nick Anderson

unread,
Sep 13, 2016, 10:22:45 AM9/13/16
to help-c...@googlegroups.com
On 09/13/2016 09:09 AM, Christian Linden wrote:
> Oh, man.. the var.. totally forgot about it, thought it's global
> automatically.. =(

It is available automatically, but you must use the fully qualified
name. Otherwise imagine all of the variable collisions you would have.

,----
| bundle agent main
| {
| vars:
| "myvar" string => "in main";
|
| methods:
| "one";
| }
|
| bundle agent one
| {
| vars:
| "myvar" string => "in one";
|
| "main_myvar" string => "$(main.myvar)";
|
| reports:
| "CFEngine Version $(sys.cf_version)";
| "myvar in bundle $(this.bundle) = $(myvar)";
| "main_myvar in bundle $(this.bundle) = $(main_myvar)";
| "myvar in bundle main $(main.myvar)";
|
| }
`----

,----
| R: CFEngine Version 3.9.0
| R: myvar in bundle one = in one
| R: main_myvar in bundle one = in main
| R: myvar in bundle main in main
`----
signature.asc

Christian Linden

unread,
Sep 13, 2016, 12:40:12 PM9/13/16
to help-cfengine
I forgot that again.. brain too small.. =(

=P 
Reply all
Reply to author
Forward
0 new messages