Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

duplicates.rb 2.14 KB
Newer Older
1 2 3 4
# replace default deep_merge before calling load_yaml_file_hierarchy
class ::Hash
  def deep_merge(other_hash)
    # merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
5 6 7 8

    merger = proc {|key, v1, v2|
      if Hash === v1 && Hash === v2
        v1.merge(v2, &merger)
9 10 11 12 13 14
      else
        v1.gsub!('!duplicated: ') if String === v1
        v2.gsub!('!duplicated: ') if String === v2
        if Hash === v1 || Hash === v2
          # for example, this occurs if there are some empty entries on yaml files that need to be deleted (ex: parasilo-1 => {})
          # return a hash because it is needed by expand_square_brackets
15
          {'v1' => v1, 'v2' => v2, 'error' => "!duplicated: #{key}"}
16 17 18
        else
          # remove the following 'if' if you want all the duplicated keys
          #if v1 != v2
19
          "!duplicated: '#{v1}' '#{v2}'"
20 21 22 23 24 25
          #else
          #  v2
          #end
        end
      end
    }
26

27 28 29 30
    self.merge(other_hash, &merger)
  end
end

31 32
def yaml_input_find_duplicates(options)
  refapi_hash = load_yaml_file_hierarchy
33 34 35 36 37 38 39 40 41 42

  refapi_hash["sites"].sort.each do |site_uid, site|
    if options.key?(:sites) && !options[:sites].include?(site_uid)
      refapi_hash["sites"].delete(site_uid)
    end

    site["clusters"].sort.each do |cluster_uid, cluster|
      if options.key?(:clusters) && !options[:clusters].include?(cluster_uid)
          site["clusters"].delete(cluster_uid)
      end
43 44 45 46 47
      cluster["nodes"].sort.each do |node_uid, node|
          if node.key?("status") && node["status"] == "retired"
            cluster["nodes"].delete(node_uid)
        end
      end
48 49
    end
  end
50 51

# remove entries that are not duplicate
52 53 54 55
  refapi_hash.deep_reject! {|k, v| !(
  (v.is_a?(Hash) && !v.empty?) ||
      v.is_a?(String) && v.start_with?('!duplicated:')
  )}
56 57

# remove ip, mac and mounted properties (as it can be use to bootstrap the installation of a cluster)
58 59 60 61 62
  refapi_hash.deep_reject! {|k, v| k == 'ip' || k == 'mac' || k == 'mounted'}
  refapi_hash.deep_reject! {|k, v| v == {}}

  if refapi_hash.empty?
    puts "OK: no duplicate entries."
63
    return true
64 65
  else
    puts refapi_hash.to_yaml
66
    return false
67 68
  end
end