Select Git revision
ByteCodeGenerator.java
vmAttach.go 2.08 KiB
package cmdVM
import (
)
import (
"os/exec"
"strconv"
"encoding/json"
"github.com/google/uuid"
u "nexus-client/utils"
g "nexus-client/globals"
)
type Attach struct {
Name string
}
func (cmd *Attach)GetName() string {
return cmd.Name
}
func (cmd *Attach)GetDesc() string {
return "Attach to a VM in order to use its desktop environment."
}
func (cmd *Attach)PrintUsage() {
u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: "+cmd.Name+" vmID")
}
func (cmd *Attach)Run(args []string) int {
client := g.GetInstance().Client
host := g.GetInstance().Host
hostname := g.GetInstance().Hostname
pubCert := g.GetInstance().PubCert
argc := len(args)
if argc != 1 {
cmd.PrintUsage()
return 1
}
id := args[0]
resp, err := client.R().Get(host+"/vms/"+id)
if err != nil {
u.PrintlnErr("Error: "+err.Error())
return 1
}
if resp.IsSuccess() {
type VMState string
const (
STOPPED VMState = "STOPPED"
RUNNING = "RUNNING"
)
type runStates struct {
State VMState
Pid int
Port int
Pwd string
}
type Capabilities map[string]int
type VM struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Cpus int `json:"cpus"`
Ram int `json:"ram"`
TemplateID uuid.UUID `json:"templateID"`
Access map[string]Capabilities `json:"access"`
Run runStates
}
var vm VM
err = json.Unmarshal(resp.Body(), &vm)
if err != nil {
u.PrintlnErr("Error: "+err.Error())
return 1
}
if vm.Run.State == RUNNING {
port := strconv.Itoa(vm.Run.Port)
spice := "spice://"+hostname+"?tls-port="+port+"&password="+vm.Run.Pwd
spiceCert := "--spice-ca-file="+pubCert
spiceSecure := "--spice-secure-channels=all"
cmd := exec.Command("remote-viewer", spice, spiceCert, spiceSecure) //, "-k", "--kiosk-quit=on-disconnect")
if err = cmd.Run(); err != nil {
u.PrintlnErr("Failed attaching to VM: "+err.Error())
return 1
}
return 0
} else {
u.PrintlnErr("Can only attach to a running VM!")
return 1
}
} else {
u.PrintlnErr("Error: "+resp.Status()+": "+resp.String())
return 1
}
}