-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathhelper.rb
88 lines (79 loc) · 2.77 KB
/
helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
module VagrantPlugins
module CommunicatorWinRM
# This is a helper module that provides some functions to the
# communicator. This is extracted into a module so that we can
# easily unit test these methods.
module Helper
# Returns the host and port to access WinRM.
#
# This asks the provider via the `winrm_info` capability if it
# exists, otherwise defaulting to its own heuristics.
#
# @param [Vagrant::Machine] machine
# @return [Hash]
def self.winrm_info(machine)
info = {}
if machine.provider.capability?(:winrm_info)
info = machine.provider.capability(:winrm_info)
raise Errors::WinRMNotReady if !info
end
info[:host] ||= winrm_address(machine)
info[:port] ||= winrm_port(machine, info[:host] == "127.0.0.1")
return info
end
# Returns the address to access WinRM. This does not contain
# the port.
#
# @param [Vagrant::Machine] machine
# @return [String]
def self.winrm_address(machine)
addr = machine.config.winrm.host
return addr if addr
ssh_info = machine.ssh_info
raise Errors::WinRMNotReady if winrm_info_invalid?(ssh_info)
return ssh_info[:host]
end
def self.winrm_info_invalid?(ssh_info)
invalid = (!ssh_info || ssh_info[:host].to_s.empty? || ssh_info[:host].start_with?("169.254"))
return invalid
end
# Returns the port to access WinRM.
#
# @param [Vagrant::Machine] machine
# @return [Integer]
def self.winrm_port(machine, local=true)
host_port = machine.config.winrm.port
if machine.config.winrm.guest_port
# If we're not requesting a local port, return
# the guest port directly.
return machine.config.winrm.guest_port if !local
# Search by guest port if we can. We use a provider capability
# if we have it. Otherwise, we just scan the Vagrantfile defined
# ports.
port = nil
if machine.provider.capability?(:forwarded_ports)
machine.provider.capability(:forwarded_ports).each do |host, guest|
if guest == machine.config.winrm.guest_port
port = host
break
end
end
end
if !port
machine.config.vm.networks.each do |type, netopts|
next if type != :forwarded_port
next if !netopts[:host]
if netopts[:guest] == machine.config.winrm.guest_port
port = netopts[:host]
break
end
end
end
# Set it if we found it
host_port = port if port
end
host_port
end
end
end
end