Skip to content

Commit

Permalink
Merge pull request #14591 from djberg96/powershell4
Browse files Browse the repository at this point in the history
Ensure remote shells generated by SCVMM are closed when finished
(cherry picked from commit 5f36311)

https://bugzilla.redhat.com/show_bug.cgi?id=1438593
  • Loading branch information
blomquisg authored and simaishi committed Apr 3, 2017
1 parent 6ff82bc commit 5c92c90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
3 changes: 1 addition & 2 deletions app/models/manageiq/providers/microsoft/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def self.raw_connect(connect_params)
require 'winrm'

connect_params[:operation_timeout] = 1800
winrm = WinRM::Connection.new(connect_params)
winrm
WinRM::Connection.new(connect_params)
end

def self.auth_url(hostname, port = nil)
Expand Down
47 changes: 35 additions & 12 deletions app/models/manageiq/providers/microsoft/infra_manager/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ def execute_powershell(connection, script)
def run_powershell_script(connection, script)
log_header = "MIQ(#{self.class.name}.#{__method__})"
script_string = IO.read(script)
results = []

begin
results = connection.shell(:powershell).run(script_string)
log_dos_error_results(results)
results
with_winrm_shell(connection) do |shell|
results = shell.run(script_string)
log_dos_error_results(results.stderr)
end
rescue Errno::ECONNREFUSED => err
$scvmm_log.error "MIQ(#{log_header} Unable to connect to SCVMM. #{err.message})"
$scvmm_log.error "MIQ(#{log_header} Unable to connect to SCVMM: #{err.message})"
raise
end

results.stdout
end

def powershell_results_to_hash(results)
Expand Down Expand Up @@ -70,6 +75,24 @@ def parse_xml_error_string(str)
string << element
end
end

def with_winrm_shell(connection, shell_type = :powershell)
shell = connection.shell(shell_type)
yield shell
ensure
shell.close
end
end

def with_winrm_shell(shell_type = :powershell)
with_provider_connection do |connection|
begin
shell = connection.shell(shell_type)
yield shell
ensure
shell.close
end
end
end

def run_dos_command(command)
Expand All @@ -78,15 +101,15 @@ def run_dos_command(command)
results = []

_result, timings = Benchmark.realtime_block(:execution) do
with_provider_connection do |connection|
results = connection.shell(:cmd).run(command)
self.class.log_dos_error_results(results)
with_winrm_shell(:cmd) do |shell|
results = shell.run(command)
self.class.log_dos_error_results(results.stderr)
end
end

$scvmm_log.debug("#{log_header} Execute DOS command <#{command}>...Complete - Timings: #{timings}")

results
results.stdout
end

def run_powershell_script(script)
Expand All @@ -95,16 +118,16 @@ def run_powershell_script(script)
results = []

_result, timings = Benchmark.realtime_block(:execution) do
with_provider_connection do |connection|
with_winrm_shell do |shell|
script_string = IO.read(script)
results = connection.shell(:powershell).run(script_string)
self.class.log_dos_error_results(results)
results = shell.run(script_string)
self.class.log_dos_error_results(results.stderr)
end
end

$scvmm_log.debug("#{log_header} Execute Powershell script... Complete - Timings: #{timings}")

results
results.stdout
end
end
end

0 comments on commit 5c92c90

Please sign in to comment.