2024-10-23 02:56:55 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2024-10-25 05:04:52 -05:00
|
|
|
"github.com/google/uuid"
|
2024-10-23 05:32:49 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-23 04:38:27 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-23 02:56:55 -05:00
|
|
|
"libvirt.org/go/libvirtxml"
|
|
|
|
)
|
|
|
|
|
2024-10-23 11:27:27 -05:00
|
|
|
// import a libvirt xml file
|
2024-10-23 04:25:24 -05:00
|
|
|
func addDomainDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
|
|
|
if domcfg == nil {
|
|
|
|
return nil, errors.New("domcfg == nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
d, _ := findDomain(domcfg)
|
|
|
|
if d != nil {
|
|
|
|
return d, errors.New(d.pb.Hostname + " droplet exists. need to update instead")
|
|
|
|
}
|
2024-10-23 05:54:59 -05:00
|
|
|
// this is a new unknown droplet (not in the config file)
|
|
|
|
d = new(DropletT)
|
2024-10-23 04:25:24 -05:00
|
|
|
|
2024-10-23 05:54:59 -05:00
|
|
|
d.pb = me.cluster.AddDroplet(domcfg.UUID, domcfg.Name, 2, 2*1024*1024)
|
2024-10-23 19:15:51 -05:00
|
|
|
d.pb.StartState = pb.DropletState_OFF
|
|
|
|
d.CurrentState = pb.DropletState_UNKNOWN
|
2024-10-23 06:06:38 -05:00
|
|
|
|
2024-10-25 05:04:52 -05:00
|
|
|
// if the domcfg doesn't have a uuid, make a new one here
|
|
|
|
if d.pb.Uuid == "" {
|
|
|
|
u := uuid.New()
|
|
|
|
d.pb.Uuid = u.String()
|
|
|
|
}
|
|
|
|
|
2024-10-23 05:54:59 -05:00
|
|
|
me.droplets = append(me.droplets, d)
|
|
|
|
me.changed = true
|
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
err := updateDroplet(d, domcfg)
|
|
|
|
if err != nil {
|
2024-10-23 05:54:59 -05:00
|
|
|
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
|
|
|
return d, errors.New("update failed for " + domcfg.Name)
|
|
|
|
}
|
2024-10-23 11:37:51 -05:00
|
|
|
log.Info("added new droplet", domcfg.Name, domcfg.UUID)
|
|
|
|
dumpNonStandardXML(domcfg)
|
2024-10-23 05:54:59 -05:00
|
|
|
return d, nil
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func findDomain(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
2024-10-23 02:56:55 -05:00
|
|
|
var found *DropletT
|
|
|
|
if domcfg == nil {
|
|
|
|
return nil, errors.New("domcfg == nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range me.droplets {
|
|
|
|
if d.pb.Hostname == domcfg.Name {
|
2024-10-23 04:25:24 -05:00
|
|
|
if d.pb.Uuid != domcfg.UUID {
|
2024-10-24 18:55:35 -05:00
|
|
|
if domcfg.UUID == "" {
|
|
|
|
// ignore blank or nonexistent UUID's
|
|
|
|
// todo: check to see if the uuid already exists ?
|
|
|
|
domcfg.UUID = d.pb.Uuid
|
|
|
|
} else {
|
|
|
|
fmt.Println("Will Change UUID from", d.pb.Uuid, "to", domcfg.UUID, "for hostname", d.pb.Hostname)
|
|
|
|
d.pb.Uuid = domcfg.UUID
|
|
|
|
me.changed = true
|
|
|
|
}
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
|
|
|
if found == nil {
|
|
|
|
found = d
|
|
|
|
} else {
|
|
|
|
fmt.Println("FOUND TWICE", d.pb.Uuid, domcfg.Name, domcfg.UUID)
|
|
|
|
return d, errors.New("Found Twice")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if d.pb.Uuid == domcfg.UUID {
|
2024-10-23 04:25:24 -05:00
|
|
|
if d.pb.Hostname != domcfg.Name {
|
2024-10-24 15:14:47 -05:00
|
|
|
fmt.Println("protobuf has: UUID and Name:", d.pb.Uuid, d.pb.Hostname)
|
|
|
|
fmt.Println("libvirt has: UUID and Name:", domcfg.UUID, domcfg.Name)
|
2024-10-23 02:56:55 -05:00
|
|
|
fmt.Println("FOUND UUID WITH MIS-MATCHED NAME", domcfg.Name, domcfg.UUID)
|
|
|
|
return d, errors.New("UUID with mis-matched names")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:25:24 -05:00
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) error {
|
|
|
|
var alle []*pb.Event
|
2024-10-23 04:25:24 -05:00
|
|
|
|
|
|
|
if d == nil {
|
2024-10-25 04:30:35 -05:00
|
|
|
return errors.New("d == nil")
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
if domcfg == nil {
|
2024-10-25 04:30:35 -05:00
|
|
|
return errors.New("domcfg == nil")
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
e, err := updateMemory(d, domcfg)
|
|
|
|
if err != nil {
|
2024-10-23 05:07:42 -05:00
|
|
|
log.Info("updateMemory() failed")
|
2024-10-25 04:30:35 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
alle = append(alle, e)
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
2024-10-23 16:18:49 -05:00
|
|
|
// update arch & machine
|
|
|
|
if (domcfg.OS != nil) && (domcfg.OS.Type != nil) {
|
|
|
|
// OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm}
|
|
|
|
t := domcfg.OS.Type
|
|
|
|
if d.pb.QemuArch != t.Arch {
|
2024-10-25 04:30:35 -05:00
|
|
|
alle = append(alle, NewChangeEvent(d.pb, "Droplet.QemuArch", d.pb.QemuArch, t.Arch))
|
2024-10-23 16:18:49 -05:00
|
|
|
d.pb.QemuArch = t.Arch
|
|
|
|
}
|
|
|
|
if d.pb.QemuMachine != t.Machine {
|
2024-10-25 04:30:35 -05:00
|
|
|
alle = append(alle, NewChangeEvent(d.pb, "Droplet.QemuMachine", d.pb.QemuMachine, t.Machine))
|
2024-10-23 16:18:49 -05:00
|
|
|
d.pb.QemuMachine = t.Machine
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:25:24 -05:00
|
|
|
// check cpus
|
|
|
|
if d.pb.Cpus != int64(domcfg.VCPU.Value) {
|
2024-10-23 11:27:27 -05:00
|
|
|
// fmt.Printf("cpus changed. VCPU = %+v\n", domcfg.VCPU)
|
|
|
|
fmt.Printf("cpus changed. from %d to %d\n", d.pb.Cpus, domcfg.VCPU.Value)
|
2024-10-25 04:30:35 -05:00
|
|
|
alle = append(alle, NewChangeEvent(d.pb, "Droplet.Cpus", d.pb.Cpus, domcfg.VCPU.Value))
|
2024-10-23 04:25:24 -05:00
|
|
|
d.pb.Cpus = int64(domcfg.VCPU.Value)
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:49:15 -05:00
|
|
|
// update spice port
|
|
|
|
if domcfg.Devices.Graphics != nil {
|
|
|
|
for _, g := range domcfg.Devices.Graphics {
|
|
|
|
if g.Spice == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var s *libvirtxml.DomainGraphicSpice
|
|
|
|
s = g.Spice
|
|
|
|
// fmt.Printf("Spice: %d %+v %s\n", i, s, s.AutoPort)
|
|
|
|
if s.AutoPort == "yes" {
|
|
|
|
// should ignore either way
|
|
|
|
} else {
|
2024-10-23 18:53:02 -05:00
|
|
|
if d.pb.SpicePort != int64(s.Port) {
|
|
|
|
// print out, but ignore the port number
|
|
|
|
d.pb.SpicePort = int64(s.Port)
|
|
|
|
fmt.Printf("Spice Port set to = %d\n", s.Port)
|
2024-10-25 04:30:35 -05:00
|
|
|
alle = append(alle, NewChangeEvent(d.pb, "Droplet.SpicePort", d.pb.SpicePort, s.Port))
|
2024-10-23 18:53:02 -05:00
|
|
|
}
|
2024-10-23 17:49:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:25:24 -05:00
|
|
|
// check type
|
|
|
|
if domcfg.Type != "kvm" {
|
|
|
|
fmt.Printf("not kvm. Virt type == %s\n", domcfg.Type)
|
2024-10-25 04:30:35 -05:00
|
|
|
return errors.New("not kvm")
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 05:35:02 -05:00
|
|
|
e, err = updateNetwork(d, domcfg)
|
|
|
|
if err != nil {
|
2024-10-23 05:07:42 -05:00
|
|
|
log.Info("updateNetwork() failed")
|
2024-10-25 04:30:35 -05:00
|
|
|
return errors.New("updateNetwork() failed")
|
2024-10-23 04:38:27 -05:00
|
|
|
}
|
2024-10-25 05:35:02 -05:00
|
|
|
if e != nil {
|
|
|
|
alle = append(alle, e)
|
|
|
|
}
|
2024-10-23 05:32:49 -05:00
|
|
|
|
|
|
|
if !updateDisk(d, domcfg) {
|
2024-10-25 04:30:35 -05:00
|
|
|
return errors.New("updateDisk() failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if alle == nil {
|
|
|
|
log.Info("libvirt xml import worked. nothing changed", domcfg.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Info("libvirt xml import worked. droplet changed", domcfg.Name)
|
|
|
|
// log.Info("all change events", alle)
|
|
|
|
me.changed = true
|
|
|
|
|
|
|
|
// append each change event
|
|
|
|
for _, e := range alle {
|
|
|
|
me.events.Events = append(me.events.Events, e)
|
2024-10-23 05:32:49 -05:00
|
|
|
}
|
2024-10-25 04:30:35 -05:00
|
|
|
return nil
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
2024-10-23 05:07:42 -05:00
|
|
|
// returns false if something went wrong
|
2024-10-25 04:30:35 -05:00
|
|
|
func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) {
|
2024-10-23 04:25:24 -05:00
|
|
|
if (d == nil) || (domcfg == nil) {
|
2024-10-25 04:30:35 -05:00
|
|
|
return nil, errors.New("domcfg == nil")
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
|
2024-10-23 17:49:15 -05:00
|
|
|
if domcfg.Memory == nil {
|
2024-10-25 04:30:35 -05:00
|
|
|
// nothing to do. libvirt xml file didn't define memory size
|
|
|
|
return nil, nil
|
2024-10-23 17:49:15 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
var m int64 = 0
|
|
|
|
switch domcfg.Memory.Unit {
|
|
|
|
case "KiB":
|
2024-10-23 04:25:24 -05:00
|
|
|
m = int64(domcfg.Memory.Value * 1024)
|
2024-10-25 04:30:35 -05:00
|
|
|
case "MiB":
|
2024-10-23 05:32:49 -05:00
|
|
|
m = int64(domcfg.Memory.Value * 1024 * 1024)
|
2024-10-25 04:30:35 -05:00
|
|
|
case "GiB":
|
2024-10-23 05:32:49 -05:00
|
|
|
m = int64(domcfg.Memory.Value * 1024 * 1024 * 1024)
|
2024-10-25 04:30:35 -05:00
|
|
|
default:
|
|
|
|
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
|
|
|
return nil, errors.New("Unknown Memory Unit " + domcfg.Memory.Unit)
|
2024-10-25 03:34:39 -05:00
|
|
|
}
|
2024-10-25 04:30:35 -05:00
|
|
|
e := d.SetMemory(m)
|
|
|
|
if e != nil {
|
2024-10-25 03:34:39 -05:00
|
|
|
fmt.Printf("Memory changed %s to %d %s\n", pb.HumanFormatBytes(d.pb.Memory), domcfg.Memory.Value, domcfg.Memory.Unit)
|
|
|
|
d.pb.Memory = m
|
2024-10-25 04:30:35 -05:00
|
|
|
// me.changed = true
|
|
|
|
}
|
|
|
|
return e, nil
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
|
2024-10-25 05:35:02 -05:00
|
|
|
func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) {
|
|
|
|
var newEvent *pb.Event
|
2024-10-23 04:38:27 -05:00
|
|
|
if (d == nil) || (domcfg == nil) {
|
2024-10-25 05:35:02 -05:00
|
|
|
return nil, errors.New("domcfg == nil")
|
2024-10-23 04:38:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 05:35:02 -05:00
|
|
|
// mac address & bridge name
|
|
|
|
var macs map[string]string
|
|
|
|
macs = make(map[string]string)
|
2024-10-23 04:38:27 -05:00
|
|
|
// Iterate over the network interfaces and print the MAC addresses
|
|
|
|
for _, iface := range domcfg.Devices.Interfaces {
|
2024-10-25 05:35:02 -05:00
|
|
|
var hwaddr string
|
|
|
|
var brname string
|
2024-10-23 17:49:15 -05:00
|
|
|
// fmt.Printf("iface: %+v\n", iface)
|
|
|
|
// fmt.Printf("MAC: %+v\n", iface.MAC)
|
|
|
|
// fmt.Printf("Source: %+v\n", iface.Source)
|
|
|
|
// fmt.Printf("Bridge: %+v\n", iface.Source.Bridge)
|
|
|
|
// fmt.Printf("Model: %+v\n", iface.Model)
|
2024-10-23 04:38:27 -05:00
|
|
|
if iface.MAC != nil {
|
|
|
|
// iface.MAC.Address = "aa:bb:aa:bb:aa:ff"
|
2024-10-23 05:07:42 -05:00
|
|
|
// log.Info("Interface:", iface.Target, "MAC Address:", iface.MAC.Address)
|
|
|
|
// fmt.Printf("source: %+v\n", iface.Source)
|
2024-10-25 05:35:02 -05:00
|
|
|
hwaddr = iface.MAC.Address
|
2024-10-23 04:38:27 -05:00
|
|
|
} else {
|
2024-10-25 05:35:02 -05:00
|
|
|
fmt.Printf("iface: %+v\n", iface)
|
2024-10-24 15:14:47 -05:00
|
|
|
fmt.Printf("Interface Target: %+v, MAC Address not available\n", iface.Target)
|
2024-10-25 05:35:02 -05:00
|
|
|
return nil, errors.New("network XML does not have a MAC Address")
|
|
|
|
}
|
|
|
|
if iface.Source != nil {
|
|
|
|
if iface.Source.Bridge != nil {
|
|
|
|
brname = iface.Source.Bridge.Bridge
|
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
}
|
2024-10-25 05:35:02 -05:00
|
|
|
macs[hwaddr] = brname
|
2024-10-23 04:38:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 05:35:02 -05:00
|
|
|
for mac, brname := range macs {
|
2024-10-23 05:07:42 -05:00
|
|
|
var found bool = false
|
2024-10-25 05:35:02 -05:00
|
|
|
// log.Info("XML has mac address:", mac, brname)
|
|
|
|
for _, eth := range d.pb.Networks {
|
2024-10-23 05:07:42 -05:00
|
|
|
if eth.Mac == mac {
|
2024-10-25 05:35:02 -05:00
|
|
|
// log.Info("OKAY. FOUND ETH:", eth.Mac, eth.Name, brname)
|
2024-10-23 05:07:42 -05:00
|
|
|
found = true
|
2024-10-25 05:35:02 -05:00
|
|
|
if eth.Name != brname {
|
|
|
|
log.Info("network changed bridge name:", eth.Mac, eth.Name, brname)
|
|
|
|
return nil, errors.New("bridge name changed")
|
|
|
|
}
|
2024-10-23 05:07:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
var eth *pb.Network
|
|
|
|
eth = new(pb.Network)
|
|
|
|
eth.Mac = mac
|
2024-10-25 05:35:02 -05:00
|
|
|
if brname == "" {
|
|
|
|
brname = "worldbr"
|
|
|
|
}
|
|
|
|
eth.Name = brname
|
2024-10-23 05:07:42 -05:00
|
|
|
d.pb.Networks = append(d.pb.Networks, eth)
|
2024-10-25 05:35:02 -05:00
|
|
|
newEvent = NewChangeEvent(d.pb, "Droplet NewNetwork", "", mac+" "+brname)
|
2024-10-23 05:07:42 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("mac addrs:", macs)
|
2024-10-25 05:35:02 -05:00
|
|
|
return newEvent, nil
|
2024-10-23 04:38:27 -05:00
|
|
|
}
|
2024-10-23 05:32:49 -05:00
|
|
|
|
2024-10-23 20:40:08 -05:00
|
|
|
/* from vm3-with-nvme-1.5GB-sec.xml
|
2024-10-23 21:46:18 -05:00
|
|
|
<disk type='block' device='disk'>
|
|
|
|
<driver name='qemu' type='raw'/>
|
|
|
|
<source dev='/dev/nvme4n1'/>
|
|
|
|
<backingStore/>
|
|
|
|
<target dev='vdb' bus='virtio'/>
|
|
|
|
<alias name='virtio-disk1'/>
|
|
|
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
|
|
|
|
</disk>
|
2024-10-23 20:40:08 -05:00
|
|
|
*/
|
|
|
|
|
2024-10-23 05:32:49 -05:00
|
|
|
// returns false if something went wrong
|
|
|
|
func updateDisk(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|
|
|
if (d == nil) || (domcfg == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, disk := range domcfg.Devices.Disks {
|
|
|
|
var t *libvirtxml.DomainDiskSourceFile
|
|
|
|
t = disk.Source.File
|
2024-10-23 20:17:33 -05:00
|
|
|
if t == nil {
|
|
|
|
fmt.Println("disk.Source.File == nil")
|
|
|
|
continue
|
|
|
|
}
|
2024-10-23 05:32:49 -05:00
|
|
|
filename := t.File
|
|
|
|
if filename == "" {
|
2024-10-23 05:54:59 -05:00
|
|
|
fmt.Println("No disk source file found.")
|
2024-10-23 05:32:49 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool = false
|
|
|
|
for _, disk := range d.pb.Disks {
|
|
|
|
if disk.Filename == filename {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("OKAY. FOUND filename", filename)
|
2024-10-23 05:32:49 -05:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
var disk *pb.Disk
|
|
|
|
disk = new(pb.Disk)
|
|
|
|
disk.Filename = filename
|
|
|
|
d.pb.Disks = append(d.pb.Disks, disk)
|
|
|
|
log.Info("New filename", filename)
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|