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
|
|
|
|
2019-03-15 11:51:23 -04:00
|
|
|
"github.com/hashicorp/packer/common/net"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-11 23:29:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
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
|
|
|
|
2019-03-15 11:51:23 -04:00
|
|
|
func (s *StepForwardSSH) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 12:08:09 -05:00
|
|
|
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
|
|
|
|
2017-06-02 15:58:34 -04:00
|
|
|
if s.CommConfig.Type == "none" {
|
|
|
|
log.Printf("Not using a communicator, skipping setting up port forwarding...")
|
|
|
|
state.Put("sshHostPort", 0)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:47:21 -04:00
|
|
|
guestPort := s.CommConfig.Port()
|
2015-06-15 00:47:53 -04:00
|
|
|
sshHostPort := 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
|
|
|
|
sshHostPort = s.l.Port
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2014-03-27 02:11:34 -04:00
|
|
|
// Create a forwarded port mapping to the VM
|
2015-09-17 07:42:58 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port %d)", sshHostPort))
|
2014-03-27 02:11:34 -04:00
|
|
|
command := []string{
|
|
|
|
"modifyvm", vmName,
|
|
|
|
"--natpf1",
|
2015-09-17 07:42:58 -04:00
|
|
|
fmt.Sprintf("packercomm,tcp,127.0.0.1,%d,,%d", sshHostPort, 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
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:51:23 -04:00
|
|
|
func (s *StepForwardSSH) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.l != nil {
|
|
|
|
err := s.l.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to unlock port lockfile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|