Class: Sfn::Command::Update

Inherits:
Sfn::Command
  • Object
show all
Includes:
Sfn::CommandModule::Base, Sfn::CommandModule::Stack, Sfn::CommandModule::Template
Defined in:
lib/sfn/command/update.rb

Overview

Update command

Constant Summary

Constants included from Sfn::CommandModule::Template

Sfn::CommandModule::Template::MAX_PARAMETER_ATTEMPTS, Sfn::CommandModule::Template::TEMPLATE_IGNORE_DIRECTORIES

Instance Method Summary collapse

Methods included from Sfn::CommandModule::Stack

included

Methods included from Sfn::CommandModule::Template

included

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#build_planner(stack) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/sfn/command/update.rb', line 157

def build_planner(stack)
  klass_name = stack.api.class.to_s.split('::').last
  if(Planner.const_defined?(klass_name))
    Planner.const_get(klass_name).new(ui, config, arguments, stack)
  else
    warn "Failed to build planner for current provider. No provider implemented. (`#{klass_name}`)"
    nil
  end
end

#display_plan_information(result) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/sfn/command/update.rb', line 167

def display_plan_information(result)
  ui.info ui.color('Pre-update resource planning report:', :bold)
  unless(print_plan_result(result))
    ui.info 'No resources life cycle changes detected in this update!'
  end
  ui.confirm 'Apply this stack update?'
end

#execute!Object

Run the stack creation command



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sfn/command/update.rb', line 13

def execute!
  name_required!
  name = name_args.first

  stack_info = "#{ui.color('Name:', :bold)} #{name}"
  begin
    stack = provider.connection.stacks.get(name)
  rescue Miasma::Error::ApiError::RequestError
    stack = nil
  end

  config[:compile_parameters] ||= Smash.new

  if(config[:file])
    s_name = [name]

    c_setter = lambda do |c_stack|
      compile_params = c_stack.outputs.detect do |output|
        output.key == 'CompileState'
      end
      if(compile_params)
        compile_params = MultiJson.load(compile_params.value)
        c_current = config[:compile_parameters].fetch(s_name.join('_'), Smash.new)
        config[:compile_parameters][s_name.join('_')] = compile_params.merge(c_current)
      end
      c_stack.nested_stacks(false).each do |n_stack|
        s_name.push(n_stack.name)
        c_setter.call(n_stack)
        s_name.pop
      end
    end

    if(stack)
      c_setter.call(stack)
    end

    file = load_template_file(:stack => stack)
    stack_info << " #{ui.color('Path:', :bold)} #{config[:file]}"
    nested_stacks = file.delete('sfn_nested_stack')
  end

  if(nested_stacks)
    unpack_nesting(name, file, :update)
  else
    unless(stack)
      ui.fatal "Failed to locate requested stack: #{ui.color(name, :red, :bold)}"
      raise "Failed to locate stack: #{name}"
    end

    ui.info "#{ui.color('SparkleFormation:', :bold)} #{ui.color('update', :green)}"

    unless(file)
      if(config[:template])
        file = config[:template]
        stack_info << " #{ui.color('(template provided)', :green)}"
      else
        stack_info << " #{ui.color('(no template update)', :yellow)}"
      end
    end
    ui.info "  -> #{stack_info}"

    if(file)
      if(config[:print_only])
        ui.puts _format_json(translate_template(file))
        return
      end

      original_template = stack.template
      original_parameters = stack.parameters

      stack.template = translate_template(file)
      apply_stacks!(stack)

      populate_parameters!(file, :current_parameters => stack.parameters)
      update_template = stack.template

      if(config[:plan])
        begin
          stack.template = original_template
          stack.parameters = original_parameters
          plan = build_planner(stack)
          if(plan)
            result = plan.generate_plan(file, config_root_parameters)
            display_plan_information(result)
          end
        rescue => e
          unless(e.message.include?('Confirmation declined'))
            ui.error "Unexpected error when generating plan information: #{e.class} - #{e}"
            ui.debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
            ui.confirm 'Continue with stack update?'
          else
            raise
          end
        end
      end

      stack.parameters = config_root_parameters
      stack.template = scrub_template(update_template)
    else
      apply_stacks!(stack)

      original_parameters = stack.parameters
      populate_parameters!(stack.template, :current_parameters => stack.parameters)

      if(config[:plan])
        stack.parameters = original_parameters
        plan = build_planner(stack)
        if(plan)
          result = plan.generate_plan(stack.template, config_root_parameters)
          display_plan_information(result)
        end
      end
      stack.parameters = config_root_parameters
    end

    begin
      api_action!(:api_stack => stack) do
        stack.save
        if(config[:poll])
          poll_stack(stack.name)
          if(stack.reload.state == :update_complete)
            ui.info "Stack update complete: #{ui.color('SUCCESS', :green)}"
            stack.resources.reload
            namespace.const_get(:Describe).new({:outputs => true}, [name]).execute!
          else
            ui.fatal "Update of stack #{ui.color(name, :bold)}: #{ui.color('FAILED', :red, :bold)}"
            raise 'Stack did not reach a successful update completion state.'
          end
        else
          ui.warn 'Stack state polling has been disabled.'
          ui.info "Stack update initialized for #{ui.color(name, :green)}"
        end
      end
    rescue Miasma::Error::ApiError::RequestError => e
      if(e.message.downcase.include?('no updates'))
        ui.warn "No updates detected for stack (#{stack.name})"
      else
        raise
      end
    end

  end
end

Print planning items

Parameters:

  • info (Hash)

    plan

  • key (Symbol)

    key of items

  • color (Symbol)

    color to flag



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/sfn/command/update.rb', line 238

def print_plan_items(info, key, color)
  max_name = info[key].keys.map(&:size).max
  max_type = info[key].values.map{|i|i[:type]}.map(&:size).max
  max_p = info[key].values.map{|i| i.fetch(:diffs, [])}.flatten(1).map{|d| d.fetch(:property_name, :path).to_s.size}.max
  max_o = info[key].values.map{|i| i.fetch(:diffs, [])}.flatten(1).map{|d| d[:original].to_s.size}.max
  info[key].each do |name, val|
    ui.print ' ' * 6
    ui.print ui.color("[#{val[:type]}]", color)
    ui.print ' ' * (max_type - val[:type].size)
    ui.print ' ' * 4
    ui.print ui.color(name, :bold)
    unless(val[:properties].nil? || val[:properties].empty?)
      ui.print ' ' * (max_name - name.size)
      ui.print ' ' * 4
      ui.print "Reason: Updated properties: `#{val[:properties].join('`, `')}`"
    end
    ui.puts
    if(config[:diffs])
      unless(val[:diffs].empty?)
        val[:diffs].each do |diff|
          if(diff[:updated] && diff[:original])
            p_name = diff.fetch(:property_name, :path)
            ui.print ' ' * 8
            ui.print "#{p_name}: "
            ui.print ' ' * (max_p - p_name.size)
            ui.print ui.color("-#{diff[:original]}", :red)
            ui.print ' ' * (max_o - diff[:original].size)
            ui.print ' '
            if(diff[:updated] == Sfn::Planner::RUNTIME_MODIFIED)
              ui.puts ui.color("+#{diff[:original]} <Dependency Modified>", :green)
            else
              ui.puts ui.color("+#{diff[:updated]}", :green)
            end
          end
        end
        ui.puts
      end
    end
  end
end


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/sfn/command/update.rb', line 176

def print_plan_result(info, names=[])
  said_any_things = false
  unless(info[:stacks].empty?)
    info[:stacks].each do |s_name, s_info|
      result = print_plan_result(s_info, [*names, s_name].compact)
      said_any_things ||= result
    end
  end
  unless(names.flatten.compact.empty?)
    said_things = false
    ui.puts
    ui.puts "  #{ui.color('Update plan for:', :bold)} #{ui.color(names.join(' > '), :blue)}"
    unless(info[:unknown].empty?)
      ui.puts "    #{ui.color('!!! Unknown update effect:', :red, :bold)}"
      print_plan_items(info, :unknown, :red)
      ui.puts
      said_any_things = said_things = true
    end
    unless(info[:unavailable].empty?)
      ui.puts "    #{ui.color('Update request not allowed:', :red, :bold)}"
      print_plan_items(info, :unavailable, :red)
      ui.puts
      said_any_things = said_things = true
    end
    unless(info[:replace].empty?)
      ui.puts "    #{ui.color('Resources to be replaced:', :red, :bold)}"
      print_plan_items(info, :replace, :red)
      ui.puts
      said_any_things = said_things = true
    end
    unless(info[:interrupt].empty?)
      ui.puts "    #{ui.color('Resources to be interrupted:', :yellow, :bold)}"
      print_plan_items(info, :interrupt, :yellow)
      ui.puts
      said_any_things = said_things = true
    end
    unless(info[:removed].empty?)
      ui.puts "    #{ui.color('Resources to be removed:', :red, :bold)}"
      print_plan_items(info, :removed, :red)
      ui.puts
      said_any_things = said_things = true
    end
    unless(info[:added].empty?)
      ui.puts "    #{ui.color('Resources to be added:', :green, :bold)}"
      print_plan_items(info, :added, :green)
      ui.puts
      said_any_things = said_things = true
    end
    unless(said_things)
      ui.puts "    #{ui.color('No resource lifecycle changes detected!', :green)}"
      ui.puts
      said_any_things = true
    end
  end
  said_any_things
end

#scrub_template(template) ⇒ Hash

Scrub sparkle/sfn customizations from the stack resource data

Parameters:

  • template (Hash)

Returns:

  • (Hash)


283
284
285
286
287
288
289
290
291
# File 'lib/sfn/command/update.rb', line 283

def scrub_template(template)
  template = Sfn::Utils::StackParameterScrubber.scrub!(template)
  (template['Resources'] || {}).each do |r_name, r_content|
    if(valid_stack_types.include?(r_content['Type']))
      (r_content['Properties'] || {}).delete('Stack')
    end
  end
  template
end