github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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 SSH 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 log.Printf("Trying port: %d", sshHostPort) 38 l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort)) 39 if err == nil { 40 defer l.Close() 41 break 42 } 43 } 44 ui.Say(fmt.Sprintf("Found port for SSH: %d.", sshHostPort)) 45 46 // Save the port we're using so that future steps can use it 47 state.Put("sshHostPort", sshHostPort) 48 49 return multistep.ActionContinue 50 } 51 52 func (s *stepForwardSSH) Cleanup(state multistep.StateBag) {}