Class: Bora::Stack

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

Constant Summary collapse

STACK_ACTION_SUCCESS_MESSAGE =
"%s stack '%s' completed successfully"
STACK_ACTION_FAILURE_MESSAGE =
"%s stack '%s' failed"
STACK_ACTION_NOT_CHANGED_MESSAGE =
"%s stack '%s' skipped as template has not changed"
STACK_DOES_NOT_EXIST_MESSAGE =
"Stack '%s' does not exist"
STACK_EVENTS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no events"
STACK_EVENTS_MESSAGE =
"Events for stack '%s'"
STACK_OUTPUTS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no outputs"
STACK_PARAMETERS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no parameters"
STACK_VALIDATE_SUCCESS_MESSAGE =
"Template for stack '%s' is valid"
STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE =
"Template has not changed"
STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE =
"Parameters have not changed"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, template_file, stack_config) ⇒ Stack

Returns a new instance of Stack.



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

def initialize(stack_name, template_file, stack_config)
  @stack_name = stack_name
  @cfn_stack_name = stack_config['cfn_stack_name'] || stack_config['stack_name'] || @stack_name
  if stack_config['stack_name']
    puts "DEPRECATED: The 'stack_name' setting is deprecated. Please use 'cfn_stack_name' instead."
  end
  @template_file = template_file
  @stack_config = stack_config
  @region = @stack_config['default_region'] || Aws::CloudFormation::Client.new.config[:region]
  @cfn_options = extract_cfn_options(stack_config)
  @cfn_stack = Cfn::Stack.new(@cfn_stack_name, @region)
  @resolver = ParameterResolver.new(self)
end

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



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

def region
  @region
end

#stack_configObject (readonly)

Returns the value of attribute stack_config.



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

def stack_config
  @stack_config
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



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

def stack_name
  @stack_name
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



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

def template_file
  @template_file
end

Instance Method Details

#apply(override_params = {}, pretty_json = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bora/stack.rb', line 43

def apply(override_params = {}, pretty_json = false)
  generate(override_params, pretty_json)
  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
  success
end

#deleteObject



56
57
58
# File 'lib/bora/stack.rb', line 56

def delete
  invoke_action("delete")
end

#diff(override_params = {}, context_lines = 3) ⇒ Object



60
61
62
63
64
# File 'lib/bora/stack.rb', line 60

def diff(override_params = {}, context_lines = 3)
  generate(override_params)
  diff_parameters
  diff_template(override_params, context_lines)
end

#eventsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bora/stack.rb', line 66

def events
  events = @cfn_stack.events
  if events
    if events.length > 0
      puts STACK_EVENTS_MESSAGE % @cfn_stack_name
      events.each { |e| puts e }
    else
      puts STACK_EVENTS_DO_NOT_EXIST_MESSAGE % @cfn_stack_name
    end
  else
    puts STACK_DOES_NOT_EXIST_MESSAGE % @cfn_stack_name
  end
  events
end

#outputsObject



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

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_OUTPUTS_DO_NOT_EXIST_MESSAGE % @cfn_stack_name
    end
  else
    puts STACK_DOES_NOT_EXIST_MESSAGE % @cfn_stack_name
  end
  outputs
end

#parametersObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bora/stack.rb', line 96

def parameters
  parameters = @cfn_stack.parameters
  if parameters
    if parameters.length > 0
      puts "Parameters for stack '#{@cfn_stack_name}'"
      parameters.each { |parameter| puts parameter }
    else
      puts STACK_PARAMETERS_DO_NOT_EXIST_MESSAGE % @cfn_stack_name
    end
  else
    puts STACK_DOES_NOT_EXIST_MESSAGE % @cfn_stack_name
  end
  parameters
end

#rake_tasksObject



39
40
41
# File 'lib/bora/stack.rb', line 39

def rake_tasks
  StackTasks.new(self)
end

#recreate(override_params = {}) ⇒ Object



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

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

#resolved_params(override_params = {}) ⇒ Object



137
138
139
140
141
# File 'lib/bora/stack.rb', line 137

def resolved_params(override_params = {})
  params = @stack_config['params'] || {}
  params.merge!(override_params)
  @resolver.resolve(params)
end

#show(override_params = {}) ⇒ Object



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

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

#show_currentObject



121
122
123
124
# File 'lib/bora/stack.rb', line 121

def show_current
  template = @cfn_stack.template
  puts template ? template : (STACK_DOES_NOT_EXIST_MESSAGE % @cfn_stack_name)
end

#statusObject



126
127
128
# File 'lib/bora/stack.rb', line 126

def status
  puts @cfn_stack.status
end

#validate(override_params = {}) ⇒ Object



130
131
132
133
134
135
# File 'lib/bora/stack.rb', line 130

def validate(override_params = {})
  generate(override_params)
  is_valid = @cfn_stack.validate(@cfn_options)
  puts STACK_VALIDATE_SUCCESS_MESSAGE % @cfn_stack_name if is_valid
  is_valid
end