Class: Vagrant::WinRM

Inherits:
Object
  • Object
show all
Includes:
Util::SafeExec
Defined in:
lib/vagrant-windows/winrm.rb

Overview

Manages WINRM access to a specific environment. Allows an environment to run commands, upload files, and check if a host is up.

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ WinRM

Returns a new instance of WinRM.



10
11
12
13
# File 'lib/vagrant-windows/winrm.rb', line 10

def initialize(vm)
  @vm = vm
  @logger = Log4r::Logger.new("vagrant::winrm")
end

Instance Method Details

#infoHash

Returns a hash of information necessary for accessing this virtual machine via WINRM.

Returns:

  • (Hash)

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-windows/winrm.rb', line 19

def info
  results = {
    :host          => @vm.config.winrm.host,
    :port          => @vm.config.winrm.port || @vm.driver.ssh_port(@vm.config.winrm.guest_port),
    :username      => @vm.config.winrm.username
  }

  # This can happen if no port is set and for some reason Vagrant
  # can't detect an SSH port.
  raise Errors::WinRMPortNotDetected if !results[:port]

  # Return the results
  return results
end

#port(opts = {}) ⇒ Object

Returns the port which is either given in the options hash or taken from the config by finding it in the forwarded ports hash based on the ‘config.ssh.forwarded_port_key`.



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
# File 'lib/vagrant-windows/winrm.rb', line 56

def port(opts={})
  # Check if port was specified in options hash
  return opts[:port] if opts[:port]

  # Check if a port was specified in the config
  return @env.config.winrm.port if @env.config.winrm.port

  # Check if we have an SSH forwarded port
  pnum_by_name = nil
  pnum_by_destination = nil
  @logger.info("Looking for winrm port: #{opts}")
  @logger.info("Looking for winrm port: #{env.config.winrm.inspect}")

  env.vm.vm.network_adapters.each do |na| 
    # Look for the port number by destination...
    pnum_by_destination = na.nat_driver.forwarded_ports.detect do |fp|
      fp.guestport == env.config.winrm.guest_port
    end
  end

  return pnum_by_destination.hostport if pnum_by_destination

  # This should NEVER happen.
  raise Errors::WinRMPortNotDetected
end

#up?Boolean

Checks if this environment’s machine is up (i.e. responding to WINRM).

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-windows/winrm.rb', line 37

def up?
  # We have to determine the port outside of the block since it uses
  # API calls which can only be used from the main thread in JRuby on
  # Windows
  ssh_port = port

  require 'timeout'
  Timeout.timeout(@env.config.ssh.timeout) do
    execute 'hostname'
  end

  true
rescue Timeout::Error, Errno::ECONNREFUSED
  return false
end