puppetlabs / puppetlabs-node_manager

Create and manage PE node groups as resources.
Apache License 2.0
10 stars 21 forks source link

groups are not idempotent when multiple classes are used #9

Closed WhatsARanjit closed 7 years ago

WhatsARanjit commented 7 years ago

Migrated from: https://github.com/puppetlabs/prosvcs-node_manager/issues/46

Because the classes are expressed in a hash puppet will compare the hash as a whole instead of doing a deep inspection of the items that might have changed in the hash. So each puppet run is no longer idempotent because the order of the hash is always different thus triggering a change.

We will need to inspect the change_to_hash and the current_hash for the classes and args so that the data is inspected and not just the order of contents.

(hash1.to_a - hash2.to_a).empty? -- possible solution ?

The PE puppet master group is a good example case since it comes with many classes by default.

WhatsARanjit commented 7 years ago

The way the provider compares current and desired. Unfortunately, I'm not sure of a way to tell it to compare other than ==. So what I've tried to do is tell the RAL and the DSL to deep-sort the classes Hash, so at least they appear equal. Added this method:

  def self.sort_hash(data)
    newhash = Hash.new
    if data.is_a?(Hash)
      # .to_h method doesn't exist until Ruby 2.1.x
      data.sort.flatten(1).each_slice(2) { |a,b| newhash[a] = b }
    end
    newhash.each do |k,v|
      if v.is_a?(Hash)
        newhash[k] = sort_hash(v)
      else
        newhash[k] = v
      end
    end
    newhash
  end

It looks much more elegant with the to_h method, but that's not available in Ruby 2.0.0. To so favor compatibility with PE 3.8.x, wrote a substitute.

WhatsARanjit commented 7 years ago

Merged https://github.com/WhatsARanjit/prosvcs-node_manager/commit/ee3b4a80b11f119a43d87609ebed32cd772ba4d7