diff --git a/lib/gems/pending/util/miq-ipmi.rb b/lib/gems/pending/util/miq-ipmi.rb index 9c6286c4..4ad996d6 100644 --- a/lib/gems/pending/util/miq-ipmi.rb +++ b/lib/gems/pending/util/miq-ipmi.rb @@ -6,7 +6,7 @@ # OpenIPMI-tools.x86_64 # freeipmi.x86_64 -require 'util/runcmd' +require 'awesome_spawn' require 'util/miq-extensions' class MiqIPMI @@ -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 @@ -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 diff --git a/lib/gems/pending/util/miq-process.rb b/lib/gems/pending/util/miq-process.rb index 2ae0b365..3d1e9f24 100644 --- a/lib/gems/pending/util/miq-process.rb +++ b/lib/gems/pending/util/miq-process.rb @@ -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 @@ -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 diff --git a/lib/gems/pending/util/mount/miq_generic_mount_session.rb b/lib/gems/pending/util/mount/miq_generic_mount_session.rb index 298dfa0c..56f400f1 100644 --- a/lib/gems/pending/util/mount/miq_generic_mount_session.rb +++ b/lib/gems/pending/util/mount/miq_generic_mount_session.rb @@ -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' diff --git a/lib/gems/pending/util/postgres_admin.rb b/lib/gems/pending/util/postgres_admin.rb index 4de7d797..e7d6b341 100644 --- a/lib/gems/pending/util/postgres_admin.rb +++ b/lib/gems/pending/util/postgres_admin.rb @@ -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 @@ -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 @@ -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) @@ -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 @@ -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] diff --git a/lib/gems/pending/util/runcmd.rb b/lib/gems/pending/util/runcmd.rb deleted file mode 100644 index 99bd2782..00000000 --- a/lib/gems/pending/util/runcmd.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'awesome_spawn' - -class MiqUtil - def self.runcmd(cmd, cmd_args = {}, test = false) - unless [Hash, Array, String].any? { |klass| cmd_args.kind_of?(klass) } - test = cmd_args - cmd_args = {} - end - - args = { :combined_output => true }.merge(cmd_args) - if !test - rv = AwesomeSpawn.run!(cmd, args) - rv.output - else - cmd_str = AwesomeSpawn::CommandLineBuilder.new.build(cmd, args[:params]) - "#{cmd_str}: Test output" - end - end -end diff --git a/lib/gems/pending/util/win32/miq-powershell.rb b/lib/gems/pending/util/win32/miq-powershell.rb index b890c023..51c1e5d5 100644 --- a/lib/gems/pending/util/win32/miq-powershell.rb +++ b/lib/gems/pending/util/win32/miq-powershell.rb @@ -1,5 +1,5 @@ +require 'awesome_spawn' require 'util/miq-xml' -require 'util/runcmd' require 'io/wait' require 'open-uri' require 'util/miq-encode' @@ -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 @@ -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 diff --git a/spec/util/miq-ipmi_spec.rb b/spec/util/miq-ipmi_spec.rb index cdee8f51..8e920eeb 100644 --- a/spec/util/miq-ipmi_spec.rb +++ b/spec/util/miq-ipmi_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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", @@ -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 @@ -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 diff --git a/spec/util/postgres_admin_spec.rb b/spec/util/postgres_admin_spec.rb index 1ae501cc..2a923a3a 100644 --- a/spec/util/postgres_admin_spec.rb +++ b/spec/util/postgres_admin_spec.rb @@ -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 diff --git a/spec/util/runcmd_spec.rb b/spec/util/runcmd_spec.rb deleted file mode 100644 index 1597df07..00000000 --- a/spec/util/runcmd_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'util/runcmd' -require 'awesome_spawn/spec_helper' - -describe 'MiqUtil.runcmd' do - include AwesomeSpawn::SpecHelper - - let(:cmd) { 'echo Hi; echo Hitoo 1>&2' } - let(:failed_cmd) { 'echo Hi; false --foo --bar -fkxv arg1' } - - # wrap in "/bin/sh -c" so this set of specs is shared between the original - # implementation and AwesomeSpawn.run! - def runcmd(cmd_str, args = {}, test = false) - MiqUtil.runcmd(cmd_str, args, test) - end - - it "returns stdout & stderr as output" do - expect(runcmd(cmd)).to eq("Hi\nHitoo\n") - end - - it "raises the resulting output" do - expect { runcmd(failed_cmd) }.to raise_error(AwesomeSpawn::CommandResultError) - end - - context "test mode" do - before { disable_spawning } - let(:cmd) { "true --opt1 foo --opt2=bar -fkvx arg1 arg2" } - let(:output) { "#{cmd}: Test output" } - - it "prints the executed command" do - expect(runcmd(cmd, true)).to eq output - end - - it "works with a params hash for cmd_args" do - args = { - :opt1 => "foo", - :opt2= => "bar", - :f => nil, - :k => nil, - :v => nil, - :x => nil, - nil => %w[arg1 arg2] - } - output.gsub!(/-fkvx/, "-f -k -v -x") - - expect(runcmd("true", {:params => args}, true)).to eq output - end - end -end