github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  
    28  	portRange := config.SSHHostPortMax - config.SSHHostPortMin + 1
    29  	offset := uint(rand.Intn(int(portRange)))
    30  
    31  	for {
    32  		sshHostPort = offset + config.SSHHostPortMin
    33  		log.Printf("Trying port: %d", sshHostPort)
    34  		l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort))
    35  		if err == nil {
    36  			defer l.Close()
    37  			break
    38  		}
    39  		offset++
    40  		if offset == portRange {
    41  			offset = 0
    42  		}
    43  	}
    44  	ui.Say(fmt.Sprintf("Found port for communicator (SSH, WinRM, etc): %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) {}