Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3010

Mustache promise changes do not propagate when used as part of another bundle

    XMLWordPrintable

    Details

      Description

      Given a Mustache template that is being rendered via a files promise, the result is not propagated correctly in case where the files promise is part of an agent bundle called via a methods promise. E.g.:

      bundle agent fs_template(path, source)
      {
      files:
              "$(path)" create => "true",
                        edit_defaults => no_backup,
                        edit_template => "$(source)",
                        template_method => "mustache",
                        classes => if_repaired("template_repaired");
      
      reports:
          template_repaired::
              "Template repaired";
      }
      
      bundle agent test
      {
      methods:
          "any" usebundle => fs_template("/target", "/source"),
              classes => if_repaired("methods_repaired");
      
      reports:
          methods_repaired::
              "Template repaired via method";
      }
      

      If the promise is being repaired, then it will report "Template repaired", but not "Template repaired via method".

      Judging from the code, the culprit here is `FinishEditContext` in "cf-agent/files_edit.c". It tries to deduce whether a given change was a real change or not by taking a look at the edit context. As `RenderTemplateMustache` will never increment `ec->num_edits`, we wrongly end up in the last `else if (ec)` branch and report that no edit change has been performed.

      Simply fixing this by incrementing `num_edits` doesn't work, though, as in this case cf-agent will try to re-safe the edits via `SaveItemListAsFile`, which results in an empty target file. The following patch fixes both the empty target file as well as the missing class propagation, but it's at least only one part of solving the problem, if not outright wrong:

      diff --git a/cf-agent/files_edit.c b/cf-agent/files_edit.c
      index dcee9fde2..c98d8deaa 100644
      --- a/cf-agent/files_edit.c
      +++ b/cf-agent/files_edit.c
      @@ -129,6 +129,10 @@ void FinishEditContext(EvalContext *ctx, EditContext *ec, Attributes a, const Pr
                       cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a,
                            "No edit changes to file '%s' need saving", ec->filename);
                   }
      +            else if (a.edit_template || a.edit_template_string)
      +            {
      +                *result = PromiseResultUpdate(*result, PROMISE_RESULT_CHANGE);
      +            }
                   else if (SaveItemListAsFile(ec->file_start, ec->filename, a, ec->new_line_mode))
                   {
                       cfPS(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_CHANGE, pp, a,
      diff --git a/cf-agent/verify_files.c b/cf-agent/verify_files.c
      index 9c29288a9..fdda4245c 100644
      --- a/cf-agent/verify_files.c
      +++ b/cf-agent/verify_files.c
      @@ -646,6 +646,10 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx, const Promise *pp,
                           cfPS(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_CHANGE, pp, a, "Updated rendering of '%s' from mustache template '%s'",
                                pp->promiser, message);
                           result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
      +                    if (edcontext->num_edits == 0)
      +                    {
      +                        edcontext->num_edits++;
      +                    }
                       }
                       else
                       {
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                vpodzime Vratislav Podzimek
                Reporter:
                pks Patrick Steinhardt
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: