packer-cn/builder/virtualbox/common/step_forward_ssh.go

85 lines
2.0 KiB
Go
Raw Normal View History

2013-12-22 12:08:09 -05:00
package common
import (
"fmt"
"log"
"math/rand"
"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
// on the guest machine.
//
// Uses:
2013-12-22 12:08:09 -05:00
// driver Driver
// ui packer.Ui
// vmName string
//
// Produces:
2013-12-22 12:08:09 -05:00
type StepForwardSSH struct {
CommConfig *communicator.Config
HostPortMin uint
HostPortMax uint
SkipNatMapping bool
2013-12-22 12:08:09 -05: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)
guestPort := s.CommConfig.Port()
sshHostPort := guestPort
2015-06-10 13:50:08 -04:00
if !s.SkipNatMapping {
log.Printf("Looking for available SSH port between %d and %d",
s.HostPortMin, s.HostPortMax)
offset := 0
portRange := int(s.HostPortMax - s.HostPortMin)
if portRange > 0 {
// Have to check if > 0 to avoid a panic
offset = rand.Intn(portRange)
}
for {
2015-06-18 04:19:46 -04:00
sshHostPort = offset + int(s.HostPortMin)
if sshHostPort >= int(s.HostPortMax) {
offset = 0
2015-06-18 04:19:46 -04:00
sshHostPort = int(s.HostPortMin)
}
log.Printf("Trying port: %d", sshHostPort)
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort))
if err == nil {
defer l.Close()
break
}
offset++
}
// Create a forwarded port mapping to the VM
ui.Say(fmt.Sprintf("Creating forwarded port mapping for SSH (host port %d)", sshHostPort))
command := []string{
"modifyvm", vmName,
"--natpf1",
fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, guestPort),
}
if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error creating port forwarding rule: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// 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)
return multistep.ActionContinue
}
2013-12-22 12:08:09 -05:00
func (s *StepForwardSSH) Cleanup(state multistep.StateBag) {}