2019-07-03 20:33:59 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/digitalocean/go-qemu/qmp"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the QMP listener.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type stepConfigureQMP struct {
|
|
|
|
monitor *qmp.SocketMonitor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigureQMP) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2019-07-16 10:55:19 -04:00
|
|
|
if !config.VNCUsePassword {
|
2019-07-03 20:33:59 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:38:56 -04:00
|
|
|
msg := fmt.Sprintf("QMP socket at: %s", config.QMPSocketPath)
|
2019-07-03 20:33:59 -04:00
|
|
|
ui.Say(msg)
|
|
|
|
log.Print(msg)
|
|
|
|
|
2019-07-03 20:38:56 -04:00
|
|
|
// Only initialize and open QMP when we have a use for it.
|
2019-07-16 10:55:19 -04:00
|
|
|
// Open QMP socket
|
|
|
|
var err error
|
|
|
|
var cmd []byte
|
|
|
|
var result []byte
|
|
|
|
s.monitor, err = qmp.NewSocketMonitor("unix", config.QMPSocketPath, 2*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error opening QMP socket: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
QMPMonitor := s.monitor
|
|
|
|
vncPassword := state.Get("vnc_password")
|
2019-07-03 20:33:59 -04:00
|
|
|
|
2019-07-16 10:55:19 -04:00
|
|
|
// Connect to QMP
|
|
|
|
// function automatically calls capabilities so is immediately ready for commands
|
|
|
|
err = QMPMonitor.Connect()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error connecting to QMP socket: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
log.Printf("QMP socket open SUCCESS")
|
2019-07-03 20:33:59 -04:00
|
|
|
|
2019-07-16 10:55:19 -04:00
|
|
|
cmd = []byte(fmt.Sprintf("{ \"execute\": \"change-vnc-password\", \"arguments\": { \"password\": \"%s\" } }",
|
|
|
|
vncPassword))
|
|
|
|
result, err = QMPMonitor.Run(cmd)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error connecting to QMP socket: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2019-07-03 20:38:56 -04:00
|
|
|
}
|
2019-07-16 10:55:19 -04:00
|
|
|
msg = fmt.Sprintf("QMP Command: %s\nResult: %s", cmd, result)
|
|
|
|
log.Printf(msg)
|
|
|
|
|
|
|
|
// Put QMP monitor in statebag in case there is a use in a following step
|
|
|
|
// Uncomment for future case as it is unused for now
|
|
|
|
//state.Put("qmp_monitor", QMPMonitor)
|
2019-07-03 20:33:59 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigureQMP) Cleanup(multistep.StateBag) {
|
2019-07-03 20:38:56 -04:00
|
|
|
if s.monitor != nil {
|
|
|
|
err := s.monitor.Disconnect()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to disconnect QMP: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 20:33:59 -04:00
|
|
|
}
|