Module: Pacemaker::Primitives

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

Overview

function related to the primitives configuration main structure “primitives”

Instance Method Summary collapse

Instance Method Details

#ban_primitive(primitive, node) ⇒ Object

ban this primitive

Parameters:

  • primitive (String)

    what primitive to ban

  • node (String)

    on which node this primitive should be banned



52
53
54
55
56
# File 'lib/pacemaker/xml/primitives.rb', line 52

def ban_primitive(primitive, node)
  options = ['--quiet', '--resource', primitive, '--node', node]
  options += ['--ban']
  retry_block { crm_resource_safe options }
end

#cib_section_primitivesArray<REXML::Element>

get all ‘primitive’ sections from CIB

Returns:

  • (Array<REXML::Element>)

    at /cib/configuration/resources/primitive



7
8
9
# File 'lib/pacemaker/xml/primitives.rb', line 7

def cib_section_primitives
  REXML::XPath.match cib, '//primitive'
end

#cleanup_primitive(primitive, node = nil) ⇒ Object

cleanup this primitive cleanups on every node if node is not given

Parameters:

  • primitive (String)

    what primitive to cleanup

  • node (String) (defaults to: nil)

    on which node to cleanup (optional)



91
92
93
94
95
96
# File 'lib/pacemaker/xml/primitives.rb', line 91

def cleanup_primitive(primitive, node = nil)
  options = ['--quiet', '--resource', primitive]
  options += ['--node', node] if node
  options += ['--cleanup']
  retry_block { crm_resource_safe options }
end

#disable_primitive(primitive) ⇒ Object Also known as: stop_primitive

disable this primitive

Parameters:

  • primitive (String)

    what primitive to disable



23
24
25
# File 'lib/pacemaker/xml/primitives.rb', line 23

def disable_primitive(primitive)
  set_primitive_meta_attribute primitive, 'target-role', 'Stopped'
end

#enable_primitive(primitive) ⇒ Object Also known as: start_primitive

enable this primitive

Parameters:

  • primitive (String)

    what primitive to enable



31
32
33
# File 'lib/pacemaker/xml/primitives.rb', line 31

def enable_primitive(primitive)
  set_primitive_meta_attribute primitive, 'target-role', 'Started'
end

#manage_primitive(primitive) ⇒ Object

manage this primitive

Parameters:

  • primitive (String)

    what primitive to manage



39
40
41
# File 'lib/pacemaker/xml/primitives.rb', line 39

def manage_primitive(primitive)
  set_primitive_meta_attribute primitive, 'is-managed', 'true'
end

#move_primitive(primitive, node) ⇒ Object

move this primitive

Parameters:

  • primitive (String)

    what primitive to un-move

  • node (String)

    to which node the primitive should be moved



72
73
74
75
76
# File 'lib/pacemaker/xml/primitives.rb', line 72

def move_primitive(primitive, node)
  options = ['--quiet', '--resource', primitive, '--node', node]
  options += ['--move']
  retry_block { crm_resource_safe options }
end

#parse_operations(operations_element) ⇒ Hash

parse the operations structure of a primitive

Parameters:

  • operations_element (REXML::Element)

Returns:

  • (Hash)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/pacemaker/xml/primitives.rb', line 167

def parse_operations(operations_element)
  return unless operations_element.is_a? REXML::Element
  operations = {}
  ops = operations_element.get_elements 'op'
  return operations unless ops.any?
  ops.each do |op|
    op_structure = attributes_to_hash op
    id = op_structure['id']
    next unless id
    instance_attributes = op.elements['instance_attributes']
    if instance_attributes
      instance_attributes_structure = children_elements_to_hash instance_attributes, 'name', 'nvpair'
      if instance_attributes_structure.key? 'OCF_CHECK_LEVEL'
        value = instance_attributes_structure.fetch('OCF_CHECK_LEVEL', {}).fetch('value', nil)
        op_structure['OCF_CHECK_LEVEL'] = value if value
      end
    end
    operations.store id, op_structure
  end
  operations
end

#primitive_class(primitive) ⇒ String?

return primitive class

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (String, nil)

    primitive class



198
199
200
201
# File 'lib/pacemaker/xml/primitives.rb', line 198

def primitive_class(primitive)
  return unless primitive_exists? primitive
  primitives[primitive]['class']
end

#primitive_complex_type(primitive) ⇒ Symbol

return primitive complex type or nil is the primitive is simple

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (Symbol)

    primitive complex type



223
224
225
226
# File 'lib/pacemaker/xml/primitives.rb', line 223

def primitive_complex_type(primitive)
  return unless primitive_is_complex? primitive
  primitives[primitive]['complex']['type'].to_sym
end

#primitive_exists?(primitive) ⇒ Boolean

check if primitive exists in the configuration

Parameters:

  • primitive

    primitive id or name

Returns:

  • (Boolean)


191
192
193
# File 'lib/pacemaker/xml/primitives.rb', line 191

def primitive_exists?(primitive)
  primitives.key? primitive
end

#primitive_full_name(primitive) ⇒ String

return the full name of the complex primitive or just a name for a simple primitive

Returns:

  • (String)

    primitive type



231
232
233
234
# File 'lib/pacemaker/xml/primitives.rb', line 231

def primitive_full_name(primitive)
  return unless primitive_exists? primitive
  primitives[primitive]['name']
end

#primitive_group(primitive) ⇒ String?

get the group name of the primitive returns nil if primitive is not in a group

Returns:

  • (String, nil)

    primitive group



268
269
270
271
# File 'lib/pacemaker/xml/primitives.rb', line 268

def primitive_group(primitive)
  return unless primitive_in_group? primitive
  primitives[primitive]['complex']['id']
end

#primitive_in_group?(primitive) ⇒ TrueClass, FalseClass

check if the primitive is assigned to a group

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


259
260
261
262
263
# File 'lib/pacemaker/xml/primitives.rb', line 259

def primitive_in_group?(primitive)
  return unless primitive_exists? primitive
  return false unless primitives[primitive].key? 'complex'
  primitives[primitive]['complex']['type'] == 'group'
end

#primitive_is_clone?(primitive) ⇒ TrueClass, FalseClass

check if primitive is clone

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


276
277
278
279
280
# File 'lib/pacemaker/xml/primitives.rb', line 276

def primitive_is_clone?(primitive)
  is_complex = primitive_is_complex? primitive
  return is_complex unless is_complex
  primitives[primitive]['complex']['type'] == 'clone'
end

#primitive_is_complex?(primitive) ⇒ TrueClass, FalseClass

check if primitive is clone or master primitives in groups are not considered complex despite having the complex structure

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


241
242
243
244
245
# File 'lib/pacemaker/xml/primitives.rb', line 241

def primitive_is_complex?(primitive)
  return unless primitive_exists? primitive
  return false unless primitives[primitive].key? 'complex'
  primitives[primitive]['complex']['type'] != 'group'
end

#primitive_is_managed?(primitive) ⇒ TrueClass, FalseClass

determine if primitive is managed

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


294
295
296
297
298
# File 'lib/pacemaker/xml/primitives.rb', line 294

def primitive_is_managed?(primitive)
  return unless primitive_exists? primitive
  is_managed = primitives.fetch(primitive).fetch('meta_attributes', {}).fetch('is-managed', {}).fetch('value', 'true')
  is_managed == 'true'
end

#primitive_is_master?(primitive) ⇒ TrueClass, FalseClass

check if primitive is master

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


285
286
287
288
289
# File 'lib/pacemaker/xml/primitives.rb', line 285

def primitive_is_master?(primitive)
  is_complex = primitive_is_complex? primitive
  return is_complex unless is_complex
  primitives[primitive]['complex']['type'] == 'master'
end

#primitive_is_simple?(primitive) ⇒ TrueClass, FalseClass

reverse of the complex? predicate but returns nil if resource doesn’t exist

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


251
252
253
254
# File 'lib/pacemaker/xml/primitives.rb', line 251

def primitive_is_simple?(primitive)
  return unless primitive_exists? primitive
  ! primitive_is_complex?(primitive)
end

#primitive_is_started?(primitive) ⇒ TrueClass, FalseClass

determine if primitive has target-state started

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (TrueClass, FalseClass)


303
304
305
306
307
# File 'lib/pacemaker/xml/primitives.rb', line 303

def primitive_is_started?(primitive)
  return unless primitive_exists? primitive
  target_role = primitives.fetch(primitive).fetch('meta_attributes', {}).fetch('target-role', {}).fetch('value', 'Started')
  target_role == 'Started'
end

#primitive_provider(primitive) ⇒ String?

return primitive provider

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (String, nil)

    primitive type



214
215
216
217
# File 'lib/pacemaker/xml/primitives.rb', line 214

def primitive_provider(primitive)
  return unless primitive_exists? primitive
  primitives[primitive]['provider']
end

#primitive_type(primitive) ⇒ String?

return primitive type

Parameters:

  • primitive (String)

    primitive id

Returns:

  • (String, nil)

    primitive type



206
207
208
209
# File 'lib/pacemaker/xml/primitives.rb', line 206

def primitive_type(primitive)
  return unless primitive_exists? primitive
  primitives[primitive]['type']
end

#primitivesHash<String => Hash>

get primitives configuration structure with primitives and their attributes

Returns:

  • (Hash<String => Hash>)


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
153
154
155
156
157
158
159
160
161
162
# File 'lib/pacemaker/xml/primitives.rb', line 116

def primitives
  return @primitives_structure if @primitives_structure
  @primitives_structure = {}
  cib_section_primitives.each do |primitive|
    id = primitive.attributes['id']
    next unless id
    primitive_structure = attributes_to_hash primitive
    primitive_structure.store 'name', id

    if read_complex_types.include?(primitive.parent.name) && primitive.parent.attributes['id']
      complex_structure = {
          'id' => primitive.parent.attributes['id'],
          'type' => primitive.parent.name
      }

      complex_meta_attributes = primitive.parent.elements['meta_attributes']
      if complex_meta_attributes
        complex_meta_attributes_structure = children_elements_to_hash complex_meta_attributes, 'name', 'nvpair'
        complex_structure.store 'meta_attributes', complex_meta_attributes_structure
      end

      primitive_structure.store 'name', complex_structure['id'] unless complex_structure['type'] == 'group'
      primitive_structure.store 'complex', complex_structure
    end

    instance_attributes = primitive.elements['instance_attributes']
    if instance_attributes
      instance_attributes_structure = children_elements_to_hash instance_attributes, 'name', 'nvpair'
      primitive_structure.store 'instance_attributes', instance_attributes_structure
    end

    meta_attributes = primitive.elements['meta_attributes']
    if meta_attributes
      meta_attributes_structure = children_elements_to_hash meta_attributes, 'name', 'nvpair'
      primitive_structure.store 'meta_attributes', meta_attributes_structure
    end

    operations = primitive.elements['operations']
    if operations
      operations_structure = parse_operations operations
      primitive_structure.store 'operations', operations_structure
    end

    @primitives_structure.store id, primitive_structure
  end
  @primitives_structure
end

#read_complex_typesArray<String>

the list of complex types the library should read from the CIB and parse their meta-data

Returns:

  • (Array<String>)


101
102
103
# File 'lib/pacemaker/xml/primitives.rb', line 101

def read_complex_types
  %w(clone master group)
end

#set_primitive_meta_attribute(primitive, attribute, value) ⇒ Object

sets the meta attribute of a primitive

Parameters:

  • primitive (String)

    primitive’s id

  • attribute (String)

    atttibute’s name

  • value (String)

    attribute’s value



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

def set_primitive_meta_attribute(primitive, attribute, value)
  options = ['--quiet', '--resource', primitive]
  options += ['--set-parameter', attribute, '--meta', '--parameter-value', value]
  retry_block { crm_resource_safe options }
end

#unban_primitive(primitive, node) ⇒ Object Also known as: clear_primitive

unban this primitive

Parameters:

  • primitive (String)

    what primitive to unban

  • node (String)

    on which node this primitive should be unbanned



61
62
63
64
65
# File 'lib/pacemaker/xml/primitives.rb', line 61

def unban_primitive(primitive, node)
  options = ['--quiet', '--resource', primitive, '--node', node]
  options += ['--clear']
  retry_block { crm_resource_safe options }
end

#unmanage_primitive(primitive) ⇒ Object

unmanage this primitive

Parameters:

  • primitive (String)

    what primitive to unmanage



45
46
47
# File 'lib/pacemaker/xml/primitives.rb', line 45

def unmanage_primitive(primitive)
  set_primitive_meta_attribute primitive, 'is-managed', 'false'
end

#unmove_primitive(primitive, node) ⇒ Object

un-move this primitive

Parameters:

  • primitive (String)

    what primitive to un-move

  • node (String)

    from which node the primitive should be un-moved



81
82
83
84
85
# File 'lib/pacemaker/xml/primitives.rb', line 81

def unmove_primitive(primitive, node)
  options = ['--quiet', '--resource', primitive, '--node', node]
  options += ['--un-move']
  retry_block { crm_resource_safe options }
end

#write_complex_typesArray<String>

the list of complex type the library should be able to create XML elements for

Returns:

  • (Array<String>)


108
109
110
# File 'lib/pacemaker/xml/primitives.rb', line 108

def write_complex_types
  %w(clone master)
end

#xml_primitive(data) ⇒ REXML::Element

generate a new XML element object and fill it with the primitive data from the provided primitive structure

Parameters:

  • data (Hash)

    primitive_structure

Returns:

  • (REXML::Element)


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/pacemaker/xml/primitives.rb', line 314

def xml_primitive(data)
  raise "Primitive data should be a hash! Got: #{data.inspect}" unless data.is_a? Hash
  primitive_skip_attributes = %w(name parent instance_attributes operations meta_attributes utilization)
  primitive_element = xml_element 'primitive', data, primitive_skip_attributes

  # instance attributes
  if data['instance_attributes'].respond_to?(:each) && data['instance_attributes'].any?
    instance_attributes_document = xml_document 'instance_attributes', primitive_element
    instance_attributes_document.add_attribute 'id', data['id'] + '-instance_attributes'
    sort_data(data['instance_attributes']).each do |instance_attribute|
      instance_attribute_element = xml_element 'nvpair', instance_attribute
      instance_attributes_document.add_element instance_attribute_element if instance_attribute_element
    end
  end

  # meta attributes
  if data['meta_attributes'].respond_to?(:each) && data['meta_attributes'].any?
    complex_meta_attributes_document = xml_document 'meta_attributes', primitive_element
    complex_meta_attributes_document.add_attribute 'id', data['id'] + '-meta_attributes'
    sort_data(data['meta_attributes']).each do |meta_attribute|
      meta_attribute_element = xml_element 'nvpair', meta_attribute
      complex_meta_attributes_document.add_element meta_attribute_element if meta_attribute_element
    end
  end

  # operations
  if data['operations'].respond_to?(:each) && data['operations'].any?
    operations_document = xml_document 'operations', primitive_element
    sort_data(data['operations']).each do |operation|
      operation_element = xml_element 'op', operation, %w(OCF_CHECK_LEVEL)
      if operation.key? 'OCF_CHECK_LEVEL'
        instance_attributes_document = xml_document 'instance_attributes', operation_element
        instance_attributes_id = operation['id'] + '-instance_attributes'
        instance_attributes_document.add_attribute 'id', instance_attributes_id
        ocf_check_level_id = instance_attributes_id + '-OCF_CHECK_LEVEL'
        ocf_check_level_structure = {
            'id' => ocf_check_level_id,
            'name' => 'OCF_CHECK_LEVEL',
            'value' => operation['OCF_CHECK_LEVEL'],
        }
        ocf_check_level_element = xml_element 'nvpair', ocf_check_level_structure
        instance_attributes_document.add_element ocf_check_level_element if ocf_check_level_element
      end
      operations_document.add_element operation_element if operation_element
    end
  end

  # complex structure
  if data['complex'].is_a?(Hash) && write_complex_types.include?(data['complex']['type'].to_s)
    skip_complex_attributes = 'type'
    complex_tag_name = data['complex']['type'].to_s
    complex_element = xml_element complex_tag_name, data['complex'], skip_complex_attributes

    # complex meta attributes
    if data['complex']['meta_attributes'].respond_to?(:each) && data['complex']['meta_attributes'].any?
      complex_meta_attributes_document = xml_document 'meta_attributes', complex_element
      complex_meta_attributes_document.add_attribute 'id', data['complex']['id'] + '-meta_attributes'
      sort_data(data['complex']['meta_attributes']).each do |meta_attribute|
        complex_meta_attribute_element = xml_element 'nvpair', meta_attribute
        complex_meta_attributes_document.add_element complex_meta_attribute_element if complex_meta_attribute_element
      end
    end

    complex_element.add_element primitive_element
    return complex_element
  end

  primitive_element
end