Class: Bora::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/bora/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, template_file, stack_config) ⇒ Stack

Returns a new instance of Stack.



10
11
12
13
14
15
16
17
# File 'lib/bora/stack.rb', line 10

def initialize(stack_name, template_file, stack_config)
  @stack_name = stack_name
  @cfn_stack_name = stack_config['stack_name'] || @stack_name
  @template_file = template_file
  @stack_config = stack_config
  @cfn_options = extract_cfn_options(stack_config)
  @cfn_stack = Cfn::Stack.new(@cfn_stack_name)
end

Instance Attribute Details

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



19
20
21
# File 'lib/bora/stack.rb', line 19

def stack_name
  @stack_name
end

Instance Method Details

#apply(override_params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bora/stack.rb', line 25

def apply(override_params = {})
  generate(override_params)
  success = invoke_action(@cfn_stack.exists? ? "update" : "create", @cfn_options)
  if success
    outputs = @cfn_stack.outputs
    if outputs && outputs.length > 0
      puts "Stack outputs"
      outputs.each { |output| puts output }
    end
  end
end

#deleteObject



37
38
39
# File 'lib/bora/stack.rb', line 37

def delete
  invoke_action("delete")
end

#diff(override_params = {}) ⇒ Object



41
42
43
44
# File 'lib/bora/stack.rb', line 41

def diff(override_params = {})
  generate(override_params)
  puts @cfn_stack.diff(@cfn_options).to_s(String.disable_colorization ? :text : :color)
end

#eventsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bora/stack.rb', line 46

def events
  events = @cfn_stack.events
  if events
    if events.length > 0
      puts "Events for stack '#{@cfn_stack_name}'"
      @cfn_stack.events.each { |e| puts e }
    else
      puts "Stack '#{@cfn_stack_name}' has no events"
    end
  else
    puts "Stack '#{@cfn_stack_name}' does not exist"
  end
end

#generate(override_params = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bora/stack.rb', line 60

def generate(override_params = {})
  params = process_params(override_params)
  if File.extname(@template_file) == ".rb"
    template_body = run_cfndsl(@template_file, params)
    template_json = JSON.parse(template_body)
    if template_json["Parameters"]
      cfn_param_keys = template_json["Parameters"].keys
      cfn_params = params.select { |k, v| cfn_param_keys.include?(k) }.map do |k, v|
        { parameter_key: k, parameter_value: v }
      end
      @cfn_options[:parameters] = cfn_params if !cfn_params.empty?
    end
    @cfn_options[:template_body] = template_body
  else
    @cfn_options[:template_url] = @template_file
    if !params.empty?
      @cfn_options[:parameters] = params.map do |k, v|
        { parameter_key: k, parameter_value: v }
      end
    end
  end
end

#outputsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bora/stack.rb', line 83

def outputs
  outputs = @cfn_stack.outputs
  if outputs
    if outputs.length > 0
      puts "Outputs for stack '#{@cfn_stack_name}'"
      outputs.each { |output| puts output }
    else
      puts "Stack '#{@cfn_stack_name}' has no outputs"
    end
  else
    puts "Stack '#{@cfn_stack_name}' does not exist"
  end
end

#rake_tasksObject



21
22
23
# File 'lib/bora/stack.rb', line 21

def rake_tasks
  StackTasks.new(self)
end

#recreate(override_params = {}) ⇒ Object



97
98
99
100
# File 'lib/bora/stack.rb', line 97

def recreate(override_params = {})
  generate(override_params)
  invoke_action("recreate", @cfn_options)
end

#show(override_params = {}) ⇒ Object



102
103
104
105
# File 'lib/bora/stack.rb', line 102

def show(override_params = {})
  generate(override_params)
  puts @cfn_stack.new_template(@cfn_options)
end

#show_currentObject



107
108
109
110
# File 'lib/bora/stack.rb', line 107

def show_current
  template = @cfn_stack.template
  puts template ? template : "Stack '#{@cfn_stack_name}' does not exist"
end

#statusObject



112
113
114
# File 'lib/bora/stack.rb', line 112

def status
  puts @cfn_stack.status
end

#validate(override_params = {}) ⇒ Object



116
117
118
119
# File 'lib/bora/stack.rb', line 116

def validate(override_params = {})
  generate(override_params)
  puts "Template for stack '#{@cfn_stack_name}' is valid" if @cfn_stack.validate(@cfn_options)
end