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_VALIDATE_SUCCESS_MESSAGE =
"Template for stack '%s' is valid"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, template_file, stack_config) ⇒ Stack

Returns a new instance of Stack.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bora/stack.rb', line 19

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']
  @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.



33
34
35
# File 'lib/bora/stack.rb', line 33

def region
  @region
end

#stack_configObject (readonly)

Returns the value of attribute stack_config.



33
34
35
# File 'lib/bora/stack.rb', line 33

def stack_config
  @stack_config
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



33
34
35
# File 'lib/bora/stack.rb', line 33

def stack_name
  @stack_name
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



33
34
35
# File 'lib/bora/stack.rb', line 33

def template_file
  @template_file
end

Instance Method Details

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bora/stack.rb', line 39

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



52
53
54
# File 'lib/bora/stack.rb', line 52

def delete
  invoke_action("delete")
end

#diff(override_params = {}) ⇒ Object



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

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

#eventsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bora/stack.rb', line 61

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bora/stack.rb', line 76

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

#rake_tasksObject



35
36
37
# File 'lib/bora/stack.rb', line 35

def rake_tasks
  StackTasks.new(self)
end

#recreate(override_params = {}) ⇒ Object



91
92
93
94
# File 'lib/bora/stack.rb', line 91

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

#resolved_params(override_params = {}) ⇒ Object



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

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

#show(override_params = {}) ⇒ Object



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

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

#show_currentObject



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

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

#statusObject



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

def status
  puts @cfn_stack.status
end

#validate(override_params = {}) ⇒ Object



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

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