-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpcsd.rb
603 lines (535 loc) · 17.1 KB
/
pcsd.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
require 'sinatra'
require 'sinatra/reloader' if development? #require 'rack/ssl'
require 'open3'
require 'rexml/document'
require 'resource.rb'
require 'remote.rb'
require 'fenceagent.rb'
require 'cluster.rb'
require 'config.rb'
require 'pcs.rb'
require 'auth.rb'
require 'webrick'
require 'pp'
require 'webrick/https'
require 'openssl'
require 'logger'
use Rack::CommonLogger
#use Rack::SSL
also_reload 'resource.rb'
also_reload 'remote.rb'
also_reload 'fenceagent.rb'
also_reload 'cluster.rb'
also_reload 'config.rb'
also_reload 'pcs.rb'
also_reload 'auth.rb'
enable :sessions
before do
@@cluster_name = get_cluster_version()
puts "SESSION:"
pp session
puts "COOKIES:"
pp request.cookies
if request.path != '/login' and not request.path == "/logout" and not request.path == '/remote/auth'
protected!
end
end
configure do
OCF_ROOT = "/usr/lib/ocf"
HEARTBEAT_AGENTS_DIR = "/usr/lib/ocf/resource.d/heartbeat/"
PENGINE = "/usr/libexec/pacemaker/pengine"
if Dir.pwd == "/var/lib/pcsd"
PCS = "/sbin/pcs"
else
PCS = "../pcs/pcs/pcs"
end
CRM_ATTRIBUTE = "/usr/sbin/crm_attribute"
COROSYNC_CONF = "/etc/corosync/corosync.conf"
SETTINGS_FILE = "pcs_settings.conf"
$user_pass_file = "pcs_users.conf"
$logger = Logger.new('/var/lib/pcsd/pcsd.log', 'weekly')
$logger.level = Logger::INFO
$stdout.reopen('/var/lib/pcsd/pcsd-debug.log', 'a')
$stdout.sync = true
$stderr.reopen($stdout)
# $stderr.reopen('/var/lib/pcsd/pcsd-stderror', 'a')
end
set :logging, true
set :run, false
if not defined? @@cur_node_name
@@cur_node_name = `hostname`.chomp
end
helpers do
def protected!
if not PCSAuth.isLoggedIn(session, request.cookies)
if request.path.start_with?('/remote')
puts "ERROR: Request without authentication"
halt [401, '{"notauthorized":"true"}']
else
session[:pre_login_path] = request.path
redirect '/login'
end
end
end
def setup
@nodes_online, @nodes_offline = get_nodes()
@nodes = {}
@nodes_online.each do |i|
@nodes[i] = Node.new(i, i, i, true)
end
@nodes_offline.each do |i|
@nodes[i] = Node.new(i, i, i, false)
end
if @nodes_online.length == 0
@pcs_node_offline = true
end
if params[:node]
@cur_node = @nodes[params[:node]]
if not @cur_node
@cur_node = @nodes.values[0]
end
else
@cur_node = @nodes.values[0]
end
if @nodes.length != 0
@loc_dep_allow, @loc_dep_disallow = getLocationDeps(@cur_node)
end
@nodes = @nodes_online.concat(@nodes_offline)
end
def getParamLine(params)
param_line = ""
params.each { |param, val|
if param.start_with?("_res_paramne_") or (param.start_with?("_res_paramempty_") and val != "")
myparam = param.sub(/^_res_paramne_/,"").sub(/^_res_paramempty_/,"")
param_line += " #{myparam}=#{val}"
end
}
param_line
end
end
get('/login'){ erb :login, :layout => :main }
get '/logout' do
session.clear
erb :login, :layout => :main
end
post '/login' do
if PCSAuth.validUser(params['username'],params['password'])
session["username"] = params['username']
if session["pre_login_path"]
plp = session["pre_login_path"]
session.delete("pre_login_path")
redirect plp
else
redirect '/'
end
else
redirect '/login?badlogin=1'
end
end
post '/fencerm' do
params.each { |k,v|
if k.index("resid-") == 0
run_cmd(PCS, "resource", "delete", k.gsub("resid-",""))
end
}
redirect "/fencedevices/"
end
get '/configure/?:page?' do
@config_options = getConfigOptions(params[:page])
@configuremenuclass = "class=\"active\""
erb :configure, :layout => :main
end
get '/fencedevices2/?:fencedevice?' do
@resources, @groups = getResourcesGroups(true)
pp @resources
if @resources.length == 0
@cur_resource = nil
@resource_agents = getFenceAgents()
else
@cur_resource = @resources[0]
if params[:fencedevice]
@resources.each do |fd|
if fd.id == params[:fencedevice]
@cur_resource = fd
break
end
end
end
@cur_resource.options = getResourceOptions(@cur_resource.id)
@resource_agents = getFenceAgents(@cur_resource.agentname)
end
erb :fencedevices, :layout => :main
end
['/resources2/?:resource?', '/resource_list/?:resource?'].each do |path|
get path do
@load_data = true
@resources, @groups = getResourcesGroups
@resourcemenuclass = "class=\"active\""
if @resources.length == 0
@cur_resource = nil
@resource_agents = getResourceAgents()
else
@cur_resource = @resources[0]
@cur_resource.options = getResourceOptions(@cur_resource.id)
if params[:resource]
@resources.each do |r|
if r.id == params[:resource]
@cur_resource = r
@cur_resource.options = getResourceOptions(r.id)
break
end
end
end
@resource_agents = getResourceAgents(@cur_resource.agentname)
@ord_dep_before, @ord_dep_after = getOrderingConstraints(@cur_resource.id)
@colo_dep_together, @colo_dep_apart = getColocationConstraints(@cur_resource.id)
@enabled_nodes, @disabled_nodes = getLocationConstraints(@cur_resource.id)
end
@nodes_online, @nodes_offline = get_nodes
if path.start_with? '/resource_list'
erb :_resource_list
else
erb :resource, :layout => :main
end
end
end
get '/resources/metadata/:resourcename/?:new?' do
return ""
@resource = ResourceAgent.new(params[:resourcename])
@resource.required_options, @resource.optional_options = getResourceMetadata(HEARTBEAT_AGENTS_DIR + params[:resourcename])
@new_resource = params[:new]
@resources, @groups = getResourcesGroups
erb :resourceagentform
end
get '/fencedevices/metadata/:fencedevicename/?:new?' do
return ""
@fenceagent = FenceAgent.new(params[:fencedevicename])
@fenceagent.required_options, @fenceagent.optional_options = getFenceAgentMetadata(params[:fencedevicename])
@new_fenceagent = params[:new]
erb :fenceagentform
end
get '/nodes/?:node?' do
setup()
@load_data = true
# @nodemenuclass = "class=\"active\""
@resources, @groups = getResourcesGroups
# @resources_running = []
# @resources.each { |r|
# @cur_node && r.nodes && r.nodes.each {|n|
# if n.name == @cur_node.id
# @resources_running << r
# end
# }
# }
@resource_agents = getResourceAgents()
@stonith_agents = getFenceAgents()
# @nodes = @nodes.sort_by{|k,v|k}
erb :nodes, :layout => :main
end
get '/manage/?' do
@manage = true
pcs_config = PCSConfig.new
@clusters = pcs_config.clusters
erb :manage, :layout => :main
end
get '/managec/:cluster/main' do
@@cluster_name = params[:cluster]
# @resources, @groups = getResourcesGroups
@load_data = true
@resources = []
@groups = []
@resource_agents = get_resource_agents_avail()
@stonith_agents = get_stonith_agents_avail()
puts "RA"
puts @resource_agents
puts "ST"
puts @stonith_agents
@nodes = get_cluster_nodes(params[:cluster])
@config_options = getConfigOptions2()
erb :nodes, :layout => :main
end
get '/managec/:cluster/status_all' do
status_all("",get_cluster_nodes(params[:cluster]))
end
get '/managec/:cluster/?*' do
puts "Lambda A GET"
pp params[:splat]
pp params
puts "Lambda B"
raw_data = request.env["rack.input"].read
p "Raw Data"
pp raw_data
if params[:cluster]
send_cluster_request_with_token(params[:cluster], "/" + params[:splat].join("/"), false, params, false, raw_data)
end
end
post '/managec/:cluster/?*' do
puts "Lambda A POST"
pp params[:splat]
pp params
puts "Lambda B"
raw_data = request.env["rack.input"].read
p "Raw Data"
pp raw_data
if params[:cluster]
send_cluster_request_with_token(params[:cluster], "/" + params[:splat].join("/"), true, params, false, raw_data)
end
end
get '/manage/:node/?*' do
if params[:node]
return send_request_with_token(params[:node], params[:splat].join("/"), false, {}, false)
end
end
post '/manage/existingcluster' do
pcs_config = PCSConfig.new
node = params['node-name']
status = JSON.parse(send_request_with_token(node, 'status'))
if status.has_key?("corosync_offline") and
status.has_key?("corosync_online") then
nodes = status["corosync_offline"] + status["corosync_online"]
pcs_config.clusters << Cluster.new(status["cluster_name"], nodes)
pcs_config.save
redirect '/manage'
else
redirect '/manage/?error=notauthorized'
end
end
post '/manage/newcluster' do
pcs_config = PCSConfig.new
@manage = true
@cluster_name = params[:clustername]
@nodes = []
params.each {|k,v|
if k.start_with?("node-") and v != ""
@nodes << v
end
}
pcs_config.clusters << Cluster.new(@cluster_name, @nodes)
pcs_config.save
run_cmd(PCS, "cluster", "setup", "--start", @cluster_name, *@nodes)
redirect '/manage'
end
post '/manage/removecluster' do
pcs_config = PCSConfig.new
params.each { |k,v|
if k.start_with?("clusterid-")
pcs_config.remove_cluster(k.sub("clusterid-",""))
end
}
pcs_config.save
redirect '/manage'
end
post '/resource_cmd/rm_constraint' do
if params[:constraint_id]
remove_constraint(params[:constraint_id])
end
end
get '/' do
print "Redirecting...\n"
call(env.merge("PATH_INFO" => '/manage'))
end
get '/remote/?:command?' do
return remote(params,request)
end
post '/remote/?:command?' do
return remote(params,request)
end
get '*' do
print params[:splat]
print "2Redirecting...\n"
return "Bad URL"
call(env.merge("PATH_INFO" => '/nodes'))
end
def getLocationDeps(cur_node)
stdin, stdout, stderror = Open3.popen3("#{PCS} constraint location show nodes #{cur_node.id}")
out = stdout.readlines
deps_allow = []
deps_disallow = []
allowed = false
disallowed = false
out.each {|line|
line = line.strip
next if line == "Location Constraints:" or line.match(/^Node:/)
if line == "Allowed to run:"
allowed = true
next
elsif line == "Not allowed to run:"
disallowed = true
next
end
if disallowed == true
deps_disallow << line.sub(/ .*/,"")
elsif allowed == true
deps_allow << line.sub(/ .*/,"")
end
}
[deps_allow, deps_disallow]
end
def getConfigOptions2()
config_options = {}
general_page = []
general_page << ConfigOption.new("Cluster Delay Time", "cluster-delay", "int", 4, "Seconds")
# general_page << ConfigOption.new("Batch Limit", "cdt", "int", 4)
# general_page << ConfigOption.new("Default Action Timeout", "cdt", "int", 4, "Seconds")
# general_page << ConfigOption.new("During timeout should cluster stop all active resources", "res_stop", "radio", "4", "", ["Yes","No"])
# general_page << ConfigOption.new("PE Error Storage", "res_stop", "radio", "4", "", ["Yes","No"])
# general_page << ConfigOption.new("PE Warning Storage", "res_stop", "radio", "4", "", ["Yes","No"])
# general_page << ConfigOption.new("PE Input Storage", "res_stop", "radio", "4", "", ["Yes","No"])
config_options["general"] = general_page
pacemaker_page = []
pacemaker_page << ConfigOption.new("Batch Limit", "batch-limit", "int", 4, "jobs")
pacemaker_page << ConfigOption.new("No Quorum Policy", "no-quorum-policy", "dropdown","" ,"", {"ignore" => "Ignore","freeze" => "Freeze", "stop" => "Stop", "suicide" => "Suicide"})
pacemaker_page << ConfigOption.new("Symmetric", "symmetric-cluster", "check")
pacemaker_page << ConfigOption.new("Stonith Enabled", "stonith-enabled", "check")
pacemaker_page << ConfigOption.new("Stonith Action", "stonith-action", "dropdown","" ,"", {"reboot" => "Reboot","poweroff" => "Poweroff"})
pacemaker_page << ConfigOption.new("Cluster Delay", "cluster-delay", "int", 4)
pacemaker_page << ConfigOption.new("Stop Orphan Resources", "stop-orphan-resources", "check")
pacemaker_page << ConfigOption.new("Stop Orphan Actions", "stop-orphan-actions", "check")
pacemaker_page << ConfigOption.new("Start Failure is Fatal", "start-failure-is-fatal", "check")
pacemaker_page << ConfigOption.new("PE Error Storage", "pe-error-series-max", "int", "4")
pacemaker_page << ConfigOption.new("PE Warning Storage", "pe-warn-series-max", "int", "4")
pacemaker_page << ConfigOption.new("PE Input Storage", "pe-input-series-max", "int", "4")
config_options["pacemaker"] = pacemaker_page
allconfigoptions = []
config_options.each { |i,k| k.each { |j| allconfigoptions << j } }
ConfigOption.getDefaultValues(allconfigoptions)
return config_options
end
def getConfigOptions(page="general")
config_options = []
case page
when "general", nil
cg1 = []
cg1 << ConfigOption.new("Cluster Delay Time", "cdt", "int", 4, "Seconds")
cg1 << ConfigOption.new("Batch Limit", "cdt", "int", 4)
cg1 << ConfigOption.new("Default Action Timeout", "cdt", "int", 4, "Seconds")
cg2 = []
cg2 << ConfigOption.new("During timeout should cluster stop all active resources", "res_stop", "radio", "4", "", ["Yes","No"])
cg3 = []
cg3 << ConfigOption.new("PE Error Storage", "res_stop", "radio", "4", "", ["Yes","No"])
cg3 << ConfigOption.new("PE Warning Storage", "res_stop", "radio", "4", "", ["Yes","No"])
cg3 << ConfigOption.new("PE Input Storage", "res_stop", "radio", "4", "", ["Yes","No"])
config_options << cg1
config_options << cg2
config_options << cg3
when "pacemaker"
cg1 = []
cg1 << ConfigOption.new("Batch Limit", "batch-limit", "int", 4, "jobs")
cg1 << ConfigOption.new("No Quorum Policy", "no-quorum-policy", "dropdown","" ,"", {"ignore" => "Ignore","freeze" => "Freeze", "stop" => "Stop", "suicide" => "Suicide"})
cg1 << ConfigOption.new("Symmetric", "symmetric-cluster", "check")
cg2 = []
cg2 << ConfigOption.new("Stonith Enabled", "stonith-enabled", "check")
cg2 << ConfigOption.new("Stonith Action", "stonith-action", "dropdown","" ,"", {"reboot" => "Reboot","poweroff" => "Poweroff"})
cg3 = []
cg3 << ConfigOption.new("Cluster Delay", "cluster-delay", "int", 4)
cg3 << ConfigOption.new("Stop Orphan Resources", "stop-orphan-resources", "check")
cg3 << ConfigOption.new("Stop Orphan Actions", "stop-orphan-actions", "check")
cg3 << ConfigOption.new("Start Failure is Fatal", "start-failure-is-fatal", "check")
cg3 << ConfigOption.new("PE Error Storage", "pe-error-series-max", "int", "4")
cg3 << ConfigOption.new("PE Warning Storage", "pe-warn-series-max", "int", "4")
cg3 << ConfigOption.new("PE Input Storage", "pe-input-series-max", "int", "4")
config_options << cg1
config_options << cg2
config_options << cg3
end
allconfigoptions = []
config_options.each { |i| i.each { |j| allconfigoptions << j } }
ConfigOption.getDefaultValues(allconfigoptions)
return config_options
end
class Node
attr_accessor :active, :id, :name, :hostname
def initialize(id=nil, name=nil, hostname=nil, active=nil)
@id, @name, @hostname, @active = id, name, hostname, active
end
end
class ConfigOption
attr_accessor :name, :configname, :type, :size, :units, :options, :default
def initialize(name, configname, type="str", size = 10, units = "", options = [])
@name = name
@configname = configname
@type = type
@size = size
@units = units
@options = options
end
def value
@@cache_value ||= {}
@@cache_value = {}
if @@cache_value[configname] == nil
resource_options = `#{CRM_ATTRIBUTE} --get-value -n #{configname} 2>&1`
resource_value = resource_options.sub(/.*value=/m,"").strip
if resource_value == "(null)"
@@cache_value[configname] = default
else
@@cache_value[configname] = resource_value
end
else
print "#{configname} is defined: #{@@cache_value[configname]}...\n"
end
return @@cache_value[configname]
end
def self.getDefaultValues(cos)
metadata = `#{PENGINE} metadata`
doc = REXML::Document.new(metadata)
puts "SET DEFAULTS"
cos.each { |co|
doc.elements.each("resource-agent/parameters/parameter[@name='#{co.configname}']/content") { |e|
co.default = e.attributes["default"]
break
}
}
end
def checked(option)
case type
when "radio"
val = value
if option == "Yes"
if val == "true"
return "checked"
end
else
if val == "false"
return "checked"
end
end
when "check"
if value == "true" || value == "on"
return "checked"
else
return ""
end
when "dropdown"
if value == option
return "selected"
end
end
end
def html
paramname = "config[#{configname}]"
hidden_paramname = "hidden[#{configname}]"
case type
when "int"
return "<input name=\"#{paramname}\" value=\"#{value}\" type=text size=#{size}>"
when "str"
return "<input name=\"#{paramname}\" value=\"#{value}\" type=text size=#{size}>"
when "radio"
ret = ""
options.each {|option|
ret += "<input type=radio #{checked(option)} name=\"#{paramname}\" value=\"#{option}\">#{option}"
}
return ret
when "check"
ret = "<input type=checkbox name=\"#{paramname}\" " + checked(nil) + ">"
ret += "<input type=hidden name=\"#{hidden_paramname}\" value=\"off\">"
return ret
when "dropdown"
ret = "<select name=\"#{paramname}\">"
options.each {|key, option|
ret += "<option #{checked(key)} value=\"#{key}\">#{option}</option>"
}
ret += "<select"
return ret
end
end
end