Class: Lono::Configset::Combiner

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/configset/combiner.rb

Instance Method Summary collapse

Constructor Details

#initialize(cfn, options = {}) ⇒ Combiner

Returns a new instance of Combiner.



5
6
7
8
9
10
# File 'lib/lono/configset/combiner.rb', line 5

def initialize(cfn, options={})
  @cfn, @options = cfn, options

  @sets = []
  @map = {} # stores resource logical id => metadata cfn-init
end

Instance Method Details

#add(registry, metadata) ⇒ Object



34
35
36
# File 'lib/lono/configset/combiner.rb', line 34

def add(registry, )
  @sets << [registry, .dup]
end

#additional_configsets?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/lono/configset/combiner.rb', line 38

def additional_configsets?
  !Register::Blueprint.configsets.empty? || !Register::Project.configsets.empty?
end

#combineObject



59
60
61
62
63
64
65
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
# File 'lib/lono/configset/combiner.rb', line 59

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

   = {}

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

    [resource] ||= {"AWS::CloudFormation::Init" => {"configSets" => {}}}
    configSets = [resource]["AWS::CloudFormation::Init"]["configSets"]

    configSets["default"] ||= []
    configSets["default"] << {"ConfigSet" => name}

    validate_structure!(name, )
    init = ["AWS::CloudFormation::Init"]

    if init.key?("configSets")
      cs = init.delete("configSets") # Only support configSets with simple Array of Strings
      validate_simple!(registry, cs)
      configSets[name] = cs["default"].map {|c| "#{padded_i}_#{c}" }
      init.transform_keys! { |c| "#{padded_i}_#{c}" }
    else # simple config
      config_key = "#{padded_i}_single_generated"
      configSets[name] = [config_key]
      init = {config_key => init["config"]}
    end

    [resource]["AWS::CloudFormation::Init"].merge!(init)
    @map[resource] = [resource]
  end
  @map
end

#existing_configsetsObject

Normalized/convert cfn template to mimic the registry format



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lono/configset/combiner.rb', line 43

def existing_configsets
  configsets = []
  @cfn["Resources"].each do |logical_id, attributes|
    init = attributes.dig("Metadata", "AWS::CloudFormation::Init")

    next unless init

    data = {
      registry: Lono::Configset::Registry.new(["#{logical_id}OriginalConfigset"], resource: logical_id),
      metdata_configset: attributes["Metadata"]
    }
    configsets << data
  end
  configsets
end

#metadata_mapObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lono/configset/combiner.rb', line 12

def 
  return {} unless additional_configsets?

  existing_configsets.each do |data|
    add(data[:registry], data[:metdata_configset])
  end

  Register::Blueprint.configsets.each do |registry|
    loader = Lono::Blueprint::Configset::Loader.new(registry, @options)
    add(registry, loader.metdata_configset)
  end
  Register::Project.configsets.each do |registry|
    loader = Loader.new(registry, @options)
    add(registry, loader.metdata_configset)
  end

  combine
  Register::Blueprint.clear! # in case of lono generate for all templates
  Register::Project.clear! # in case of lono generate for all templates
  @map
end

#validate_simple!(registry, cs) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lono/configset/combiner.rb', line 108

def validate_simple!(registry, cs)
  has_complex_type = cs["default"].detect { |s| !s.is_a?(String) }
  if has_complex_type
    message =<<~EOL
      ERROR: The configset #{registry.name} has a configSets property with a complex type.
      configSets:

          #{cs}

      lono configsets only supports combining configSets with an Array of Strings.
    EOL
    if ENV['LONO_TEST']
      raise message
    else
      puts message.color(:red)
      exit 1
    end
  end
end

#validate_structure!(name, metadata) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/lono/configset/combiner.rb', line 100

def validate_structure!(name, )
  return if .is_a?(Hash) && .key?("AWS::CloudFormation::Init")

  puts "ERROR: The #{name} configset does not appear to have a AWS::CloudFormation::Init key".color(:red)
  puts "Please double check the #{name} configset.yml structure"
  exit 1
end