2024-10-23 02:56:55 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test add some ethernet devices
|
|
|
|
macs := getMacs(domcfg)
|
|
|
|
fmt.Printf("Virt mac addr:%s\n", macs)
|
|
|
|
|
|
|
|
fmt.Println("ADD FAILED", domcfg.Name, domcfg.UUID)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
|
|
|
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 02:56:55 -05:00
|
|
|
// test add some ethernet devices
|
|
|
|
macs := getMacs(domcfg)
|
|
|
|
fmt.Printf("Virt mac addr:%s\n", macs)
|
2024-10-23 04:25:24 -05:00
|
|
|
|
|
|
|
// fmt.Println("UPDATE FAILED", domcfg.Name, domcfg.UUID)
|
|
|
|
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
|
|
|
}
|