-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Winrm v2 and let winrm-fs do file uploads #543
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,27 +22,24 @@ class WinRM < Chef::Provisioning::Transport | |
# The actual connection is made as ::WinRM::WinRMWebService.new(endpoint, type, options) | ||
# | ||
def initialize(endpoint, type, options, global_config) | ||
@endpoint = endpoint | ||
@type = type | ||
@options = options | ||
@options[:endpoint] = endpoint | ||
@options[:transport] = type | ||
|
||
# WinRM v2 switched from :pass to :password | ||
# we accept either to avoid having to update every driver | ||
@options[:password] = @options[:password] || @options[:pass] | ||
|
||
@config = global_config | ||
end | ||
|
||
attr_reader :endpoint | ||
attr_reader :type | ||
attr_reader :options | ||
attr_reader :config | ||
|
||
def execute(command, execute_options = {}) | ||
output = with_execute_timeout(execute_options) do | ||
session.set_timeout(execute_timeout(execute_options)) | ||
command_executor = ::WinRM::CommandExecutor.new(session) | ||
block = Proc.new { |stdout, stderr| stream_chunk(execute_options, stdout, stderr) } | ||
if execute_options[:raw] | ||
command_executor.run_cmd(command, &block) | ||
else | ||
command_executor.run_powershell_script(command, &block) | ||
end | ||
session.run(command, &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwrock @tyler-ball we need to default to session.run_powershell_script and only use session.run_cmd if raw options are passed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
end | ||
WinRMResult.new(command, execute_options, config, output) | ||
end | ||
|
@@ -57,25 +54,13 @@ def read_file(path) | |
end | ||
|
||
def write_file(path, content) | ||
execute("New-Item -Type Directory -Force -Path #{escape(::File.dirname(path))}").error! | ||
chunk_size = options[:chunk_size] || 1024 | ||
# TODO if we could marshal this data directly, we wouldn't have to base64 or do this godawful slow stuff :( | ||
index = 0 | ||
execute(" | ||
$ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))}) | ||
$file = [System.IO.File]::Open(#{escape(path)}, 'Create', 'Write') | ||
$file.Write($ByteArray, 0, $ByteArray.Length) | ||
$file.Close | ||
").error! | ||
index += chunk_size | ||
while index < content.length | ||
execute(" | ||
$ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))}) | ||
$file = [System.IO.File]::Open(#{escape(path)}, 'Append', 'Write') | ||
$file.Write($ByteArray, 0, $ByteArray.Length) | ||
$file.Close | ||
").error! | ||
index += chunk_size | ||
file = Tempfile.new('provisioning-upload') | ||
begin | ||
file.write(content) | ||
file.close | ||
file_transporter.upload(file.path, path) | ||
ensure | ||
file.unlink | ||
end | ||
end | ||
|
||
|
@@ -114,9 +99,15 @@ def make_url_available_to_remote(local_url) | |
|
||
def session | ||
@session ||= begin | ||
require 'kconv' # Really, people? *sigh* | ||
require 'winrm' | ||
::WinRM::WinRMWebService.new(endpoint, type, options) | ||
::WinRM::Connection.new(options).shell(:powershell) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting shell to powershell here may get rid of the need for the :raw block above |
||
end | ||
end | ||
|
||
def file_transporter | ||
@file_transporter ||= begin | ||
require 'winrm-fs' | ||
::WinRM::FS::Core::FileTransporter.new(session) | ||
end | ||
end | ||
|
||
|
@@ -125,13 +116,9 @@ def initialize(command, options, config, output) | |
@command = command | ||
@options = options | ||
@config = config | ||
@exitstatus = output[:exitcode] | ||
@stdout = '' | ||
@stderr = '' | ||
output[:data].each do |data| | ||
@stdout << data[:stdout] if data[:stdout] | ||
@stderr << data[:stderr] if data[:stderr] | ||
end | ||
@exitstatus = output.exitcode | ||
@stdout = output.stdout | ||
@stderr = output.stderr | ||
end | ||
|
||
attr_reader :stdout | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mwrock @tyler-ball this is a must have! We need ExecutionPolicy Unrestricted in order to run powershell
/cc @ajeba99