Added "names" function to show all VMs and CTs across the cluster - 0.1.3

This commit is contained in:
phil committed 2026-10-29 22:16:19 +00:00
1 parent 5550771ab1
commit 7f23486cb9
2 files changed
+53 -3

No files matched your search

+53 -3
View File
@@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@@ -11,11 +12,13 @@ import (
"strings" "strings"
) )
const version = "V0.1.3"
var ( var (
tmpDir string tmpDir string
numeric = regexp.MustCompile(`^[0-9]+$`) numeric = regexp.MustCompile(`^[0-9]+$`)
) )
const version = "V0.1.2"
func main() { func main() {
if os.Geteuid() != 0 { if os.Geteuid() != 0 {
fmt.Fprintf(os.Stderr, "Run this program as root, for example: sudo %s\n", os.Args[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() { func vmMenu() {
for { for {
choice := menu( choice := menu(
"Virtual Machines (QEMU)", "Virtual Machines (QEMU)",
"Choose an action:", "Choose an action:",
16, 72, 8, 18, 72, 10,
"list", "List VMs (qm list)", "list", "List VMs (qm list)",
"names", "List VM names",
"config", "Show VM configuration", "config", "Show VM configuration",
"memory", "Show VM memory setting", "memory", "Show VM memory setting",
"unlock", "Unlock a VM", "unlock", "Unlock a VM",
@@ -304,6 +345,10 @@ func vmMenu() {
if requireCommand("qm") { if requireCommand("qm") {
showOutput("VM list", "qm", "list") showOutput("VM list", "qm", "list")
} }
case "names":
if requireCommand("pvesh") {
showResourceNames("VM names", "qemu")
}
case "config", "memory", "unlock": case "config", "memory", "unlock":
if !requireCommand("qm") { if !requireCommand("qm") {
continue continue
@@ -338,8 +383,9 @@ func containerMenu() {
choice := menu( choice := menu(
"Containers (LXC)", "Containers (LXC)",
"Choose an action:", "Choose an action:",
14, 72, 6, 16, 72, 8,
"list", "List containers (pct list)", "list", "List containers (pct list)",
"names", "List container names",
"config", "Show container configuration", "config", "Show container configuration",
"unlock", "Unlock a container", "unlock", "Unlock a container",
"back", "Back", "back", "Back",
@@ -350,6 +396,10 @@ func containerMenu() {
if requireCommand("pct") { if requireCommand("pct") {
showOutput("Container list", "pct", "list") showOutput("Container list", "pct", "list")
} }
case "names":
if requireCommand("pvesh") {
showResourceNames("Container names", "lxc")
}
case "config", "unlock": case "config", "unlock":
if !requireCommand("pct") { if !requireCommand("pct") {
continue continue
BIN
View File
Binary file not shown.