Class: Lono::Param::Generator
- Inherits:
-
Object
- Object
- Lono::Param::Generator
- Defined in:
- lib/lono/param/generator.rb
Class Method Summary collapse
- .generate_all(options) ⇒ Object
-
.param_names(folder) ⇒ Object
Returns param names Example: Given params: params/base/a.txt params/base/b.txt params/base/c.txt Returns: param_names(“base”) => [“a”, “b”, “c”].
Instance Method Summary collapse
-
#context ⇒ Object
Context for ERB rendering.
- #convert_to_cfn_format(contents, casing = :camel) ⇒ Object
- #generate ⇒ Object
-
#initialize(name, options) ⇒ Generator
constructor
A new instance of Generator.
- #output_path ⇒ Object
-
#params(casing = :underscore) ⇒ Object
useful for when calling CloudFormation via the aws-sdk gem.
- #parse_contents(contents) ⇒ Object
-
#process_erb ⇒ Object
Reads both the base source and env source and overlay the two Example 1: params/base/mystack.txt - base path params/prod/mystack.txt - env path.
- #render_erb(path) ⇒ Object
-
#source_exist? ⇒ Boolean
Checks both base and source path for existing of the param file.
- #write_output(json) ⇒ Object
Constructor Details
#initialize(name, options) ⇒ Generator
Returns a new instance of Generator.
25 26 27 28 29 30 |
# File 'lib/lono/param/generator.rb', line 25 def initialize(name, ) @name = "#{Lono.env}/#{name}" @options = @env_path = [:path] || "#{Lono.config.params_path}/#{@name}.txt" @base_path = @env_path.sub("/#{Lono.env}/", "/base/") end |
Class Method Details
.generate_all(options) ⇒ Object
2 3 4 5 6 7 8 9 10 |
# File 'lib/lono/param/generator.rb', line 2 def self.generate_all() puts "Generating parameter files:" params = param_names("base") + param_names(Lono.env) params.uniq.each do |name| param = Lono::Param::Generator.new(name, ) param.generate end end |
.param_names(folder) ⇒ Object
Returns param names Example: Given params:
params/base/a.txt params/base/b.txt params/base/c.txt
Returns:
param_names("base") => ["a", "b", "c"]
18 19 20 21 22 23 |
# File 'lib/lono/param/generator.rb', line 18 def self.param_names(folder) base_folder = "#{Lono.config.params_path}/#{folder}" # Example: "./params/base" Dir.glob("#{base_folder}/**/*.txt").map do |path| path.sub("#{base_folder}/", '').sub('.txt','') end end |
Instance Method Details
#context ⇒ Object
Context for ERB rendering. This is where we control what references get passed to the ERB rendering.
93 94 95 |
# File 'lib/lono/param/generator.rb', line 93 def context @context ||= Lono::Template::Context.new(@options) end |
#convert_to_cfn_format(contents, casing = :camel) ⇒ Object
116 117 118 119 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 |
# File 'lib/lono/param/generator.rb', line 116 def convert_to_cfn_format(contents, casing=:camel) 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} 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" { "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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lono/param/generator.rb', line 32 def generate # useful option for lono cfn return if @options[:allow_no_file] && !source_exist? if source_exist? contents = process_erb data = convert_to_cfn_format(contents) json = JSON.pretty_generate(data) write_output(json) # Example: @name = stag/ecs/private # pretty_name = ecs/private pretty_name = @name.sub("#{Lono.env}/", '') puts " #{output_path}" unless @options[:mute] else puts "#{@base_path} or #{@env_path} could not be found? Are you sure it exist?" exit 1 end json end |
#output_path ⇒ Object
149 150 151 152 |
# File 'lib/lono/param/generator.rb', line 149 def output_path name = @name.sub("#{Lono.env}/", "") # remove the Lono.env from the output path "#{Lono.config.output_path}/params/#{name}.json".sub(/\.\//,'') end |
#params(casing = :underscore) ⇒ Object
useful for when calling CloudFormation via the aws-sdk gem
53 54 55 56 57 58 59 |
# File 'lib/lono/param/generator.rb', line 53 def params(casing = :underscore) # useful option for lono cfn return {} if @options[:allow_no_file] && !source_exist? contents = process_erb convert_to_cfn_format(contents, casing) end |
#parse_contents(contents) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/lono/param/generator.rb', line 105 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 |
#process_erb ⇒ Object
Reads both the base source and env source and overlay the two Example 1:
params/base/mystack.txt - base path
params/prod/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/prod/mystack.txt - env path
the prod/mystack.txt is used to produced a prod/mystack.txt
78 79 80 81 82 83 |
# File 'lib/lono/param/generator.rb', line 78 def process_erb contents = [] contents << render_erb(@base_path) contents << render_erb(@env_path) contents.compact.join("\n") end |
#render_erb(path) ⇒ Object
85 86 87 88 89 |
# File 'lib/lono/param/generator.rb', line 85 def render_erb(path) if File.exist?(path) RenderMePretty.result(path, context: context) end end |
#source_exist? ⇒ Boolean
Checks both base and source path for existing of the param file. Example:
params/base/mystack.txt - base path
params/prod/mystack.txt - source path
101 102 103 |
# File 'lib/lono/param/generator.rb', line 101 def source_exist? File.exist?(@base_path) || File.exist?(@env_path) end |
#write_output(json) ⇒ Object
154 155 156 157 158 |
# File 'lib/lono/param/generator.rb', line 154 def write_output(json) dir = File.dirname(output_path) FileUtils.mkdir_p(dir) IO.write(output_path, json) end |