Skip to content

Commit

Permalink
Add qemu guest pause and hibernate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Sep 13, 2022
1 parent ee8e24d commit 0815cb9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-hibernate.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-pause.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 16 additions & 9 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 0815cb9

Please sign in to comment.