Class: LonoCfn::Base
- Inherits:
-
Object
- Object
- LonoCfn::Base
- Includes:
- AwsServices, Util
- Defined in:
- lib/lono_cfn/base.rb
Instance Method Summary collapse
- #check_files ⇒ Object
- #check_for_errors ⇒ Object
- #convention_path(name, type) ⇒ Object
-
#detect_format ⇒ Object
Returns String with value of “yml” or “json”.
- #exist_unless_updatable(status) ⇒ Object
- #generate_params(options = {}) ⇒ Object
- #generate_templates ⇒ Object
-
#get_source_path(path, type) ⇒ Object
if existing in params path then use that if it doesnt assume it is a full path and check that else fall back to convention, which also eventually gets checked in check_for_errors.
-
#initialize(stack_name, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#quit(signal) ⇒ Object
To allow mocking in specs.
- #run ⇒ Object
-
#stack_status(stack_name) ⇒ Object
All CloudFormation states listed here: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html.
Methods included from Util
Methods included from AwsServices
#cfn, #stack_exists?, #testing_update?
Constructor Details
#initialize(stack_name, options = {}) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/lono_cfn/base.rb', line 9 def initialize(stack_name, ={}) @stack_name = stack_name @options = @project_root = [:project_root] || '.' template_name = [:template] || @stack_name params_name = [:params] || template_name @template_path = get_source_path(template_name, :template) @params_path = get_source_path(params_name, :params) puts "Using template: #{@template_path}" puts "Using parameters: #{@params_path}" end |
Instance Method Details
#check_files ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lono_cfn/base.rb', line 60 def check_files errors, warns = [], [] unless File.exist?(@template_path) errors << "Template file missing: could not find #{@template_path}" end if @options[:params] && !File.exist?(@params_path) warns << "Parameters file missing: could not find #{@params_path}" end [errors, warns] end |
#check_for_errors ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lono_cfn/base.rb', line 47 def check_for_errors errors, warns = check_files unless errors.empty? puts "Please double check the command you ran. There were some errors." puts "ERROR: #{errors.join("\n")}".colorize(:red) exit end unless errors.empty? puts "Please double check the command you ran. There were some warnings." puts "WARN: #{errors.join("\n")}".colorize(:yellow) end end |
#convention_path(name, type) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lono_cfn/base.rb', line 84 def convention_path(name, type) path = case type when :template format = detect_format "#{@project_root}/output/#{name}.#{format}" when :params "#{@project_root}/params/#{name}.txt" else raise "hell: dont come here" end path.sub(/^\.\//, '') end |
#detect_format ⇒ Object
Returns String with value of “yml” or “json”.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lono_cfn/base.rb', line 98 def detect_format formats = Dir.glob("#{@project_root}/output/**/*").map { |path| path }. reject { |s| s =~ %r{/params/} }. # reject output/params folder map { |path| File.extname(path) }. reject { |s| s.empty? }. # reject "" uniq if formats.size > 1 puts "ERROR: Detected multiple formats: #{formats.join(", ")}".colorize(:red) puts "All the output files must use the same format. Either all json or all yml." exit 1 else formats.first.sub(/^\./,'') end end |
#exist_unless_updatable(status) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/lono_cfn/base.rb', line 123 def exist_unless_updatable(status) return true if testing_update? return false if @options[:noop] unless status =~ /_COMPLETE$/ puts "Cannot create a change set for the stack because the #{@stack_name} is not in an updatable state. Stack status: #{status}" quit(1) end end |
#generate_params(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lono_cfn/base.rb', line 36 def generate_params(={}) = { project_root: @project_root, path: @params_path, allow_no_file: true }.merge() generator = LonoParams::Generator.new(@stack_name, ) generator.generate # Writes the json file in CamelCase keys format generator.params # Returns Array in underscore keys format end |
#generate_templates ⇒ Object
29 30 31 32 33 34 |
# File 'lib/lono_cfn/base.rb', line 29 def generate_templates Lono::DSL.new( project_root: @project_root, pretty: true ).run end |
#get_source_path(path, type) ⇒ Object
if existing in params path then use that if it doesnt assume it is a full path and check that else fall back to convention, which also eventually gets checked in check_for_errors
Type - :params or :template
76 77 78 79 80 81 82 |
# File 'lib/lono_cfn/base.rb', line 76 def get_source_path(path, type) default_convention_path = convention_path(@stack_name, type) return default_convention_path if path.nil? # convention path based on the input from the user convention_path(path, type) end |
#quit(signal) ⇒ Object
To allow mocking in specs
134 135 136 |
# File 'lib/lono_cfn/base.rb', line 134 def quit(signal) exit signal end |
#run ⇒ Object
22 23 24 25 26 27 |
# File 'lib/lono_cfn/base.rb', line 22 def run generate_templates if @options[:lono] check_for_errors params = generate_params save_stack(params) # defined in the sub class end |
#stack_status(stack_name) ⇒ Object
All CloudFormation states listed here: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html
115 116 117 118 119 120 121 |
# File 'lib/lono_cfn/base.rb', line 115 def stack_status(stack_name) return true if testing_update? return false if @options[:noop] resp = cfn.describe_stacks(stack_name: stack_name) status = resp.stacks[0].stack_status end |