summaryrefslogtreecommitdiff
path: root/lib/chef/util/powershell/cmdlet.rb
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-24 10:21:14 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-24 10:21:14 -0700
commitd3e4e565fdf7c210acfd8efd0de6b674883a5e0f (patch)
tree5fd2f1b237e39c81d8a7b54e0cdc0279e7528435 /lib/chef/util/powershell/cmdlet.rb
parent17014504259f8ccb19d782bd3b63b24657e4c9ca (diff)
parent157852156703daf96366dd773903147baf0292d9 (diff)
downloadchef-12.2.0.rc.0.tar.gz
Merge pull request #3128 from chef/jdm/prepare-12.2.012.2.0.rc.0
prepare 12.2.0 RC 0
Diffstat (limited to 'lib/chef/util/powershell/cmdlet.rb')
-rw-r--r--lib/chef/util/powershell/cmdlet.rb48
1 files changed, 41 insertions, 7 deletions
diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb
index 40edbb13c6..47d63a2b85 100644
--- a/lib/chef/util/powershell/cmdlet.rb
+++ b/lib/chef/util/powershell/cmdlet.rb
@@ -20,7 +20,9 @@ require 'mixlib/shellout'
require 'chef/mixin/windows_architecture_helper'
require 'chef/util/powershell/cmdlet_result'
-class Chef::Util::Powershell
+class Chef
+class Util
+class Powershell
class Cmdlet
def initialize(node, cmdlet, output_format=nil, output_format_options={})
@output_format = output_format
@@ -46,6 +48,10 @@ class Chef::Util::Powershell
attr_reader :output_format
def run(switches={}, execution_options={}, *arguments)
+ streams = { :json => CmdletStream.new('json'),
+ :verbose => CmdletStream.new('verbose'),
+ }
+
arguments_string = arguments.join(' ')
switches_string = command_switches_string(switches)
@@ -56,21 +62,25 @@ class Chef::Util::Powershell
json_depth = @output_format_options[:depth]
end
- json_command = @json_format ? " | convertto-json -compress -depth #{json_depth}" : ""
- command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive -command \"trap [Exception] {write-error -exception ($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} #{arguments_string}#{json_command}\";if ( ! $? ) { exit 1 }"
+ json_command = @json_format ? " | convertto-json -compress -depth #{json_depth} "\
+ "> #{streams[:json].path}" : ""
+ redirections = "4> '#{streams[:verbose].path}'"
+ command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\
+ "-command \"trap [Exception] {write-error -exception "\
+ "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\
+ "#{arguments_string} #{redirections}"\
+ "#{json_command}\";if ( ! $? ) { exit 1 }"
augmented_options = {:returns => [0], :live_stream => false}.merge(execution_options)
command = Mixlib::ShellOut.new(command_string, augmented_options)
- os_architecture = "#{ENV['PROCESSOR_ARCHITEW6432']}" == 'AMD64' ? :x86_64 : :i386
-
status = nil
with_os_architecture(@node) do
status = command.run_command
end
- CmdletResult.new(status, @output_format)
+ CmdletResult.new(status, streams, @output_format)
end
def run!(switches={}, execution_options={}, *arguments)
@@ -131,6 +141,30 @@ class Chef::Util::Powershell
command_switches.join(' ')
end
+
+ class CmdletStream
+ def initialize(name)
+ @filename = Dir::Tmpname.create(name) {}
+ ObjectSpace.define_finalizer(self, self.class.destroy(@filename))
+ end
+
+ def path
+ @filename
+ end
+
+ def read
+ if File.exist? @filename
+ File.open(@filename, 'rb:bom|UTF-16LE') do |f|
+ f.read.encode('UTF-8')
+ end
+ end
+ end
+
+ def self.destroy(name)
+ proc { File.delete(name) if File.exists? name }
+ end
+ end
end
end
-
+end
+end