Re: per-promise 'inform => "false"'?

"'Nick Anderson' via help-cfengine" <[email protected]> Fri, 14 Jul 2023 10:08:44 -0500
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
        For reference here is the problematic part (bundle
        slightly reduced here) where we dump all classes, several
        hundred of them, into a state file:

        ----------------------------------------------------------
        bundle agent dls_reports { vars: "allclasses" slist =>
        classesmatching(".*");

          files: cfengine_3::
            "$(sys.workdir)/state/allclasses.txt" create =>
            "true", perms => mog("0644", "root", "root"),
            edit_defaults => empty, edit_line =>
            insert_lines(@(allclasses)); }
        ----------------------------------------------------------

        Try that in a test framework. Up to and including version
        3.15.5, "cf-agent -I ..." (manually at the console) is
        decently quiet at this point (a single diagnostic line
        telling us the file has been edited). But at 3.18 its
        output swamps us!

  Manual executions with -K and -I are super common, it's definitely
  embedded in my muscle memory.

  First thing that I notice in that policy: since you are managing the
  full file content, I think you should use mustache or the `content'
  attribute instead of `edit_line', any time you are using `edit_default
  => empty' it's worth re-evaluating the choice and considering
  something that was designed for full file management.

  Instead of recording ALL classes you could be more selective and
  perhaps filter out classes that change with each run, like
  `time_based' classes.

  ,----
  | bundle agent __main__
  | {
  |     vars:
  |        "allclasses" slist => classesmatching(".*");
  |        "time_based_classes" slist => classesmatching(".*", "time_based");
  |        "allclasses_excluding_unstable"
  |          with => concat( "(", join( "|", time_based_classes), ")" ),
  |          slist => filter( "$(with)",         # Filter
  |                           "allclasses",      # source list
  |                           "true",            # is regex?
  |                           "true",            # invert result?
  |                           inf );             # max return
  |
  |     reports:
  |       "allclasses has $(with) elements" with => length( "allclasses" );
  |       "allclasses_excluding_unstable has $(with) elements" with => length( "allclasses_excluding_unstable" );
  |
  | }
  `----
  Listing 1: Example Policy

  Another option would be to use `reports' with `report_to_file'
  /instead/ of `files' promises for managing the content of a file, you
  would have to be sure to cull the file first or it's basically a log.

  Of course if you use a `files' promise and run with inform you will
  get noise about it's deletion.

  ,----
  | bundle agent __main__
  | {
  |   vars:
  |       "allclasses" slist => classesmatching(".*");
  |       "time_based_classes" slist => classesmatching(".*", "time_based");
  |
  |       "allclasses_excluding_unstable"
  |         with => concat( "(", join( "|", time_based_classes), ")" ),
  |         slist => sort( filter( "$(with)",         # Filter
  |                                "allclasses",      # source list
  |                                "true",            # is regex?
  |                                "true",            # invert result?
  |                                inf ),             # max return
  |                        "lex" );
  |
  |   files:
  |       "/tmp/allclasses_excluding_unstable.txt"
  |         delete => default:tidy,
  |         handle => "cull_allclasses_statefile";
  |
  |   reports:
  |       "allclasses has $(with) elements" with => length( "allclasses" );
  |       "allclasses_excluding_unstable has $(with) elements" with => length( "allclasses_excluding_unstable" );
  |
  |       "$(allclasses_excluding_unstable)"
  |         report_to_file => "/tmp/allclasses_excluding_unstable.txt",
  |         depends_on => { "cull_allclasses_statefile" };
  | }
  `----

  ,----
  |     info: Deleted file '/tmp/allclasses_excluding_unstable.txt'
  | R: allclasses has 281 elements
  | R: allclasses_excluding_unstable has 258 elements
  `----


  So, you could use `commands' where you can suppress inform.

  ,----
  | bundle agent __main__
  | {
  |   vars:
  |       "allclasses" slist => classesmatching(".*");
  |       "time_based_classes" slist => classesmatching(".*", "time_based");
  |
  |       "allclasses_excluding_unstable"
  |         with => concat( "(", join( "|", time_based_classes), ")" ),
  |         slist => sort( filter( "$(with)",         # Filter
  |                                "allclasses",      # source list
  |                                "true",            # is regex?
  |                                "true",            # invert result?
  |                                inf ),             # max return
  |                        "lex" );
  |
  |   commands:
  |       "/bin/rm /tmp/allclasses_excluding_unstable.txt"
  |         inform => "false",
  |         contain => silent,
  |         handle => "cull_allclasses_statefile",
  |         if => fileexists( "/tmp/allclasses_excluding_unstable.txt" );
  |
  |   reports:
  |       "allclasses has $(with) elements" with => length( "allclasses" );
  |       "allclasses_excluding_unstable has $(with) elements" with => length( "allclasses_excluding_unstable" );
  |
  |       "$(allclasses_excluding_unstable)"
  |         report_to_file => "/tmp/allclasses_excluding_unstable.txt",
  |         depends_on => { "cull_allclasses_statefile" };
  | }
  `----

  ,----
  | R: allclasses has 281 elements
  | R: allclasses_excluding_unstable has 258 elements
  `----


  It would be nice to be able to set the log level per promise and not
  just increase it, I think there is a ticket somewhere but I couldn't
  find in a couple minutes of searching.

  And, finally, instead of managing that state file, you could get
  Enterprise reporting :D.

-- 
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/87351qr0n7.fsf%40northern.tech.