Skip to content

Commit

Permalink
Detect windows also with ssh transport, add PowerShell wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Scherer <[email protected]>
  • Loading branch information
StefanScherer committed Mar 7, 2019
1 parent a585b0a commit 35ccea5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/train/extras/command_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,52 @@ def safe_string(str)
end
end

# Wrap windows commands.
class WindowsCommand < CommandWrapperBase
Train::Options.attach(self)

option :shell_command, default: nil

def initialize(backend, options)
@backend = backend
validate_options(options)

@shell_command = options[:shell_command] # e.g. 'powershell'
end

# (see CommandWrapperBase::run)
def run(command)
powershell_wrap(command)
end

def self.active?(options)
options.is_a?(Hash) && (
options[:sudo] ||
options[:shell]
)
end

private

# Wrap the cmd in an encoded command to allow pipes and quotes
def powershell_wrap(cmd)
shell = @shell_command || 'powershell'

# Prevent progress stream from leaking into stderr
script = "$ProgressPreference='SilentlyContinue';" + cmd

# Encode script so PowerShell can use it
script = script.encode('UTF-16LE', 'UTF-8')
base64_script = Base64.strict_encode64(script)

cmd = "#{shell} -NoProfile -EncodedCommand #{base64_script}"
cmd
end
end

class CommandWrapper
include_options LinuxCommand
include_options WindowsCommand

def self.load(transport, options)
if transport.os.unix?
Expand All @@ -134,6 +178,9 @@ def self.load(transport, options)
raise Train::UserError.new("Sudo failed: #{msg}", reason)
end
res
elsif transport.os.windows?
res = WindowsCommand.new(transport, options)
res
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/train/platforms/detect/specifications/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def self.load
is_windows = true if ruby_host_os(/mswin|mingw32|windows/)
end

# Try to detect windows even for ssh transport
if !is_windows && detect_windows == true
is_windows = true
end

is_windows
}
# windows platform
Expand Down
2 changes: 2 additions & 0 deletions lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def file_via_connection(path)
Train::File::Remote::Unix.new(self, path)
elsif os[:name] == 'qnx'
Train::File::Remote::Qnx.new(self, path)
elsif os.windows?
Train::File::Remote::Windows.new(self, path)
else
Train::File::Remote::Linux.new(self, path)
end
Expand Down

0 comments on commit 35ccea5

Please sign in to comment.