-
Notifications
You must be signed in to change notification settings - Fork 56
/
domain.go
747 lines (647 loc) · 21 KB
/
domain.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
package libvirt
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"net"
"os"
"strings"
"math/rand"
"github.com/davecgh/go-spew/spew"
libvirt "github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
providerconfigv1 "github.com/openshift/cluster-api-provider-libvirt/cloud/libvirt/providerconfig/v1alpha1"
"github.com/openshift/cluster-api-provider-libvirt/lib/cidr"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)
const (
baseVolumePath = "/var/lib/libvirt/images/"
)
// LibVirtConIsNil contains a nil connection error message
var LibVirtConIsNil = "the libvirt connection was nil"
// Client libvirt
type Client struct {
libvirt *libvirt.Connect
}
type pendingMapping struct {
mac string
hostname string
network *libvirt.Network
}
func newDomainDef() libvirtxml.Domain {
domainDef := libvirtxml.Domain{
OS: &libvirtxml.DomainOS{
Type: &libvirtxml.DomainOSType{
Type: "hvm",
},
},
Memory: &libvirtxml.DomainMemory{
Unit: "MiB",
Value: 512,
},
VCPU: &libvirtxml.DomainVCPU{
Placement: "static",
Value: 1,
},
CPU: &libvirtxml.DomainCPU{},
Devices: &libvirtxml.DomainDeviceList{
Graphics: []libvirtxml.DomainGraphic{
{
Spice: &libvirtxml.DomainGraphicSpice{
AutoPort: "yes",
},
},
},
Channels: []libvirtxml.DomainChannel{
{
Target: &libvirtxml.DomainChannelTarget{
VirtIO: &libvirtxml.DomainChannelTargetVirtIO{
Name: "org.qemu.guest_agent.0",
},
},
},
},
RNGs: []libvirtxml.DomainRNG{
{
Model: "virtio",
Backend: &libvirtxml.DomainRNGBackend{
Random: &libvirtxml.DomainRNGBackendRandom{},
},
},
},
},
Features: &libvirtxml.DomainFeatureList{
PAE: &libvirtxml.DomainFeature{},
ACPI: &libvirtxml.DomainFeature{},
APIC: &libvirtxml.DomainFeatureAPIC{},
},
}
if v := os.Getenv("TERRAFORM_LIBVIRT_TEST_DOMAIN_TYPE"); v != "" {
domainDef.Type = v
} else {
domainDef.Type = "kvm"
}
return domainDef
}
func getHostArchitecture(virConn *libvirt.Connect) (string, error) {
type HostCapabilities struct {
XMLName xml.Name `xml:"capabilities"`
Host struct {
XMLName xml.Name `xml:"host"`
CPU struct {
XMLName xml.Name `xml:"cpu"`
Arch string `xml:"arch"`
}
}
}
info, err := virConn.GetCapabilities()
if err != nil {
return "", err
}
capabilities := HostCapabilities{}
xml.Unmarshal([]byte(info), &capabilities)
return capabilities.Host.CPU.Arch, nil
}
func getHostCapabilities(virConn *libvirt.Connect) (libvirtxml.Caps, error) {
// We should perhaps think of storing this on the connect object
// on first call to avoid the back and forth
caps := libvirtxml.Caps{}
capsXML, err := virConn.GetCapabilities()
if err != nil {
return caps, err
}
xml.Unmarshal([]byte(capsXML), &caps)
log.Printf("[TRACE] Capabilities of host \n %+v", caps)
return caps, nil
}
func getGuestForArchType(caps libvirtxml.Caps, arch string, virttype string) (libvirtxml.CapsGuest, error) {
for _, guest := range caps.Guests {
log.Printf("[TRACE] Checking for %s/%s against %s/%s\n", arch, virttype, guest.Arch.Name, guest.OSType)
if guest.Arch.Name == arch && guest.OSType == virttype {
log.Printf("[DEBUG] Found %d machines in guest for %s/%s", len(guest.Arch.Machines), arch, virttype)
return guest, nil
}
}
return libvirtxml.CapsGuest{}, fmt.Errorf("[DEBUG] Could not find any guests for architecure type %s/%s", virttype, arch)
}
func getCanonicalMachineName(caps libvirtxml.Caps, arch string, virttype string, targetmachine string) (string, error) {
log.Printf("[INFO] getCanonicalMachineName")
guest, err := getGuestForArchType(caps, arch, virttype)
if err != nil {
return "", err
}
for _, machine := range guest.Arch.Machines {
if machine.Name == targetmachine {
if machine.Canonical != "" {
return machine.Canonical, nil
}
return machine.Name, nil
}
}
return "", fmt.Errorf("[WARN] Cannot find machine type %s for %s/%s in %v", targetmachine, virttype, arch, caps)
}
func newDomainDefForConnection(virConn *libvirt.Connect) (libvirtxml.Domain, error) {
d := newDomainDef()
arch, err := getHostArchitecture(virConn)
if err != nil {
return d, err
}
d.OS.Type.Arch = arch
caps, err := getHostCapabilities(virConn)
if err != nil {
return d, err
}
guest, err := getGuestForArchType(caps, d.OS.Type.Arch, d.OS.Type.Type)
if err != nil {
return d, err
}
d.Devices.Emulator = guest.Arch.Emulator
if len(guest.Arch.Machines) > 0 {
d.OS.Type.Machine = guest.Arch.Machines[0].Name
}
canonicalmachine, err := getCanonicalMachineName(caps, d.OS.Type.Arch, d.OS.Type.Type, d.OS.Type.Machine)
if err != nil {
return d, err
}
d.OS.Type.Machine = canonicalmachine
return d, nil
}
func setCoreOSIgnition(domainDef *libvirtxml.Domain, ignKey string) error {
if ignKey == "" {
return fmt.Errorf("error setting coreos ignition, ignKey is empty")
}
domainDef.QEMUCommandline = &libvirtxml.DomainQEMUCommandline{
Args: []libvirtxml.DomainQEMUCommandlineArg{
{
// https://github.com/qemu/qemu/blob/master/docs/specs/fw_cfg.txt
Value: "-fw_cfg",
},
{
Value: fmt.Sprintf("name=opt/com.coreos/config,file=%s", ignKey),
},
},
}
return nil
}
// note, source is not initialized
func newDefDisk(i int) libvirtxml.DomainDisk {
return libvirtxml.DomainDisk{
Device: "disk",
Target: &libvirtxml.DomainDiskTarget{
Bus: "virtio",
Dev: fmt.Sprintf("vd%s", diskLetterForIndex(i)),
},
Driver: &libvirtxml.DomainDiskDriver{
Name: "qemu",
Type: "qcow2",
},
}
}
var diskLetters = []rune("abcdefghijklmnopqrstuvwxyz")
const oui = "05abcd"
// diskLetterForIndex return diskLetters for index
func diskLetterForIndex(i int) string {
q := i / len(diskLetters)
r := i % len(diskLetters)
letter := diskLetters[r]
if q == 0 {
return fmt.Sprintf("%c", letter)
}
return fmt.Sprintf("%s%c", diskLetterForIndex(q-1), letter)
}
func randomWWN(strlen int) string {
const chars = "abcdef0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return oui + string(result)
}
func setDisks(domainDef *libvirtxml.Domain, virConn *libvirt.Connect, volumeKey string) error {
disk := newDefDisk(0)
log.Printf("[INFO] LookupStorageVolByKey")
diskVolume, err := virConn.LookupStorageVolByKey(volumeKey)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", volumeKey)
}
log.Printf("[INFO] diskVolume")
diskVolumeFile, err := diskVolume.GetPath()
if err != nil {
return fmt.Errorf("Error retrieving volume file: %s", err)
}
log.Printf("[INFO] DomainDiskSource")
disk.Source = &libvirtxml.DomainDiskSource{
File: &libvirtxml.DomainDiskSourceFile{
File: diskVolumeFile,
},
}
domainDef.Devices.Disks = append(domainDef.Devices.Disks, disk)
return nil
}
// randomMACAddress returns a randomized MAC address
func randomMACAddress() (string, error) {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
return "", err
}
// set local bit and unicast
buf[0] = (buf[0] | 2) & 0xfe
// Set the local bit
buf[0] |= 2
// avoid libvirt-reserved addresses
if buf[0] == 0xfe {
buf[0] = 0xee
}
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]), nil
}
// Network interface used to expose a libvirt.Network
type Network interface {
GetXMLDesc(flags libvirt.NetworkXMLFlags) (string, error)
}
func newDefNetworkfromLibvirt(network Network) (libvirtxml.Network, error) {
networkXMLDesc, err := network.GetXMLDesc(0)
if err != nil {
return libvirtxml.Network{}, fmt.Errorf("Error retrieving libvirt domain XML description: %s", err)
}
networkDef := libvirtxml.Network{}
err = xml.Unmarshal([]byte(networkXMLDesc), &networkDef)
if err != nil {
return libvirtxml.Network{}, fmt.Errorf("Error reading libvirt network XML description: %s", err)
}
return networkDef, nil
}
// HasDHCP checks if the network has a DHCP server managed by libvirt
func HasDHCP(net libvirtxml.Network) bool {
if net.Forward != nil {
if net.Forward.Mode == "nat" || net.Forward.Mode == "route" || net.Forward.Mode == "" {
return true
}
}
return false
}
// Tries to update first, if that fails, it will add it
func updateOrAddHost(n *libvirt.Network, ip, mac, name string) error {
err := updateHost(n, ip, mac, name)
if virErr, ok := err.(libvirt.Error); ok && virErr.Code == libvirt.ERR_OPERATION_INVALID && virErr.Domain == libvirt.FROM_NETWORK {
return addHost(n, ip, mac, name)
}
return err
}
// Adds a new static host to the network
func addHost(n *libvirt.Network, ip, mac, name string) error {
xmlDesc := getHostXMLDesc(ip, mac, name)
log.Printf("Adding host with XML:\n%s", xmlDesc)
return n.Update(libvirt.NETWORK_UPDATE_COMMAND_ADD_LAST, libvirt.NETWORK_SECTION_IP_DHCP_HOST, -1, xmlDesc, libvirt.NETWORK_UPDATE_AFFECT_CURRENT)
}
func getHostXMLDesc(ip, mac, name string) string {
dd := libvirtxml.NetworkDHCPHost{
IP: ip,
MAC: mac,
Name: name,
}
tmp := struct {
XMLName xml.Name `xml:"host"`
libvirtxml.NetworkDHCPHost
}{xml.Name{}, dd}
xml, err := xmlMarshallIndented(tmp)
if err != nil {
panic("could not marshall host")
}
return xml
}
// return an indented XML
func xmlMarshallIndented(b interface{}) (string, error) {
buf := new(bytes.Buffer)
enc := xml.NewEncoder(buf)
enc.Indent(" ", " ")
if err := enc.Encode(b); err != nil {
return "", fmt.Errorf("could not marshall this:\n%s", spew.Sdump(b))
}
return buf.String(), nil
}
// Update a static host from the network
func updateHost(n *libvirt.Network, ip, mac, name string) error {
xmlDesc := getHostXMLDesc(ip, mac, name)
log.Printf("Updating host with XML:\n%s", xmlDesc)
return n.Update(libvirt.NETWORK_UPDATE_COMMAND_MODIFY, libvirt.NETWORK_SECTION_IP_DHCP_HOST, -1, xmlDesc, libvirt.NETWORK_UPDATE_AFFECT_CURRENT)
}
func setNetworkInterfaces(domainDef *libvirtxml.Domain,
virConn *libvirt.Connect, partialNetIfaces map[string]*pendingMapping,
waitForLeases *[]*libvirtxml.DomainInterface,
networkInterfaceHostname string, networkInterfaceName string, networkInterfaceAddress string, offset int) error {
// TODO: support more than 1 interface
for i := 0; i < 1; i++ {
netIface := libvirtxml.DomainInterface{
Model: &libvirtxml.DomainInterfaceModel{
Type: "virtio",
},
}
// calculate the MAC address
var err error
mac, err := randomMACAddress()
if err != nil {
return fmt.Errorf("Error generating mac address: %s", err)
}
netIface.MAC = &libvirtxml.DomainInterfaceMAC{
Address: mac,
}
if networkInterfaceName != "" {
// when using a "network_id" we are referring to a "network resource"
// we have defined somewhere else...
network, err := virConn.LookupNetworkByName(networkInterfaceName)
if err != nil {
return fmt.Errorf("Can't retrieve network name %s", networkInterfaceName)
}
defer network.Free()
networkName, err := network.GetName()
if err != nil {
return fmt.Errorf("Error retrieving network name: %s", err)
}
networkDef, err := newDefNetworkfromLibvirt(network)
if HasDHCP(networkDef) {
hostname := domainDef.Name
if networkInterfaceHostname != "" {
hostname = networkInterfaceHostname
}
log.Printf("Networkaddress %v", networkInterfaceAddress)
if networkInterfaceAddress != "" {
_, networkCIDR, err := net.ParseCIDR(networkInterfaceAddress)
if err != nil {
return fmt.Errorf("failed to parse libvirt network ipRange: %v", err)
}
var ip net.IP
if ip, err = cidr.GenerateIP(networkCIDR, offset); err != nil {
return fmt.Errorf("failed to generate ip: %v", err)
}
log.Printf("[INFO] Adding IP/MAC/host=%s/%s/%s to %s", ip.String(), mac, hostname, networkName)
if err := updateOrAddHost(network, ip.String(), mac, hostname); err != nil {
return err
}
} else {
// no IPs provided: if the hostname has been provided, wait until we get an IP
wait := false
for _, iface := range *waitForLeases {
if iface == &netIface {
wait = true
break
}
}
if !wait {
return fmt.Errorf("Cannot map '%s': we are not waiting for DHCP lease and no IP has been provided", hostname)
}
// the resource specifies a hostname but not an IP, so we must wait until we
// have a valid lease and then read the IP we have been assigned, so we can
// do the mapping
log.Printf("[DEBUG] Do not have an IP for '%s' yet: will wait until DHCP provides one...", hostname)
partialNetIfaces[strings.ToUpper(mac)] = &pendingMapping{
mac: strings.ToUpper(mac),
hostname: hostname,
network: network,
}
}
}
netIface.Source = &libvirtxml.DomainInterfaceSource{
Network: &libvirtxml.DomainInterfaceSourceNetwork{
Network: networkName,
},
}
}
domainDef.Devices.Interfaces = append(domainDef.Devices.Interfaces, netIface)
}
return nil
}
// Config struct for the libvirt-provider
type Config struct {
URI string
}
// Client libvirt, generate libvirt client given URI
func buildClient(URI string) (*Client, error) {
libvirtClient, err := libvirt.NewConnect(URI)
if err != nil {
return nil, err
}
log.Println("[INFO] Created libvirt client")
client := &Client{
libvirt: libvirtClient,
}
return client, nil
}
// MachineProviderConfigFromClusterAPIMachineSpec gets the machine provider config MachineSetSpec from the
// specified cluster-api MachineSpec.
func MachineProviderConfigFromClusterAPIMachineSpec(ms *clusterv1.MachineSpec) (*providerconfigv1.LibvirtMachineProviderConfig, error) {
if ms.ProviderConfig.Value == nil {
return nil, fmt.Errorf("no Value in ProviderConfig")
}
obj, gvk, err := providerconfigv1.Codecs.UniversalDecoder(providerconfigv1.SchemeGroupVersion).Decode([]byte(ms.ProviderConfig.Value.Raw), nil, nil)
if err != nil {
return nil, err
}
spec, ok := obj.(*providerconfigv1.LibvirtMachineProviderConfig)
if !ok {
return nil, fmt.Errorf("unexpected object when parsing machine provider config: %#v", gvk)
}
return spec, nil
}
func domainDefInit(domainDef *libvirtxml.Domain, name string, machineConfig providerconfigv1.LibvirtMachineProviderConfig) error {
if name != "" {
domainDef.Name = name
} else {
return fmt.Errorf("machine does not have an name set")
}
if machineConfig.DomainMemory != 0 {
domainDef.Memory = &libvirtxml.DomainMemory{
Value: uint(machineConfig.DomainMemory),
Unit: "MiB",
}
} else {
return fmt.Errorf("machine does not have an DomainMemory set")
}
if machineConfig.DomainVcpu != 0 {
domainDef.VCPU = &libvirtxml.DomainVCPU{
Value: machineConfig.DomainVcpu,
}
} else {
return fmt.Errorf("machine does not have an DomainVcpu set")
}
//setConsoles(d, &domainDef)
//setCmdlineArgs(d, &domainDef)
//setFirmware(d, &domainDef)
//setBootDevices(d, &domainDef)
return nil
}
func createDomain(name string, machineProviderConfig *providerconfigv1.LibvirtMachineProviderConfig, offset int) error {
if name == "" {
return fmt.Errorf("Failed to create domain, name is empty")
}
client, err := buildClient(machineProviderConfig.URI)
if err != nil {
return fmt.Errorf("Failed to build libvirt client: %s", err)
}
log.Printf("[DEBUG] Create resource libvirt_domain")
virConn := client.libvirt
// Get default values from Host
domainDef, err := newDomainDefForConnection(virConn)
if err != nil {
return fmt.Errorf("Failed to newDomainDefForConnection: %s", err)
}
// Get values from machineProviderConfig
if err := domainDefInit(&domainDef, name, *machineProviderConfig); err != nil {
return fmt.Errorf("Failed to init domain definition from machineProviderConfig: %v", err)
}
log.Printf("[INFO] setCoreOSIgnition")
if machineProviderConfig.IgnKey != "" {
if err := setCoreOSIgnition(&domainDef, machineProviderConfig.IgnKey); err != nil {
return err
}
} else {
return fmt.Errorf("machine does not has a IgnKey value")
}
log.Printf("[INFO] setDisks")
var volumeName string
if machineProviderConfig.Volume.VolumeName != "" {
volumeName = machineProviderConfig.Volume.VolumeName
} else {
volumeName = name
}
VolumeKey := fmt.Sprintf(baseVolumePath+"%s", volumeName)
if err := setDisks(&domainDef, virConn, VolumeKey); err != nil {
return fmt.Errorf("Failed to setDisks: %s", err)
}
log.Printf("[INFO] setNetworkInterfaces")
var waitForLeases []*libvirtxml.DomainInterface
var hostName string
if machineProviderConfig.NetworkInterfaceHostname != "" {
hostName = machineProviderConfig.NetworkInterfaceHostname
} else {
hostName = name
}
// TODO: support more than 1 interface
partialNetIfaces := make(map[string]*pendingMapping, 1)
if err := setNetworkInterfaces(&domainDef, virConn, partialNetIfaces, &waitForLeases,
hostName, machineProviderConfig.NetworkInterfaceName,
machineProviderConfig.NetworkInterfaceAddress, offset); err != nil {
return err
}
// TODO: support setFilesystems
//if err := setFilesystems(d, &domainDef); err != nil {
// return err
//}
connectURI, err := virConn.GetURI()
if err != nil {
return fmt.Errorf("error retrieving libvirt connection URI: %v", err)
}
log.Printf("[INFO] Creating libvirt domain at %s", connectURI)
data, err := xmlMarshallIndented(domainDef)
if err != nil {
return fmt.Errorf("error serializing libvirt domain: %v", err)
}
log.Printf("[DEBUG] Creating libvirt domain with XML:\n%s", data)
domain, err := virConn.DomainDefineXML(data)
if err != nil {
return fmt.Errorf("error defining libvirt domain: %v", err)
}
if err := domain.SetAutostart(machineProviderConfig.Autostart); err != nil {
return fmt.Errorf("error setting Autostart: %v", err)
}
err = domain.Create()
if err != nil {
return fmt.Errorf("error creating libvirt domain: %v", err)
}
defer domain.Free()
id, err := domain.GetUUIDString()
if err != nil {
return fmt.Errorf("error retrieving libvirt domain id: %v", err)
}
log.Printf("[INFO] Domain ID: %s", id)
return nil
}
func deleteDomain(name string, machineProviderConfig *providerconfigv1.LibvirtMachineProviderConfig) error {
log.Printf("[DEBUG] Delete a domain")
client, err := buildClient(machineProviderConfig.URI)
if err != nil {
return fmt.Errorf("Failed to build libvirt client: %s", err)
}
virConn := client.libvirt
if virConn == nil {
return fmt.Errorf(LibVirtConIsNil)
}
log.Printf("[DEBUG] Deleting domain %s", name)
domain, err := virConn.LookupDomainByName(name)
if err != nil {
return fmt.Errorf("Error retrieving libvirt domain: %s", err)
}
defer domain.Free()
state, _, err := domain.GetState()
if err != nil {
return fmt.Errorf("Couldn't get info about domain: %s", err)
}
if state == libvirt.DOMAIN_RUNNING || state == libvirt.DOMAIN_PAUSED {
if err := domain.Destroy(); err != nil {
return fmt.Errorf("Couldn't destroy libvirt domain: %s", err)
}
}
if err := domain.UndefineFlags(libvirt.DOMAIN_UNDEFINE_NVRAM); err != nil {
if e := err.(libvirt.Error); e.Code == libvirt.ERR_NO_SUPPORT || e.Code == libvirt.ERR_INVALID_ARG {
log.Printf("libvirt does not support undefine flags: will try again without flags")
if err := domain.Undefine(); err != nil {
return fmt.Errorf("Couldn't undefine libvirt domain: %s", err)
}
} else {
return fmt.Errorf("Couldn't undefine libvirt domain with flags: %s", err)
}
}
return nil
}
// CreateVolumeAndMachine creates a volume and domain which consumes the former one
func CreateVolumeAndMachine(machine *clusterv1.Machine, offset int) error {
machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
return fmt.Errorf("error getting machineProviderConfig from spec: %v", err)
}
if err := createVolume(machine.Name, machineProviderConfig); err != nil {
return fmt.Errorf("error creating volume: %v", err)
}
if err = createDomain(machine.Name, machineProviderConfig, offset); err != nil {
return fmt.Errorf("error creating domain: %v", err)
}
return nil
}
// DeleteVolumeAndDomain deletes a domain and its referenced volume
func DeleteVolumeAndDomain(machine *clusterv1.Machine) error {
machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
if err != nil {
return fmt.Errorf("error getting machineProviderConfig from spec: %v", err)
}
if err := deleteDomain(machine.Name, machineProviderConfig); err != nil {
return fmt.Errorf("error deleting domain: %v", err)
}
if err := deleteVolume(machine.Name, machineProviderConfig.URI); err != nil {
return fmt.Errorf("error deleting domain: %v", err)
}
return nil
}
// DomainExists verify a domain exists for given machine
func DomainExists(machine *clusterv1.Machine) (bool, error) {
log.Printf("[DEBUG] Check if a domain exists")
machineProviderConfig, err := MachineProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
client, err := buildClient(machineProviderConfig.URI)
if err != nil {
return false, fmt.Errorf("Failed to build libvirt client: %s", err)
}
virConn := client.libvirt
if virConn == nil {
return false, fmt.Errorf(LibVirtConIsNil)
}
domain, err := virConn.LookupDomainByName(machine.Name)
if err != nil {
if err.(libvirt.Error).Code == libvirt.ERR_NO_DOMAIN {
return false, nil
}
return false, err
}
defer domain.Free()
return true, nil
}