events are in cluster.E

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-30 18:10:40 -05:00
parent 9608bb680f
commit 18053caca8
6 changed files with 69 additions and 62 deletions

4
add.go
View File

@ -125,6 +125,6 @@ func (c *Cluster) BlankFields() {
} }
} }
func (c *Cluster) AppendEvent(e *Event) { func (epb *Events) AppendEvent(e *Event) {
c.Events = append(c.Events, e) epb.Events = append(epb.Events, e)
} }

View File

@ -153,7 +153,7 @@ func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error {
now := time.Now() now := time.Now()
e.Start = timestamppb.New(now) e.Start = timestamppb.New(now)
c.Events = append(c.Events, e) c.E.Events = append(c.E.Events, e)
return nil return nil
} }
@ -185,7 +185,7 @@ func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error {
now := time.Now() now := time.Now()
e.Start = timestamppb.New(now) e.Start = timestamppb.New(now)
c.Events = append(c.Events, e) c.E.Events = append(c.E.Events, e)
// update the droplet record // update the droplet record
d.CurrentHypervisor = newh.Hostname d.CurrentHypervisor = newh.Hostname

View File

@ -8,7 +8,12 @@ import "event.proto";
message Cluster { message Cluster {
int64 id = 1; int64 id = 1;
repeated string dirs = 2; repeated string dirs = 2;
repeated Droplet droplets = 3; repeated Droplet droplets = 3;
repeated Hypervisor hypervisors = 4; repeated Hypervisor hypervisors = 4;
repeated Event events = 5; // repeated Event events = 5;
Droplets d = 6;
Hypervisors h = 7;
Events e = 8;
} }

View File

@ -47,14 +47,11 @@ func (c *Cluster) ConfigSave() error {
return err return err
} }
var e *Events if err := ConfigWriteJSON(c.E, "events.json"); err != nil {
e = new(Events)
e.Events = c.Events
if err := ConfigWriteJSON(e, "events.json"); err != nil {
fmt.Println("events.json write failed") fmt.Println("events.json write failed")
return err return err
} }
if err := ConfigWriteTEXT(e, "events.text"); err != nil { if err := ConfigWriteTEXT(c.E, "events.text"); err != nil {
fmt.Println("events.json write failed") fmt.Println("events.json write failed")
return err return err
} }
@ -68,7 +65,7 @@ func (c *Cluster) ConfigSave() error {
newc.Dirs = c.Dirs newc.Dirs = c.Dirs
newc.Droplets = nil newc.Droplets = nil
newc.Hypervisors = nil newc.Hypervisors = nil
newc.Events = nil newc.E = nil
if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil { if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil {
fmt.Println("cluster.json write failed") fmt.Println("cluster.json write failed")
return err return err
@ -88,45 +85,6 @@ func backupConfigFiles() error {
return backupFiles(srcDir, destDir) return backupFiles(srcDir, destDir)
} }
/*
func (c *Cluster) ConfigLoadOld2() error {
if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
}
// erase or zero fields that shouldn't ever be written to the config file
c.BlankFields()
// load the cluster config file
if data, err := loadFile("virtigo.json"); err == nil {
if err = protojson.Unmarshal(data, c); err != nil {
fmt.Println("broken cluster.json config file")
fmt.Println(err)
return errors.New("cluster.json file is broken")
}
} else {
return err
}
var e *Events
e = new(Events)
// load the events config file
if data, err := loadFile("events.json"); err == nil {
if err = protojson.Unmarshal(data, e); err != nil {
fmt.Println("broken events.json config file")
return err
}
} else {
return err
}
// copy them over. is this needed? does the memory free otherwise?
for _, a := range e.Events {
c.Events = append(c.Events, a)
}
return nil
}
*/
func (c *Cluster) ConfigLoad() error { func (c *Cluster) ConfigLoad() error {
if c == nil { if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster") return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
@ -177,20 +135,51 @@ func (c *Cluster) ConfigLoad() error {
c.Hypervisors = append(c.Hypervisors, a) c.Hypervisors = append(c.Hypervisors, a)
} }
var e *Events if err := c.loadEvents(); err != nil {
e = new(Events) return err
}
return nil
}
func (c *Cluster) loadEvents() error {
var data []byte
var err error
if c == nil {
fmt.Println("cluster == nil")
os.Exit(-1)
}
if c.E == nil {
fmt.Println("cluster.E == nil")
c.E = new(Events)
}
if c.E == nil {
fmt.Println("cluster.E == nil")
os.Exit(-1)
}
// load the events config file // load the events config file
if data, err := loadFile("events.json"); err == nil { if data, err = loadFile("events.json"); err != nil {
if err = protojson.Unmarshal(data, e); err != nil {
fmt.Println("broken events.json config file") fmt.Println("broken events.json config file")
return err return err
} }
} else {
return err err = protojson.Unmarshal(data, c.E)
if err != nil {
fmt.Println("broken events.json config file")
// json load failed. try loading prototext
if data, err = loadFile("events.text"); err != nil {
fmt.Println("broken events.text config file")
fmt.Println(err)
return errors.New("events.text file is broken")
}
if err = prototext.Unmarshal(data, c.E); err != nil {
fmt.Println("broken events.text config file")
fmt.Println(err)
return errors.New("events.text file is broken")
} }
// copy them over. is this needed? does the memory free otherwise?
for _, a := range e.Events {
c.Events = append(c.Events, a)
} }
return nil return nil
} }

View File

@ -6,6 +6,7 @@ package virtbuf
import ( import (
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
) )
// human readable JSON // human readable JSON
@ -77,3 +78,7 @@ func (d *Droplet) UnmarshalJSON(data []byte) error {
func (e *Events) UnmarshalJSON(data []byte) error { func (e *Events) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, e) return protojson.Unmarshal(data, e)
} }
func (d *Droplet) Unmarshal(data []byte) error {
return proto.Unmarshal(data, d)
}

8
test.proto Normal file
View File

@ -0,0 +1,8 @@
syntax = "proto3";
package virtbuf;
message Cluster {
int64 id = 1;
repeated string s = 2;
repeated int i = 3;
}