2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-09-02 23:23:52 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-07-03 20:38:56 -04:00
|
|
|
"math/rand"
|
2017-03-28 21:06:50 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/net"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the VNC server.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
2019-03-19 09:47:21 -04:00
|
|
|
// vnc_port int - The port that VNC is configured to listen on.
|
2019-03-18 08:29:12 -04:00
|
|
|
type stepConfigureVNC struct {
|
|
|
|
l *net.Listener
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-07-03 20:38:56 -04:00
|
|
|
func VNCPassword() string {
|
|
|
|
length := int(8)
|
|
|
|
|
|
|
|
charSet := []byte("012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
charSetLength := len(charSet)
|
|
|
|
|
|
|
|
password := make([]byte, length)
|
|
|
|
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
password[i] = charSet[rand.Intn(charSetLength)]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(password)
|
|
|
|
}
|
|
|
|
|
2019-03-18 08:29:12 -04:00
|
|
|
func (s *stepConfigureVNC) Run(ctx context.Context, 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)
|
2017-03-28 21:06:50 -04:00
|
|
|
log.Print(msg)
|
2015-06-17 23:23:04 -04:00
|
|
|
|
2019-07-03 20:38:56 -04:00
|
|
|
var vncPassword string
|
2019-03-18 08:29:12 -04:00
|
|
|
var err error
|
|
|
|
s.l, err = net.ListenRangeConfig{
|
|
|
|
Addr: config.VNCBindAddress,
|
|
|
|
Min: config.VNCPortMin,
|
|
|
|
Max: config.VNCPortMax,
|
|
|
|
Network: "tcp",
|
|
|
|
}.Listen(ctx)
|
|
|
|
if err != nil {
|
2019-07-03 20:38:56 -04:00
|
|
|
err := fmt.Errorf("Error finding VNC port: %s", err)
|
2019-03-18 08:29:12 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
2019-03-18 08:29:12 -04:00
|
|
|
s.l.Listener.Close() // free port, but don't unlock lock file
|
|
|
|
vncPort := s.l.Port
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-07-03 20:38:56 -04:00
|
|
|
if config.VNCUsePassword {
|
|
|
|
vncPassword = VNCPassword()
|
|
|
|
} else {
|
|
|
|
vncPassword = ""
|
|
|
|
}
|
|
|
|
|
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)
|
2019-07-03 20:38:56 -04:00
|
|
|
state.Put("vnc_password", vncPassword)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-03-18 08:29:12 -04:00
|
|
|
func (s *stepConfigureVNC) Cleanup(multistep.StateBag) {
|
|
|
|
if s.l != nil {
|
|
|
|
err := s.l.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to unlock port lockfile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|