// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 package main import ( "errors" "fmt" "go.wit.com/log" "libvirt.org/go/libvirtxml" ) 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") } log.Info("ADD FAILED", domcfg.Name, domcfg.UUID) return nil, errors.New("not found") } func findDomain(domcfg *libvirtxml.Domain) (*DropletT, error) { var found *DropletT if domcfg == nil { return nil, errors.New("domcfg == nil") } for _, d := range me.droplets { if d.pb.Hostname == domcfg.Name { if d.pb.Uuid != domcfg.UUID { fmt.Println("CHANGED UUID", d.pb.Uuid, domcfg.UUID) d.pb.Uuid = domcfg.UUID me.changed = true } 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 { if d.pb.Hostname != domcfg.Name { fmt.Println("FOUND UUID WITH MIS-MATCHED NAME", domcfg.Name, domcfg.UUID) return d, errors.New("UUID with mis-matched names") } } } return found, nil } func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool { var ok bool = true if d == nil { return false } if domcfg == nil { return false } if updateMemory(d, domcfg) { // updateMemory failed ok = false } // check cpus if d.pb.Cpus != int64(domcfg.VCPU.Value) { fmt.Printf("cpus changed. VCPU = %+v\n", domcfg.VCPU) d.pb.Cpus = int64(domcfg.VCPU.Value) me.changed = true } // check type if domcfg.Type != "kvm" { fmt.Printf("not kvm. Virt type == %s\n", domcfg.Type) ok = false } if updateNetwork(d, domcfg) { // updateNetwork failed ok = false } return ok } func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) bool { if (d == nil) || (domcfg == nil) { return false } // check memory if domcfg.Memory.Unit == "KiB" { var m int64 m = int64(domcfg.Memory.Value * 1024) if d.pb.Memory != m { d.pb.Memory = m me.changed = true fmt.Printf("Memory changed %d, %d %s\n", d.pb.Memory, domcfg.Memory.Value, domcfg.Memory.Unit) return true } return false } fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit) return true } func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) bool { if (d == nil) || (domcfg == nil) { return false } var macs []string // Iterate over the network interfaces and print the MAC addresses for _, iface := range domcfg.Devices.Interfaces { if iface.MAC != nil { // iface.MAC.Address = "aa:bb:aa:bb:aa:ff" // fmt.Printf("MAC Address: %+v\n", iface.MAC) // fmt.Printf("Interface: %s, MAC Address: %s\n", iface.Target.Dev, iface.MAC.Address) macs = append(macs, iface.MAC.Address) } else { fmt.Printf("Interface: %s, MAC Address: not available\n", iface.Target.Dev) } } // for _, iface := range domcfg.Devices.Interfaces { log.Info("mac addrs:", macs) return false }