From e4345c8ad6584f4fc5393c7844bb1967d6564d63 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 24 Mar 2025 21:54:13 -0500 Subject: moving to a cluster.proto config file --- Makefile | 7 ++++++- add.go | 20 ++++++++++---------- change.go | 4 ++-- cluster.go | 48 ------------------------------------------------ cluster.proto | 16 ++++++++++++++++ config.go | 44 ++++++++++++++++++++++++++++++-------------- helpers.go | 8 ++++---- human.go | 2 +- oldCluster.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ sampleData.go | 2 +- 10 files changed, 118 insertions(+), 81 deletions(-) delete mode 100644 cluster.go create mode 100644 cluster.proto create mode 100644 oldCluster.go diff --git a/Makefile b/Makefile index 2a7fd08..b159e53 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # cd ~/go/src/google.golang.org/protobuf/cmd/protoc-gen-go # go install -all: droplet.pb.go hypervisor.pb.go event.pb.go goimports vet +all: proto goimports vet vet: @GO111MODULE=off go vet @@ -22,6 +22,8 @@ clean: rm -f *.pb.go -rm -f go.* +proto:droplet.pb.go hypervisor.pb.go event.pb.go cluster.pb.go + droplet.pb.go: droplet.proto autogenpb --proto droplet.proto @@ -31,6 +33,9 @@ hypervisor.pb.go: hypervisor.proto event.pb.go: event.proto autogenpb --proto event.proto +cluster.pb.go: cluster.proto + autogenpb --proto cluster.proto + deps: apt install golang-goprotobuf-dev apt install protobuf-compiler diff --git a/add.go b/add.go index ce3285c..28d103c 100644 --- a/add.go +++ b/add.go @@ -9,7 +9,7 @@ import ( ) /* -func (c *Cluster) InitDroplet(hostname string) (*Droplet, error) { +func (c *OldCluster) InitDroplet(hostname string) (*Droplet, error) { var d *Droplet d = new(Droplet) d.Current = new(Current) @@ -50,7 +50,7 @@ func (x *Hypervisor) SetMemoryGB(gb int) { x.Memory = int64(gb * 1024 * 1024 * 1024) } -func (c *Cluster) FindDropletByName(name string) *Droplet { +func (c *OldCluster) FindDropletByName(name string) *Droplet { loop := c.d.All() // get the list of droplets for loop.Scan() { d := loop.Next() @@ -61,7 +61,7 @@ func (c *Cluster) FindDropletByName(name string) *Droplet { return nil } -func (c *Cluster) FindHypervisorByName(name string) *Hypervisor { +func (c *OldCluster) FindHypervisorByName(name string) *Hypervisor { for _, h := range c.H.Hypervisors { if h.Hostname == name { return h @@ -70,7 +70,7 @@ func (c *Cluster) FindHypervisorByName(name string) *Hypervisor { return nil } -func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor { +func (c *OldCluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor { h := c.FindHypervisorByName(hostname) if h != nil { return h @@ -91,7 +91,7 @@ func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor return h } -func (c *Cluster) AddEvent(e *Event) { +func (c *OldCluster) AddEvent(e *Event) { c.e.Events = append(c.e.Events, e) } @@ -109,7 +109,7 @@ func NewDefaultDroplet(hostname string) *Droplet { return d } -func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet { +func (c *OldCluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet { d := c.FindDropletByName(hostname) if d != nil { return d @@ -131,7 +131,7 @@ func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem i } // This isn't for the marketing department -func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet { +func (c *OldCluster) AddDropletLocal(name string, hypername string) *Droplet { d := &Droplet{ Hostname: name, } @@ -149,7 +149,7 @@ func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet { return d } -func (c *Cluster) BlankFields() { +func (c *OldCluster) BlankFields() { loop := c.d.All() // get the list of droplets for loop.Scan() { d := loop.Next() @@ -161,7 +161,7 @@ func (epb *Events) AppendEvent(e *Event) { epb.Events = append(epb.Events, e) } -func (c *Cluster) ClusterStable() (bool, string) { +func (c *OldCluster) ClusterStable() (bool, string) { last := time.Since(c.Unstable.AsTime()) if last > c.UnstableTimeout.AsDuration() { // the cluster has not been stable for 133 seconds @@ -173,7 +173,7 @@ func (c *Cluster) ClusterStable() (bool, string) { } // check the cluster and droplet to make sure it's ready to start -func (c *Cluster) DropletReady(d *Droplet) (bool, string) { +func (c *OldCluster) DropletReady(d *Droplet) (bool, string) { if c == nil { return false, "cluster == nil" } diff --git a/change.go b/change.go index 726c4fd..133f173 100644 --- a/change.go +++ b/change.go @@ -163,7 +163,7 @@ func (d *Droplet) SetState(newState DropletState) { } // records an event that the droplet changed state (aka turned on, turned off, etc) -func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error { +func (c *OldCluster) ChangeDropletState(d *Droplet, newState DropletState) error { if c == nil { return errors.New("cluster is nil") } @@ -190,7 +190,7 @@ func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error { } // records an event that the droplet migrated to another hypervisor -func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error { +func (c *OldCluster) DropletMoved(d *Droplet, newh *Hypervisor) error { if c == nil { return errors.New("cluster is nil") } diff --git a/cluster.go b/cluster.go deleted file mode 100644 index 94f9ff2..0000000 --- a/cluster.go +++ /dev/null @@ -1,48 +0,0 @@ -package virtpb - -import ( - sync "sync" - - durationpb "google.golang.org/protobuf/types/known/durationpb" - "google.golang.org/protobuf/types/known/timestamppb" -) - -type Cluster struct { - sync.RWMutex - - Dirs []string - d *Droplets - H *Hypervisors - e *Events - Unstable *timestamppb.Timestamp - UnstableTimeout *durationpb.Duration -} - -func (c *Cluster) GetDropletsPB() *Droplets { - return c.d -} - -func (c *Cluster) GetHypervisorsPB() *Hypervisors { - return c.H -} - -func (c *Cluster) GetEventsPB() *Events { - return c.e -} - -// adds a new droplet. enforce unique hostnames -func (c *Cluster) AddDroplet(newd *Droplet) bool { - c.Lock() - defer c.Unlock() - - for _, d := range c.d.Droplets { - if newd.Hostname == d.Hostname { - // boo. that one is already here - return false - } - } - - // everything is ok, this hostname is new - c.d.Droplets = append(c.d.Droplets, newd) - return true -} diff --git a/cluster.proto b/cluster.proto new file mode 100644 index 0000000..f81b89b --- /dev/null +++ b/cluster.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package virtpb; + +import "google/protobuf/timestamp.proto"; + +message Cluster { + string uuid = 1; // `autogenpb:unique` + string URL = 2; // `autogenpb:unique` + google.protobuf.Timestamp ctime = 3; // when the cluster was created +} + +message Clusters { // `autogenpb:marshal` + string uuid = 1; // `autogenpb:uuid:57ddd763-75f6-4003-bf0e-8dd0f8a44044` + string version = 2; // `autogenpb:version:v0.0.1` + repeated Cluster clusters = 3; +} diff --git a/config.go b/config.go index c1acb92..81bfec4 100644 --- a/config.go +++ b/config.go @@ -16,7 +16,7 @@ import ( // writes out the cluster information it seperate files // to make it humanly possible to hand edit things as needed -func (c *Cluster) ConfigSave() error { +func (c *OldCluster) ConfigSave() error { // try to backup the current cluster config files if err := backupConfig(); err != nil { return err @@ -64,7 +64,7 @@ func (c *Cluster) ConfigSave() error { return nil } -func (c *Cluster) ConfigLoad() error { +func (c *OldCluster) ConfigLoad() error { if c == nil { return errors.New("It's not safe to run ConfigLoad() on a nil cluster") } @@ -157,6 +157,23 @@ func ConfigWriteJSON(a any, filename string) error { return nil } +func (c *OldCluster) configWriteDroplets() error { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.new.text") + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + defer cfgfile.Close() + if err != nil { + fmt.Println("open config file :", err) + return err + } + loop := c.d.All() // get the list of droplets + for loop.Scan() { + d := loop.Next() + text := prototext.Format(d) + fmt.Fprintln(cfgfile, text) + } + return nil +} + func ConfigWriteTEXT(a any, filename string) error { fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename) cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) @@ -174,19 +191,18 @@ func ConfigWriteTEXT(a any, filename string) error { return nil } -func (c *Cluster) configWriteDroplets() error { - fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.new.text") - cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer cfgfile.Close() - if err != nil { - fmt.Println("open config file :", err) - return err +func (c *Clusters) ConfigLoad() error { + if c == nil { + return errors.New("It's not safe to run ConfigLoad() on a nil cluster") } - loop := c.d.All() // get the list of droplets - for loop.Scan() { - d := loop.Next() - text := prototext.Format(d) - fmt.Fprintln(cfgfile, text) + + if data, err := loadFile("cluster.text"); err == nil { + if err = prototext.Unmarshal(data, c); err != nil { + fmt.Println("broken cluster.textconfig file") + return err + } + } else { + return err } return nil } diff --git a/helpers.go b/helpers.go index 5e716dc..ca1c92b 100644 --- a/helpers.go +++ b/helpers.go @@ -3,15 +3,15 @@ package virtpb // functions to import and export the protobuf // data to and from config files -func InitCluster() *Cluster { - var c *Cluster - c = new(Cluster) +func InitCluster() *OldCluster { + var c *OldCluster + c = new(OldCluster) c.d = new(Droplets) c.H = new(Hypervisors) c.e = new(Events) return c } -func (c *Cluster) DropletsAll() *DropletIterator { +func (c *OldCluster) DropletsAll() *DropletIterator { return c.d.All() } diff --git a/human.go b/human.go index bb64530..a9b2521 100644 --- a/human.go +++ b/human.go @@ -185,7 +185,7 @@ func (d *Droplet) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, e return t, nil } -func (c *Cluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) { +func (c *OldCluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) { hostname := r.URL.Query().Get("hostname") d := c.FindDropletByName(hostname) if d == nil { diff --git a/oldCluster.go b/oldCluster.go new file mode 100644 index 0000000..c6d6fd9 --- /dev/null +++ b/oldCluster.go @@ -0,0 +1,48 @@ +package virtpb + +import ( + sync "sync" + + durationpb "google.golang.org/protobuf/types/known/durationpb" + "google.golang.org/protobuf/types/known/timestamppb" +) + +type OldCluster struct { + sync.RWMutex + + Dirs []string + d *Droplets + H *Hypervisors + e *Events + Unstable *timestamppb.Timestamp + UnstableTimeout *durationpb.Duration +} + +func (c *OldCluster) GetDropletsPB() *Droplets { + return c.d +} + +func (c *OldCluster) GetHypervisorsPB() *Hypervisors { + return c.H +} + +func (c *OldCluster) GetEventsPB() *Events { + return c.e +} + +// adds a new droplet. enforce unique hostnames +func (c *OldCluster) AddDroplet(newd *Droplet) bool { + c.Lock() + defer c.Unlock() + + for _, d := range c.d.Droplets { + if newd.Hostname == d.Hostname { + // boo. that one is already here + return false + } + } + + // everything is ok, this hostname is new + c.d.Droplets = append(c.d.Droplets, newd) + return true +} diff --git a/sampleData.go b/sampleData.go index 7cdf313..4a5af36 100644 --- a/sampleData.go +++ b/sampleData.go @@ -51,7 +51,7 @@ func CreateSampleEvents(total int) *Events { return e } -func CreateSampleCluster(total int) *Cluster { +func CreateSampleCluster(total int) *OldCluster { c := InitCluster() for i := 0; i < total; i++ { -- cgit v1.2.3