Commit 7fc69828 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/virtualbox: fix forwarding to work with WinRM

parent cf570a71
...@@ -2,11 +2,13 @@ package common ...@@ -2,11 +2,13 @@ package common
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/packer"
) )
// This step adds a NAT port forwarding definition so that SSH is available // This step adds a NAT port forwarding definition so that SSH is available
...@@ -19,7 +21,7 @@ import ( ...@@ -19,7 +21,7 @@ import (
// //
// Produces: // Produces:
type StepForwardSSH struct { type StepForwardSSH struct {
GuestPort uint CommConfig *communicator.Config
HostPortMin uint HostPortMin uint
HostPortMax uint HostPortMax uint
SkipNatMapping bool SkipNatMapping bool
...@@ -30,20 +32,21 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction { ...@@ -30,20 +32,21 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string) vmName := state.Get("vmName").(string)
sshHostPort := s.GuestPort guestPort := s.CommConfig.Port()
sshHostPort := guestPort
if !s.SkipNatMapping { if !s.SkipNatMapping {
log.Printf("Looking for available SSH port between %d and %d", log.Printf("Looking for available SSH port between %d and %d",
s.HostPortMin, s.HostPortMax) s.HostPortMin, s.HostPortMax)
var offset uint = 0 offset := 0
portRange := int(s.HostPortMax - s.HostPortMin) portRange := int(s.HostPortMax - s.HostPortMin)
if portRange > 0 { if portRange > 0 {
// Have to check if > 0 to avoid a panic // Have to check if > 0 to avoid a panic
offset = uint(rand.Intn(portRange)) offset = rand.Intn(portRange)
} }
for { for {
sshHostPort = offset + s.HostPortMin sshHostPort = offset + int(s.HostPortMin)
log.Printf("Trying port: %d", sshHostPort) log.Printf("Trying port: %d", sshHostPort)
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort)) l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort))
if err == nil { if err == nil {
...@@ -57,7 +60,7 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction { ...@@ -57,7 +60,7 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
command := []string{ command := []string{
"modifyvm", vmName, "modifyvm", vmName,
"--natpf1", "--natpf1",
fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, s.GuestPort), fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, guestPort),
} }
if err := driver.VBoxManage(command...); err != nil { if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error creating port forwarding rule: %s", err) err := fmt.Errorf("Error creating port forwarding rule: %s", err)
......
...@@ -254,7 +254,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -254,7 +254,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
new(vboxcommon.StepAttachFloppy), new(vboxcommon.StepAttachFloppy),
&vboxcommon.StepForwardSSH{ &vboxcommon.StepForwardSSH{
GuestPort: uint(b.config.SSHConfig.Comm.SSHPort), CommConfig: &b.config.SSHConfig.Comm,
HostPortMin: b.config.SSHHostPortMin, HostPortMin: b.config.SSHHostPortMin,
HostPortMax: b.config.SSHHostPortMax, HostPortMax: b.config.SSHHostPortMax,
SkipNatMapping: b.config.SSHSkipNatMapping, SkipNatMapping: b.config.SSHSkipNatMapping,
......
...@@ -83,7 +83,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -83,7 +83,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
new(vboxcommon.StepAttachFloppy), new(vboxcommon.StepAttachFloppy),
&vboxcommon.StepForwardSSH{ &vboxcommon.StepForwardSSH{
GuestPort: uint(b.config.SSHConfig.Comm.SSHPort), CommConfig: &b.config.SSHConfig.Comm,
HostPortMin: b.config.SSHHostPortMin, HostPortMin: b.config.SSHHostPortMin,
HostPortMax: b.config.SSHHostPortMax, HostPortMax: b.config.SSHHostPortMax,
SkipNatMapping: b.config.SSHSkipNatMapping, SkipNatMapping: b.config.SSHSkipNatMapping,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment