Re: Re: How to make global variable with summary value

"'Nick Anderson' via help-cfengine" <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
      Hello all, I know that is a very bad idea for cfengine but I
      want counting modifications by cfengine. I prepared small
      testing example:

I think your right this is a bad idea, but still can be a fun exercise
to read the policy....

,----
| bundle common success_add(value){
|   vars:
|       "sum"
|         string => eval("${sum} + ${value}", "math", "infix"),
|         if => isvariable("sum");
| 
|       "sum"
|         string => "${value}",
|         unless => isvariable("sum");
| 
|   reports:
|       "success: add=${value}, total=${sum}";
| }
| 
| bundle agent summary {
|   reports:
|       "SUMMARY: ${success_add.sum}";
| }
| 
| bundle agent __main__ {
|   methods:
|       "add_100" usebundle => success_add("100");
|       "add_010" usebundle => success_add("10");
|       "add_001" usebundle => success_add("1");
|       "summary";
| }
`----
Listing 1: Example Policy

      I expect SUMMARY=111 but i get very different result:

,----
| R: success: add=100, total=400.000000
| R: success: add=100, total=500.000000
| R: success: add=100, total=600.000000
| R: success: add=10, total=630.000000
| R: success: add=10, total=640.000000
| R: success: add=10, total=650.000000
| R: success: add=1, total=653.000000
| R: success: add=1, total=654.000000
| R: success: add=1, total=655.000000
| R: SUMMARY: 655.000000
`----

      OK, cfengine runs every bundle 3 times.

CFEngine will run each bundle in the `bundlesequence' and will take 3
passes in that bundle. But, a methods promise is only going to be called
once, that is to say `success_add(100)' isn't going to be run 3 times.

      Does there exist any solution to get the total value 111?

Seems with CFEngine, there is /almost/ always *a* way, here is one:

,----
| bundle common success_add(value, label){
|   vars:
|       "$(label)"
|         string => "$(value)",
|         meta => { "sum" };
| 
|       "_sum_vars" data => variablesmatching_as_data( "$(this.namespace):$(this.bundle)\..*", "sum" );
|       "sum" real => sum( getvalues( _sum_vars ) );
| 
|   reports:
|       "success: add=${value}, total=${sum}";
| 
| }
| 
| bundle agent summary {
|   reports:
|       "SUMMARY: ${success_add.sum}";
| }
| 
| bundle agent __main__ {
|   methods:
|       "add_100"
|         usebundle => success_add("100", "$(this.promiser)" );
|       "add_010"
|         usebundle => success_add("10", "$(this.promiser)");
|       "add_001"
|         usebundle => success_add("1", "$(this.promiser)");
|       "summary";
| }
`----
Listing 2: a way to get 111

,----
| R: success: add=100, total=100.000000
| R: success: add=10, total=110.000000
| R: success: add=1, total=111.000000
| R: SUMMARY: 111.000000
`----


      Why is the result 655 and not 666?

I don't want to mentally step through evaluation to count this up but
this is most likely related to pre-evaluation and multiple passes within
a given bundle.

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/878ry2sgvw.fsf%40northern.tech.

-- 
Nick Anderson | Doer of Things | (+1) 785-550-1767 | https://northern.tech

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/878ry2sgvw.fsf%40northern.tech.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.