step_forward_ssh.go 1.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package virtualbox

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:
//
// Produces:
type stepForwardSSH struct{}

20 21 22 23 24
func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*config)
	driver := state.Get("driver").(Driver)
	ui := state.Get("ui").(packer.Ui)
	vmName := state.Get("vmName").(string)
25 26 27 28 29 30 31 32 33 34 35 36 37 38

	log.Printf("Looking for available SSH port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax)
	var sshHostPort uint
	portRange := int(config.SSHHostPortMax - config.SSHHostPortMin)
	for {
		sshHostPort = uint(rand.Intn(portRange)) + config.SSHHostPortMin
		log.Printf("Trying port: %d", sshHostPort)
		l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort))
		if err == nil {
			defer l.Close()
			break
		}
	}

39
	// Create a forwarded port mapping to the VM
40 41 42 43 44 45 46
	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, config.SSHPort),
	}
	if err := driver.VBoxManage(command...); err != nil {
47
		err := fmt.Errorf("Error creating port forwarding rule: %s", err)
48
		state.Put("error", err)
49
		ui.Error(err.Error())
50 51 52
		return multistep.ActionHalt
	}

53
	// Save the port we're using so that future steps can use it
54
	state.Put("sshHostPort", sshHostPort)
55

56 57 58
	return multistep.ActionContinue
}

59
func (s *stepForwardSSH) Cleanup(state multistep.StateBag) {}