From ade54fdedac3bf89636f914b89fd17eaa972947f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 26 Oct 2024 06:01:19 -0500 Subject: events should not be plural Signed-off-by: Jeff Carr --- Makefile | 9 +++++---- cluster.proto | 8 +++++--- config.go | 36 ++++++++++++++++++++++++++++++++++++ event.proto | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ events.proto | 49 ------------------------------------------------- helpers.go | 32 +++++++------------------------- 6 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 event.proto delete mode 100644 events.proto diff --git a/Makefile b/Makefile index 74da8df..1e7c471 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ # go install -all: droplet.pb.go hypervisor.pb.go cluster.pb.go events.pb.go experiments.pb.go +all: droplet.pb.go hypervisor.pb.go cluster.pb.go event.pb.go experiments.pb.go make -C example vet: lint @@ -42,11 +42,11 @@ hypervisor.pb.go: hypervisor.proto --go_opt=Mhypervisor.proto=go.wit.com/lib/protobuf/virtbuf \ hypervisor.proto -events.pb.go: events.proto +event.pb.go: event.proto cd ~/go/src && protoc --go_out=. \ --proto_path=go.wit.com/lib/protobuf/virtbuf \ - --go_opt=Mevents.proto=go.wit.com/lib/protobuf/virtbuf \ - events.proto + --go_opt=Mevent.proto=go.wit.com/lib/protobuf/virtbuf \ + event.proto experiments.pb.go: experiments.proto cd ~/go/src && protoc --go_out=. \ @@ -59,6 +59,7 @@ cluster.pb.go: cluster.proto --go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \ --go_opt=Mcluster.proto=go.wit.com/lib/protobuf/virtbuf \ --go_opt=Mhypervisor.proto=go.wit.com/lib/protobuf/virtbuf \ + --go_opt=Mevent.proto=go.wit.com/lib/protobuf/virtbuf \ cluster.proto deps: diff --git a/cluster.proto b/cluster.proto index 8631e43..0d07d5a 100644 --- a/cluster.proto +++ b/cluster.proto @@ -3,10 +3,12 @@ package virtbuf; import "droplet.proto"; import "hypervisor.proto"; +import "event.proto"; message Cluster { int64 id = 1; - repeated Droplet droplets = 2; - repeated Hypervisor hypervisors = 3; - repeated string dirs = 4; + repeated string dirs = 2; + repeated Droplet droplets = 3; + repeated Hypervisor hypervisors = 4; + repeated Event events = 5; } diff --git a/config.go b/config.go index 9bbf524..639844d 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,34 @@ func (e *Events) ConfigSave() error { return ConfigWriteTEXT(e, "events.text") } +func (c *Cluster) ConfigSave() error { + var d *Droplets + d = new(Droplets) + d.Droplets = c.Droplets + if err := ConfigWriteJSON(d, "newdroplets.json"); err != nil { + fmt.Println("droplets.json write failed") + return err + } + + var h *Hypervisors + h = new(Hypervisors) + h.Hypervisors = c.Hypervisors + if err := ConfigWriteJSON(h, "newhypervisors.json"); err != nil { + fmt.Println("hypervisors.json write failed") + return err + } + + var e *Events + e = new(Events) + e.Events = c.Events + if err := ConfigWriteJSON(h, "newEvents.json"); err != nil { + fmt.Println("newEvents.json write failed") + return err + } + + return nil +} + // read in the events log file // reads in from the prototext file // prototext file formats are not garrenteed to be stable. todo: hammer that out @@ -211,10 +239,18 @@ func (d *Droplets) FormatJSON() string { return protojson.Format(d) } +func (d *Droplet) FormatJSON() string { + return protojson.Format(d) +} + func (e *Events) FormatJSON() string { return protojson.Format(e) } +func (h *Hypervisors) FormatJSON() string { + return protojson.Format(h) +} + // apparently this isn't supposed to be used? // https://protobuf.dev/reference/go/faq/#unstable-text // this is a shame because this is much nicer output than JSON Format() diff --git a/event.proto b/event.proto new file mode 100644 index 0000000..49a8543 --- /dev/null +++ b/event.proto @@ -0,0 +1,49 @@ +syntax = "proto3"; +package virtbuf; + +import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp +import "google/protobuf/any.proto"; // Import the well-known type for Timestamp + +message Events { + string uuid = 1; // I guess why not just have this on each file + string version = 2; // maybe can be used for protobuf schema change violations + int64 event_size = 3; // max events to store in a single + repeated Event events = 4; // all the events +} + +// this information leans towards being human readable not programatic +// in other words, it's better to just have the droplet name here rather than the uuid +// at least for now in the early days. but maybe forever. +// homelab clouds normally don't have many events. +// we are talking less than 1 a minute. even 1 an hour is often a lot +message Event { + int32 id = 1; + EventType etype = 2; + string droplet = 3; // name of the droplet + string droplet_uuid = 4; // uuid of the droplet + string hypervisor = 5; // name of the hypervisor + string hypervisor_uuid = 6; // uuid of the hypervisor + google.protobuf.Timestamp start = 7; // start time + google.protobuf.Timestamp end = 8; // end time + string field_name = 9; // the field name that changed + string orig_val = 10; // original value + string new_val = 11; // new value + google.protobuf.Any orig_any = 12; // anypb format. probably overkill + google.protobuf.Any new_any = 13; // anypb format +} + +enum EventType { + ADD = 0; + DELETE = 1; + POWERON = 2; + POWEROFF = 3; // should indicate a "normal" shutdown + HIBERNATE = 4; + MIGRATE = 5; + DEMO = 6; + GET = 7; // request something + LOGIN = 8; // attempt to login + OK = 9; // everything is ok + FAIL = 10; // everything failed + CRASH = 11; // droplet hard crashed + CHANGE = 12; // droplet or hypervisor config change +} diff --git a/events.proto b/events.proto deleted file mode 100644 index 49a8543..0000000 --- a/events.proto +++ /dev/null @@ -1,49 +0,0 @@ -syntax = "proto3"; -package virtbuf; - -import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp -import "google/protobuf/any.proto"; // Import the well-known type for Timestamp - -message Events { - string uuid = 1; // I guess why not just have this on each file - string version = 2; // maybe can be used for protobuf schema change violations - int64 event_size = 3; // max events to store in a single - repeated Event events = 4; // all the events -} - -// this information leans towards being human readable not programatic -// in other words, it's better to just have the droplet name here rather than the uuid -// at least for now in the early days. but maybe forever. -// homelab clouds normally don't have many events. -// we are talking less than 1 a minute. even 1 an hour is often a lot -message Event { - int32 id = 1; - EventType etype = 2; - string droplet = 3; // name of the droplet - string droplet_uuid = 4; // uuid of the droplet - string hypervisor = 5; // name of the hypervisor - string hypervisor_uuid = 6; // uuid of the hypervisor - google.protobuf.Timestamp start = 7; // start time - google.protobuf.Timestamp end = 8; // end time - string field_name = 9; // the field name that changed - string orig_val = 10; // original value - string new_val = 11; // new value - google.protobuf.Any orig_any = 12; // anypb format. probably overkill - google.protobuf.Any new_any = 13; // anypb format -} - -enum EventType { - ADD = 0; - DELETE = 1; - POWERON = 2; - POWEROFF = 3; // should indicate a "normal" shutdown - HIBERNATE = 4; - MIGRATE = 5; - DEMO = 6; - GET = 7; // request something - LOGIN = 8; // attempt to login - OK = 9; // everything is ok - FAIL = 10; // everything failed - CRASH = 11; // droplet hard crashed - CHANGE = 12; // droplet or hypervisor config change -} diff --git a/helpers.go b/helpers.go index e9864ae..d55f64a 100644 --- a/helpers.go +++ b/helpers.go @@ -25,32 +25,14 @@ func (x *Hypervisor) GetMemoryPrintable() string { return fmt.Sprintf("%d GB", i) } -/* -func (c *Cluster) MarshalJSON() ([]byte, error) { - return protojson.Marshal(c) -} - -func (c *Cluster) FormatJSON() string { - return protojson.Format(c) -} - -func (c *Cluster) UnmarshalJSON(data []byte) error { - return protojson.Unmarshal(data, c) -} -*/ - -// apparently this isn't supposed to be used? -// https://protobuf.dev/reference/go/faq/#unstable-text -// this is a shame because this is much nicer output than JSON Format() -/* -func (c *Cluster) FormatTEXT() string { - return prototext.Format(c) -} - -func (d *Droplets) FormatTEXT() string { - return prototext.Format(d) +func (all *Droplets) FindDroplet(name string) *Droplet { + for _, d := range all.Droplets { + if d.Hostname == name { + return d + } + } + return nil } -*/ func (c *Cluster) FindDroplet(name string) *Droplet { for _, d := range c.Droplets { -- cgit v1.2.3