Class: VagrantWindows::Communication::WinRMCommunicator
- Inherits:
-
Object
- Object
- VagrantWindows::Communication::WinRMCommunicator
- Includes:
- Vagrant::Util::ANSIEscapeCodeRemover, Vagrant::Util::Retryable
- Defined in:
- lib/vagrant-windows/communication/winrmcommunicator.rb
Overview
Provides communication with the VM via WinRM.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#machine ⇒ Object
readonly
Returns the value of attribute machine.
Class Method Summary collapse
Instance Method Summary collapse
- #download(from, to = nil) ⇒ Object
- #execute(command, opts = nil, &block) ⇒ Object
-
#initialize(machine) ⇒ WinRMCommunicator
constructor
A new instance of WinRMCommunicator.
- #new_session ⇒ Object
- #ready? ⇒ Boolean
- #session ⇒ Object
-
#sudo(command, opts = nil, &block) ⇒ Object
Wrap Sudo in execute.…
- #test(command, opts = nil) ⇒ Object
- #upload(from, to) ⇒ Object
Constructor Details
#initialize(machine) ⇒ WinRMCommunicator
Returns a new instance of WinRMCommunicator.
24 25 26 27 28 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 24 def initialize(machine) @machine = machine @logger = Log4r::Logger.new("vagrant_windows::communication::winrmcommunicator") @logger.debug("initializing WinRMCommunicator") end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
17 18 19 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 17 def logger @logger end |
#machine ⇒ Object (readonly)
Returns the value of attribute machine.
18 19 20 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 18 def machine @machine end |
Class Method Details
.match?(machine) ⇒ Boolean
20 21 22 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 20 def self.match?(machine) machine.config.vm.guest.eql? :windows end |
Instance Method Details
#download(from, to = nil) ⇒ Object
95 96 97 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 95 def download(from, to=nil) @logger.warn("Downloading: #{from} to #{to} not supported on Windows guests") end |
#execute(command, opts = nil, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 44 def execute(command, opts=nil, &block) opts = { :error_check => true, :error_class => Errors::WinRMExecutionError, :error_key => :winrm_bad_exit_status, :command => command, :sudo => false, :shell => :powershell }.merge(opts || {}) if opts[:shell].eql? :powershell command = VagrantWindows.load_script("command_alias.ps1") << "\r\n" << command end exit_status = 0 begin # Connect via WinRM and execute the command in the shell. exceptions = [HTTPClient::KeepAliveDisconnected] exit_status = retryable(:tries => @machine.config.winrm.max_tries, :on => exceptions, :sleep => 10) do shell_execute(command, opts[:shell], &block) end rescue StandardError => e # return a more specific auth error for 401 errors if e..include?("401") raise Errors::WinRMAuthorizationError, :user => @machine.config.winrm.username, :password => @machine.config.winrm.password, :endpoint => endpoint, :message => e. end # failed for an unknown reason, didn't even get an exit status raise Errors::WinRMExecutionError, :shell => opts[:shell], :command => command, :message => e. end # Check for any exit status errors if opts[:error_check] && exit_status != 0 error_opts = opts.merge(:_key => opts[:error_key], :exit_status => exit_status) raise error_opts[:error_class], error_opts end exit_status end |
#new_session ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 127 def new_session opts = () logger.debug("Creating WinRM session to #{endpoint} with options: #{opts}") client = ::WinRM::WinRMWebService.new(endpoint, :plaintext, opts) client.set_timeout(opts[:operation_timeout]) client.toggle_nori_type_casting(:off) #we don't want coersion of types client end |
#ready? ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 30 def ready? logger.debug("Checking whether WinRM is ready...") Timeout.timeout(@machine.config.winrm.timeout) do execute("hostname") do |type, data| @logger.debug("hostname: #{data}") end end # If we reached this point then we successfully connected logger.debug("WinRM is ready!") true end |
#session ⇒ Object
137 138 139 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 137 def session @session ||= new_session end |
#sudo(command, opts = nil, &block) ⇒ Object
Wrap Sudo in execute.… One day we could integrate with UAC, but Icky
91 92 93 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 91 def sudo(command, opts=nil, &block) execute(command, opts, &block) end |
#test(command, opts = nil) ⇒ Object
99 100 101 102 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 99 def test(command, opts=nil) opts = { :error_check => false }.merge(opts || {}) execute(command, opts) == 0 end |
#upload(from, to) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 104 def upload(from, to) @logger.debug("Uploading: #{from} to #{to}") file = "winrm-upload-#{rand()}" file_name = (session.cmd("echo %TEMP%\\#{file}"))[:data][0][:stdout].chomp session.powershell <<-EOH if(Test-Path #{to}) { rm #{to} } EOH Base64.encode64(IO.binread(from)).gsub("\n",'').chars.to_a.each_slice(8000-file_name.size) do |chunk| out = session.cmd( "echo #{chunk.join} >> \"#{file_name}\"" ) end execute "mkdir $([System.IO.Path]::GetDirectoryName(\"#{to}\"))" execute <<-EOH $base64_string = Get-Content \"#{file_name}\" $bytes = [System.Convert]::FromBase64String($base64_string) $new_file = [System.IO.Path]::GetFullPath(\"#{to}\") [System.IO.File]::WriteAllBytes($new_file,$bytes) EOH end |