Class: Kontena::Stacks::ChangeResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/stacks/change_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_data, new_data) ⇒ ChangeResolver

Creates a change analysis from two sets of stack data. The format is a flat hash of all related stacks.

Parameters:

  • old_data (DataSet, Hash)
  • new_data (DataSet, Hash)


13
14
15
16
17
# File 'lib/kontena/stacks/change_resolver.rb', line 13

def initialize(old_data, new_data)
  @old_data = old_data.is_a?(StackDataSet) ? old_data : StackDataSet.new(old_data)
  @new_data = new_data.is_a?(StackDataSet) ? new_data : StackDataSet.new(new_data)
  analyze
end

Instance Attribute Details

#new_dataObject (readonly)

Returns the value of attribute new_data.



6
7
8
# File 'lib/kontena/stacks/change_resolver.rb', line 6

def new_data
  @new_data
end

#old_dataObject (readonly)

Returns the value of attribute old_data.



6
7
8
# File 'lib/kontena/stacks/change_resolver.rb', line 6

def old_data
  @old_data
end

Instance Method Details

#added_servicesArray<String>

Returns an array of services that should be added.

Returns:

  • (Array<String>)

    an array of services that should be added



20
21
22
# File 'lib/kontena/stacks/change_resolver.rb', line 20

def added_services
  @added_services ||= []
end

#added_stacksArray<String>

Returns an array of stack installation names that should be removed.

Returns:

  • (Array<String>)

    an array of stack installation names that should be removed



40
41
42
# File 'lib/kontena/stacks/change_resolver.rb', line 40

def added_stacks
  @added_stacks ||= []
end

#analyzeObject



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
# File 'lib/kontena/stacks/change_resolver.rb', line 64

def analyze
  old_names = old_data.stack_names
  new_names = new_data.stack_names

  removed_stacks.concat(old_names - new_names)
  added_stacks.concat(new_names - old_names)
  (new_names & old_names).each do |candidate|
    upgraded_stacks << candidate if stack_upgraded?(candidate)
  end

  removed_stacks.each do |removed_stack|
    removed_services.concat(
      old_data.stack(removed_stack).service_names.map { |name| "#{removed_stack}/#{name}"}
    )
  end

  added_stacks.each do |added_stack|
    added_services.concat(
      new_data.stack(added_stack).service_names.map { |name| "#{added_stack}/#{name}"}
    )
  end

  upgraded_stacks.each do |upgraded_stack|
    old_stack = old_data.stack(upgraded_stack).stack_name
    new_stack = new_data.stack(upgraded_stack).stack_name

    unless old_stack == new_stack
      replaced_stacks[upgraded_stack] = { from: old_stack, to: new_stack }
    end

    old_services = old_data.stack(upgraded_stack).service_names.map { |name| "#{upgraded_stack}/#{name}" }
    new_services = new_data.stack(upgraded_stack).service_names.map { |name| "#{upgraded_stack}/#{name}" }

    removed_services.concat(old_services - new_services)
    added_services.concat(new_services - old_services)
    upgraded_services.concat(new_services & old_services)
  end
end

#remaining_stacksArray<String>

Returns an array of installed stack names that should exist after upgrade.

Returns:

  • (Array<String>)

    an array of installed stack names that should exist after upgrade



55
56
57
# File 'lib/kontena/stacks/change_resolver.rb', line 55

def remaining_stacks
  @remaining_stacks ||= added_stacks + upgraded_stacks
end

#removed_servicesArray<String>

Returns an array of services that should be removed.

Returns:

  • (Array<String>)

    an array of services that should be removed



25
26
27
# File 'lib/kontena/stacks/change_resolver.rb', line 25

def removed_services
  @removed_services ||= []
end

#removed_stacksArray<String>

Returns an array of stack installation names that should be removed.

Returns:

  • (Array<String>)

    an array of stack installation names that should be removed



35
36
37
# File 'lib/kontena/stacks/change_resolver.rb', line 35

def removed_stacks
  @removed_stacks ||= []
end

#replaced_stacksHash

Returns a hash of “installed-stack-name” => { :from => ‘stackname’, :to => ‘new-stackname’ }.

Returns:

  • (Hash)

    a hash of “installed-stack-name” => { :from => ‘stackname’, :to => ‘new-stackname’ }



50
51
52
# File 'lib/kontena/stacks/change_resolver.rb', line 50

def replaced_stacks
  @replaced_stacks ||= {}
end

#safe?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/kontena/stacks/change_resolver.rb', line 60

def safe?
  removed_stacks.empty? && replaced_stacks.empty? && removed_services.empty?
end

#stack_upgraded?(name) ⇒ Boolean

Stack is upgraded if version, stack name, variables change or stack is root

Parameters:

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
# File 'lib/kontena/stacks/change_resolver.rb', line 107

def stack_upgraded?(name)
  old_stack = old_data.stack(name)
  new_stack = new_data.stack(name)
  return true if new_stack.root?
  return true if old_stack.version != new_stack.version
  return true if old_stack.stack_name != new_stack.stack_name
  return true if old_stack.variables != new_stack.variables

  false
end

#upgraded_servicesArray<String>

Returns an array of services that should be upgraded.

Returns:

  • (Array<String>)

    an array of services that should be upgraded



30
31
32
# File 'lib/kontena/stacks/change_resolver.rb', line 30

def upgraded_services
  @upgraded_services ||= []
end

#upgraded_stacksArray<String>

Returns an array of stack installation names that should be upgraded.

Returns:

  • (Array<String>)

    an array of stack installation names that should be upgraded



45
46
47
# File 'lib/kontena/stacks/change_resolver.rb', line 45

def upgraded_stacks
  @upgraded_stacks ||= []
end