Module: Sfn::CommandModule::Planning

Included in:
Sfn::Command::Plan, Sfn::Command::Realize, Sfn::Command::Update
Defined in:
lib/sfn/command_module/planning.rb

Overview

Planning helpers

Instance Method Summary collapse

Instance Method Details

#build_planner(stack) ⇒ Sfn::Planner

Create a new planner instance

Parameters:

  • (Miasma::Models::Orchestration::Stack)

Returns:



12
13
14
15
16
17
18
19
20
# File 'lib/sfn/command_module/planning.rb', line 12

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

Display plan result on the UI

Parameters:

  • result (Miasma::Models::Orchestration::Stack::Plan)


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

def display_plan_information(result)
  ui.info ui.color("Pre-update resource planning report:", :bold)
  unless print_plan_result(result, [result.name])
    ui.info "No resources life cycle changes detected in this update!"
  end
  if config[:plan_apply]
    return ui.info "Realizing this stack plan..."
  elsif config[:plan_only]
    return
  end
  ui.confirm "Realize this stack plan?"
end

Print planning items

Parameters:

  • info (Miasma::Models::Orchestration::Stack::Plan)

    plan

  • key (Symbol)

    key of items

  • color (Symbol)

    color to flag



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
# File 'lib/sfn/command_module/planning.rb', line 105

def print_plan_items(info, key, color)
  collection = info.send(key)
  max_name = collection.map(&:name).map(&:size).max
  max_type = collection.map(&:type).map(&:size).max
  max_p = collection.map(&:diffs).flatten(1).map(&:name).map(&:to_s).map(&:size).max
  max_o = collection.map(&:diffs).flatten(1).map(&:current).map(&:to_s).map(&:size).max
  collection.each do |val|
    name = val.name
    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)
    properties = Array(val.diffs).map(&:name)
    unless properties.empty?
      ui.print " " * (max_name - name.size)
      ui.print " " * 4
      ui.print "Reason: `#{properties.join("`, `")}`"
    end
    ui.puts
    if config[:diffs]
      unless val.diffs.empty?
        p_name = nil
        val.diffs.each do |diff|
          if !diff.proposed.nil? || !diff.current.nil?
            p_name = diff.name
            ui.print " " * 8
            ui.print "#{p_name}: "
            ui.print " " * (max_p - p_name.size)
            ui.print ui.color("-#{diff.current}", :red) if diff.current
            ui.print " " * (max_o - diff.current.to_s.size)
            ui.print " "
            if diff.proposed == Sfn::Planner::RUNTIME_MODIFIED
              ui.puts ui.color("+#{diff.current} <Dependency Modified>", :green)
            else
              if diff.proposed.nil?
                ui.puts
              else
                ui.puts ui.color("+#{diff.proposed.to_s.gsub("__MODIFIED_REFERENCE_VALUE__", "<Dependency Modified>")}", :green)
              end
            end
          end
        end
        ui.puts if p_name
      end
    end
  end
end

Print plan information to the UI

Parameters:

  • info (Miasma::Models::Orchestration::Stack::Plan)
  • names (Array<String>) (defaults to: [])

    nested names



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
# File 'lib/sfn/command_module/planning.rb', line 42

def print_plan_result(info, names = [])
  said_any_things = false
  unless Array(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
  if !names.flatten.compact.empty? || info.name
    said_things = false
    output_name = names.empty? ? info.name : names.join(" > ")
    ui.puts
    ui.puts "  #{ui.color("Update plan for:", :bold)} #{ui.color(names.join(" > "), :blue)}"
    unless Array(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 Array(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 Array(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 Array(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 Array(info.remove).empty?
      ui.puts "    #{ui.color("Resources to be removed:", :red, :bold)}"
      print_plan_items(info, :remove, :red)
      ui.puts
      said_any_things = said_things = true
    end
    unless Array(info.add).empty?
      ui.puts "    #{ui.color("Resources to be added:", :green, :bold)}"
      print_plan_items(info, :add, :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