Class: LonoCfn::Base

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Util
Defined in:
lib/lono-cfn/base.rb

Direct Known Subclasses

Create, Diff, Preview, Update

Instance Method Summary collapse

Methods included from Util

#are_you_sure?

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, options={})
  @stack_name = stack_name
  @options = options
  @project_root = options[:project_root] || '.'

  template_name = options[:template] || @stack_name
  params_name = options[:params] || template_name
  @template_path = get_source_path(template_name, :template)
  @params_path = get_source_path(params_name, :params)
  puts "Using template: #{@template_path}" unless @options[:mute_using]
  puts "Using parameters: #{@params_path}" unless @options[:mute_using]
end

Instance Method Details

#check_filesObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/lono-cfn/base.rb', line 64

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_errorsObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lono-cfn/base.rb', line 51

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



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lono-cfn/base.rb', line 88

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_formatObject

Returns String with value of “yml” or “json”.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lono-cfn/base.rb', line 102

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



127
128
129
130
131
132
133
134
135
# File 'lib/lono-cfn/base.rb', line 127

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_allObject



27
28
29
30
31
# File 'lib/lono-cfn/base.rb', line 27

def generate_all
  generate_templates if @options[:lono]
  check_for_errors
  generate_params(mute: @options[:mute_params])
end

#generate_params(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/lono-cfn/base.rb', line 40

def generate_params(options={})
  generator_options = {
    project_root: @project_root,
    path: @params_path,
    allow_no_file: true
  }.merge(options)
  generator = LonoParams::Generator.new(@stack_name, generator_options)
  generator.generate  # Writes the json file in CamelCase keys format
  generator.params    # Returns Array in underscore keys format
end

#generate_templatesObject



33
34
35
36
37
38
# File 'lib/lono-cfn/base.rb', line 33

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



80
81
82
83
84
85
86
# File 'lib/lono-cfn/base.rb', line 80

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



138
139
140
# File 'lib/lono-cfn/base.rb', line 138

def quit(signal)
  exit signal
end

#runObject



22
23
24
25
# File 'lib/lono-cfn/base.rb', line 22

def run
  params = generate_all
  save_stack(params) # defined in the sub class
end

#stack_status(stack_name) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/lono-cfn/base.rb', line 119

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