github.com/rothwerx/packer@v0.9.0/builder/qemu/step_forward_ssh.go (about) 1 package qemu 2 3 import ( 4 "fmt" 5 "log" 6 "math/rand" 7 "net" 8 9 "github.com/mitchellh/multistep" 10 "github.com/mitchellh/packer/packer" 11 ) 12 13 // This step adds a NAT port forwarding definition so that SSH is available 14 // on the guest machine. 15 // 16 // Uses: 17 // 18 // Produces: 19 type stepForwardSSH struct{} 20 21 func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction { 22 config := state.Get("config").(*Config) 23 ui := state.Get("ui").(packer.Ui) 24 25 log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax) 26 var sshHostPort uint 27 var offset uint = 0 28 29 portRange := int(config.SSHHostPortMax - config.SSHHostPortMin) 30 if portRange > 0 { 31 // Have to check if > 0 to avoid a panic 32 offset = uint(rand.Intn(portRange)) 33 } 34 35 for { 36 sshHostPort = offset + config.SSHHostPortMin 37 if sshHostPort >= config.SSHHostPortMax { 38 offset = 0 39 sshHostPort = config.SSHHostPortMin 40 } 41 log.Printf("Trying port: %d", sshHostPort) 42 l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort)) 43 if err == nil { 44 defer l.Close() 45 break 46 } 47 offset++ 48 } 49 ui.Say(fmt.Sprintf("Found port for communicator (SSH, WinRM, etc): %d.", sshHostPort)) 50 51 // Save the port we're using so that future steps can use it 52 state.Put("sshHostPort", sshHostPort) 53 54 return multistep.ActionContinue 55 } 56 57 func (s *stepForwardSSH) Cleanup(state multistep.StateBag) {}