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".freeze
STACK_ACTION_FAILURE_MESSAGE =
"%s stack '%s' failed".freeze
STACK_ACTION_NOT_CHANGED_MESSAGE =
"%s stack '%s' skipped as template has not changed".freeze
STACK_DOES_NOT_EXIST_MESSAGE =
"Stack '%s' does not exist".freeze
STACK_EVENTS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no events".freeze
STACK_EVENTS_MESSAGE =
"Events for stack '%s'".freeze
STACK_OUTPUTS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no outputs".freeze
STACK_PARAMETERS_DO_NOT_EXIST_MESSAGE =
"Stack '%s' has no parameters".freeze
STACK_VALIDATE_SUCCESS_MESSAGE =
"Template for stack '%s' is valid".freeze
STACK_DIFF_TEMPLATE_UNCHANGED_MESSAGE =
'Template has not changed'.freeze
STACK_DIFF_PARAMETERS_UNCHANGED_MESSAGE =
'Parameters have not changed'.freeze
STACK_DIFF_NO_CHANGES_MESSAGE =
'No changes will be applied'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, template_file, stack_config) ⇒ Stack

Returns a new instance of Stack.



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

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_stack = Cfn::Stack.new(@cfn_stack_name, @region)
  @resolver = ParameterResolver.new(self)
end

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



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

def region
  @region
end

#stack_configObject (readonly)

Returns the value of attribute stack_config.



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

def stack_config
  @stack_config
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



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

def stack_name
  @stack_name
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



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

def template_file
  @template_file
end

Instance Method Details

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



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

def apply(override_params = {}, pretty_json = false)
  cfn_options = generate(override_params, pretty_json)
  action = @cfn_stack.exists? ? 'update' : 'create'
  success = invoke_action(action.capitalize, action, cfn_options)
  if success
    outputs = @cfn_stack.outputs
    if outputs && !outputs.empty?
      puts 'Stack outputs'
      outputs.each { |output| puts output }
    end
  end
  success
end

#create_change_set(change_set_name, description = nil, override_params = {}, pretty_json = false) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/bora/stack.rb', line 140

def create_change_set(change_set_name, description = nil, override_params = {}, pretty_json = false)
  puts "Creating change set '#{change_set_name}' for stack '#{@cfn_stack_name}' in region #{@region}"
  cfn_options = generate(override_params, pretty_json)
  cfn_options[:description] = description if description
  change_set = @cfn_stack.create_change_set(change_set_name, cfn_options)
  puts change_set
  change_set
end

#deleteObject



58
59
60
# File 'lib/bora/stack.rb', line 58

def delete
  invoke_action('Delete', 'delete')
end

#delete_change_set(change_set_name) ⇒ Object



157
158
159
160
# File 'lib/bora/stack.rb', line 157

def delete_change_set(change_set_name)
  @cfn_stack.delete_change_set(change_set_name)
  puts "Deleted change set '#{change_set_name}' for stack '#{@cfn_stack_name}' in region #{@region}"
end

#describe_change_set(change_set_name) ⇒ Object



153
154
155
# File 'lib/bora/stack.rb', line 153

def describe_change_set(change_set_name)
  puts @cfn_stack.describe_change_set(change_set_name)
end

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



62
63
64
65
66
67
# File 'lib/bora/stack.rb', line 62

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

#eventsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bora/stack.rb', line 69

def events
  events = @cfn_stack.events
  if events
    if !events.empty?
      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

#execute_change_set(change_set_name) ⇒ Object



162
163
164
# File 'lib/bora/stack.rb', line 162

def execute_change_set(change_set_name)
  invoke_action("Execute change set '#{change_set_name}'", 'execute_change_set', change_set_name)
end

#list_change_setsObject



149
150
151
# File 'lib/bora/stack.rb', line 149

def list_change_sets
  puts @cfn_stack.list_change_sets.map(&:to_s).join("\n")
end

#outputsObject



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

def outputs
  outputs = @cfn_stack.outputs
  if outputs
    if !outputs.empty?
      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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bora/stack.rb', line 99

def parameters
  parameters = @cfn_stack.parameters
  if parameters
    if !parameters.empty?
      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



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

def rake_tasks
  StackTasks.new(self)
end

#recreate(override_params = {}) ⇒ Object



114
115
116
117
# File 'lib/bora/stack.rb', line 114

def recreate(override_params = {})
  cfn_options = generate(override_params)
  invoke_action('Recreate', 'recreate', cfn_options)
end

#resolved_params(override_params = {}) ⇒ Object



166
167
168
169
170
# File 'lib/bora/stack.rb', line 166

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

#show(override_params = {}) ⇒ Object



119
120
121
122
# File 'lib/bora/stack.rb', line 119

def show(override_params = {})
  cfn_options = generate(override_params)
  puts get_new_template(cfn_options)
end

#show_currentObject



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

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

#statusObject



129
130
131
# File 'lib/bora/stack.rb', line 129

def status
  puts @cfn_stack.status
end

#validate(override_params = {}) ⇒ Object



133
134
135
136
137
138
# File 'lib/bora/stack.rb', line 133

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