Module: Pacemaker::Constraints

Included in:
Puppet::Provider::PacemakerXML, Serverspec::Type::PacemakerXML
Defined in:
lib/pacemaker/xml/constraints.rb

Overview

functions related to constraints (order, location, colocation) main structure “constraints” this structure is used by other specific location colocation and order submodules to form their data structures

Instance Method Summary collapse

Instance Method Details

#cib_section_constraint_rules(constraint) ⇒ Array<REXML::Element>

get all rule elements from the constraint element

Returns:

  • (Array<REXML::Element>)

    at /cib/configuration/constraints/*/rule



15
16
17
18
# File 'lib/pacemaker/xml/constraints.rb', line 15

def cib_section_constraint_rules(constraint)
  return unless constraint.is_a? REXML::Element
  REXML::XPath.match constraint, 'rule'
end

#cib_section_constraintsArray<REXML::Element>

get all ‘rsc_location’, ‘rsc_order’ and ‘rsc_colocation’ sections from CIB

Returns:

  • (Array<REXML::Element>)

    at /cib/configuration/constraints/*



9
10
11
# File 'lib/pacemaker/xml/constraints.rb', line 9

def cib_section_constraints
  REXML::XPath.match cib, '//constraints/*'
end

#constraint_exists?(id) ⇒ TrueClass, FalseClass

check if a constraint exists

Parameters:

  • id (String)

    the constraint id

Returns:

  • (TrueClass, FalseClass)


73
74
75
# File 'lib/pacemaker/xml/constraints.rb', line 73

def constraint_exists?(id)
  constraints.key? id
end

#constraints(type = nil) ⇒ Hash<String => Hash>

constraints found in the CIB filter them by the provided tag name

Parameters:

  • type (String) (defaults to: nil)

    filter this location type

Returns:

  • (Hash<String => Hash>)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pacemaker/xml/constraints.rb', line 57

def constraints(type = nil)
  constraints = {}
  cib_section_constraints.each do |constraint|
    constraint_structure = decode_constraint constraint
    next unless constraint_structure
    next unless constraint_structure['id']
    next if type && !(constraint_structure['type'] == type)
    constraint_structure.delete 'type'
    constraints.store constraint_structure['id'], constraint_structure
  end
  constraints
end

#decode_constraint(element) ⇒ Hash<String => String>

decode a single constraint element to the data structure

Parameters:

  • element (REXML::Element)

Returns:

  • (Hash<String => String>)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pacemaker/xml/constraints.rb', line 40

def decode_constraint(element)
  return unless element.is_a? REXML::Element
  return unless element.attributes['id']
  return unless element.name

  constraint_structure = attributes_to_hash element
  constraint_structure.store 'type', element.name

  rules = decode_constraint_rules element
  constraint_structure.store 'rules', rules if rules.any?
  constraint_structure
end

#decode_constraint_rules(element) ⇒ Hash<String => Hash>

parse constraint rule elements to the rule structure

Parameters:

  • element (REXML::Element)

Returns:

  • (Hash<String => Hash>)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pacemaker/xml/constraints.rb', line 23

def decode_constraint_rules(element)
  rules = cib_section_constraint_rules element
  return [] unless rules.any?
  rules_array = []
  rules.each do |rule|
    rule_structure = attributes_to_hash rule
    next unless rule_structure['id']
    rule_expressions = children_elements_to_array rule, 'expression'
    rule_structure.store 'expressions', rule_expressions if rule_expressions
    rules_array << rule_structure
  end
  rules_array.sort_by { |rule| rule['id'] }
end