more common code. it might work again. would be nice

to have a kuma check, but no. I fucking deleted it. and
of course with no backup or memory of where I mapped it to. kuma needs
an undelete! or an event log of changes to kuma. ironic since all it
does is track changes in state but it doesn't track it's own changes

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-26 09:38:02 -05:00
parent 161bfe395e
commit a50f387b96
3 changed files with 9 additions and 145 deletions

View File

@ -111,12 +111,12 @@ func updateDroplet(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error
// OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm}
t := domcfg.OS.Type
if d.QemuArch != t.Arch {
e := NewChangeEvent(d, "Droplet.QemuArch", d.QemuArch, t.Arch)
e := d.NewChangeEvent("Droplet.QemuArch", d.QemuArch, t.Arch)
alle = append(alle, e)
d.QemuArch = t.Arch
}
if d.QemuMachine != t.Machine {
e := NewChangeEvent(d, "Droplet.QemuMachine", d.QemuMachine, t.Machine)
e := d.NewChangeEvent("Droplet.QemuMachine", d.QemuMachine, t.Machine)
alle = append(alle, e)
d.QemuMachine = t.Machine
}
@ -126,7 +126,7 @@ func updateDroplet(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error
if d.Cpus != int64(domcfg.VCPU.Value) {
// fmt.Printf("cpus changed. VCPU = %+v\n", domcfg.VCPU)
fmt.Printf("cpus changed. from %d to %d\n", d.Cpus, domcfg.VCPU.Value)
alle = append(alle, NewChangeEvent(d, "Droplet.Cpus", d.Cpus, domcfg.VCPU.Value))
alle = append(alle, d.NewChangeEvent("Droplet.Cpus", d.Cpus, domcfg.VCPU.Value))
d.Cpus = int64(domcfg.VCPU.Value)
}
@ -146,7 +146,7 @@ func updateDroplet(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error
// print out, but ignore the port number
d.SpicePort = int64(s.Port)
fmt.Printf("Spice Port set to = %d\n", s.Port)
alle = append(alle, NewChangeEvent(d, "Droplet.SpicePort", d.SpicePort, s.Port))
alle = append(alle, d.NewChangeEvent("Droplet.SpicePort", d.SpicePort, s.Port))
}
}
}
@ -324,7 +324,7 @@ func updateNetwork(d *pb.Droplet, domcfg *libvirtxml.Domain) ([]*pb.Event, error
}
eth.Name = brname
d.Networks = append(d.Networks, eth)
allEvents = append(allEvents, NewChangeEvent(d, "Droplet NewNetwork", "", mac+" "+brname))
allEvents = append(allEvents, d.NewChangeEvent("Droplet NewNetwork", "", mac+" "+brname))
}
}

136
change.go
View File

@ -1,136 +0,0 @@
package main
import (
// "reflect"
"errors"
"fmt"
"time"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
pb "go.wit.com/lib/protobuf/virtbuf"
"go.wit.com/log"
)
func convertToAnypb(x any) *anypb.Any {
switch v := x.(type) {
case int64:
var a *anypb.Any
a, _ = anypb.New(wrapperspb.Int64(x.(int64)))
return a
case string:
var a *anypb.Any
a, _ = anypb.New(wrapperspb.String(x.(string)))
return a
case int:
var a *anypb.Any
a, _ = anypb.New(wrapperspb.Int64(x.(int64)))
return a
case bool:
var a *anypb.Any
a, _ = anypb.New(wrapperspb.Bool(x.(bool)))
return a
default:
log.Error(errors.New("convertToAnypb() unknown type"), "v =", v, "x =", x)
return nil
}
return nil
}
func convertToString(x any) string {
switch v := x.(type) {
case int64:
return fmt.Sprintf("%d", x.(int64))
case string:
return x.(string)
case int:
return fmt.Sprintf("%d", x.(int))
case uint:
return fmt.Sprintf("%d", x.(uint))
case bool:
if x.(bool) {
return "true"
}
return "false"
default:
log.Info("convertToSTring() unknown type", v)
log.Error(errors.New("convertToSTring() unknown type"), "v =", v, "x =", x)
return ""
}
return ""
}
// Wrapping the int into a protobuf message
func NewChangeEvent(d *pb.Droplet, fname string, origval any, newval any) *pb.Event {
var e *pb.Event
e = new(pb.Event)
e.Droplet = d.Hostname
e.OrigVal = convertToString(origval)
e.NewVal = convertToString(newval)
e.FieldName = fname
now := time.Now()
e.Start = timestamppb.New(now)
// this also works, but it's a bit overkill
// e.NewAny = convertToAnypb(newval)
// me.events.Events = append(me.events.Events, e)
// stuff := me.events.FormatJSON()
// log.Info("events:", stuff)
return e
}
// work in progress
func NewAddEvent(a any, fname string, newval any) *pb.Event {
var e *pb.Event
e = new(pb.Event)
switch v := a.(type) {
case *pb.Droplet:
var d *pb.Droplet
d = a.(*pb.Droplet)
e.Droplet = d.Hostname
case *pb.Cluster:
e.Droplet = "Cluster"
case nil:
e.Droplet = "<nil>"
default:
log.Info("newAddEvent() unknown type", v)
e.Droplet = "on something somewhere"
}
e.NewVal = convertToString(newval)
e.FieldName = fname
now := time.Now()
e.Start = timestamppb.New(now)
return e
}
/*
// update the droplet memory
func (d *pb.Droplet) 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)
return nil
}
log.Info("droplet", d.pb.Hostname, "memory change from", oldm, "to", newm)
return NewChangeEvent(d.pb, "Droplet.Memory", d.pb.Memory, b)
}
*/
/*
// update the droplet memory
func (d *pb.Droplet) SetCpus(b int64) {
log.Info("Set the number of cpus for the droplet", b)
}
*/

View File

@ -51,7 +51,7 @@ func addClusterFilepath(dir string) *pb.Event {
if !found {
if dir != "." {
// make a new Add Event
e = NewAddEvent(nil, "Add Cluster Directory", dir)
e = pb.NewAddEvent(nil, "Add Cluster Directory", dir)
me.cluster.Dirs = append(me.cluster.Dirs, dir)
}
}
@ -93,7 +93,7 @@ func insertFilename(d *pb.Droplet, filename string) (*pb.Event, error) {
}
}
// make a new Add Event
e := NewChangeEvent(d, "Add Disk", "", filename)
e := d.NewChangeEvent("Add Disk", "", filename)
// add the disk protobuf entry
var disk *pb.Disk
@ -146,7 +146,7 @@ func checkDiskFilenames() []*pb.Event {
addClusterFilepath(dir)
if disk.Filename != filebase {
// update filename
e := NewChangeEvent(d, "Disk.Filename", disk.Filename, filebase)
e := d.NewChangeEvent("Disk.Filename", disk.Filename, filebase)
alle = append(alle, e)
disk.Filename = filebase
}
@ -158,7 +158,7 @@ func checkDiskFilenames() []*pb.Event {
}
if disk.Filepath != dir {
// update filename
e := NewChangeEvent(d, "Disk.Filepath", disk.Filepath, dir)
e := d.NewChangeEvent("Disk.Filepath", disk.Filepath, dir)
alle = append(alle, e)
disk.Filepath = dir
}