Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update guest IP address discovery for hyper-v guests. #8831

Merged
merged 1 commit into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions plugins/providers/hyperv/action/wait_for_ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ def call(env)
return if env[:interrupted]

# Try to get the IP
network_info = env[:machine].provider.driver.read_guest_ip
guest_ip = network_info["ip"]

if guest_ip
begin
IPAddr.new(guest_ip)
break
rescue IPAddr::InvalidAddressError
# Ignore, continue looking.
@logger.warn("Invalid IP address returned: #{guest_ip}")
begin
network_info = env[:machine].provider.driver.read_guest_ip
guest_ip = network_info["ip"]

if guest_ip
begin
IPAddr.new(guest_ip)
break
rescue IPAddr::InvalidAddressError
# Ignore, continue looking.
@logger.warn("Invalid IP address returned: #{guest_ip}")
end
end
rescue Errors::PowerShellError
# Ignore, continue looking.
@logger.warn("Failed to read guest IP.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth it here to include the guest_ip attempt in the warning log?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@briancain guest_ip won't be set if that exception is rescued

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh true!

end

sleep 1
end
end
Expand Down
33 changes: 24 additions & 9 deletions plugins/providers/hyperv/scripts/get_network_config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@ Param(
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))

$ip_address = ""
$vm = Get-VM -Id $VmId -ErrorAction "Stop"
$networks = Get-VMNetworkAdapter -VM $vm
foreach ($network in $networks) {
if ($network.IpAddresses.Length -gt 0) {
$ip_address = $network.IpAddresses[0]
if (-Not ([string]::IsNullOrEmpty($ip_address))) {
# We found our IP address!
break
foreach ($ip_address in $network.IpAddresses) {
if ($ip_address.Contains(".")) {
$ip4_address = $ip_address
} 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 = @{
ip = "$ip_address"
if (-Not ([string]::IsNullOrEmpty($ip4_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