virtigo/addDroplet.go

127 lines
2.8 KiB
Go
Raw Normal View History

// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
package main
import (
"errors"
"fmt"
"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")
}
// 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) {
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
}
// 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
}
// test add some ethernet devices
macs := getMacs(domcfg)
fmt.Printf("Virt mac addr:%s\n", macs)
// 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
}