2013-12-22 12:08:09 -05:00
|
|
|
package common
|
2013-06-11 23:29:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step adds a NAT port forwarding definition so that SSH is available
|
|
|
|
// on the guest machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 12:08:09 -05:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
2013-06-11 23:29:39 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2013-12-22 12:08:09 -05:00
|
|
|
type StepForwardSSH struct {
|
|
|
|
GuestPort uint
|
|
|
|
HostPortMin uint
|
|
|
|
HostPortMax uint
|
|
|
|
}
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2013-12-22 12:08:09 -05:00
|
|
|
func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2013-12-22 12:08:09 -05:00
|
|
|
log.Printf("Looking for available SSH port between %d and %d",
|
|
|
|
s.HostPortMin, s.HostPortMax)
|
2013-06-11 23:29:39 -04:00
|
|
|
var sshHostPort uint
|
2013-11-04 17:20:26 -05:00
|
|
|
var offset uint = 0
|
|
|
|
|
2013-12-22 12:08:09 -05:00
|
|
|
portRange := int(s.HostPortMax - s.HostPortMin)
|
2013-11-04 17:20:26 -05:00
|
|
|
if portRange > 0 {
|
|
|
|
// Have to check if > 0 to avoid a panic
|
|
|
|
offset = uint(rand.Intn(portRange))
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
for {
|
2013-12-22 12:08:09 -05:00
|
|
|
sshHostPort = offset + s.HostPortMin
|
2013-06-11 23:29:39 -04:00
|
|
|
log.Printf("Trying port: %d", sshHostPort)
|
2013-12-22 12:08:09 -05:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort))
|
2013-06-11 23:29:39 -04:00
|
|
|
if err == nil {
|
|
|
|
defer l.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:44:48 -04:00
|
|
|
// Create a forwarded port mapping to the VM
|
2013-06-11 23:29:39 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating forwarded port mapping for SSH (host port %d)", sshHostPort))
|
|
|
|
command := []string{
|
|
|
|
"modifyvm", vmName,
|
|
|
|
"--natpf1",
|
2013-12-22 12:08:09 -05:00
|
|
|
fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, s.GuestPort),
|
2013-06-11 23:29:39 -04:00
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error creating port forwarding rule: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 23:29:39 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:30:07 -04:00
|
|
|
// Save the port we're using so that future steps can use it
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("sshHostPort", sshHostPort)
|
2013-06-11 23:30:07 -04:00
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:08:09 -05:00
|
|
|
func (s *StepForwardSSH) Cleanup(state multistep.StateBag) {}
|