Class: VagrantWindows::Communication::WinRMShell

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-windows/communication/winrmshell.rb

Constant Summary collapse

@@exceptions_to_retry_on =

These are the exceptions that we retry because they represent errors that are generally fixed from a retry and don’t necessarily represent immediate failure cases.

[
  HTTPClient::KeepAliveDisconnected,
  WinRM::WinRMHTTPTransportError,
  Errno::EACCES,
  Errno::EADDRINUSE,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
  Errno::EHOSTUNREACH,
  Timeout::Error
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, options = {}) ⇒ WinRMShell

Returns a new instance of WinRMShell.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 36

def initialize(host, username, password, options = {})
  @logger = Log4r::Logger.new("vagrant_windows::communication::winrmshell")
  @logger.debug("initializing WinRMShell")
  
  @host = host
  @port = options[:port] || 5985
  @username = username
  @password = password
  @timeout_in_seconds = options[:timeout_in_seconds] || 60
  @max_tries = options[:max_tries] || 20
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



31
32
33
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 31

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



28
29
30
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 28

def logger
  @logger
end

#max_triesObject (readonly)

Returns the value of attribute max_tries.



34
35
36
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 34

def max_tries
  @max_tries
end

#passwordObject (readonly)

Returns the value of attribute password.



30
31
32
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 30

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



32
33
34
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 32

def port
  @port
end

#timeout_in_secondsObject (readonly)

Returns the value of attribute timeout_in_seconds.



33
34
35
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 33

def timeout_in_seconds
  @timeout_in_seconds
end

#usernameObject (readonly)

Returns the value of attribute username.



29
30
31
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 29

def username
  @username
end

Instance Method Details

#cmd(command, &block) ⇒ Object



52
53
54
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 52

def cmd(command, &block)
  execute_shell(command, :cmd, &block)
end

#download(from, to) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 80

def download(from, to)
  @logger.debug("Downloading: #{from} to #{to}")
  output = powershell("[System.convert]::ToBase64String([System.IO.File]::ReadAllBytes(\"#{from}\"))")
  contents = output[:data].map!{|line| line[:stdout]}.join.gsub("\\n\\r", '')
  out = Base64.decode64(contents)
  IO.binwrite(to, out)
end

#powershell(command, &block) ⇒ Object



48
49
50
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 48

def powershell(command, &block)
  execute_shell(command, :powershell, &block)
end

#upload(from, to) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 60

def upload(from, to)
  @logger.debug("Uploading: #{from} to #{to}")
  file_name = (cmd("echo %TEMP%\\winrm-upload-#{rand()}"))[:data][0][:stdout].chomp
  powershell "    if(Test-Path \#{to}) {\n      rm \#{to}\n    }\n  EOH\n  Base64.encode64(IO.binread(from)).gsub(\"\\n\",'').chars.to_a.each_slice(8000-file_name.size) do |chunk|\n    out = cmd(\"echo \#{chunk.join} >> \\\"\#{file_name}\\\"\")\n  end\n  powershell <<-EOH\n    mkdir $([System.IO.Path]::GetDirectoryName(\\\"\#{to}\\\"))\n    $base64_string = Get-Content \\\"\#{file_name}\\\"\n    $bytes  = [System.Convert]::FromBase64String($base64_string) \n    $new_file = [System.IO.Path]::GetFullPath(\\\"\#{to}\\\")\n    [System.IO.File]::WriteAllBytes($new_file,$bytes)\n  EOH\nend\n"

#wql(query) ⇒ Object



56
57
58
# File 'lib/vagrant-windows/communication/winrmshell.rb', line 56

def wql(query)
  execute_wql(query)
end