Module: Pacemaker::ConstraintLocations

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

Overview

functions related to locations constraints main structure “constraint_locations”

Instance Method Summary collapse

Instance Method Details

#constraint_location_add(location_structure) ⇒ Object

add a location constraint

Parameters:

  • location_structure (Hash<String => String>)

    the location data structure



54
55
56
57
58
59
60
# File 'lib/pacemaker/xml/constraint_locations.rb', line 54

def constraint_location_add(location_structure)
  location_patch = xml_document
  location_element = xml_rsc_location location_structure
  raise "Could not create XML patch from location '#{location_structure.inspect}'!" unless location_element
  location_patch.add_element location_element
  wait_for_constraint_create xml_pretty_format(location_patch.root), location_structure['id']
end

#constraint_location_exists?(id) ⇒ TrueClass, FalseClass

check if locations constraint exists

Parameters:

  • id (String)

    the constraint id

Returns:

  • (TrueClass, FalseClass)


71
72
73
# File 'lib/pacemaker/xml/constraint_locations.rb', line 71

def constraint_location_exists?(id)
  constraint_locations.key? id
end

#constraint_location_remove(id) ⇒ Object

remove a location constraint

Parameters:

  • id (String)

    the constraint id



64
65
66
# File 'lib/pacemaker/xml/constraint_locations.rb', line 64

def constraint_location_remove(id)
  wait_for_constraint_remove "<rsc_location id='#{id}'/>\n", id
end

#constraint_locationsHash<String => Hash>

get location constraints and use mnemoization on the list

Returns:

  • (Hash<String => Hash>)


47
48
49
50
# File 'lib/pacemaker/xml/constraint_locations.rb', line 47

def constraint_locations
  return @locations_structure if @locations_structure
  @locations_structure = constraints 'rsc_location'
end

#service_location_add(primitive, node, score = 100) ⇒ Object

add a location constraint to enable a service on a node

Parameters:

  • primitive (String)

    the primitive’s name

  • node (String)

    the node’s name

  • score (Numeric, String) (defaults to: 100)

    score value



27
28
29
30
31
32
33
34
35
# File 'lib/pacemaker/xml/constraint_locations.rb', line 27

def service_location_add(primitive, node, score = 100)
  location_structure = {
      'id' => service_location_name(primitive, node),
      'node' => node,
      'rsc' => primitive,
      'score' => score,
  }
  constraint_location_add location_structure
end

#service_location_exists?(primitive, node) ⇒ true, false

check if service location exists for this primitive on this node

Parameters:

  • primitive (String)

    the primitive’s name

  • node (String)

    the node’s name

Returns:

  • (true, false)


18
19
20
21
# File 'lib/pacemaker/xml/constraint_locations.rb', line 18

def service_location_exists?(primitive, node)
  id = service_location_name primitive, node
  constraint_location_exists? id
end

#service_location_name(primitive, node) ⇒ String

construct the constraint unique name from primitive’s and node’s names

Parameters:

  • primitive (String)
  • node (String)

Returns:

  • (String)


10
11
12
# File 'lib/pacemaker/xml/constraint_locations.rb', line 10

def service_location_name(primitive, node)
  "#{primitive}-on-#{node}"
end

#service_location_remove(primitive, node) ⇒ Object

remove the service location on this node

Parameters:

  • primitive (String)

    the primitive’s name

  • node (String)

    the node’s name



40
41
42
43
# File 'lib/pacemaker/xml/constraint_locations.rb', line 40

def service_location_remove(primitive, node)
  id = service_location_name primitive, node
  constraint_location_remove id
end

#xml_rsc_location(data) ⇒ REXML::Element

generate rsc_location elements from data structure

Parameters:

  • data (Hash)

Returns:

  • (REXML::Element)


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
# File 'lib/pacemaker/xml/constraint_locations.rb', line 78

def xml_rsc_location(data)
  return unless data && data.is_a?(Hash)
  # create an element from the top level hash and skip 'rules' attribute
  # because if should be processed as children elements and useless 'type' attribute
  rsc_location_element = xml_element 'rsc_location', data, %w(rules type)

  # there are no rule elements
  return rsc_location_element unless data['rules'] && data['rules'].respond_to?(:each)

  # create a rule element with attributes and treat expressions as children elements
  sort_data(data['rules']).each do |rule|
    next unless rule.is_a? Hash
    rule_element = xml_element 'rule', rule, 'expressions'
    # add expression children elements to the rule element if the are present
    if rule['expressions'] && rule['expressions'].respond_to?(:each)
      sort_data(rule['expressions']).each do |expression|
        next unless expression.is_a? Hash
        expression_element = xml_element 'expression', expression
        rule_element.add_element expression_element
      end
    end
    rsc_location_element.add_element rule_element
  end
  rsc_location_element
end