2013-12-22 12:08:09 -05:00
|
|
|
package common
|
2013-06-11 23:29:39 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 23:29:39 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-02-16 08:32:22 -05:00
|
|
|
"strings"
|
2015-06-15 00:47:53 -04:00
|
|
|
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/net"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-06-11 23:29:39 -04:00
|
|
|
)
|
|
|
|
|
2020-01-09 11:37:30 -05:00
|
|
|
// This step adds a NAT port forwarding definition so that SSH or WinRM is available
|
2013-06-11 23:29:39 -04:00
|
|
|
// on the guest machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-12-22 12:08:09 -05:00
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2013-12-22 12:08:09 -05:00
|
|
|
// vmName string
|
2013-06-11 23:29:39 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2020-01-09 11:37:30 -05:00
|
|
|
type StepPortForwarding struct {
|
2015-06-15 00:47:53 -04:00
|
|
|
CommConfig *communicator.Config
|
2019-03-19 09:47:21 -04:00
|
|
|
HostPortMin int
|
|
|
|
HostPortMax int
|
2014-03-27 02:11:34 -04:00
|
|
|
SkipNatMapping bool
|
2019-03-15 11:51:23 -04:00
|
|
|
|
|
|
|
l *net.Listener
|
2013-12-22 12:08:09 -05:00
|
|
|
}
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2020-01-09 11:37:30 -05:00
|
|
|
func (s *StepPortForwarding) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 12:08:09 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-08-31 15:44:58 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2017-06-02 15:58:34 -04:00
|
|
|
if s.CommConfig.Type == "none" {
|
|
|
|
log.Printf("Not using a communicator, skipping setting up port forwarding...")
|
2020-01-09 11:37:30 -05:00
|
|
|
state.Put("commHostPort", 0)
|
2017-06-02 15:58:34 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:47:21 -04:00
|
|
|
guestPort := s.CommConfig.Port()
|
2020-01-09 11:37:30 -05:00
|
|
|
commHostPort := guestPort
|
2015-06-10 13:50:08 -04:00
|
|
|
if !s.SkipNatMapping {
|
2015-09-17 07:42:58 -04:00
|
|
|
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d",
|
2014-03-27 02:11:34 -04:00
|
|
|
s.HostPortMin, s.HostPortMax)
|
2013-11-04 17:20:26 -05:00
|
|
|
|
2019-03-15 11:51:23 -04:00
|
|
|
var err error
|
|
|
|
s.l, err = net.ListenRangeConfig{
|
|
|
|
Addr: "127.0.0.1",
|
|
|
|
Min: s.HostPortMin,
|
|
|
|
Max: s.HostPortMax,
|
|
|
|
Network: "tcp",
|
|
|
|
}.Listen(ctx)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating port forwarding rule: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-06-11 23:29:39 -04:00
|
|
|
}
|
2019-03-15 11:51:23 -04:00
|
|
|
s.l.Listener.Close() // free port, but don't unlock lock file
|
2020-01-09 11:37:30 -05:00
|
|
|
commHostPort = s.l.Port
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2019-12-19 11:03:31 -05:00
|
|
|
// Make sure to configure the network interface to NAT
|
|
|
|
command := []string{
|
|
|
|
"modifyvm", vmName,
|
|
|
|
"--nic1",
|
|
|
|
"nat",
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Failed to configure NAT interface: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-03-27 02:11:34 -04:00
|
|
|
// Create a forwarded port mapping to the VM
|
2020-01-09 11:37:30 -05:00
|
|
|
ui.Say(fmt.Sprintf("Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port %d)", commHostPort))
|
2019-12-19 11:03:31 -05:00
|
|
|
command = []string{
|
2014-03-27 02:11:34 -04:00
|
|
|
"modifyvm", vmName,
|
|
|
|
"--natpf1",
|
2020-01-09 11:37:30 -05:00
|
|
|
fmt.Sprintf("packercomm,tcp,127.0.0.1,%d,,%d", commHostPort, guestPort),
|
2014-03-27 02:11:34 -04:00
|
|
|
}
|
2019-03-10 10:38:38 -04:00
|
|
|
retried := false
|
|
|
|
retry:
|
2014-03-27 02:11:34 -04:00
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2019-03-10 10:38:38 -04:00
|
|
|
if !strings.Contains(err.Error(), "A NAT rule of this name already exists") || retried {
|
2019-02-16 08:32:22 -05:00
|
|
|
err := fmt.Errorf("Error creating port forwarding rule: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2019-03-10 10:38:38 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("A packer NAT rule already exists. Trying to delete ...")
|
|
|
|
delcommand := []string{
|
|
|
|
"modifyvm", vmName,
|
|
|
|
"--natpf1",
|
|
|
|
"delete", "packercomm",
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(delcommand...); err != nil {
|
|
|
|
err := fmt.Errorf("Error deleting packer NAT forwarding rule: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
goto retry
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|
2014-03-27 02:11:34 -04:00
|
|
|
}
|
2013-06-11 23:29:39 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 23:30:07 -04:00
|
|
|
// Save the port we're using so that future steps can use it
|
2020-01-09 11:37:30 -05:00
|
|
|
state.Put("commHostPort", commHostPort)
|
2013-06-11 23:30:07 -04:00
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:37:30 -05:00
|
|
|
func (s *StepPortForwarding) Cleanup(state multistep.StateBag) {
|
2019-03-15 11:51:23 -04:00
|
|
|
if s.l != nil {
|
|
|
|
err := s.l.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to unlock port lockfile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|