Class: Lono::Param::Generator

Inherits:
AbstractBase show all
Defined in:
lib/lono/param/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractBase

#initialize, #reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::AbstractBase

Instance Attribute Details

#base_pathObject (readonly)

set when generate is called



3
4
5
# File 'lib/lono/param/generator.rb', line 3

def base_path
  @base_path
end

#env_pathObject (readonly)

set when generate is called



3
4
5
# File 'lib/lono/param/generator.rb', line 3

def env_path
  @env_path
end

Instance Method Details

#contextObject

Context for ERB rendering. This is where we control what references get passed to the ERB rendering.



40
41
42
# File 'lib/lono/param/generator.rb', line 40

def context
  @context ||= Lono::Template::Context.new(@options)
end

#convert_to_cfn_format(contents, casing = :underscore) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lono/param/generator.rb', line 44

def convert_to_cfn_format(contents, casing=:underscore)
  lines = parse_contents(contents)

  # First use a Hash structure so that overlay env files will override
  # the base param file.
  data = {}
  lines.each do |line|
    key,value = line.strip.split("=").map {|x| x.strip}
    value = remove_surrounding_quotes(value)
    data[key] = value
  end

  # Now build up the aws json format for parameters
  params = []
  data.each do |key,value|
    param = if value == "use_previous_value" || value == "UsePreviousValue"
              {
                "ParameterKey": key,
                "UsePreviousValue": true
              }
            elsif value
              {
                "ParameterKey": key,
                "ParameterValue": value
              }
            end
    if param
      param = param.to_snake_keys if casing == :underscore
      params << param
    end
  end
  params
end

#generateObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lono/param/generator.rb', line 5

def generate
  puts "Generating parameter files for blueprint #{@blueprint.color(:green)}:"

  contents = []
  layering = Lono::Layering.new("params", @options, Lono.env)
  layering.locations.each do |path|
    contents << render_erb(path)
  end
  contents = contents.compact.join("\n") # result

  data = convert_to_cfn_format(contents)
  camel_data = convert_to_cfn_format(contents, :camel)
  json = JSON.pretty_generate(camel_data)
  write_output(json)
  unless @options[:mute]
    short_output_path = output_path.sub("#{Lono.root}/","")
    puts "  #{short_output_path}"
  end

  data
end

#output_pathObject



99
100
101
# File 'lib/lono/param/generator.rb', line 99

def output_path
  "#{Lono.root}/output/#{@blueprint}/params/#{@stack}.json"
end

#parametersObject



27
28
29
# File 'lib/lono/param/generator.rb', line 27

def parameters
  generate
end

#parse_contents(contents) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/lono/param/generator.rb', line 78

def parse_contents(contents)
  lines = contents.split("\n")
  # remove comment at the end of the line
  lines.map! { |l| l.sub(/#.*/,'').strip }
  # filter out commented lines
  lines = lines.reject { |l| l =~ /(^|\s)#/i }
  # filter out empty lines
  lines = lines.reject { |l| l.strip.empty? }
  lines
end

#remove_surrounding_quotes(s) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/lono/param/generator.rb', line 89

def remove_surrounding_quotes(s)
  if s =~ /^"/ && s =~ /"$/
    s.sub(/^["]/, '').gsub(/["]$/,'') # remove surrounding double quotes
  elsif s =~ /^'/ && s =~ /'$/
    s.sub(/^[']/, '').gsub(/[']$/,'') # remove surrounding single quotes
  else
    s
  end
end

#render_erb(path) ⇒ Object



31
32
33
34
35
36
# File 'lib/lono/param/generator.rb', line 31

def render_erb(path)
  return unless path
  if File.exist?(path)
    RenderMePretty.result(path, context: context)
  end
end

#write_output(json) ⇒ Object



103
104
105
106
107
# File 'lib/lono/param/generator.rb', line 103

def write_output(json)
  dir = File.dirname(output_path)
  FileUtils.mkdir_p(dir)
  IO.write(output_path, json)
end