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



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

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



30
31
32
# File 'lib/bora/stack.rb', line 30

def region
  @region
end

#stack_configObject (readonly)

Returns the value of attribute stack_config.



30
31
32
# File 'lib/bora/stack.rb', line 30

def stack_config
  @stack_config
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



30
31
32
# File 'lib/bora/stack.rb', line 30

def stack_name
  @stack_name
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



30
31
32
# File 'lib/bora/stack.rb', line 30

def template_file
  @template_file
end

Instance Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bora/stack.rb', line 36

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



49
50
51
# File 'lib/bora/stack.rb', line 49

def delete
  invoke_action("delete")
end

#diff(override_params = {}) ⇒ Object



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

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

#eventsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bora/stack.rb', line 58

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bora/stack.rb', line 73

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



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

def rake_tasks
  StackTasks.new(self)
end

#recreate(override_params = {}) ⇒ Object



88
89
90
91
# File 'lib/bora/stack.rb', line 88

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

#resolved_params(override_params = {}) ⇒ Object



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

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

#show(override_params = {}) ⇒ Object



93
94
95
96
# File 'lib/bora/stack.rb', line 93

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

#show_currentObject



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

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

#statusObject



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

def status
  puts @cfn_stack.status
end

#validate(override_params = {}) ⇒ Object



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

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