Skip to content

basic file transport via winrm #21

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

Merged
merged 3 commits into from
Nov 2, 2015
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
1 change: 1 addition & 0 deletions lib/train/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Train::Extras
autoload :CommandWrapper, 'train/extras/command_wrapper'
autoload :FileCommon, 'train/extras/file_common'
autoload :LinuxFile, 'train/extras/linux_file'
autoload :WindowsFile, 'train/extras/windows_file'
autoload :OSCommon, 'train/extras/os_common'
autoload :Stat, 'train/extras/stat'

Expand Down
85 changes: 85 additions & 0 deletions lib/train/extras/windows_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann

require 'train/extras/stat'

# PS C:\Users\Administrator> Get-Item -Path C:\test.txt | Select-Object -Property BaseName, FullName, IsReadOnly, Exists,
# LinkType, Mode, VersionInfo, Owner, Archive, Hidden, ReadOnly, System | ConvertTo-Json

module Train::Extras
class WindowsFile < FileCommon
attr_reader :path
def initialize(backend, path)
@backend = backend
@path = path
@spath = Shellwords.escape(@path)
end

def content
return @content if defined?(@content)
@content = @backend.run_command(
"Get-Content(\"#{@spath}\") | Out-String").stdout
return @content unless @content.empty?
@content = nil if directory? # or size.nil? or size > 0
@content
end

def exist?
nil
end

def link_target
nil
end

def link_path
nil
end

def mounted?
nil
end

def type
:unknown
end

%w{
mode owner group mtime size selinux_label
}.each do |field|
define_method field.to_sym do
nil
end
end

def product_version
nil
end

def file_version
nil
end

def stat
nil
end

private

def attributes
return @attributes if defined?(@attributes)
@attributes = @backend.run_command(
"(Get-ItemProperty -Path \"#{@spath}\").attributes.ToString()").stdout.chomp.split(',')
end

def target_type
if attributes.include?('Archive')
return :file
elsif attributes.include?('Directory')
return :directory
end
:unknown
end
end
end
7 changes: 6 additions & 1 deletion lib/train/transports/winrm_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class Train::Transports::WinRM
# @author Fletcher Nichol <[email protected]>
class Connection < BaseConnection # rubocop:disable Metrics/ClassLength
def initialize(options)
super
super(options)
@endpoint = @options.delete(:endpoint)
@rdp_port = @options.delete(:rdp_port)
@winrm_transport = @options.delete(:winrm_transport)
@connection_retries = @options.delete(:connection_retries)
@connection_retry_sleep = @options.delete(:connection_retry_sleep)
@max_wait_until_ready = @options.delete(:max_wait_until_ready)
@files = {}
end

# (see Base::Connection#close)
Expand All @@ -53,6 +54,10 @@ def os
@os ||= OS.new(self)
end

def file(path)
@files[path] ||= WindowsFile.new(self, path)
end

def run_command(command)
return if command.nil?
logger.debug("[WinRM] #{self} (#{command})")
Expand Down