hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.18k stars 4.43k forks source link

port forvarding in hyperv #7776

Open maxx-ukoo opened 8 years ago

maxx-ukoo commented 8 years ago

Host operating system

Windows 10

Guest operating system

any

Expected behavior

Vagrant hyperv provider should forward connections to VM Windows has command: netsh interface portproxy add v4tov4 listenport=27017 connectaddress=192.168.0.151 connectport=27017 vagrant can use it.

Actual behavior

hyperv provider doesn't forward ports to vm

xpicio commented 7 years ago

👍

Would be a great simplification, at the moment to work with .NET remote debugger with vagrant and hyperv i'm using a couple of scripts:

run-me.bat

@echo off

echo *** Set ExecutionPolicy to RemoteSigned ***
@powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
echo.

echo *** Create devops local user ***
@powershell .\add-devops-users.ps1
echo.

echo *** Setup Vagrant plugins ***
vagrant plugin install vagrant-reload
vagrant plugin install vagrant-address

echo *** Vagrant up ***
vagrant up 

if %errorlevel% == 0 (
    call ::port_forwarding_setup
) else (
    exit /b 1
)

goto :end

:port_forwarding_setup
    set command="vagrant address"
    for /f %%i in (' %command% ') do set VIRTUAL_MACHINE_IP=%%i

    @powershell .\port-forwarding-setup.ps1 -GuestIPAddress %VIRTUAL_MACHINE_IP%
goto :eof

:end

port-forwarding-setup.ps1

param (
    [string]$GuestIPAddress = $(throw "-GuestIPAddress is required. ")
 )

 $guestAddress = $GuestIPAddress
 $settings = Import-LocalizedData -BaseDirectory . -FileName develop-config.psd1

# validate guest ip address
try { 
    $ip = [System.Net.IPAddress]::parse($guestAddress)
} 
catch { 
    "`"{0}`" is not a valid IP address" -f $guestAddress
    exit 1
} 

# clean up portproxy forward rules
Write-Host *** Cleanup local port forwarding from localhost to guest virtual machine ***`n

netsh interface portproxy delete v4tov4 protocol=tcp `
    listenport=$($settings.RemoteDebuggerHostListenPortx64) listenaddress=localhost | Out-Null

netsh interface portproxy delete v4tov4 protocol=tcp `
    listenport=$($settings.RemoteDebuggerHostListenPortx86) listenaddress=localhost | Out-Null

# add new portproxy forward rules
Write-Host *** Add local port forwarding from localhost to guest virtual machine ***`n

netsh interface portproxy add v4tov4 protocol=tcp `
    listenport=$($settings.RemoteDebuggerHostListenPortx64) listenaddress=localhost `
    connectport=4018 connectaddress=$guestAddress | Out-Null

netsh interface portproxy add v4tov4 protocol=tcp `
    listenport=$($settings.RemoteDebuggerHostListenPortx86) listenaddress=localhost `
    connectport=4019 connectaddress=$guestAddress | Out-Null

# print out result
Write-Host *** Port forwarding has been set as following ***
netsh interface portproxy dump | findstr $guestAddress

exit 0

develop-config.psd1

@{
    RemoteDebuggerHostListenPortx64 = 60134
    RemoteDebuggerHostListenPortx86 = 60135
    RemoteDebuggerGuestListenPortx64 = 4018
    RemoteDebuggerGuestListenPortx86 = 4019
}