-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
426 lines (398 loc) · 9.66 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// vmcontrol project main.go
package main
import (
"errors"
"fmt"
"os"
"strconv"
"time"
_ "github.com/davecgh/go-spew/spew"
"github.com/docopt/docopt-go"
"github.com/joernott/go-proxmox"
"github.com/joernott/go-foreman"
)
var proxmoxhost string
var proxmoxuser string
var proxmoxpass string
var foremanhost string
var foremanuser string
var foremanpass string
var p *proxmox.ProxMox
var f *foreman.Foreman
func getLoginParams(arguments map[string]interface{}) (string, string, string, string, string, string, error) {
var proxmoxhost string
var proxmoxuser string
var proxmoxpass string
var foremanhost string
var foremanuser string
var foremanpass string
var ok bool
if proxmoxhost, ok = arguments["--proxmoxhost"].(string); !ok {
return "", "", "", "", "", "", errors.New("No proxmox host provided.")
}
if proxmoxuser, ok = arguments["--proxmoxuser"].(string); !ok {
return "", "", "", "", "", "", errors.New("No proxmox user provided.")
}
if proxmoxpass, ok = arguments["--proxmoxpass"].(string); !ok {
return "", "", "", "", "", "", errors.New("No proxmox password provided.")
}
if foremanhost, ok = arguments["--foremanhost"].(string); !ok {
return proxmoxhost, proxmoxuser, proxmoxpass, "", "", "", nil //no error if no foreman host is there, we don't need the others
}
if foremanuser, ok = arguments["--foremanuser"].(string); !ok {
return proxmoxhost, proxmoxuser, proxmoxpass, "", "", "", errors.New("No foreman user provided.")
}
if foremanpass, ok = arguments["--foremanpass"].(string); !ok {
return proxmoxhost, proxmoxuser, proxmoxpass, "", "", "", errors.New("No foreman password provided.")
}
return proxmoxhost, proxmoxuser, proxmoxpass, foremanhost, foremanuser, foremanpass, nil
}
func getCreateVMParameters(arguments map[string]interface{}) (string, int64, int64, int64, string, int, bool, error) {
var s string
var name string
var cpu int64
var cores int64
var mem int64
var disksize string
var hostgroup int
var start bool
var ok bool
var err error
if name, ok = arguments["--name"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No Name.")
}
if s, ok = arguments["--cpu"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No CPUs.")
}
cpu, err = strconv.ParseInt(s, 10, 32)
if err != nil {
return "", 0, 0, 0, "", 0, false, errors.New("Illegal cpu count.")
}
if cpu < 1 {
return "", 0, 0, 0, "", 0, false, errors.New("Not enough CPUs.")
}
if s, ok = arguments["--cores"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No Cores.")
}
cores, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return "", 0, 0, 0, "", 0, false, errors.New("Illegal core count.")
}
if cores < 1 {
return "", 0, 0, 0, "", 0, false, errors.New("Not enough cores.")
}
if s, ok = arguments["--mem"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No memory.")
}
mem, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return "", 0, 0, 0, "", 0, false, errors.New("Illegal memory.")
}
if mem < 64 {
return "", 0, 0, 0, "", 0, false, errors.New("Everybody will need more than 64 MiB of memory.")
}
if disksize, ok = arguments["--disk"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No disk.")
}
if s, ok = arguments["--hostgroup"].(string); !ok {
return "", 0, 0, 0, "", 0, false, errors.New("No Hostgroup ID.")
}
hostgroup, err = strconv.Atoi(s)
if err != nil {
return "", 0, 0, 0, "", 0, false, errors.New("Illegal host group.")
}
if _, ok = arguments["--start"].(bool); ok {
start = true
}
return name, cpu, cores, mem, disksize, hostgroup, start, nil
}
func CreateVM(arguments map[string]interface{}) {
var name string
var cpu int64
var cores int64
var mem int64
var disksize string
var hostgroup int
var start bool
var err error
var node proxmox.Node
var qemuList proxmox.QemuList
var qemu proxmox.QemuVM
var vmId string
var qemuConfig proxmox.QemuConfig
var mac string
var foremanId string
var data map[string]interface{}
var interfaces []interface{}
var IP string
var ok bool
name, cpu, cores, mem, disksize, hostgroup, start, err = getCreateVMParameters(arguments)
if err != nil {
fmt.Println(err)
os.Exit(11)
}
node, err = p.DetermineVMPlacement(cpu, cores, mem, 0, 0)
if err != nil {
fmt.Println(err)
os.Exit(12)
}
vmId, err = node.CreateQemuVM(name, int(cpu), int(cores), int(mem), disksize)
if err != nil {
fmt.Println("Could not create VM on " + node.Node + ".")
fmt.Println(err)
os.Exit(13)
}
qemuList, err = node.Qemu()
if err != nil {
fmt.Println(err)
os.Exit(13)
}
qemu = qemuList[vmId]
qemuConfig, err = qemu.Config()
mac = qemuConfig.Net["net0"]["virtio"]
foremanId, err = f.CreateHost(hostgroup, name, mac)
if err != nil {
fmt.Println("Could not set up host in foreman")
fmt.Println(err)
_, err := qemu.Delete()
if err != nil {
fmt.Println("Could not delete VM in Proxmox: Manual cleanup needed!")
fmt.Println(err)
}
os.Exit(14)
}
if start {
err = qemu.Start()
if err != nil {
fmt.Println("Error starting VM")
fmt.Println(err)
err = f.DeleteHost(vmId)
if err != nil {
fmt.Println("Could not delete host in foreman: Manual cleanup needed!")
}
_, err = qemu.Delete()
if err != nil {
fmt.Println("Could not delete VM in Proxmox: Manual cleanup needed!")
fmt.Println(err)
}
os.Exit(15)
}
}
data, err = f.Get("hosts/" + foremanId)
if err != nil {
fmt.Println(err)
IP = ""
}
interfaces = data["interfaces"].([]interface{})
if data, ok = interfaces[0].(map[string]interface{}); ok {
IP = data["ip"].(string)
} else {
fmt.Println("Did not find interface 0")
}
fmt.Println("VMID=" + vmId)
fmt.Println("MAC=" + mac)
fmt.Println("IP=" + IP)
fmt.Println("FOREMANID=" + foremanId)
os.Exit(0)
}
func DeleteVM(arguments map[string]interface{}) {
var vmId string
var foremanId string
var err error
var ok bool
var qemu proxmox.QemuVM
if vmId, ok = arguments["--vmid"].(string); !ok {
fmt.Println("No Vm ID")
os.Exit(21)
}
if foremanId, ok = arguments["--foremanid"].(string); !ok {
fmt.Println("No Foreman ID")
os.Exit(21)
}
ok = true
err = f.DeleteHost(foremanId)
if err != nil {
fmt.Println(err)
ok = false
}
qemu, err = p.FindVM(vmId)
if err != nil {
fmt.Println(err)
ok = false
}
if ok {
err = doVM(p, vmId, "stop")
if err != nil {
fmt.Println(err)
os.Exit(23)
}
err = qemu.WaitForStatus("stopped", 60)
if err != nil {
fmt.Println(err)
os.Exit(23)
}
_, err = qemu.Delete()
if err != nil {
fmt.Println(err)
os.Exit(23)
}
os.Exit(0)
} else {
os.Exit(22)
}
}
func doVM(p *proxmox.ProxMox, vmId string, action string) error {
var qemu proxmox.QemuVM
var err error
qemu, err = p.FindVM(vmId)
if err != nil {
return err
}
switch action {
case "start":
err = qemu.Start()
if err != nil {
err = qemu.WaitForStatus("started", 30)
}
case "stop":
err = qemu.Stop()
if err != nil {
err = qemu.WaitForStatus("stopped", 30)
}
}
return err
}
func StartVM(arguments map[string]interface{}) {
var vmId string
var ok bool
var err error
if vmId, ok = arguments["--vmid"].(string); !ok {
fmt.Println("No Vm ID")
os.Exit(31)
}
err = doVM(p, vmId, "start")
if err != nil {
fmt.Println(err)
os.Exit(32)
}
}
func StopVM(arguments map[string]interface{}) {
var vmId string
var ok bool
var err error
if vmId, ok = arguments["--vmid"].(string); !ok {
fmt.Println("No Vm ID")
os.Exit(41)
}
err = doVM(p, vmId, "start")
if err != nil {
fmt.Println(err)
os.Exit(42)
}
}
func CloneVM(arguments map[string]interface{}) {
os.Exit(0)
}
func DumpVM(arguments map[string]interface{}) {
var vmId string
var ok bool
var err error
var vm proxmox.QemuVM
var upid string
var tasks proxmox.TaskList
var task proxmox.Task
var exitstatus string
if vmId, ok = arguments["--vmid"].(string); !ok {
fmt.Println("No Vm ID")
os.Exit(51)
}
vm, err = p.FindVM(vmId)
if err != nil {
fmt.Println(err)
os.Exit(42)
}
upid, err = vm.Node.VZDump(vmId, 65536, "lzo", 0, 2, "stop")
if err != nil {
fmt.Println(err)
os.Exit(43)
}
time.Sleep(time.Second * 4)
tasks, err = p.Tasks()
if err != nil {
fmt.Println(err)
os.Exit(44)
}
if task, ok = tasks[upid]; !ok {
fmt.Println("Could not get task for upid " + upid)
os.Exit(44)
}
exitstatus, err = task.WaitForStatus("stopped", 1800)
if err != nil {
fmt.Println(err)
os.Exit(45)
}
if exitstatus != "OK" {
fmt.Println("Dumping the VM failed with exit status " + exitstatus)
os.Exit(46)
}
os.Exit(0)
}
func main() {
var err error
var action interface{}
var ok bool
arguments, err := docopt.Parse(usage, nil, true, "vmcontrol v 0.1", false)
if err != nil {
fmt.Println(err)
fmt.Println(usage)
os.Exit(2)
}
//spew.Dump(arguments)
proxmoxhost, proxmoxuser, proxmoxpass, foremanhost, foremanuser, foremanpass, err = getLoginParams(arguments)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
p, err = proxmox.NewProxMox(proxmoxhost, proxmoxuser, proxmoxpass)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
f = foreman.NewForeman(foremanhost, foremanuser, foremanpass)
if action, ok = arguments["create-vm"]; ok {
if action.(bool) {
CreateVM(arguments)
}
}
if action, ok := arguments["delete-vm"]; ok {
if action.(bool) {
DeleteVM(arguments)
}
}
if action, ok := arguments["start-vm"]; ok {
if action.(bool) {
StartVM(arguments)
os.Exit(0)
}
}
if action, ok := arguments["stop-vm"]; ok {
if action.(bool) {
StopVM(arguments)
os.Exit(0)
}
}
if action, ok := arguments["clone-vm"]; ok {
if action.(bool) {
CloneVM(arguments)
os.Exit(0)
}
}
if action, ok := arguments["dump-vm"]; ok {
if action.(bool) {
DumpVM(arguments)
os.Exit(0)
}
}
fmt.Println("No valid action")
os.Exit(100)
}