2024-10-23 02:56:55 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
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 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 04:38:27 -05:00
|
|
|
log.Info("ADD FAILED", domcfg.Name, domcfg.UUID)
|
2024-10-23 04:25:24 -05:00
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 02:56:55 -05:00
|
|
|
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 {
|
2024-10-23 04:25:24 -05:00
|
|
|
if d.pb.Hostname != 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|
|
|
var ok bool = true
|
|
|
|
|
|
|
|
if d == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if domcfg == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:38:27 -05:00
|
|
|
if updateMemory(d, domcfg) {
|
|
|
|
// updateMemory failed
|
2024-10-23 04:25:24 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:38:27 -05:00
|
|
|
if updateNetwork(d, domcfg) {
|
|
|
|
// updateNetwork failed
|
|
|
|
ok = false
|
|
|
|
}
|
2024-10-23 04:25:24 -05:00
|
|
|
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
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|