Class: Lono::Param::Generator
Instance Attribute Summary collapse
Instance Method Summary
collapse
#initialize, #reinitialize, #template_path
#find_blueprint_root, #set_blueprint_root
Instance Attribute Details
#base_path ⇒ Object
set when generate is called
3
4
5
|
# File 'lib/lono/param/generator.rb', line 3
def base_path
@base_path
end
|
#env_path ⇒ Object
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
#config_locations ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/lono/param/generator.rb', line 36
def config_locations
@base_path = lookup_config_location("base")
@env_path = lookup_config_location(Lono.env)
if ENV['LONO_DEBUG_PARAM']
puts "LONO_DEBUG_PARAM enabled"
puts " @base_path #{@base_path.inspect}"
puts " @env_path #{@env_path.inspect}"
end
[@base_path, @env_path]
end
|
#context ⇒ Object
Context for ERB rendering. This is where we control what references get passed to the ERB rendering.
105
106
107
|
# File 'lib/lono/param/generator.rb', line 105
def context
@context ||= Lono::Template::Context.new(@options)
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/lono/param/generator.rb', line 120
def convert_to_cfn_format(contents, casing=:underscore)
lines = parse_contents(contents)
data = {}
lines.each do |line|
key,value = line.strip.split("=").map {|x| x.strip}
value = remove_surrounding_quotes(value)
data[key] = value
end
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
|
#generate ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/lono/param/generator.rb', line 5
def generate
puts "Generating parameter files for blueprint #{@blueprint.color(:green)}:"
@base_path, @env_path = config_locations
return {} unless @base_path || @env_path
return {} if @options[:allow_not_exists] && !params_exist?
if params_exist?
contents = process_erb
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
else
puts "#{@base_path} or #{@env_path} could not be found? Are you sure it exist?"
exit 1
end
data
end
|
#lookup_config_location(env) ⇒ Object
49
50
51
52
|
# File 'lib/lono/param/generator.rb', line 49
def lookup_config_location(env)
location = Lono::ConfigLocation.new("params", @options, env)
env == "base" ? location.lookup_base : location.lookup
end
|
#output_path ⇒ Object
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/lono/param/generator.rb', line 164
def output_path
output = Lono.config.output_path.sub("#{Lono.root}/","")
path = if @base_path && !@env_path
@base_path.sub("configs", output).sub("base", Lono.env)
else
@env_path.sub("configs", output)
end
path.sub(/\.txt$/,'.json')
end
|
#parameters ⇒ Object
32
33
34
|
# File 'lib/lono/param/generator.rb', line 32
def parameters
generate
end
|
#params_exist? ⇒ Boolean
Checks both base and source path for existing of the param file. Example:
params/base/mystack.txt - base path
params/production/mystack.txt - source path
67
68
69
70
|
# File 'lib/lono/param/generator.rb', line 67
def params_exist?
@base_path && File.exist?(@base_path) ||
@env_path && File.exist?(@env_path)
end
|
#parse_contents(contents) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/lono/param/generator.rb', line 109
def parse_contents(contents)
lines = contents.split("\n")
lines.map! { |l| l.sub(/#.*/,'').strip }
lines = lines.reject { |l| l =~ /(^|\s)#/i }
lines = lines.reject { |l| l.strip.empty? }
lines
end
|
#process_erb ⇒ Object
Reads both the base source and env source and overlay the two Example 1:
params/base/mystack.txt - base path
params/production/mystack.txt - env path
the base/mystack.txt gets combined with the prod/mystack.txt
it produces a final prod/mystack.txt
Example 2:
params/base/mystack.txt - base path
the base/mystack.txt is used to produced a prod/mystack.txt
Example 3:
params/production/mystack.txt - env path
the prod/mystack.txt is used to produced a prod/mystack.txt
89
90
91
92
93
94
|
# File 'lib/lono/param/generator.rb', line 89
def process_erb
contents = []
contents << render_erb(@base_path)
contents << render_erb(@env_path)
contents.compact.join("\n") end
|
#puts_param_message(type) ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/lono/param/generator.rb', line 54
def puts_param_message(type)
path = send("#{type}_path")
return unless path
if param_file?(path)
pretty_path = path.sub("#{Lono.root}/",'')
puts "Using param for #{type}: #{pretty_path}".color(:yellow)
end
end
|
#remove_surrounding_quotes(s) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/lono/param/generator.rb', line 154
def remove_surrounding_quotes(s)
if s =~ /^"/ && s =~ /"$/
s.sub(/^["]/, '').gsub(/["]$/,'') elsif s =~ /^'/ && s =~ /'$/
s.sub(/^[']/, '').gsub(/[']$/,'') else
s
end
end
|
#render_erb(path) ⇒ Object
96
97
98
99
100
101
|
# File 'lib/lono/param/generator.rb', line 96
def render_erb(path)
return unless path
if File.exist?(path)
RenderMePretty.result(path, context: context)
end
end
|
#write_output(json) ⇒ Object
175
176
177
178
179
|
# File 'lib/lono/param/generator.rb', line 175
def write_output(json)
dir = File.dirname(output_path)
FileUtils.mkdir_p(dir)
IO.write(output_path, json)
end
|