Class: VagrantWindows::Communication::WinRMCommunicator

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-windows/communication/winrmcommunicator.rb

Overview

Provides communication channel for Vagrant commands via WinRM.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ WinRMCommunicator

Returns a new instance of WinRMCommunicator.



18
19
20
21
22
23
24
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 18

def initialize(machine)
  @windows_machine = VagrantWindows::WindowsMachine.new(machine)
  @winrm_shell_factory = WinRMShellFactory.new(@windows_machine, WinRMFinder.new(@windows_machine))

  @logger = Log4r::Logger.new("vagrant_windows::communication::winrmcommunicator")
  @logger.debug("initializing WinRMCommunicator")
end

Class Method Details

.match?(machine) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 14

def self.match?(machine)
  VagrantWindows::WindowsMachine.is_windows?(machine)
end

Instance Method Details

#download(from, to) ⇒ Object



78
79
80
81
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 78

def download(from, to)
  @logger.debug("Downloading: #{from} to #{to}")
  winrmshell.download(from, to)
end

#execute(command, opts = {}, &block) ⇒ Object Also known as: sudo



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 47

def execute(command, opts={}, &block)
  opts = {
    :error_check => true,
    :error_class => VagrantWindows::Errors::WinRMExecutionError,
    :error_key   => :winrm_execution_error,
    :command     => command,
    :shell       => :powershell
  }.merge(opts || {})
  exit_status = do_execute(command, opts[:shell], &block)
  if opts[:error_check] && exit_status != 0
    raise_execution_error(opts, exit_status)
  end
  exit_status
end

#ready?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 26

def ready?
  @logger.debug("Checking whether WinRM is ready...")

  Timeout.timeout(@windows_machine.winrm_config.timeout) do
    winrmshell.powershell("hostname")
  end

  @logger.info("WinRM is ready!")
  return true

rescue Vagrant::Errors::VagrantError => e
  # We catch a `VagrantError` which would signal that something went
  # wrong expectedly in the `connect`, which means we didn't connect.
  @logger.info("WinRM not up: #{e.inspect}")
  # We reset the shell to trigger calling of winrm_finder again.
  # This resolves a problem when using vSphere where the ssh_info was not refreshing
  # thus never getting the correct hostname.
  @winrmshell = nil
  return false
end

#test(command, opts = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 63

def test(command, opts=nil)
  @logger.debug("Testing: #{command}")
  
  # HACK: to speed up Vagrant 1.2 OS detection, skip checking for *nix OS
  return false unless (command =~ /^uname|^cat \/etc|^cat \/proc|grep 'Fedora/).nil?

  opts = { :error_check => false }.merge(opts || {})
  execute(command, opts) == 0
end

#upload(from, to) ⇒ Object



73
74
75
76
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 73

def upload(from, to)
  @logger.debug("Uploading: #{from} to #{to}")
  winrmshell.upload(from, to)
end

#winrmshellObject



87
88
89
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 87

def winrmshell
  @winrmshell ||= @winrm_shell_factory.create_winrm_shell()
end

#winrmshell=(winrmshell) ⇒ Object



83
84
85
# File 'lib/vagrant-windows/communication/winrmcommunicator.rb', line 83

def winrmshell=(winrmshell)
  @winrmshell = winrmshell
end