File: resource.rb

package info (click to toggle)
pcs 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,148 kB
  • sloc: python: 238,810; xml: 20,833; ruby: 13,203; makefile: 1,595; sh: 484
file content (193 lines) | stat: -rw-r--r-- 5,506 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require 'pathname'

def get_normalized_role(role)
  role_map = {
    'Promoted' => 'Master',
    'Unpromoted' => 'Slave',
  }
  return role_map.fetch(role, role)
end

def run_crm_mon_xml(auth_user)
  stdout, stderr, _ = run_cmd(auth_user, CRM_MON, '--help-all')
  new_format = (
    stdout.join("\n").include?('--output-as=') or
    stderr.join("\n").include?('--output-as=')
  )
  cmd = [CRM_MON, '--one-shot', '--inactive']
  if new_format
    cmd << '--output-as=xml'
  else
    cmd << '--as-xml'
  end
  stdout, stderr, retval = run_cmd(auth_user, *cmd)
  return stdout, stderr, retval
end


def getAllConstraints(constraints_dom)
  constraints = {}
  doc = constraints_dom

  doc.elements.each() { |e|
    if e.name == 'rsc_location' and e.has_elements?()
      rule_export = RuleToExpression.new()
      e.elements.each('rule') { |rule|
        rule_info = {
          'rule_string' => rule_export.export(rule),
        }
        if e.attributes["rsc-pattern"]
          rule_info["rsc-pattern"] = e.attributes["rsc-pattern"]
        else
          rule_info["rsc"] = e.attributes["rsc"]
        end
        rule.attributes.each { |name, value|
          rule_info[name] = value unless name == 'boolean-op'
        }
        if constraints[e.name]
          constraints[e.name] << rule_info
        else
          constraints[e.name] = [rule_info]
        end
      }
    elsif e.has_elements?()
      constraint_info = {}
      e.attributes.each { |name, value| constraint_info[name] = value }
      constraint_info['sets'] = []
      e.elements.each('resource_set') { |set_el|
        set_info = {}
        set_el.attributes.each { |name, value| set_info[name] = value }
        set_info['resources'] = []
        set_el.elements.each('resource_ref') { |res_el|
          set_info['resources'] << res_el.attributes['id']
        }
        constraint_info['sets'] << set_info
      }
      if constraints[e.name]
        constraints[e.name] << constraint_info
      else
        constraints[e.name] = [constraint_info]
      end
    else
      if constraints[e.name]
        constraints[e.name] << e.attributes
      else
        constraints[e.name] = [e.attributes]
      end
    end
  }
  return constraints
end

def getResourceAgents(auth_user)
  stdout, stderr, retval = run_cmd(
    auth_user, PCS, "--nodesc", "--", "resource", "list"
  )
  if retval != 0
    $logger.error("Error running 'pcs resource list --nodesc")
    $logger.error(stdout + stderr)
    return []
  end

  return stdout.map{|agent_name| agent_name.chomp}
end


def get_resource_agent_name_structure(agent_name)
  [
    # full_agent_name could be for example systemd:lvm2-pvscan@252:2
    # note that the second colon is not separator of provider and type
    /^(?<standard>systemd|service):(?<type>[^:@]+@.*)$/,
    /^(?<standard>[^:]+)(:(?<provider>[^:]+))?:(?<type>[^:]+)$/,
  ].each{|expression|
    match = expression.match(agent_name)
    if match
      provider = match.names.include?('provider') ? match[:provider] : nil
      return {
        :full_name => agent_name,
        :class => match[:standard],
        :provider => provider,
        :type => match[:type],
      }
    end
  }
  return nil
end


class RuleToExpression
  def export(rule)
    boolean_op = 'and'
    if rule.attributes.key?('boolean-op')
      boolean_op = rule.attributes['boolean-op']
    end
    part_list = []
    rule.elements.each { |element|
      case element.name
        when 'expression'
          part_list << exportExpression(element)
        when 'date_expression'
          part_list << exportDateExpression(element)
        when 'rule'
          part_list << "(#{export(element)})"
      end
    }
    return part_list.join(" #{boolean_op} ")
  end

  private

  def exportExpression(expression)
    part_list = []
    if expression.attributes.key?('value')
      part_list << expression.attributes['attribute']
      part_list << expression.attributes['operation']
      if expression.attributes.key?('type')
        part_list << expression.attributes['type']
      end
      value = expression.attributes['value']
      value = "\"#{value}\"" if value.include?(' ')
      part_list << value
    else
      part_list << expression.attributes['operation']
      part_list << expression.attributes['attribute']
    end
    return part_list.join(' ')
  end

  def exportDateExpression(expression)
    part_list = []
    operation = expression.attributes['operation']
    if operation == 'date_spec'
      part_list << 'date-spec'
      expression.elements.each('date_spec') { |date_spec|
        date_spec.attributes.each { |name, value|
          part_list << "#{name}=#{value}" if name != 'id'
        }
      }
    elsif operation == 'in_range'
      part_list << 'date' << operation
      if expression.attributes.key?('start')
        part_list << expression.attributes['start'] << 'to'
      end
      if expression.attributes.key?('end')
        part_list << expression.attributes['end']
      end
      expression.elements.each('duration') { |duration|
        part_list << 'duration'
        duration.attributes.each { |name, value|
          part_list << "#{name}=#{value}" if name != 'id'
        }
      }
    else
      part_list << 'date' << operation
      if expression.attributes.key?('start')
        part_list << expression.attributes['start']
      end
      if expression.attributes.key?('end')
        part_list << expression.attributes['end']
      end
    end
    return part_list.join(' ')
  end
end