-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource.rb
297 lines (273 loc) · 8.38 KB
/
resource.rb
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
require 'pp'
def getResourcesGroups(get_fence_devices = false, get_all_options = false)
stdout, stderror, retval = run_cmd("crm_mon", "--one-shot", "-r", "--as-xml")
if retval != 0
return [],[], retval
end
crm_output = stdout
doc = REXML::Document.new(crm_output.join("\n"))
resource_list = []
group_list = []
doc.elements.each('crm_mon/resources/resource') do |e|
if e.attributes["resource_agent"] && e.attributes["resource_agent"].index('stonith:') == 0
get_fence_devices && resource_list.push(Resource.new(e))
else
!get_fence_devices && resource_list.push(Resource.new(e))
end
end
doc.elements.each('crm_mon/resources/group/resource') do |e|
if e.attributes["resource_agent"] && e.attributes["resource_agent"].index('stonith:') == 0
get_fence_devices && resource_list.push(Resource.new(e,e.parent.attributes["id"]))
else
!get_fence_devices && resource_list.push(Resource.new(e,e.parent.attributes["id"]))
end
end
doc.elements.each('crm_mon/resources/clone/resource') do |e|
if e.attributes["resource_agent"] && e.attributes["resource_agent"].index('stonith:') == 0
get_fence_devices && resource_list.push(Resource.new(e))
else
!get_fence_devices && resource_list.push(Resource.new(e,nil,true))
end
end
doc.elements.each('crm_mon/resources/clone/group/resource') do |e|
if e.attributes["resource_agent"] && e.attributes["resource_agent"].index('stonith:') == 0
get_fence_devices && resource_list.push(Resource.new(e,e.parent.parent.attributes["id"] + "/" + e.parent.attributes["id"]))
else
!get_fence_devices && resource_list.push(Resource.new(e,e.parent.parent.attributes["id"] + "/" + e.parent.attributes["id"]),true)
end
end
doc.elements.each('crm_mon/resources/group') do |e|
group_list.push(e.attributes["id"])
end
resource_list.sort_by!{|a| (a.group ? "1" : "0").to_s + a.group.to_s + "-" + a.id}
if get_all_options
stdout, stderror, retval = run_cmd("cibadmin", "-Q")
cib_output = stdout
resources_attr_map = {}
begin
doc = REXML::Document.new(cib_output.join("\n"))
doc.elements.each('//primitive') do |r|
resources_attr_map[r.attributes["id"]] = {}
r.each_recursive do |ia|
if ia.node_type == :element and ia.name == "nvpair"
resources_attr_map[r.attributes["id"]][ia.attributes["name"]] = ia.attributes["value"]
end
end
end
resource_list.each {|r|
r.options = resources_attr_map[r.id]
}
rescue REXML::ParseException
$logger.info("ERROR: Parse Exception parsing cibadmin -Q")
end
end
[resource_list, group_list, 0]
end
def getResourceOptions(resource_id)
ret = {}
resource_options = `#{PCS} resource show #{resource_id}`
resource_options.each_line { |line|
keyval = line.strip.split(/: /,2)
ret[keyval[0]] = keyval[1]
}
return ret
end
def getAllConstraints()
stdout, stderror, retval = run_cmd("cibadmin", "-Q", "--xpath", "//constraints")
constraints = {}
if retval != 0
return {}
end
doc = REXML::Document.new(stdout.join("\n"))
constraints = {}
doc.elements.each('constraints/*') do |e|
if constraints[e.name]
constraints[e.name] << e.attributes
else
constraints[e.name] = [e.attributes]
end
end
return constraints
end
# Returns two arrays, one that lists resources that start before
# one that lists resources that start after
def getOrderingConstraints(resource_id)
ordering_constraints = `#{PCS} constraint order show all`
before = []
after = []
ordering_constraints.each_line { |line|
if line.start_with?("Ordering Constraints:")
next
end
line.strip!
sline = line.split(/ /,6)
if (sline[0] == resource_id)
after << [sline[-1].to_s[4..-2],sline[2]]
end
if (sline[2] == resource_id)
before << [sline[-1].to_s[4..-2],sline[0]]
end
}
return before,after
end
# Returns two arrays, one that lists nodes that can run resource
# one that lists nodes that cannot
def getLocationConstraints(resource_id)
location_constraints = `#{PCS} constraint location show all`
enabled_nodes = {}
disabled_nodes = {}
inResource = false
location_constraints.each_line { |line|
line.strip!
next if line.start_with?("Location Constraints:")
if line.start_with?("Resource:")
if line == "Resource: " + resource_id
inResource = true
else
inResource = false
end
next
end
next if !inResource
if line.start_with?("Enabled on:")
prev = nil
line.split(/: /,2)[1].split(/ /).each { |n|
if n.start_with?("(id:")
enabled_nodes[prev][0] = n[4..-2]
elsif n.start_with?("(")
enabled_nodes[prev][1] = n[1..-2]
else
enabled_nodes[n] = []
prev = n
end
}
end
if line.start_with?("Disabled on:")
prev = nil
line.split(/: /,2)[1].split(/ /).each { |n|
if n.start_with?("(id:")
disabled_nodes[prev][0] = n[4..-2]
elsif n.start_with?("(")
disabled_nodes[prev][1] = n[1..-2]
else
disabled_nodes[n] = []
prev = n
end
}
end
}
return enabled_nodes,disabled_nodes
end
# Returns two arrays, one that lists resources that should be together
# one that lists resources that should be apart
def getColocationConstraints(resource_id)
colocation_constraints = `#{PCS} constraint colocation show all`
together = []
apart = []
colocation_constraints.each_line { |line|
if line.start_with?("Colocation Constraints:")
next
end
line.strip!
sline = line.split(/ /,5)
score = []
score[0] = sline[4][4..-2]
score[1] = sline[3][1..-2]
if (sline[0] == resource_id)
if score[1] == "INFINITY" or (score[1] != "-INFINITY" and score[1].to_i >= 0)
together << [sline[2],score]
else
apart << [sline[2],score]
end
end
if (sline[2] == resource_id)
if score[1] == "INFINITY" or (score[1] != "-INFINITY" and score[1].to_i >= 0)
together << [sline[0],score]
else
apart << [sline[0],score]
end
end
}
return together,apart
end
def getResourceMetadata(resourcepath)
ENV['OCF_ROOT'] = OCF_ROOT
metadata = `#{resourcepath} meta-data`
doc = REXML::Document.new(metadata)
options_required = {}
options_optional = {}
doc.elements.each('resource-agent/parameters/parameter') { |param|
if param.attributes["required"] == "1"
options_required[param.attributes["name"]] = ""
else
options_optional[param.attributes["name"]] = ""
end
}
[options_required, options_optional]
end
def getResourceAgents(resource_agent = nil)
resource_agent_list = {}
agents = Dir.glob(HEARTBEAT_AGENTS_DIR + '*')
agents.each { |a|
ra = ResourceAgent.new
ra.name = "ocf::heartbeat:" + a.sub(/.*\//,"")
if resource_agent and a.sub(/.*\//,"") == resource_agent.sub(/.*:/,"")
required_options, optional_options = getResourceMetadata(a)
ra.required_options = required_options
ra.optional_options = optional_options
end
resource_agent_list[ra.name] = ra
}
resource_agent_list
end
class Resource
attr_accessor :id, :name, :type, :agent, :agentname, :role, :active,
:orphaned, :managed, :failed, :failure_ignored, :nodes, :location,
:options, :group, :clone, :stonith
def initialize(e, group = nil, clone = false)
@id = e.attributes["id"]
@agentname = e.attributes["resource_agent"]
@active = e.attributes["active"] == "true" ? true : false
@orphaned = e.attributes["orphaned"] == "true" ? true : false
@failed = e.attributes["failed"] == "true" ? true : false
@active = e.attributes["active"] == "true" ? true : false
@nodes = []
@group = group
@clone = clone
@stonith = false
e.elements.each do |n|
node = Node.new
node.name = n.attributes["name"]
node.id = n.attributes["id"]
@nodes.push(node)
end
if @nodes.length != 0
@location = @nodes[0].name
else
@location = ""
end
end
end
class ResourceAgent
attr_accessor :name, :resource_class, :required_options, :optional_options
def initialize(name=nil, required_options={}, optional_options={}, resource_class=nil)
@name = name
@required_options = {}
@optional_options = {}
@required_options = required_options
@optional_options = optional_options
@resource_class = nil
end
def provider
name.gsub(/::.*/,"")
end
def class
name.gsub(/.*::(.*):.*/,"$1")
end
def type
name.gsub(/.*:/,"")
end
def to_json(options = {})
JSON.generate({"type" => type})
end
end