Update guest IP address discovery for hyper-v guests.
Powershell helper script now returns error when guest IP address cannot be discovered. When reading addresses from a network device both IPv4 and IPv6 are stored and the IPv4 address has precedence on returned address.
This commit is contained in:
parent
a7b228eb69
commit
38fed54697
|
@ -23,19 +23,23 @@ module VagrantPlugins
|
||||||
return if env[:interrupted]
|
return if env[:interrupted]
|
||||||
|
|
||||||
# Try to get the IP
|
# Try to get the IP
|
||||||
network_info = env[:machine].provider.driver.read_guest_ip
|
begin
|
||||||
guest_ip = network_info["ip"]
|
network_info = env[:machine].provider.driver.read_guest_ip
|
||||||
|
guest_ip = network_info["ip"]
|
||||||
|
|
||||||
if guest_ip
|
if guest_ip
|
||||||
begin
|
begin
|
||||||
IPAddr.new(guest_ip)
|
IPAddr.new(guest_ip)
|
||||||
break
|
break
|
||||||
rescue IPAddr::InvalidAddressError
|
rescue IPAddr::InvalidAddressError
|
||||||
# Ignore, continue looking.
|
# Ignore, continue looking.
|
||||||
@logger.warn("Invalid IP address returned: #{guest_ip}")
|
@logger.warn("Invalid IP address returned: #{guest_ip}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
rescue Errors::PowerShellError
|
||||||
|
# Ignore, continue looking.
|
||||||
|
@logger.warn("Failed to read guest IP.")
|
||||||
end
|
end
|
||||||
|
|
||||||
sleep 1
|
sleep 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,21 +7,36 @@ Param(
|
||||||
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
|
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
|
||||||
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))
|
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))
|
||||||
|
|
||||||
$ip_address = ""
|
|
||||||
$vm = Get-VM -Id $VmId -ErrorAction "Stop"
|
$vm = Get-VM -Id $VmId -ErrorAction "Stop"
|
||||||
$networks = Get-VMNetworkAdapter -VM $vm
|
$networks = Get-VMNetworkAdapter -VM $vm
|
||||||
foreach ($network in $networks) {
|
foreach ($network in $networks) {
|
||||||
if ($network.IpAddresses.Length -gt 0) {
|
if ($network.IpAddresses.Length -gt 0) {
|
||||||
$ip_address = $network.IpAddresses[0]
|
foreach ($ip_address in $network.IpAddresses) {
|
||||||
if (-Not ([string]::IsNullOrEmpty($ip_address))) {
|
if ($ip_address.Contains(".")) {
|
||||||
# We found our IP address!
|
$ip4_address = $ip_address
|
||||||
break
|
} elseif ($ip_address.Contains(":")) {
|
||||||
|
$ip6_address = $ip_address
|
||||||
|
}
|
||||||
|
if (-Not ([string]::IsNullOrEmpty($ip4_address)) -Or -Not ([string]::IsNullOrEmpty($ip6_address))) {
|
||||||
|
# We found our IP address!
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$resultHash = @{
|
if (-Not ([string]::IsNullOrEmpty($ip4_address))) {
|
||||||
ip = "$ip_address"
|
$guest_ipaddress = $ip4_address
|
||||||
|
} elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) {
|
||||||
|
$guest_ipaddress = $ip6_address
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) {
|
||||||
|
$resultHash = @{
|
||||||
|
ip = $guest_ipaddress
|
||||||
|
}
|
||||||
|
$result = ConvertTo-Json $resultHash
|
||||||
|
Write-Output-Message $result
|
||||||
|
} else {
|
||||||
|
Write-Error-Message "Failed to determine IP address"
|
||||||
}
|
}
|
||||||
$result = ConvertTo-Json $resultHash
|
|
||||||
Write-Output-Message $result
|
|
||||||
|
|
Loading…
Reference in New Issue