-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (107 loc) · 2.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"fmt"
"github.com/charmbracelet/huh"
"github.com/luthermonson/go-proxmox"
"github.com/rs/zerolog/log"
)
var (
virtualMachinesToPowerOn []string // huh data model
)
func getVirtualMachinesToPowerOff(vms proxmox.VirtualMachines, virtualMachinesToPowerOn []string) []string {
var virtualMachinesAll []string
for _, v := range vms {
virtualMachinesAll = append(virtualMachinesAll, v.Name)
}
return subtractArrays(virtualMachinesAll, virtualMachinesToPowerOn)
}
func startVm(vms proxmox.VirtualMachines, virtualMachinesToPowerOn []string, taskChan chan error) {
vmPowerOnCounter := 0
defer close(taskChan)
for _, vmName := range virtualMachinesToPowerOn {
for _, vm := range vms {
if vmName == vm.Name {
if vm.Status != "running" {
fmt.Printf("▶️ Starting %s...\n", vmName)
vmPowerOnCounter = vmPowerOnCounter + 1
_, err := vm.Start(context.Background())
taskChan <- err
}
}
}
}
if vmPowerOnCounter == 0 {
fmt.Println("No virtual machines to start")
}
}
func stopVm(vms proxmox.VirtualMachines, virtualMachinesToPowerOff []string, taskChan chan error) {
vmPowerOffCounter := 0
defer close(taskChan)
for _, vmName := range virtualMachinesToPowerOff {
for _, vm := range vms {
if vmName == vm.Name {
if vm.Status == "running" {
fmt.Printf("⏸️ Stopping %s...\n", vmName)
vmPowerOffCounter = vmPowerOffCounter + 1
_, err := vm.Stop(context.Background())
taskChan <- err
}
}
}
}
if vmPowerOffCounter == 0 {
fmt.Println("No virtual machines to stop")
}
}
func main() {
// init
client := initClient()
// print version
version, err := getVersion(client)
if err != nil {
log.Fatal().Err(err).Msg("Cannot obtain Proxmox VE Version.")
} else {
fmt.Printf("Proxmox VE Version: %s\n", version)
}
// list VMs
node, err := getNode(client)
if err != nil {
log.Fatal().Err(err).Msgf("Specified Proxmox VE Node does not exist.")
}
vms, err := getVMs(node)
if err != nil {
log.Fatal().Err(err).Msgf("Cannot obtain list of VMs.")
}
// init form
form := huh.NewForm(
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Select Virtual Machines you want to Power On").
Options(
createFormOptions(vms)...,
).
Value(&virtualMachinesToPowerOn),
),
)
err = form.Run()
if err != nil {
log.Fatal().Err(err)
}
// start/stop VMs
virtualMachinesToPowerOff := getVirtualMachinesToPowerOff(vms, virtualMachinesToPowerOn)
startVmChan := make(chan error)
go startVm(vms, virtualMachinesToPowerOn, startVmChan)
for err := range startVmChan {
if err != nil {
log.Error().Msg("Starting VM Failed")
}
}
stopVmChan := make(chan error)
go stopVm(vms, virtualMachinesToPowerOff, stopVmChan)
for err := range stopVmChan {
if err != nil {
log.Err(err).Msg("Stopping VM Failed")
}
}
}