better libvirt xml processing
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
9518e70d7a
commit
fb722a3dc9
|
@ -31,17 +31,11 @@ func addDomainDroplet(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
|||
me.droplets = append(me.droplets, d)
|
||||
me.changed = true
|
||||
|
||||
if updateDroplet(d, domcfg) {
|
||||
if me.changed {
|
||||
log.Info("updateDroplet() worked. droplet changed")
|
||||
} else {
|
||||
log.Verbose("updateDroplet() worked. nothing changed")
|
||||
}
|
||||
} else {
|
||||
err := updateDroplet(d, domcfg)
|
||||
if err != nil {
|
||||
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
||||
return d, errors.New("update failed for " + domcfg.Name)
|
||||
}
|
||||
|
||||
log.Info("added new droplet", domcfg.Name, domcfg.UUID)
|
||||
dumpNonStandardXML(domcfg)
|
||||
return d, nil
|
||||
|
@ -87,19 +81,23 @@ func findDomain(domcfg *libvirtxml.Domain) (*DropletT, error) {
|
|||
return found, nil
|
||||
}
|
||||
|
||||
func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
||||
var ok bool = true
|
||||
func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) error {
|
||||
var alle []*pb.Event
|
||||
|
||||
if d == nil {
|
||||
return false
|
||||
return errors.New("d == nil")
|
||||
}
|
||||
if domcfg == nil {
|
||||
return false
|
||||
return errors.New("domcfg == nil")
|
||||
}
|
||||
|
||||
if !updateMemory(d, domcfg) {
|
||||
e, err := updateMemory(d, domcfg)
|
||||
if err != nil {
|
||||
log.Info("updateMemory() failed")
|
||||
ok = false
|
||||
return err
|
||||
}
|
||||
if e != nil {
|
||||
alle = append(alle, e)
|
||||
}
|
||||
|
||||
// update arch & machine
|
||||
|
@ -107,12 +105,12 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|||
// OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm}
|
||||
t := domcfg.OS.Type
|
||||
if d.pb.QemuArch != t.Arch {
|
||||
alle = append(alle, NewChangeEvent(d.pb, "Droplet.QemuArch", d.pb.QemuArch, t.Arch))
|
||||
d.pb.QemuArch = t.Arch
|
||||
me.changed = true
|
||||
}
|
||||
if d.pb.QemuMachine != t.Machine {
|
||||
alle = append(alle, NewChangeEvent(d.pb, "Droplet.QemuMachine", d.pb.QemuMachine, t.Machine))
|
||||
d.pb.QemuMachine = t.Machine
|
||||
me.changed = true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,8 +118,8 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|||
if d.pb.Cpus != int64(domcfg.VCPU.Value) {
|
||||
// fmt.Printf("cpus changed. VCPU = %+v\n", domcfg.VCPU)
|
||||
fmt.Printf("cpus changed. from %d to %d\n", d.pb.Cpus, domcfg.VCPU.Value)
|
||||
alle = append(alle, NewChangeEvent(d.pb, "Droplet.Cpus", d.pb.Cpus, domcfg.VCPU.Value))
|
||||
d.pb.Cpus = int64(domcfg.VCPU.Value)
|
||||
me.changed = true
|
||||
}
|
||||
|
||||
// update spice port
|
||||
|
@ -140,7 +138,7 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|||
// 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
|
||||
alle = append(alle, NewChangeEvent(d.pb, "Droplet.SpicePort", d.pb.SpicePort, s.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,55 +147,63 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
|||
// check type
|
||||
if domcfg.Type != "kvm" {
|
||||
fmt.Printf("not kvm. Virt type == %s\n", domcfg.Type)
|
||||
ok = false
|
||||
return errors.New("not kvm")
|
||||
}
|
||||
|
||||
if !updateNetwork(d, domcfg) {
|
||||
log.Info("updateNetwork() failed")
|
||||
ok = false
|
||||
return errors.New("updateNetwork() failed")
|
||||
}
|
||||
|
||||
if !updateDisk(d, domcfg) {
|
||||
log.Info("updateDisk() failed")
|
||||
ok = false
|
||||
return errors.New("updateDisk() failed")
|
||||
}
|
||||
return ok
|
||||
|
||||
if alle == nil {
|
||||
log.Info("libvirt xml import worked. nothing changed", domcfg.Name)
|
||||
return nil
|
||||
}
|
||||
log.Info("libvirt xml import worked. droplet changed", domcfg.Name)
|
||||
// log.Info("all change events", alle)
|
||||
me.changed = true
|
||||
|
||||
// append each change event
|
||||
for _, e := range alle {
|
||||
me.events.Events = append(me.events.Events, e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// returns false if something went wrong
|
||||
func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) bool {
|
||||
func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) {
|
||||
if (d == nil) || (domcfg == nil) {
|
||||
return false
|
||||
return nil, errors.New("domcfg == nil")
|
||||
}
|
||||
|
||||
if domcfg.Memory == nil {
|
||||
return true
|
||||
// nothing to do. libvirt xml file didn't define memory size
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var m int64 = 0
|
||||
|
||||
// check memory
|
||||
if domcfg.Memory.Unit == "KiB" {
|
||||
switch domcfg.Memory.Unit {
|
||||
case "KiB":
|
||||
m = int64(domcfg.Memory.Value * 1024)
|
||||
}
|
||||
|
||||
if domcfg.Memory.Unit == "MiB" {
|
||||
case "MiB":
|
||||
m = int64(domcfg.Memory.Value * 1024 * 1024)
|
||||
}
|
||||
|
||||
if domcfg.Memory.Unit == "GiB" {
|
||||
case "GiB":
|
||||
m = int64(domcfg.Memory.Value * 1024 * 1024 * 1024)
|
||||
default:
|
||||
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
||||
return nil, errors.New("Unknown Memory Unit " + domcfg.Memory.Unit)
|
||||
}
|
||||
if e := d.SetMemory(m); e != nil {
|
||||
e := d.SetMemory(m)
|
||||
if e != nil {
|
||||
fmt.Printf("Memory changed %s to %d %s\n", pb.HumanFormatBytes(d.pb.Memory), domcfg.Memory.Value, domcfg.Memory.Unit)
|
||||
d.pb.Memory = m
|
||||
me.changed = true
|
||||
return true
|
||||
} else {
|
||||
// nothing changed
|
||||
return true
|
||||
// me.changed = true
|
||||
}
|
||||
fmt.Println("Unknown Memory Unit", domcfg.Memory.Unit)
|
||||
return false
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// returns false if something went wrong
|
||||
|
|
16
change.go
16
change.go
|
@ -59,7 +59,7 @@ func convertToString(x any) string {
|
|||
}
|
||||
|
||||
// Wrapping the int into a protobuf message
|
||||
func NewChangeEvent(d *pb.Droplet, origval any, newval any) *pb.Event {
|
||||
func NewChangeEvent(d *pb.Droplet, fname string, origval any, newval any) *pb.Event {
|
||||
var e *pb.Event
|
||||
e = new(pb.Event)
|
||||
|
||||
|
@ -70,7 +70,9 @@ func NewChangeEvent(d *pb.Droplet, origval any, newval any) *pb.Event {
|
|||
// this also works, but it's a bit overkill
|
||||
// e.NewAny = convertToAnypb(newval)
|
||||
|
||||
me.events.Events = append(me.events.Events, e)
|
||||
// me.events.Events = append(me.events.Events, e)
|
||||
// stuff := me.events.FormatJSON()
|
||||
// log.Info("events:", stuff)
|
||||
return e
|
||||
}
|
||||
|
||||
|
@ -79,18 +81,12 @@ func (d *DropletT) SetMemory(b int64) *pb.Event {
|
|||
oldm := pb.HumanFormatBytes(d.pb.Memory)
|
||||
newm := pb.HumanFormatBytes(b)
|
||||
if d.pb.Memory == b {
|
||||
log.Info("droplet", d.pb.Hostname, "memory unchanged", oldm, "to", newm)
|
||||
// log.Info("droplet", d.pb.Hostname, "memory unchanged", oldm, "to", newm)
|
||||
return nil
|
||||
}
|
||||
log.Info("droplet", d.pb.Hostname, "memory change from", oldm, "to", newm)
|
||||
|
||||
var e *pb.Event
|
||||
e = NewChangeEvent(d.pb, d.pb.Memory, b)
|
||||
e.FieldName = "Droplet.Memory"
|
||||
|
||||
stuff := me.events.FormatJSON()
|
||||
log.Info("events:", stuff)
|
||||
return e
|
||||
return NewChangeEvent(d.pb, "Droplet.Memory", d.pb.Memory, b)
|
||||
}
|
||||
|
||||
// update the droplet memory
|
||||
|
|
20
main.go
20
main.go
|
@ -52,6 +52,7 @@ func main() {
|
|||
// sanity check the droplets
|
||||
checkDroplets()
|
||||
|
||||
// ok tracks if all the libvirt xml files imported ok
|
||||
var ok bool = true
|
||||
for _, filename := range argv.Xml {
|
||||
domcfg, err := readXml(filename)
|
||||
|
@ -79,19 +80,18 @@ func main() {
|
|||
}
|
||||
} else {
|
||||
// this droplet is already here
|
||||
if updateDroplet(d, domcfg) {
|
||||
if me.changed {
|
||||
log.Info("updateDroplet() worked. droplet changed")
|
||||
} else {
|
||||
log.Info(filename, "nothing changed")
|
||||
}
|
||||
} else {
|
||||
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
||||
err := updateDroplet(d, domcfg)
|
||||
if err != nil {
|
||||
log.Info("updateDroplet() error", d.pb.Hostname, err)
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(argv.Xml) != 0 {
|
||||
if !ok {
|
||||
log.Info("importing xml files had errors")
|
||||
os.Exit(-1)
|
||||
}
|
||||
if me.changed {
|
||||
if argv.Save {
|
||||
writeConfigFile()
|
||||
|
@ -100,10 +100,6 @@ func main() {
|
|||
log.Info("Not saving changes (use --save to save)")
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
log.Info("adding xml files failed")
|
||||
os.Exit(-1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue