2019-07-03 20:33:59 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-01-07 05:18:01 -05:00
|
|
|
"os"
|
2019-07-03 20:33:59 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/digitalocean/go-qemu/qmp"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-07-03 20:33:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the QMP listener.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2019-07-03 20:33:59 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type stepConfigureQMP struct {
|
2020-05-03 09:47:03 -04:00
|
|
|
monitor *qmp.SocketMonitor
|
|
|
|
QMPSocketPath string
|
2019-07-03 20:33:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigureQMP) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-05-03 09:47:03 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-07-03 20:33:59 -04:00
|
|
|
|
2020-05-03 09:47:03 -04:00
|
|
|
if !config.QMPEnable {
|
2019-07-03 20:33:59 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-01-07 05:18:01 -05:00
|
|
|
ui.Say(fmt.Sprintf("QMP socket at: %s", s.QMPSocketPath))
|
2019-07-03 20:33:59 -04:00
|
|
|
|
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
|
2020-01-07 05:18:01 -05:00
|
|
|
s.monitor, err = qmp.NewSocketMonitor("unix", s.QMPSocketPath, 2*time.Second)
|
2019-07-16 10:55:19 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error opening QMP socket: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
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
|
2020-05-03 09:47:03 -04:00
|
|
|
err = s.monitor.Connect()
|
2019-07-16 10:55:19 -04:00
|
|
|
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
|
|
|
|
2020-05-03 09:47:03 -04:00
|
|
|
vncPassword := state.Get("vnc_password")
|
|
|
|
if vncPassword != "" {
|
|
|
|
cmd = []byte(fmt.Sprintf("{ \"execute\": \"change-vnc-password\", \"arguments\": { \"password\": \"%s\" } }",
|
|
|
|
vncPassword))
|
|
|
|
result, err = s.monitor.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
|
|
|
|
}
|
|
|
|
log.Printf("QMP Command: %s\nResult: %s", cmd, result)
|
2019-07-03 20:38:56 -04:00
|
|
|
}
|
2020-01-07 05:18:01 -05:00
|
|
|
|
2020-05-03 09:47:03 -04:00
|
|
|
// make the qmp_monitor available to other steps.
|
|
|
|
state.Put("qmp_monitor", s.monitor)
|
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)
|
|
|
|
}
|
2020-01-07 05:18:01 -05:00
|
|
|
// Delete file associated with qmp socket.
|
|
|
|
if err := os.Remove(s.QMPSocketPath); err != nil {
|
|
|
|
log.Printf("Failed to delete the qmp socket file: %s", err)
|
|
|
|
}
|
2019-07-03 20:38:56 -04:00
|
|
|
}
|
2019-07-03 20:33:59 -04:00
|
|
|
}
|