Skip to content

Commit

Permalink
Merge pull request #240 from MalloZup/safe_x_sle12
Browse files Browse the repository at this point in the history
Sle12: improve command execution
  • Loading branch information
MalloZup authored Feb 16, 2021
2 parents 828814f + cc15a61 commit 6939b42
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion hawk/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def login_from_cookie
user = cookies['hawk_remember_me_id']
return if user.nil?
# read from attrd
values, _, _ = Util.capture3("/usr/sbin/attrd_updater", "-R", "-Q","-A", "-n", "hawk_session_#{user}").scan(/value=\"(.*)\"/).flatten(1)
values = Util.safe_x("/usr/sbin/attrd_updater", "-R", "-Q","-A", "-n", "hawk_session_#{user}").scan(/value=\"(.*)\"/).flatten(1)
user if values.include? cookies['hawk_remember_me_key']
end

Expand Down
2 changes: 1 addition & 1 deletion hawk/app/controllers/wizards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def cib_writable
end

def cluster_online
%x[/usr/sbin/crm_mon -s >/dev/null 2>&1]
Util.safe_x('/usr/sbin/crm_mon', '-s', '>/dev/null', '2>&1')

if $?.exitstatus == Errno::ENOTCONN::Errno
respond_to do |format|
Expand Down
8 changes: 4 additions & 4 deletions hawk/app/models/cib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def initialize(id, user, use_file = false, stonithwarning = false)

# check stonith history
if can_fence
fence_history = %x[/usr/sbin/stonith_admin -H #{uname} 2>/dev/null].strip
fence_history = Util.safe_x('/usr/sbin/stonith_admin', '-H', "#{uname}", '2>/dev/null').strip
else
fence_history = ""
end
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def initialize(id, user, use_file = false, stonithwarning = false)
# TODO(should): Can we just use cib attribute dc-uuid? Or is that not viable
# during cluster bringup, given we're using cibadmin -l?
# Note that crmadmin will wait a long time if the cluster isn't up yet - cap it at 100ms
@dc = %x[/usr/sbin/crmadmin -t 100 -D 2>/dev/null].strip
@dc = Util.safe_x('/usr/sbin/crmadmin', '-t', '100', '-D', '2>/dev/null').strip
s = @dc.rindex(' ')
@dc.slice!(0, s + 1) if s
@dc = _('Unknown') if @dc.empty?
Expand Down Expand Up @@ -1095,7 +1095,7 @@ def initialize(id, user, use_file = false, stonithwarning = false)
resource_node = REXML::XPath.first(elem, "ancestor::primitive")
if booth_resource_id != resource_node.attributes["id"]
booth_resource_id = resource_node.attributes["id"]
ip = %x[crm_resource -r #{booth_resource_id} -g ip].strip
ip = Util.safe_x('crm_resource', '-r', "#{booth_resource_id}", '-g', 'ip').strip
next unless @booth[:sites].include?(ip)

if !@booth[:me]
Expand All @@ -1120,7 +1120,7 @@ def initialize(id, user, use_file = false, stonithwarning = false)
end

# try to get a bit more ticket info
%x[/usr/sbin/booth client list 2>/dev/null].split("\n").each do |line|
Util.safe_x('/usr/sbin/booth', 'client', 'list', '2>/dev/null').split("\n").each do |line|
t = nil
line.split(",").each do |pair|
m = pair.match(/(ticket):\s*(.*)/)
Expand Down
2 changes: 1 addition & 1 deletion hawk/app/models/crm_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def mapping
].each do |cmd|
path = "#{Rails.configuration.x.crm_daemon_dir}/#{cmd}"
next unless File.executable? path

#todo: this doesn't work with safe_x. research why.
REXML::Document.new(%x[#{path} metadata 2>/dev/null]).tap do |xml|
return unless xml.root

Expand Down
4 changes: 2 additions & 2 deletions hawk/app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def transitions(hb_report)
source = hb_report.path if File.directory?(hb_report.path)

pcmk_version = nil
m = `/usr/sbin/cibadmin -!`.match(/^Pacemaker ([^ ]+) \(Build: ([^)]+)\)/)
m = Util.safe_x('/usr/sbin/cibadmin', '-!').match(/^Pacemaker ([^ ]+) \(Build: ([^)]+)\)/)
pcmk_version = "#{m[1]}-#{m[2]}" if m

[].tap do |peinputs|
Expand Down Expand Up @@ -85,7 +85,7 @@ def transitions(hb_report)
end

def peinput_version(path)
nvpair = `CIB_file="#{path}" cibadmin -Q --xpath "/cib/configuration//crm_config//nvpair[@name='dc-version']" 2>/dev/null`
nvpair = Util.safe_x("CIB_file=\"#{path}\"", 'cibadmin', '-Q', '--xpath', "/cib/configuration//crm_config//nvpair[@name='dc-version']", '2>/dev/null')
m = nvpair.match(/value="([^"]+)"/)
return nil unless m
m[1]
Expand Down
2 changes: 1 addition & 1 deletion hawk/app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def unique_id!(other)
end

def rsc_constraints
outp = %x[/usr/sbin/crm_resource --resource "#{id}" -A 2>/dev/null]
outp = Util.safe_x('/usr/sbin/crm_resource', '--resource', "#{id}", '-A', '2>/dev/null')
info = {}
outp.each_line do |l|
l.strip!
Expand Down
2 changes: 1 addition & 1 deletion hawk/app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def cib_type
def options
Rails.cache.fetch(:crm_ra_classes, expires_in: 2.hours) do
{}.tap do |result|
clazzes = %x[/usr/sbin/crm ra classes].split(/\n/)
clazzes = Util.safe_x('/usr/sbin/crm', 'ra', 'classes').split(/\n/)
clazzes.delete("heartbeat") unless File.directory?("/etc/ha.d/resource.d")

clazzes.each do |clazz|
Expand Down
16 changes: 8 additions & 8 deletions hawk/lib/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,31 +296,30 @@ def has_feature?(feature)
case feature
when :crm_history
Rails.cache.fetch(:has_crm_history) {
%x[echo quit | /usr/sbin/crm history 2>&1]
Util.safe_x('echo', 'quit', '|', '/usr/sbin/crm history', '2>&1')
$?.exitstatus == 0
}
when :rsc_ticket
Rails.cache.fetch(:has_rsc_ticket) {
%x[/usr/sbin/crm configure help rsc_ticket >/dev/null 2>&1]
Util.safe_x('/usr/sbin/crm', 'configure', 'help', 'rsc_ticket', '>/dev/null', '2>&1')
$?.exitstatus == 0
}
when :rsc_template
Rails.cache.fetch(:has_rsc_template) {
%x[/usr/sbin/crm configure help rsc_template >/dev/null 2>&1]
Util.safe_x('/usr/sbin/crm', 'configure', 'help', 'rsc_template', '>/dev/null', '2>&1')
$?.exitstatus == 0
}
when :sim_ticket
Rails.cache.fetch(:has_sim_ticket) {
%x[/usr/sbin/crm_simulate -h 2>&1].include?("--ticket-grant")
Util.safe_x('/usr/sbin/crm_simulate', '-h', '2>&1').include?("--ticket-grant")
}
when :acl_support
Rails.cache.fetch(:has_acl_support) {
%x[/usr/sbin/cibadmin -!].split(/\s+/).include?("acls")
Util.safe_x('/usr/sbin/cibadmin', '-!').split(/\s+/).include?("acls")
}
when :tags
Rails.cache.fetch(:has_tags) {
# TODO: fix this
%x[/usr/sbin/cibadmin -t 5 -Ql -A /cib/configuration/tags >/dev/null 2>&1]
Util.safe_x('/usr/sbin/cibadmin', '-t', '5', '-Ql', '-A', '/cib/configuration/tags', '>/dev/null/', '2>&1')
$?.exitstatus == 0
}
else
Expand All @@ -342,7 +341,7 @@ def acl_enabled?

def acl_version
Rails.cache.fetch(:get_acl_version) do
m = `/usr/sbin/cibadmin -t 5 -Ql --xpath /cib[@validate-with]`.shellescape.lines.first.to_s.match(/validate-with=\"pacemaker-([0-9.]+)\"/)
m = Util.safe_x('/usr/sbin/cibadmin', '-t', '5', '-Ql', '--xpath', "/cib[@validate-with").shellescape.lines.first.to_s.match(/validate-with=\"pacemaker-([0-9.]+)\"/)
return m.captures[0].to_f if m
2.0
end
Expand Down Expand Up @@ -401,4 +400,5 @@ def get_xml_text(elem)
elem ? (elem.text.strip || '') : ''
end
module_function :get_xml_text

end

0 comments on commit 6939b42

Please sign in to comment.