Method: Lono::Configset::Combiner#combine

Defined in:
lib/lono/configset/combiner.rb

#combineObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lono/configset/combiner.rb', line 66

def combine
  # Remove duplicate configsets. Can happen if same configset is in blueprint and project.
  # Ugly because of the sets structure.
  @sets.uniq! do |array|
    registry, _ = array
    registry.name
  end

   = {}
  configsets_map = {}

  @sets.each_with_index do |array, i|
    padded_i = "%03d" % i
    registry,  = array

    # metadata example (full structure):
    #
    #     {"Metadata"=>
    #       {"AWS::CloudFormation::Init"=>
    #         {"configSets"=>{"default"=>["aaa1", "aaa2"]},
    #         "aaa1"=>{"commands"=>{"test"=>{"command"=>"echo from-aaa1 > test1.txt"}}},
    #         "aaa2"=>
    #           {"commands"=>{"test"=>{"command"=>"echo from-aaa2 > test1.txt"}}}}}}

    name, resource = registry.name, registry.resource
    configsets = configsets_map[resource] ||= {}

    validate_structure!(name, )

     = ["Metadata"].dup
    init = ["AWS::CloudFormation::Init"] # important: adjust data by reference

    if init.key?("configSets")
      validate_simple!(registry, ["AWS::CloudFormation::Init"]["configSets"]) # validate original configset for only simple elements

      # 1. expand each config as its own config, flattening to top-level
      cs = init.delete("configSets") # Only support configSets with simple Array of Strings
      new_config_set = {}
      new_config_set[name] = cs["default"].map {|c| "#{padded_i}_#{c}" }
      init.transform_keys! { |c| "#{padded_i}_#{c}" }

      # Rebuild default configSet, append the new complex ConfigSet structure with each iteration
      configsets["default"] ||= []
      configsets["default"] << {"ConfigSet" => name}
      configsets.merge!(new_config_set) # add each config from #1 to the top-level

      init["configSets"] = configsets # replace new configset
    else # simple config
      init["configSets"] = configsets # adjust data by reference
      configsets["default"] ||= []
      configsets["default"] << {"ConfigSet" => name}

      # build new config
      config_key = "#{padded_i}_single_generated"
      configsets[name] = [config_key]
      new_config = {config_key => init["config"]}
      # replace old config with new one
      init.delete("config") # delete original simple config
      init.merge!(new_config)
    end

    [resource] ||= {"Metadata" => {}}
    [resource]["Metadata"].deep_merge!()
    @map[resource] = [resource]
  end
  @map
end