github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/qemu/step_configure_vnc.go (about)

     1  package qemu
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"math/rand"
     7  	"net"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/mitchellh/multistep"
    11  )
    12  
    13  // This step configures the VM to enable the VNC server.
    14  //
    15  // Uses:
    16  //   config *config
    17  //   ui     packer.Ui
    18  //
    19  // Produces:
    20  //   vnc_port uint - The port that VNC is configured to listen on.
    21  type stepConfigureVNC struct{}
    22  
    23  func (stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
    24  	config := state.Get("config").(*Config)
    25  	ui := state.Get("ui").(packer.Ui)
    26  
    27  	// Find an open VNC port. Note that this can still fail later on
    28  	// because we have to release the port at some point. But this does its
    29  	// best.
    30  	msg := fmt.Sprintf("Looking for available port between %d and %d on %s", config.VNCPortMin, config.VNCPortMax, config.VNCBindAddress)
    31  	ui.Say(msg)
    32  	log.Print(msg)
    33  	var vncPort uint
    34  	portRange := int(config.VNCPortMax - config.VNCPortMin)
    35  	for {
    36  		if portRange > 0 {
    37  			vncPort = uint(rand.Intn(portRange)) + config.VNCPortMin
    38  		} else {
    39  			vncPort = config.VNCPortMin
    40  		}
    41  
    42  		log.Printf("Trying port: %d", vncPort)
    43  		l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.VNCBindAddress, vncPort))
    44  		if err == nil {
    45  			defer l.Close()
    46  			break
    47  		}
    48  	}
    49  
    50  	log.Printf("Found available VNC port: %d on IP: %s", vncPort, config.VNCBindAddress)
    51  	state.Put("vnc_port", vncPort)
    52  	state.Put("vnc_ip", config.VNCBindAddress)
    53  
    54  	return multistep.ActionContinue
    55  }
    56  
    57  func (stepConfigureVNC) Cleanup(multistep.StateBag) {}