From 0815cb994436ded0643e6570a27361a50ca5fb9f Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Tue, 13 Sep 2022 18:51:33 +0200 Subject: [PATCH] Add qemu guest pause and hibernate commands --- .../guest/qemu/guest-qemu-hibernate.go | 25 +++++++++++++++++++ cli/command/guest/qemu/guest-qemu-pause.go | 25 +++++++++++++++++++ proxmox/client.go | 25 ++++++++++++------- 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 cli/command/guest/qemu/guest-qemu-hibernate.go create mode 100644 cli/command/guest/qemu/guest-qemu-pause.go diff --git a/cli/command/guest/qemu/guest-qemu-hibernate.go b/cli/command/guest/qemu/guest-qemu-hibernate.go new file mode 100644 index 00000000..95740b26 --- /dev/null +++ b/cli/command/guest/qemu/guest-qemu-hibernate.go @@ -0,0 +1,25 @@ +package qemu + +import ( + "github.com/Telmate/proxmox-api-go/cli" + "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/spf13/cobra" +) + +var qemu_hibernateCmd = &cobra.Command{ + Use: "hibernate GUESTID", + Short: "Hibernates the speciefid guest", + RunE: func(cmd *cobra.Command, args []string) (err error) { + vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID")) + c := cli.NewClient() + _, err = c.HibernateVm(vmr) + if err == nil { + cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "suspended to disk") + } + return + }, +} + +func init() { + qemuCmd.AddCommand(qemu_hibernateCmd) +} diff --git a/cli/command/guest/qemu/guest-qemu-pause.go b/cli/command/guest/qemu/guest-qemu-pause.go new file mode 100644 index 00000000..7d0c42bf --- /dev/null +++ b/cli/command/guest/qemu/guest-qemu-pause.go @@ -0,0 +1,25 @@ +package qemu + +import ( + "github.com/Telmate/proxmox-api-go/cli" + "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/spf13/cobra" +) + +var qemu_pauseCmd = &cobra.Command{ + Use: "pause GUESTID", + Short: "Pauses the speciefid guest", + RunE: func(cmd *cobra.Command, args []string) (err error) { + vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID")) + c := cli.NewClient() + _, err = c.PauseVm(vmr) + if err == nil { + cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "paused") + } + return + }, +} + +func init() { + qemuCmd.AddCommand(qemu_pauseCmd) +} diff --git a/proxmox/client.go b/proxmox/client.go index 9d5a0c49..0ab75580 100644 --- a/proxmox/client.go +++ b/proxmox/client.go @@ -455,14 +455,14 @@ func (c *Client) GetTaskExitstatus(taskUpid string) (exitStatus interface{}, err return } -func (c *Client) StatusChangeVm(vmr *VmRef, setStatus string) (exitStatus string, err error) { +func (c *Client) StatusChangeVm(vmr *VmRef, params map[string]interface{}, setStatus string) (exitStatus string, err error) { err = c.CheckVmRef(vmr) if err != nil { return } url := fmt.Sprintf("/nodes/%s/%s/%d/status/%s", vmr.node, vmr.vmType, vmr.vmId, setStatus) for i := 0; i < 3; i++ { - exitStatus, err = c.CreateItemWithTask(nil, url) + exitStatus, err = c.CreateItemWithTask(params, url) if err != nil { time.Sleep(TaskStatusCheckInterval * time.Second) } else { @@ -473,27 +473,34 @@ func (c *Client) StatusChangeVm(vmr *VmRef, setStatus string) (exitStatus string } func (c *Client) StartVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "start") + return c.StatusChangeVm(vmr, nil, "start") } func (c *Client) StopVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "stop") + return c.StatusChangeVm(vmr, nil, "stop") } func (c *Client) ShutdownVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "shutdown") + return c.StatusChangeVm(vmr, nil, "shutdown") } func (c *Client) ResetVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "reset") + return c.StatusChangeVm(vmr, nil, "reset") } -func (c *Client) SuspendVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "suspend") +func (c *Client) PauseVm(vmr *VmRef) (exitStatus string, err error) { + return c.StatusChangeVm(vmr, nil, "suspend") +} + +func (c *Client) HibernateVm(vmr *VmRef) (exitStatus string, err error) { + params := map[string]interface{}{ + "todisk": true, + } + return c.StatusChangeVm(vmr, params, "suspend") } func (c *Client) ResumeVm(vmr *VmRef) (exitStatus string, err error) { - return c.StatusChangeVm(vmr, "resume") + return c.StatusChangeVm(vmr, nil, "resume") } func (c *Client) DeleteVm(vmr *VmRef) (exitStatus string, err error) {