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

Unable to edit a value in a region if the value appeared in another region first

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Done
    • Priority: Medium
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.7.2
    • Labels:
      None

      Description

      Given the following starting data:

      <pre>
      [Foo]
      X=1
      Y=2
      Z=3

      [Bar]
      G=5
      </pre>

      I am unable to set variable values for X, Y or Z in section Bar with the following policy:

      <pre>
      bundle agent test

      { vars: "config[Bar][X]" string=> "1"; "config[Bar][Y]" string=> "10"; "config[Bar][Z]" string=> "3"; files: "$(this.promise_dirname)/foobar.txt" edit_line => set_variable_values_ini("$(this.bundle).config", "Bar"); } </pre> I ended up hacking at set_variable_values in order to improve the verbose logging output and make it more clear what was happening. In the verbose output using my hacked set_variable_values_ini bundle I see that nothing happens when it tries to make sure an existing entry for the key Z is set appropriately (because currently no Z key exists in section Bar). <pre> bundle edit_line hacked_set_variable_values_ini(tab, sectionName) # @brief Sets the RHS of configuration items in the file of the form # `LHS=RHS` # # If the line is commented out with `#`, it gets uncommented first. # Adds a new line if none exists. # # @param tab An associative array containing `tab[sectionName][LHS]="RHS"`. # The value is not changed when the `RHS` is "dontchange" # @param sectionName The section in the file within which values should be # modified # # **See also:** `manage_variable_values_ini()` { vars: "index" slist => getindices("$(tab)[$(sectionName)]"); # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); field_edits: # match a line starting like the key something "$(index)\s*=.*" handle => "field_edit_section_$(sectionName)_existing_entry_key_$(index)_value_$($(tab)[$(sectionName)][$(index)]))", edit_field => col("=","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section("$(sectionName)"), comment => "Ensure existing entry for $(index) in section $(sectionName) is set appropriately", classes => scoped_classes_generic("bundle", "section_$(sectionName)_key_$(index)_value_$($(tab)[$(sectionName)][$(index)]))"); insert_lines: "[$(sectionName)]" handle => "insert_section_header_for_$(sectionName)", location => start, comment => "We must ensure the section we are trying to manage exists"; "$(index)=$($(tab)[$(sectionName)][$(index)])" handle => "insert_section_$(sectionName)_key_$(index)_value_$($(tab)[$(sectionName)][$(index)])", select_region => INI_section("$(sectionName)"), comment => "We must ensure that section $(sectionName) has key $(index) with value $($(tab)[$(sectionName)][$(index)])", ifvarclass => "!section_$(sectionName)_key_$(index)_value_$($(tab)[$(sectionName)][$(index)])_repaired"; }

      </pre>

      I can see as expected that the filed edits make no change as the values for X, Y and Z do not exist in section Bar.

      <pre>
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'field_edit_section_Bar_existing_entry_key_Z_value_3)' of type "field_edits" (pass 1)
      verbose: P: Promiser/affected object: 'Z\s*=.*'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/field_edits/'Z\s:=.:'[0]'
      verbose: P:
      verbose: P: Comment: Ensure existing entry for Z in section Bar is set appropriately
      verbose: P: .........................................................
      verbose:
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'field_edit_section_Bar_existing_entry_key_X_value_1)' of type "field_edits" (pass 1)
      verbose: P: Promiser/affected object: 'X\s*=.*'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/field_edits/'X\s:=.:'[1]'
      verbose: P:
      verbose: P: Comment: Ensure existing entry for X in section Bar is set appropriately
      verbose: P: .........................................................
      verbose:
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'field_edit_section_Bar_existing_entry_key_Y_value_10)' of type "field_edits" (pass 1)
      verbose: P: Promiser/affected object: 'Y\s*=.*'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/field_edits/'Y\s:=.:'[2]'
      verbose: P:
      verbose: P: Comment: Ensure existing entry for Y in section Bar is set appropriately
      verbose: P: .........................................................
      verbose:
      </pre>

      When it reaches the insert_lines section I expected that it would insert the key values for X, Y and Z as they do not exist in section Bar. However it seems only the keyvalue for Y was reported as a promise repaired.

      <pre>
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'insert_section_Bar_key_Z_value_3' of type "insert_lines" (pass 1)
      verbose: P: Promiser/affected object: 'Z=3'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: "if" class condition: !section_Bar_key_Z_value_3_repaired
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/insert_lines/'Z=3'[0]'
      verbose: P:
      verbose: P: Comment: We must ensure that section Bar has key Z with value 3
      verbose: P: .........................................................
      verbose:
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'insert_section_Bar_key_X_value_1' of type "insert_lines" (pass 1)
      verbose: P: Promiser/affected object: 'X=1'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: "if" class condition: !section_Bar_key_X_value_1_repaired
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/insert_lines/'X=1'[1]'
      verbose: P:
      verbose: P: Comment: We must ensure that section Bar has key X with value 1
      verbose: P: .........................................................
      verbose:
      verbose: P: .........................................................
      verbose: P: BEGIN promise 'insert_section_Bar_key_Y_value_10' of type "insert_lines" (pass 1)
      verbose: P: Promiser/affected object: 'Y=10'
      verbose: P: Part of bundle: hacked_set_variable_values_ini
      verbose: P: Base context class: any
      verbose: P: "if" class condition: !section_Bar_key_Y_value_10_repaired
      verbose: P: Container path : '/default/main/methods/'test'/default/test/files/'/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'/default/hacked_set_variable_values_ini/insert_lines/'Y=10'[2]'
      verbose: P:
      verbose: P: Comment: We must ensure that section Bar has key Y with value 10
      verbose: P: .........................................................
      verbose:
      info: Edit file '/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'
      verbose: Handling file existence constraints on '/home/nickanderson/CFEngine/policy/prototypes/set_variable_values_ini/./foobar.txt'
      verbose: A: Promise REPAIRED
      </pre>

      Sure enough, when I inspect the resulting file I find that only the key value for Y is inserted into section Bar.

      <pre>
      [Foo]
      X=1
      Y=2
      Z=3

      [Bar]
      G=5
      Y=10
      </pre>

      In my hacked set_variable_values_ini bundle I made sure to have unique handles and unique classes based on the section, key and value triplicate. Unless I am missing something this seems to be a bug related to inserting values within selected regions.

        Attachments

          Activity

            People

            • Assignee:
              a10053 Marcin Pasinski
              Reporter:
              a10042 Nick Anderson
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Summary Panel

                  Time Tracking

                  Estimated:
                  Original Estimate - 2 days, 5 hours
                  2d 5h
                  Remaining:
                  Not Specified
                  Logged:
                  Time Not Required
                  Not Specified