Added "names" function to show all VMs and CTs across the cluster - 0.1.3
This commit is contained in:
1 parent
5550771ab1
commit
7f23486cb9
2 files changed
+53
-3
No files matched your search
+53
-3
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -11,11 +12,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const version = "V0.1.3"
|
||||
|
||||
var (
|
||||
tmpDir string
|
||||
numeric = regexp.MustCompile(`^[0-9]+$`)
|
||||
)
|
||||
const version = "V0.1.2"
|
||||
|
||||
func main() {
|
||||
if os.Geteuid() != 0 {
|
||||
fmt.Fprintf(os.Stderr, "Run this program as root, for example: sudo %s\n", os.Args[0])
|
||||
@@ -286,13 +289,51 @@ func showText(title, text string) {
|
||||
)
|
||||
}
|
||||
|
||||
func showResourceNames(title, resourceType string) {
|
||||
output, err := exec.Command(
|
||||
"pvesh", "get", "/cluster/resources", "--output-format", "json",
|
||||
).CombinedOutput()
|
||||
if err != nil {
|
||||
text := string(output)
|
||||
if text == "" {
|
||||
text = err.Error()
|
||||
}
|
||||
showText(title, text)
|
||||
return
|
||||
}
|
||||
|
||||
var resources []struct {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(output, &resources); err != nil {
|
||||
showText(title, "Could not parse pvesh output:\n"+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var names []string
|
||||
for _, resource := range resources {
|
||||
if resource.Type == resourceType {
|
||||
names = append(names, resource.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if len(names) == 0 {
|
||||
showText(title, "No matching resources found.\n")
|
||||
return
|
||||
}
|
||||
|
||||
showText(title, strings.Join(names, "\n")+"\n")
|
||||
}
|
||||
|
||||
func vmMenu() {
|
||||
for {
|
||||
choice := menu(
|
||||
"Virtual Machines (QEMU)",
|
||||
"Choose an action:",
|
||||
16, 72, 8,
|
||||
18, 72, 10,
|
||||
"list", "List VMs (qm list)",
|
||||
"names", "List VM names",
|
||||
"config", "Show VM configuration",
|
||||
"memory", "Show VM memory setting",
|
||||
"unlock", "Unlock a VM",
|
||||
@@ -304,6 +345,10 @@ func vmMenu() {
|
||||
if requireCommand("qm") {
|
||||
showOutput("VM list", "qm", "list")
|
||||
}
|
||||
case "names":
|
||||
if requireCommand("pvesh") {
|
||||
showResourceNames("VM names", "qemu")
|
||||
}
|
||||
case "config", "memory", "unlock":
|
||||
if !requireCommand("qm") {
|
||||
continue
|
||||
@@ -338,8 +383,9 @@ func containerMenu() {
|
||||
choice := menu(
|
||||
"Containers (LXC)",
|
||||
"Choose an action:",
|
||||
14, 72, 6,
|
||||
16, 72, 8,
|
||||
"list", "List containers (pct list)",
|
||||
"names", "List container names",
|
||||
"config", "Show container configuration",
|
||||
"unlock", "Unlock a container",
|
||||
"back", "Back",
|
||||
@@ -350,6 +396,10 @@ func containerMenu() {
|
||||
if requireCommand("pct") {
|
||||
showOutput("Container list", "pct", "list")
|
||||
}
|
||||
case "names":
|
||||
if requireCommand("pvesh") {
|
||||
showResourceNames("Container names", "lxc")
|
||||
}
|
||||
case "config", "unlock":
|
||||
if !requireCommand("pct") {
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user