2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the VNC server.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// vnc_port uint - The port that VNC is configured to listen on.
|
|
|
|
type stepConfigureVNC struct{}
|
|
|
|
|
|
|
|
func (stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-09-02 23:23:52 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
// Find an open VNC port. Note that this can still fail later on
|
|
|
|
// because we have to release the port at some point. But this does its
|
|
|
|
// best.
|
2016-05-25 05:10:12 -04:00
|
|
|
msg := fmt.Sprintf("Looking for available port between %d and %d on %s", config.VNCPortMin, config.VNCPortMax, config.VNCBindAddress)
|
2013-09-02 23:23:52 -04:00
|
|
|
ui.Say(msg)
|
|
|
|
log.Printf(msg)
|
|
|
|
var vncPort uint
|
|
|
|
portRange := int(config.VNCPortMax - config.VNCPortMin)
|
|
|
|
for {
|
2015-06-17 15:31:55 -04:00
|
|
|
if portRange > 0 {
|
2015-06-17 23:23:04 -04:00
|
|
|
vncPort = uint(rand.Intn(portRange)) + config.VNCPortMin
|
2015-06-17 15:31:55 -04:00
|
|
|
} else {
|
|
|
|
vncPort = config.VNCPortMin
|
|
|
|
}
|
2015-06-17 23:23:04 -04:00
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
log.Printf("Trying port: %d", vncPort)
|
2016-05-25 05:10:12 -04:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.VNCBindAddress, vncPort))
|
2013-09-02 23:23:52 -04:00
|
|
|
if err == nil {
|
|
|
|
defer l.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 05:10:12 -04:00
|
|
|
log.Printf("Found available VNC port: %d on IP: %s", vncPort, config.VNCBindAddress)
|
2013-09-02 23:23:52 -04:00
|
|
|
state.Put("vnc_port", vncPort)
|
2016-05-25 05:10:12 -04:00
|
|
|
state.Put("vnc_ip", config.VNCBindAddress)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stepConfigureVNC) Cleanup(multistep.StateBag) {}
|