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"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
2015-06-15 00:47:53 -04:00
|
|
|
|
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
|
2014-03-27 02:11:34 -04:00
|
|
|
HostPortMin uint
|
|
|
|
HostPortMax uint
|
|
|
|
SkipNatMapping bool
|
2013-12-22 12:08:09 -05:00
|
|
|
}
|
2013-06-11 23:29:39 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepForwardSSH) Run(_ 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
|
|
|
|
}
|
|
|
|
|
2015-06-15 00:47:53 -04:00
|
|
|
guestPort := s.CommConfig.Port()
|
|
|
|
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
|
|
|
|
2015-09-28 23:09:38 -04:00
|
|
|
portRange := int(s.HostPortMax - s.HostPortMin + 1)
|
|
|
|
offset := rand.Intn(portRange)
|
2013-11-04 17:20:26 -05:00
|
|
|
|
2014-03-27 02:11:34 -04:00
|
|
|
for {
|
2015-06-18 04:19:46 -04:00
|
|
|
sshHostPort = offset + int(s.HostPortMin)
|
2014-03-27 02:11:34 -04:00
|
|
|
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
|
|
|
|
}
|
2015-06-18 04:18:17 -04:00
|
|
|
offset++
|
2015-09-28 23:09:38 -04:00
|
|
|
if offset == portRange {
|
|
|
|
offset = 0
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:08:09 -05:00
|
|
|
func (s *StepForwardSSH) Cleanup(state multistep.StateBag) {}
|