builder/virtualbox: Create forwarded port mapping
This commit is contained in:
parent
081b0d6853
commit
ad1c59f34e
@ -26,6 +26,9 @@ type config struct {
|
|||||||
ISOMD5 string `mapstructure:"iso_md5"`
|
ISOMD5 string `mapstructure:"iso_md5"`
|
||||||
ISOUrl string `mapstructure:"iso_url"`
|
ISOUrl string `mapstructure:"iso_url"`
|
||||||
OutputDir string `mapstructure:"output_directory"`
|
OutputDir string `mapstructure:"output_directory"`
|
||||||
|
SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
|
||||||
|
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
|
||||||
|
SSHPort uint `mapstructure:"ssh_port"`
|
||||||
VMName string `mapstructure:"vm_name"`
|
VMName string `mapstructure:"vm_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +46,18 @@ func (b *Builder) Prepare(raw interface{}) error {
|
|||||||
b.config.OutputDir = "virtualbox"
|
b.config.OutputDir = "virtualbox"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.config.SSHHostPortMin == 0 {
|
||||||
|
b.config.SSHHostPortMin = 2222
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.config.SSHHostPortMax == 0 {
|
||||||
|
b.config.SSHHostPortMax = 4444
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.config.SSHPort == 0 {
|
||||||
|
b.config.SSHPort = 22
|
||||||
|
}
|
||||||
|
|
||||||
if b.config.VMName == "" {
|
if b.config.VMName == "" {
|
||||||
b.config.VMName = "packer"
|
b.config.VMName = "packer"
|
||||||
}
|
}
|
||||||
@ -114,6 +129,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer
|
|||||||
new(stepCreateVM),
|
new(stepCreateVM),
|
||||||
new(stepCreateDisk),
|
new(stepCreateDisk),
|
||||||
new(stepAttachISO),
|
new(stepAttachISO),
|
||||||
|
new(stepForwardSSH),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the state bag
|
// Setup the state bag
|
||||||
|
@ -38,6 +38,18 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
|
|||||||
t.Errorf("bad output dir: %s", b.config.OutputDir)
|
t.Errorf("bad output dir: %s", b.config.OutputDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.config.SSHHostPortMin != 2222 {
|
||||||
|
t.Errorf("bad min ssh host port: %d", b.config.SSHHostPortMin)
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.config.SSHHostPortMax != 4444 {
|
||||||
|
t.Errorf("bad max ssh host port: %d", b.config.SSHHostPortMax)
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.config.SSHPort != 22 {
|
||||||
|
t.Errorf("bad ssh port: %d", b.config.SSHPort)
|
||||||
|
}
|
||||||
|
|
||||||
if b.config.VMName != "packer" {
|
if b.config.VMName != "packer" {
|
||||||
t.Errorf("bad vm name: %s", b.config.VMName)
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// This step attaches the ISO to the virtual machine.
|
// This step attaches the ISO to the virtual machine.
|
||||||
@ -39,7 +38,6 @@ func (s *stepAttachISO) Run(state map[string]interface{}) multistep.StepAction {
|
|||||||
// Track the path so that we can unregister it from VirtualBox later
|
// Track the path so that we can unregister it from VirtualBox later
|
||||||
s.diskPath = isoPath
|
s.diskPath = isoPath
|
||||||
|
|
||||||
time.Sleep(15 * time.Second)
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +48,17 @@ func (s *stepAttachISO) Cleanup(state map[string]interface{}) {
|
|||||||
|
|
||||||
driver := state["driver"].(Driver)
|
driver := state["driver"].(Driver)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state["ui"].(packer.Ui)
|
||||||
|
vmName := state["vmName"].(string)
|
||||||
|
|
||||||
if err := driver.VBoxManage("closemedium", "disk", s.diskPath); err != nil {
|
command := []string{
|
||||||
|
"storageattach", vmName,
|
||||||
|
"--storagectl", "IDE Controller",
|
||||||
|
"--port", "0",
|
||||||
|
"--device", "1",
|
||||||
|
"--medium", "none",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := driver.VBoxManage(command...); err != nil {
|
||||||
ui.Error(fmt.Sprintf("Error unregistering ISO: %s", err))
|
ui.Error(fmt.Sprintf("Error unregistering ISO: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@ import (
|
|||||||
|
|
||||||
// This step creates the virtual disk that will be used as the
|
// This step creates the virtual disk that will be used as the
|
||||||
// hard drive for the virtual machine.
|
// hard drive for the virtual machine.
|
||||||
type stepCreateDisk struct {
|
type stepCreateDisk struct {}
|
||||||
diskPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
config := state["config"].(*config)
|
config := state["config"].(*config)
|
||||||
@ -38,9 +36,6 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction
|
|||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the path so that we can delete it later
|
|
||||||
s.diskPath = path
|
|
||||||
|
|
||||||
// Add the IDE controller so we can later attach the disk
|
// Add the IDE controller so we can later attach the disk
|
||||||
controllerName := "IDE Controller"
|
controllerName := "IDE Controller"
|
||||||
err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide")
|
err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide")
|
||||||
@ -66,16 +61,4 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction
|
|||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateDisk) Cleanup(state map[string]interface{}) {
|
func (s *stepCreateDisk) Cleanup(state map[string]interface{}) {}
|
||||||
if s.diskPath == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
driver := state["driver"].(Driver)
|
|
||||||
ui := state["ui"].(packer.Ui)
|
|
||||||
|
|
||||||
ui.Say("Unregistering and deleting hard disk...")
|
|
||||||
if err := driver.VBoxManage("closemedium", "disk", s.diskPath, "--delete"); err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Error deleting hard drive: %s", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
56
builder/virtualbox/step_forward_ssh.go
Normal file
56
builder/virtualbox/step_forward_ssh.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package virtualbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This step adds a NAT port forwarding definition so that SSH is available
|
||||||
|
// on the guest machine.
|
||||||
|
//
|
||||||
|
// Uses:
|
||||||
|
//
|
||||||
|
// Produces:
|
||||||
|
type stepForwardSSH struct{}
|
||||||
|
|
||||||
|
func (s *stepForwardSSH) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
|
config := state["config"].(*config)
|
||||||
|
driver := state["driver"].(Driver)
|
||||||
|
ui := state["ui"].(packer.Ui)
|
||||||
|
vmName := state["vmName"].(string)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attach the disk to the controller
|
||||||
|
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 {
|
||||||
|
ui.Error(fmt.Sprintf("Error creating port forwarding rule: %s", err))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stepForwardSSH) Cleanup(state map[string]interface{}) {}
|
Loading…
x
Reference in New Issue
Block a user