-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.go
228 lines (171 loc) · 4.77 KB
/
clone.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"context"
"fmt"
"os"
"strconv"
"github.com/luthermonson/go-proxmox"
)
// help function
func cloneHelp() {
fmt.Println("Usage: proxmox clone [options]")
fmt.Println("Clone a template VM and create on the least used node with the next available VM ID")
fmt.Println("Options:")
fmt.Println(" -n, --name <name> - (required) - Name of the VM")
fmt.Println(" -s, --source <sourceVM> - (required) - Source VM to clone")
fmt.Println(" -e, --environment <environment> - (required) - Environment to use")
fmt.Println(" -p, --poweron - (optional) - Power on the VM after creation")
fmt.Println("Examples:")
fmt.Println(" proxmox create -n test-vm -s test-vm-template")
}
func cloneMapper() {
// get options from parameters
params := os.Args[2:]
if checkParams(params, "help", false) {
cloneHelp()
os.Exit(0)
}
if !checkParams(params, "name", true) {
logger.Print("Name is required")
cloneHelp()
os.Exit(0)
}
if !checkParams(params, "source", true) {
logger.Print("Source is required")
cloneHelp()
os.Exit(0)
}
if !checkParams(params, "environment", true) {
logger.Print("Environment is required")
cloneHelp()
os.Exit(0)
}
cloneVM(params)
}
func cloneVM(params []string) {
if len(params) < 3 {
cloneHelp()
os.Exit(1)
}
// get source and destination
source := getParams(params, "source")
vmName := getParams(params, "name")
environment := getParams(params, "environment")
powerOn := false
if checkParams(params, "poweron", false) {
powerOn = true
}
if checkParams(params, "target", false) {
logger.Fatal("Target not implemented")
}
sourceint, err := strconv.ParseUint(source, 10, 64)
if err != nil {
logger.Fatal("Source is not a VM ID")
}
// get source template
templateVM := getTemplate(sourceint)
// get the least used node
targetNode := getLeastUsedNode()
// clone the VM
vmID := cloneLocally(templateVM, environment, vmName)
// update nodes
updateNodes()
// get current node for VM
vmNode := getNodeByVMID(vmID)
// get the new VM
newVM := getVMbyID(vmID)
if vmNode.Node.Name != targetNode.Node.Name {
// migrate to the least used node
migrateVM(newVM, targetNode)
}
migratedVM := getVMbyID(vmID)
if powerOn {
powerOnVM(migratedVM)
}
logger.Print(
"All operations completed successfully for creation of VM: ", vmName,
" with ID: ", vmID, " in environment: ", environment, " on node: ", targetNode.Node.Name,
" from template: ", source, " and is currently powered: ", newVM.Status,
)
}
func cloneLocally(templateVM VirtualMachine, environment string, name string) uint64 {
calculatedVMID, parseErr := strconv.ParseUint(getEnvironment(environment), 10, 64)
maxRange := calculatedVMID + 100
if parseErr != nil {
logger.Fatal("Error parsing environment range")
}
//get the next available VM ID
for _, vm := range activeVMids {
if vm == calculatedVMID {
calculatedVMID++
}
}
if calculatedVMID > maxRange {
logger.Fatal("No more VM IDs available")
}
cloneOptions := proxmox.VirtualMachineCloneOptions{
Full: 1,
NewID: int(calculatedVMID),
Pool: environment,
Name: name,
}
_, proxmoxTask, err := templateVM.Clone(context.Background(), &cloneOptions)
if err != nil {
logger.Fatal("Error cloning VM: ", err.Error())
}
for {
status, complete, err := proxmoxTask.WaitForCompleteStatus(context.Background(), 60, 10)
if err != nil {
logger.Fatal("Error waiting for clone operation to complete: ", err.Error())
}
if complete {
if status {
break
} else {
logger.Fatal("Clone failed")
}
}
}
logger.Print("Successfully cloned VM: ", name, " with ID: ", calculatedVMID, " from template: ", templateVM.Name)
return calculatedVMID
}
func migrateVM(vm VirtualMachine, targetNode ProxmoxNode) {
migrateOptions := proxmox.VirtualMachineMigrateOptions{
Target: targetNode.Node.Name,
TargetStorage: getStorage(&targetNode.Node).Name,
}
proxmoxTask, err := vm.Migrate(context.Background(), &migrateOptions)
if err != nil {
logger.Fatal("Error migrating VM: ", err.Error())
}
// wait for the task to complete
for {
status, complete, err := proxmoxTask.WaitForCompleteStatus(context.Background(), 60, 10)
if err != nil {
logger.Fatal("Error waiting for migrate operation to complete: ", err.Error())
}
if complete {
if status {
break
} else {
logger.Fatal("Migrate failed")
}
}
}
logger.Print("Migration of ", vm.Name, " to ", targetNode.Node.Name, " complete")
}
func powerOnVM(vm VirtualMachine) {
_, err := vm.Start(context.Background())
if err != nil {
logger.Fatal("Error starting VM: ", err.Error())
}
for {
targetVM := getVMbyID(uint64(vm.VMID))
if targetVM.IsRunning() {
break
} else {
logger.Fatal("Start failed")
}
}
logger.Print(vm.Name, " has been successfully powered on")
}