Module: Pacemaker::Type
- Included in:
- Puppet::Provider::PacemakerXML, Serverspec::Type::PacemakerXML
- Defined in:
- lib/pacemaker/type.rb
Overview
contains functions that can be included to the pacemaker types
Instance Method Summary collapse
-
#add_to_operations_array(operations, new_op) ⇒ Object
Maintains an array of operation hashes as if it was a sorted set.
-
#compare_meta_attributes(is, should) ⇒ TrueClass, FalseClass
compare meta_attribute hashes excluding status meta attributes.
-
#compare_operations(is, should) ⇒ TrueClass, FalseClass
sort operations array before insync? to make different order and same data arrays equal.
-
#inspect_to_s(data) ⇒ String
return inspected data structure, used in should_to_s and is_to_s functions.
-
#insync_debug(is, should, tag = nil) ⇒ Object
output IS and SHOULD values for debugging.
-
#munge_meta_attributes(attributes_from) ⇒ Hash
remove status related meta attributes from the meta attributes hash.
-
#munge_operation(operation) ⇒ Object
munge a single operations hash.
-
#munge_operations_array(operations_input) ⇒ Object
Munges the input into an Array of munged operations.
-
#munge_rule(rule, rule_number, title) ⇒ Hash
normalize a single location rule.
-
#primitive_base_name(primitive) ⇒ String
remove “-clone” or “-master” suffix and “role” suffix (:Master, :Slave) from a primitive’s name.
-
#stringify_data(data) ⇒ Object
convert data structure’s keys and values to strings.
Instance Method Details
#add_to_operations_array(operations, new_op) ⇒ Object
Maintains an array of operation hashes as if it was a sorted set. These are in Array-of-Hash format ({ ‘name’ => ‘monitor’, ‘interval’ => …}), not { ‘monitor’ => … } ones. The unicity is done on the name and interval operation keys. The input is expected to have been stringified and munged.
Modifies the operations argument and returns it.
We can’t use a real Set as it doesn’t serialize correctly in Puppet’s transaction store. This datastructure is always small, so performance is irrelevant.
54 55 56 57 58 |
# File 'lib/pacemaker/type.rb', line 54 def add_to_operations_array(operations, new_op) operations.delete_if { |op| op['name'] == new_op['name'] && op['interval'] == new_op['interval'] } operations << new_op operations.sort_by! { |op| "#{op['name']} #{op['interval']}" } end |
#compare_meta_attributes(is, should) ⇒ TrueClass, FalseClass
compare meta_attribute hashes excluding status meta attributes
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pacemaker/type.rb', line 110 def (is, should) return unless is.is_a?(Hash) && should.is_a?(Hash) is_without_state = is.reject do |k, _v| [:status_meta_attributes].include? k.to_s end should_without_state = should.reject do |k, _v| [:status_meta_attributes].include? k.to_s end result = is_without_state == should_without_state debug "compare_meta_attributes: #{result}" result end |
#compare_operations(is, should) ⇒ TrueClass, FalseClass
sort operations array before insync? to make different order and same data arrays equal
128 129 130 131 132 133 134 |
# File 'lib/pacemaker/type.rb', line 128 def compare_operations(is, should) is = is.first if is.is_a? Array should = should.first if should.is_a? Array result = (is == should) debug "compare_operations: #{result}" result end |
#inspect_to_s(data) ⇒ String
return inspected data structure, used in should_to_s and is_to_s functions
17 18 19 |
# File 'lib/pacemaker/type.rb', line 17 def inspect_to_s(data) data.inspect end |
#insync_debug(is, should, tag = nil) ⇒ Object
output IS and SHOULD values for debugging
8 9 10 11 12 |
# File 'lib/pacemaker/type.rb', line 8 def insync_debug(is, should, tag = nil) debug "insync?: #{tag}" if tag debug "IS: #{is.inspect} #{is.class}" debug "SH: #{should.inspect} #{should.class}" end |
#munge_meta_attributes(attributes_from) ⇒ Hash
remove status related meta attributes from the meta attributes hash
140 141 142 143 144 145 146 147 |
# File 'lib/pacemaker/type.rb', line 140 def (attributes_from) attributes_to = {} attributes_from.each do |name, parameters| next if [:status_meta_attributes].include? name attributes_to.store name, parameters end attributes_to end |
#munge_operation(operation) ⇒ Object
munge a single operations hash
97 98 99 100 101 102 103 104 |
# File 'lib/pacemaker/type.rb', line 97 def munge_operation(operation) raise "invalid pacemaker_resource.operations element: #{operation}" unless operation.is_a? Hash operation['name'] = 'monitor' unless operation['name'] operation['interval'] = '0' unless operation['name'] == 'monitor' operation['interval'] = '0' unless operation['interval'] operation['role'].capitalize! if operation['role'] operation end |
#munge_operations_array(operations_input) ⇒ Object
Munges the input into an Array of munged operations.
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 |
# File 'lib/pacemaker/type.rb', line 62 def munge_operations_array(operations_input) operations_input = stringify_data(operations_input) operations_input = [operations_input] unless operations_input.is_a? Array operations = [] operations_input.each do |operation| # operations were provided as an array of hashes if operation.is_a? Hash and operation['name'] munge_operation operation add_to_operations_array(operations, operation) elsif operation.is_a? Hash # operations were provided as a hash of hashes operation.each do |operation_name, operation_data| raise "invalid operation in a hash of hashes: #{operation_data}" unless operation_data.is_a? Hash operation = {} if operation_name.include? ':' operation_name_array = operation_name.split(':') operation_name = operation_name_array[0] if not operation_data['role'] and operation_name_array[1] operation_data['role'] = operation_name_array[1] end end operation['name'] = operation_name operation.merge! operation_data munge_operation operation add_to_operations_array(operations, operation) if operation.any? end else raise "invalid pacemaker_resource.operations input: #{operations_input}" end end operations end |
#munge_rule(rule, rule_number, title) ⇒ Hash
normalize a single location rule
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pacemaker/type.rb', line 154 def munge_rule(rule, rule_number, title) rule['id'] = "#{title}-rule-#{rule_number}" unless rule['id'] rule['boolean-op'] = 'or' unless rule['boolean-op'] rule['score'].gsub! 'inf', 'INFINITY' if rule['score'] if rule['expressions'] unless rule['expressions'].is_a? Array expressions_array = [] expressions_array << rule['expressions'] rule['expressions'] = expressions_array end expression_number = 0 rule['expressions'].each do |expression| unless expression['id'] expression['id'] = "#{title}-rule-#{rule_number}-expression-#{expression_number}" end expression_number += 1 end end rule end |
#primitive_base_name(primitive) ⇒ String
remove “-clone” or “-master” suffix and “role” suffix (:Master, :Slave) from a primitive’s name
179 180 181 182 |
# File 'lib/pacemaker/type.rb', line 179 def primitive_base_name(primitive) primitive = primitive.split(':').first primitive.gsub(/-clone$|-master$/, '') end |
#stringify_data(data) ⇒ Object
convert data structure’s keys and values to strings
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pacemaker/type.rb', line 24 def stringify_data(data) if data.is_a? Hash new_data = {} data.each do |key, value| new_data.store stringify_data(key), stringify_data(value) end data.clear data.merge! new_data elsif data.is_a? Array data.map! do |element| stringify_data element end elsif data.is_a? Set raise "unexpected Set data: #{data}" else data.to_s end end |