Skip to content
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

Merged
merged 1 commit into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chef-provisioning.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency 'net-ssh-gateway', '~> 1.2.0'
s.add_dependency 'inifile', '>= 2.0.2'
s.add_dependency 'cheffish', '~> 4.0'
s.add_dependency 'winrm', '~> 1.3'
s.add_dependency 'winrm-fs', '~> 1.0'
s.add_dependency "mixlib-install", "~> 1.0"

s.add_development_dependency 'chef', '~> 12.1', "!= 12.4.0" # 12.4.0 is incompatible.
Expand Down
8 changes: 4 additions & 4 deletions lib/chef/provisioning/convergence_strategy/install_msi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def setup_convergence(action_handler, machine)

action_handler.open_stream(machine.node['name']) do |stdout|
action_handler.open_stream(machine.node['name']) do |stderr|
machine.execute(action_handler, "powershell.exe -ExecutionPolicy Unrestricted -NoProfile \"& \"\"#{convergence_options[:install_script_path]}\"\"\"",
:raw => true,
machine.execute(action_handler, "& \"#{convergence_options[:install_script_path]}\"",
Copy link
Contributor

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

:stream_stdout => stdout,
:stream_stderr => stderr)
end
Expand All @@ -54,10 +53,11 @@ def converge(action_handler, machine)

action_handler.open_stream(machine.node['name']) do |stdout|
action_handler.open_stream(machine.node['name']) do |stderr|
command_line = "chef-client"
# We just installed chef in this shell so refresh PATH from System.Environment
command_line = "$env:path = [System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE');"
command_line << "chef-client"
command_line << " -l #{config[:log_level].to_s}" if config[:log_level]
machine.execute(action_handler, command_line,
:raw => true,
:stream_stdout => stdout,
:stream_stderr => stderr,
:timeout => @chef_client_timeout)
Expand Down
65 changes: 26 additions & 39 deletions lib/chef/provisioning/transport/winrm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 :raw options
/cc @ajeba99

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
WinRMResult.new(command, execute_options, config, output)
end
Expand All @@ -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

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Expand All @@ -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
Expand Down