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 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 06:06:38 -05:00
|
|
|
d.pb.StartState = "off"
|
|
|
|
|
2024-10-23 05:54:59 -05:00
|
|
|
me.droplets = append(me.droplets, d)
|
|
|
|
me.changed = true
|
|
|
|
|
|
|
|
if updateDroplet(d, domcfg) {
|
|
|
|
if me.changed {
|
|
|
|
log.Info("updateDroplet() worked. droplet changed")
|
|
|
|
} else {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("updateDroplet() worked. nothing changed")
|
2024-10-23 05:54:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
|
|
|
return d, errors.New("update failed for " + domcfg.Name)
|
|
|
|
}
|
2024-10-23 11:27:27 -05:00
|
|
|
|
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-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 05:32:49 -05:00
|
|
|
if !updateMemory(d, domcfg) {
|
2024-10-23 05:07:42 -05:00
|
|
|
log.Info("updateMemory() failed")
|
2024-10-23 04:25:24 -05:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
d.pb.QemuArch = t.Arch
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
if d.pb.QemuMachine != t.Machine {
|
|
|
|
d.pb.QemuMachine = t.Machine
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 04:25:24 -05:00
|
|
|
d.pb.Cpus = int64(domcfg.VCPU.Value)
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// print out, but ignore the port number
|
|
|
|
d.pb.SpicePort = int64(s.Port)
|
|
|
|
fmt.Printf("Spice Port set to = %d\n", s.Port)
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:25:24 -05:00
|
|
|
// check type
|
|
|
|
if domcfg.Type != "kvm" {
|
|
|
|
fmt.Printf("not kvm. Virt type == %s\n", domcfg.Type)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
2024-10-23 05:32:49 -05:00
|
|
|
if !updateNetwork(d, domcfg) {
|
2024-10-23 05:07:42 -05:00
|
|
|
log.Info("updateNetwork() failed")
|
2024-10-23 04:38:27 -05:00
|
|
|
ok = false
|
|
|
|
}
|
2024-10-23 05:32:49 -05:00
|
|
|
|
|
|
|
if !updateDisk(d, domcfg) {
|
|
|
|
log.Info("updateDisk() failed")
|
|
|
|
ok = false
|
|
|
|
}
|
2024-10-23 04:25:24 -05:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2024-10-23 05:07:42 -05:00
|
|
|
// returns false if something went wrong
|
2024-10-23 04:25:24 -05:00
|
|
|
func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|
|
|
if (d == nil) || (domcfg == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:49:15 -05:00
|
|
|
if domcfg.Memory == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:25:24 -05:00
|
|
|
// 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
|
2024-10-23 11:27:27 -05:00
|
|
|
fmt.Printf("Memory changed %s to %d %s\n", pb.HumanFormatBytes(d.pb.Memory), domcfg.Memory.Value, domcfg.Memory.Unit)
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
2024-10-23 05:07:42 -05:00
|
|
|
return true
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
2024-10-23 05:32:49 -05:00
|
|
|
|
|
|
|
if domcfg.Memory.Unit == "MiB" {
|
|
|
|
var m int64
|
|
|
|
m = int64(domcfg.Memory.Value * 1024 * 1024)
|
|
|
|
if d.pb.Memory != m {
|
|
|
|
d.pb.Memory = m
|
|
|
|
me.changed = true
|
2024-10-23 11:27:27 -05:00
|
|
|
fmt.Printf("Memory changed %s to %d %s\n", pb.HumanFormatBytes(d.pb.Memory), domcfg.Memory.Value, domcfg.Memory.Unit)
|
2024-10-23 05:32:49 -05:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if domcfg.Memory.Unit == "GiB" {
|
|
|
|
var m int64
|
|
|
|
m = int64(domcfg.Memory.Value * 1024 * 1024 * 1024)
|
|
|
|
if d.pb.Memory != m {
|
|
|
|
d.pb.Memory = m
|
|
|
|
me.changed = true
|
2024-10-23 18:06:46 -05:00
|
|
|
fmt.Printf("Memory changed %s, %d %s\n", pb.HumanFormatBytes(d.pb.Memory), domcfg.Memory.Value, domcfg.Memory.Unit)
|
2024-10-23 05:32:49 -05:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2024-10-23 04:25:24 -05:00
|
|
|
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
2024-10-23 05:07:42 -05:00
|
|
|
return false
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
|
2024-10-23 05:07:42 -05:00
|
|
|
// returns false if something went wrong
|
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 {
|
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"
|
|
|
|
// fmt.Printf("MAC Address: %+v\n", iface.MAC)
|
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-23 04:38:27 -05:00
|
|
|
macs = append(macs, iface.MAC.Address)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Interface: %s, MAC Address: not available\n", iface.Target.Dev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 05:07:42 -05:00
|
|
|
for _, mac := range macs {
|
|
|
|
var found bool = false
|
|
|
|
for i, eth := range d.pb.Networks {
|
|
|
|
if eth.Mac == mac {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("OKAY. FOUND ETH:", i, eth.Mac, eth.Name)
|
2024-10-23 05:07:42 -05:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
var eth *pb.Network
|
|
|
|
eth = new(pb.Network)
|
|
|
|
eth.Mac = mac
|
|
|
|
eth.Name = "worldbr"
|
|
|
|
d.pb.Networks = append(d.pb.Networks, eth)
|
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
2024-10-23 04:38:27 -05:00
|
|
|
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("mac addrs:", macs)
|
2024-10-23 05:07:42 -05:00
|
|
|
return true
|
2024-10-23 04:38:27 -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
|
|
|
|
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
|
|
|
|
}
|