Skip to content

Commit

Permalink
Remove MiqUtil.runcmd in favor of AwesomeSpawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Jan 28, 2021
1 parent e6c0391 commit 729d5c0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 105 deletions.
27 changes: 15 additions & 12 deletions lib/gems/pending/util/miq-ipmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# OpenIPMI-tools.x86_64
# freeipmi.x86_64

require 'util/runcmd'
require 'awesome_spawn'
require 'util/miq-extensions'

class MiqIPMI
Expand Down Expand Up @@ -185,10 +185,12 @@ def run_command(ipmi_cmd, *args)
command_args[nil] = args unless args.empty?

begin
return MiqUtil.runcmd("ipmitool", :params => command_args)
rescue => err
return err.to_s if continue_on_error == true && $?.exitstatus == 1
raise "Command:<#{command_line}> exited with status:<#{$?.exitstatus}>\nCommand output:\n#{err}"
AwesomeSpawn.run!("ipmitool", :params => command_args, :combined_output => true).output
rescue AwesomeSpawn::CommandResultError => err
result = err.result
return err.to_s if continue_on_error == true && result.exit_status == 1

raise "Command:<#{result.command_line}> exited with status:<#{result.exit_status}>\nCommand output:\n#{err}"
end
end

Expand Down Expand Up @@ -219,12 +221,13 @@ def self.is_1_5_available?(ip_address)
end

def self.is_available_check(ip_address, version = nil)
if version.nil?
MiqUtil.runcmd("ipmiping", :params => [[ip_address], [:c, 1]])
else
MiqUtil.runcmd("ipmiping", :params => [[ip_address], [:r, version], [:c, 1]])
end
rescue
false
params =
if version.nil?
[[ip_address], [:c, 1]]
else
[[ip_address], [:r, version], [:c, 1]]
end

AwesomeSpawn.run("ipmiping", :params => params).success?
end
end
4 changes: 2 additions & 2 deletions lib/gems/pending/util/miq-process.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# encoding: US-ASCII

require 'awesome_spawn'
require 'sys-uname'
require 'sys/proctable'
require 'util/runcmd'
require 'util/miq-system'

class MiqProcess
Expand Down Expand Up @@ -180,7 +180,7 @@ def self.process_list_wmi(wmi = nil, pid = nil)

def self.process_list_linux(cmd_str, skip_header = false)
pl, i = {}, 0
rc = MiqUtil.runcmd(cmd_str)
rc = AwesomeSpawn.run!(cmd_str, :combined_output => true).output
rc.each_line do |ps_str|
i += 1
next if i == 1 && skip_header == true
Expand Down
2 changes: 1 addition & 1 deletion lib/gems/pending/util/mount/miq_generic_mount_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
require 'logger'
require 'sys-uname'
require 'uri'
require 'awesome_spawn'

require 'util/miq-exception'
require 'util/miq_file_storage'
require 'util/runcmd'

class MiqGenericMountSession < MiqFileStorage::Interface
require 'util/mount/miq_local_mount_session'
Expand Down
26 changes: 15 additions & 11 deletions lib/gems/pending/util/postgres_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def self.logical_volume_path
end

def self.database_size(opts)
result = runcmd("psql", opts, :command => "SELECT pg_database_size('#{opts[:dbname]}');")
result = run_command("psql", opts, :command => "SELECT pg_database_size('#{opts[:dbname]}');")
result.match(/^\s+([0-9]+)\n/)[1].to_i
end

Expand Down Expand Up @@ -140,7 +140,7 @@ def self.backup_pg_dump(opts)
args = combine_command_args(opts, :format => "c", :file => opts[:local_file], nil => dbname)
args = handle_multi_value_pg_dump_args!(opts, args)

runcmd_with_logging("pg_dump", opts, args)
run_command_with_logging("pg_dump", opts, args)
opts[:local_file]
end

Expand Down Expand Up @@ -177,9 +177,8 @@ def self.backup_pg_compress(opts)
def self.recreate_db(opts)
dbname = opts[:dbname]
opts = opts.merge(:dbname => 'postgres')
runcmd("psql", opts, :command => "DROP DATABASE IF EXISTS #{dbname}")
runcmd("psql", opts,
:command => "CREATE DATABASE #{dbname} WITH OWNER = #{opts[:username] || 'root'} ENCODING = 'UTF8'")
run_command("psql", opts, :command => "DROP DATABASE IF EXISTS #{dbname}")
run_command("psql", opts, :command => "CREATE DATABASE #{dbname} WITH OWNER = #{opts[:username] || 'root'} ENCODING = 'UTF8'")
end

def self.restore_pg_dump(opts)
Expand All @@ -199,7 +198,7 @@ def self.restore_pg_dump(opts)
handle_error(cmd, process_status.exitstatus, error_path)
else
args[nil] = opts[:local_file]
runcmd("pg_restore", opts, args)
run_command("pg_restore", opts, args)
end
opts[:local_file]
end
Expand Down Expand Up @@ -245,24 +244,29 @@ def self.vacuum(opts)
args[:full] = nil if opts[:full]
args[:verbose] = nil if opts[:verbose]
args[:table] = opts[:table] if opts[:table]
runcmd("vacuumdb", opts, args)
run_command("vacuumdb", opts, args)
end

def self.reindex(opts)
args = {}
args[:table] = opts[:table] if opts[:table]
runcmd("reindexdb", opts, args)
run_command("reindexdb", opts, args)
end

def self.runcmd(cmd_str, opts, args)
runcmd_with_logging(cmd_str, opts, combine_command_args(opts, args))
def self.run_command(cmd_str, opts, args)
run_command_with_logging(cmd_str, opts, combine_command_args(opts, args))
end

def self.runcmd_with_logging(cmd_str, opts, params = {})
def self.run_command_with_logging(cmd_str, opts, params = {})
$log.info("MIQ(#{name}.#{__method__}) Running command... #{AwesomeSpawn.build_command_line(cmd_str, params)}")
AwesomeSpawn.run!(cmd_str, :params => params, :env => pg_env(opts)).output
end

class << self
# Temporary alias due to manageiq core stubbing this method
alias runcmd_with_logging run_command_with_logging
end

private_class_method def self.combine_command_args(opts, args)
default_args = {:no_password => nil}
default_args[:dbname] = opts[:dbname] if opts[:dbname]
Expand Down
19 changes: 0 additions & 19 deletions lib/gems/pending/util/runcmd.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/gems/pending/util/win32/miq-powershell.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'awesome_spawn'
require 'util/miq-xml'
require 'util/runcmd'
require 'io/wait'
require 'open-uri'
require 'util/miq-encode'
Expand Down Expand Up @@ -28,7 +28,7 @@ def self.execute(ps_command)
ps_exec = exepath
command = "start /wait cmd /c #{ps_exec} #{ps_command}"
$log.debug "PowerShell: Running command: [#{command}]" if $log
ret = MiqUtil.runcmd(command).chomp
ret = AwesomeSpawn.run!(command, :combined_output => true).output.chomp
$log.debug "PowerShell: Return from command: [#{ret}]" if $log
ret
end
Expand All @@ -47,7 +47,7 @@ def self.execute_async(ps_command)
def self.kill_process(pid)
command = "taskkill /PID #{pid} /F"
$log.debug "PowerShell: kill_process: [#{command}]" if $log
ret = MiqUtil.runcmd(command).chomp
ret = AwesomeSpawn.run!(command, :combined_output => true).output.chomp
$log.debug "PowerShell: Return from command: [#{ret}]" if $log
ret
end
Expand Down
20 changes: 12 additions & 8 deletions spec/util/miq-ipmi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe MiqIPMI do
subject { described_class.new }

def command_result(output = "")
double(AwesomeSpawn::CommandResult, :output => output)
end

it "#chassis_status" do
allow(described_class).to receive(:is_2_0_available?).and_return(true)
response = <<-EOF
Expand Down Expand Up @@ -50,7 +54,7 @@
}

expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "chassis", nil => ["status"]}
expect(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).twice.and_return(response)
expect(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).twice.and_return(command_result(response))
expect(subject.chassis_status).to eq(result)
end

Expand All @@ -63,7 +67,7 @@
end

it "#run_command" do
expect(MiqUtil).to receive(:runcmd).with("ipmitool", :params => a_hash_including(:I => "lan"))
expect(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => a_hash_including(:I => "lan"))).and_return(command_result)
subject.run_command("chassis power status")
end
end
Expand All @@ -77,19 +81,19 @@

it "#power_state" do
expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "chassis", nil => ["power", "status"]}
allow(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).and_return("Chassis Power is off")
allow(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).and_return(command_result("Chassis Power is off"))
expect(subject.power_state).to eq("off")
end

it "#power_on" do
expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "chassis", nil => ["power", "on"]}
allow(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).and_return("Chassis Power Control: Up/On")
allow(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).and_return(command_result("Chassis Power Control: Up/On"))
expect(subject.power_on).to eq("Chassis Power Control: Up/On")
end

it "#power_off" do
expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "chassis", nil => ["power", "off"]}
allow(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).and_return("Chassis Power Control: Down/Off")
allow(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).and_return(command_result("Chassis Power Control: Down/Off"))
expect(subject.power_off).to eq("Chassis Power Control: Down/Off")
end

Expand Down Expand Up @@ -139,7 +143,7 @@

it "#mc_info" do
expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "mc", nil => ["info"]}
allow(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).and_return(mc_info_response)
allow(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).and_return(command_result(mc_info_response))
expect(subject.mc_info).to eq(
"Device Available" => "yes",
"Device ID" => "32",
Expand All @@ -156,7 +160,7 @@

it "#manufacturer" do
expected_args = {:I => "lanplus", :H => nil, :U => nil, :E => "mc", nil => ["info"]}
allow(MiqUtil).to receive(:runcmd).with("ipmitool", :params => expected_args).and_return(mc_info_response)
allow(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => expected_args)).and_return(command_result(mc_info_response))
expect(subject.manufacturer).to eq("DELL Inc")
end
end
Expand All @@ -166,7 +170,7 @@
end

it "#run_command" do
expect(MiqUtil).to receive(:runcmd).with("ipmitool", :params => a_hash_including(:I => "lanplus"))
expect(AwesomeSpawn).to receive(:run!).with("ipmitool", a_hash_including(:params => a_hash_including(:I => "lanplus"))).and_return(command_result)
subject.run_command("chassis power status")
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/util/postgres_admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
end

before do
expect(subject).to receive(:runcmd_with_logging).with("pg_dump", expected_opts, expected_args)
expect(subject).to receive(:run_command_with_logging).with("pg_dump", expected_opts, expected_args)
end

context "with empty args" do
Expand Down
48 changes: 0 additions & 48 deletions spec/util/runcmd_spec.rb

This file was deleted.

0 comments on commit 729d5c0

Please sign in to comment.