Skip to content

Commit

Permalink
Merge pull request #192 from MalloZup/sle-12-sp4
Browse files Browse the repository at this point in the history
Backport pr to sle12sp4
  • Loading branch information
MalloZup authored Mar 31, 2020
2 parents 20cbdcc + 7820270 commit 71a31cc
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 68 deletions.
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion hawk/app/controllers/agents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def show
else
@name = params[:id]
end
@agent = Hash.from_xml(Util.safe_x("/usr/sbin/crm_resource", "--show-metadata", @name))
@agent = Util.get_metadata_hash(@name)

if @agent
respond_to do |format|
Expand Down
81 changes: 36 additions & 45 deletions hawk/app/models/crm_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,55 +194,46 @@ def mapping
"crmd",
"cib"
].each do |cmd|
[
"/usr/libexec/pacemaker/#{cmd}",
"/usr/lib64/pacemaker/#{cmd}",
"/usr/lib/pacemaker/#{cmd}",
"/usr/lib64/heartbeat/#{cmd}",
"/usr/lib/heartbeat/#{cmd}"
].each do |path|
next unless File.executable? path

REXML::Document.new(%x[#{path} metadata 2>/dev/null]).tap do |xml|
return unless xml.root

xml.elements.each("//parameter") do |param|
name = param.attributes["name"]
content = param.elements["content"]
shortdesc = param.elements["shortdesc[@lang=\"#{I18n.locale.to_s.gsub("-", "_")}\"]|shortdesc[@lang=\"en\"]"].text || ""
longdesc = param.elements["longdesc[@lang=\"#{I18n.locale.to_s.gsub("-", "_")}\"]|longdesc[@lang=\"en\"]"].text || ""

type = content.attributes["type"]
default = content.attributes["default"]

advanced = shortdesc.match(/advanced use only/i) || longdesc.match(/advanced use only/i)

crm_config[name] = {
type: content.attributes["type"],
readonly: false,
shortdesc: shortdesc,
longdesc: longdesc,
advanced: advanced ? true : false,
default: default
}

if type == "enum"
match = longdesc.match(/Allowed values:(.*)/i)

if match
values = match[1].split(",").map do |value|
value.strip
end.reject do |value|
value.empty?
end

crm_config[name][:values] = values unless values.empty?
path = "#{Rails.configuration.x.crm_daemon_dir}/#{cmd}"
next unless File.executable? path

REXML::Document.new(%x[#{path} metadata 2>/dev/null]).tap do |xml|
return unless xml.root

xml.elements.each("//parameter") do |param|
name = param.attributes["name"]
content = param.elements["content"]
shortdesc = param.elements["shortdesc[@lang=\"#{I18n.locale.to_s.gsub("-", "_")}\"]|shortdesc[@lang=\"en\"]"].text || ""
longdesc = param.elements["longdesc[@lang=\"#{I18n.locale.to_s.gsub("-", "_")}\"]|longdesc[@lang=\"en\"]"].text || ""

type = content.attributes["type"]
default = content.attributes["default"]

advanced = shortdesc.match(/advanced use only/i) || longdesc.match(/advanced use only/i)

crm_config[name] = {
type: content.attributes["type"],
readonly: false,
shortdesc: shortdesc,
longdesc: longdesc,
advanced: advanced ? true : false,
default: default
}

if type == "enum"
match = longdesc.match(/Allowed values:(.*)/i)

if match
values = match[1].split(",").map do |value|
value.strip
end.reject do |value|
value.empty?
end

crm_config[name][:values] = values unless values.empty?
end
end
end

break
end
end

Expand Down
24 changes: 23 additions & 1 deletion hawk/app/models/primitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,29 @@ def provider_with_template
end
end

alias_method_chain :provider, :template
alias_method :provider_without_template, :provider
alias_method :provider, :provider_with_template

validate :validate_params

def validate_params
required_params = []
res = Util.get_metadata_hash(agent_name)
param_res = res["resource_agent"]["parameters"]["parameter"]
if param_res
param_res.each do |items|
if items.key?("required") && items["required"] == "1"
required_params << items["name"]
end
end
end

params.each do |param, value|
if value.blank? && required_params.include?(param)
errors.add(:base, "In Parameters, #{param}'s value is blank!")
end
end
end

def type_with_template
if template.present?
Expand Down
19 changes: 19 additions & 0 deletions hawk/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ class Application < Rails::Application

config.x.hawk_is_sles = system("cat /etc/os-release | grep 'ID=.*sles' >/dev/null 2>&1")

def lookup_daemon_dir
[
"/usr/libexec/pacemaker",
"/usr/lib64/pacemaker",
"/usr/lib/pacemaker",
"/usr/lib64/heartbeat",
"/usr/lib/heartbeat"
].each do |dir|
[
"crmd",
"pacemaker-controld",
].each do |cmd|
return dir if File.executable? "#{dir}/#{cmd}"
end
end
"/usr/libexec/pacemaker"
end

::Sass::Script::Number.precision = [10, ::Sass::Script::Number.precision].max
config.x.crm_daemon_dir = lookup_daemon_dir
end
end
46 changes: 46 additions & 0 deletions hawk/lib/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,52 @@ def acl_version
end
module_function :acl_version

# Hash#dig from Ruby 2.3
# https://apidock.com/ruby/Hash/dig
def dig_hash(hash, *keys)
res = hash
keys.each do |key|
res = res[key]
return nil if res.nil?
end
res
end
module_function :dig_hash

def get_metadata_hash(agent)
sub_map = { 'type="string"' => 'type="text"',
'(type="boolean".*)default="(yes|1)"' => '\1default="true"',
'(type="boolean".*)default="(no|0)"' => '\1default="false"' }

xml = safe_x("/usr/sbin/crm_resource", "--show-metadata", agent)
sub_map.each { |k,v| xml.gsub!(/#{k}/i, v) }
res = Hash.from_xml(xml)

# For stonith agents, complete the instance attributes with the ones that
# are built into Pacemaker and are available for all stonith-class
# resources.
if agent.start_with?("stonith:") && res && (res_param = dig_hash(res, "resource_agent", "parameters", "parameter"))
# by default we assume we are using the latest stonith binary path (sles15sp1 and higher)
# if we are using lower version, we adapt the path to other binary old accordingly
stonith_bin = "#{Rails.configuration.x.crm_daemon_dir}/pacemaker-fenced"
unless File.exists? "#{Rails.configuration.x.crm_daemon_dir}/pacemaker-fenced"
stonith_bin = "/usr/lib/pacemaker/stonithd"
end
# execute binary and collect/append the paramters to the hash which will be visualised
stonith_xml = safe_x(stonith_bin, "metadata")
sub_map.each { |k,v| stonith_xml.gsub!(/#{k}/i, v) }
stonith = Hash.from_xml(stonith_xml)

if stonith && (stonith_param = dig_hash(stonith, "resource_agent", "parameters", "parameter"))
res_param.concat(stonith_param)
end
end

res
end
module_function :get_metadata_hash


# get text child of xml element - returns empty string if elem is nil or
# text child is empty. trims leading and trailing whitespace
def get_xml_text(elem)
Expand Down

0 comments on commit 71a31cc

Please sign in to comment.