From 87b7bc17b313c04806981408943923d8e66f960a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 24 Oct 2024 16:57:50 -0500 Subject: seperate config files for droplets, hypervisors & events Signed-off-by: Jeff Carr --- .gitignore | 2 +- Makefile | 6 +-- config.go | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++ configfile/Makefile | 15 ------ configfile/main.go | 98 --------------------------------------- droplet.proto | 4 +- example/Makefile | 15 ++++++ example/configfile | Bin 0 -> 5544235 bytes example/main.go | 98 +++++++++++++++++++++++++++++++++++++++ helpers.go | 6 ++- hypervisor.proto | 6 +++ 11 files changed, 261 insertions(+), 120 deletions(-) create mode 100644 config.go delete mode 100644 configfile/Makefile delete mode 100644 configfile/main.go create mode 100644 example/Makefile create mode 100755 example/configfile create mode 100644 example/main.go diff --git a/.gitignore b/.gitignore index c741eed..6cce53e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ go.* *.pb.go -configfile/configfile +example/example diff --git a/Makefile b/Makefile index fc490a4..9f4d4ce 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ all: droplet.pb.go hypervisor.pb.go cluster.pb.go events.pb.go - make -C configfile + make -C example vet: lint GO111MODULE=off go vet @@ -17,7 +17,7 @@ lint: # autofixes your import headers in your golang files goimports: goimports -w *.go - make -C configfile goimports + make -C example goimports redomod: rm -f go.* @@ -27,7 +27,7 @@ redomod: clean: rm -f *.pb.go -rm -f go.* - make -C configfile clean + make -C example clean droplet.pb.go: droplet.proto # protoc --go_out=. droplet.proto diff --git a/config.go b/config.go new file mode 100644 index 0000000..192d60d --- /dev/null +++ b/config.go @@ -0,0 +1,131 @@ +package virtbuf + +// functions to import and export the protobuf +// data to and from config files + +import ( + "fmt" + "os" + "path/filepath" + + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" +) + +func WriteConfig(d *Droplets, h *Hypervisors, e *Events) { + d.WriteConfigJSON() + d.WriteConfigTEXT() + + e.WriteConfigJSON() + e.WriteConfigTEXT() +} + +// export as json +func (e *Events) WriteConfigJSON() { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json") + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666) + defer cfgfile.Close() + if err != nil { + fmt.Println("open config file :", err) + return + } + text := e.FormatJSON() + fmt.Fprintln(cfgfile, text) + fmt.Println("Write:", fullname, "OK") +} + +// export as prototext +func (e *Events) WriteConfigTEXT() { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text") + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666) + defer cfgfile.Close() + if err != nil { + fmt.Println("open config file :", err) + return + } + text := e.FormatTEXT() + fmt.Fprintln(cfgfile, text) + fmt.Println("Write:", fullname, "OK") +} + +// export as json +func (d *Droplets) WriteConfigJSON() { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json") + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666) + defer cfgfile.Close() + if err != nil { + fmt.Println("open config file :", err) + return + } + text := d.FormatJSON() + fmt.Fprintln(cfgfile, text) + fmt.Println("Write:", fullname, "OK") +} + +// export as prototext +func (d *Droplets) WriteConfigTEXT() { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.text") + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666) + defer cfgfile.Close() + if err != nil { + fmt.Println("open config file :", err) + return + } + text := d.FormatTEXT() + fmt.Fprintln(cfgfile, text) + fmt.Println("Write:", fullname, "OK") +} + +// human readable JSON +func (c *Cluster) FormatJSON() string { + return protojson.Format(c) +} + +func (d *Droplets) FormatJSON() string { + return protojson.Format(d) +} + +func (e *Events) FormatJSON() string { + return protojson.Format(e) +} + +// 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 (e *Events) FormatTEXT() string { + return prototext.Format(e) +} + +// marshal +func (c *Cluster) MarshalJSON() ([]byte, error) { + return protojson.Marshal(c) +} + +func (d *Droplets) MarshalJSON() ([]byte, error) { + return protojson.Marshal(d) +} + +func (e *Events) MarshalJSON() ([]byte, error) { + return protojson.Marshal(e) +} + +// unmarshal +func (c *Cluster) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, c) +} + +func (d *Droplets) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, d) +} + +func (e *Events) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, e) +} diff --git a/configfile/Makefile b/configfile/Makefile deleted file mode 100644 index a52de6a..0000000 --- a/configfile/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -build: - GO111MODULE=off go build - ./configfile - -goimports: - goimports -w *.go - -prep: - go get -v -t -u - -run: - go run *.go - -clean: - -rm -f configfile diff --git a/configfile/main.go b/configfile/main.go deleted file mode 100644 index d54803d..0000000 --- a/configfile/main.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "fmt" - "io/ioutil" - "log" - "os" - - "google.golang.org/protobuf/proto" - - pb "go.wit.com/lib/protobuf/virtbuf" -) - -// -// saves entries in a config file -// - -func main() { - TestWriteCluster() - - in, err := ioutil.ReadFile("/tmp/testing4.protobuf") - if err != nil { - log.Fatalln("Error reading file:", err) - } - - var aCluster pb.Cluster - if err := proto.Unmarshal(in, &aCluster); err != nil { - log.Fatalln("Failed to parse droplet:", err) - } - - log.Println(aCluster.String()) - // show the droplets to STDOUT - for _, d := range aCluster.Droplets { - fmt.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor) - } - - // show the hypervisors to STDOUT - for _, h := range aCluster.Hypervisors { - fmt.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable()) - } - - json := aCluster.FormatJSON() - fmt.Println(json) - - data, _ := aCluster.MarshalJSON() - fmt.Println(string(data)) - - text := aCluster.FormatTEXT() - fmt.Println(text) -} - -func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) { - buf, err := proto.Marshal(c) - if err != nil { - log.Fatal("marshaling error: ", err) - } - tmp, err := myWriter.Write(buf) - myWriter.Flush() - log.Println("bufio.Write() tmp, err = ", tmp, err) - - buf, err = proto.Marshal(c) - tmp2, err := myWriter.Write(buf) - myWriter.Flush() - log.Println("bufio.Write() tmp2, err = ", tmp2, err) -} - -func TestWriteCluster() { - buf := new(bytes.Buffer) - - c := pb.CreateSampleCluster(7) - - got := buf.String() - log.Println(got) - - newfile, _ := os.Create("/tmp/testing4.protobuf") - myWriter := bufio.NewWriter(newfile) - marshalWriteToFile(myWriter, c) - - // marshalUnmarshal() -} - -func marshalUnmarshal() { - test := pb.CreateSampleCluster(7) - data, err := proto.Marshal(test) - if err != nil { - log.Fatal("marshaling error: ", err) - } - - newTest := &pb.Cluster{} - err = proto.Unmarshal(data, newTest) - if err != nil { - log.Fatal("unmarshaling error: ", err) - } else { - log.Println("proto.Marshal() and proto.Unmarshal() worked") - } -} diff --git a/droplet.proto b/droplet.proto index 9dbba2c..60af988 100644 --- a/droplet.proto +++ b/droplet.proto @@ -4,7 +4,9 @@ package virtbuf; import "google/protobuf/any.proto"; message Droplets { - repeated Droplet droplets = 1; + 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 + repeated Droplet droplets = 3; } message Droplet { diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..ba930d7 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,15 @@ +build: + GO111MODULE=off go build + ./example + +goimports: + goimports -w *.go + +prep: + go get -v -t -u + +run: + go run *.go + +clean: + -rm -f example diff --git a/example/configfile b/example/configfile new file mode 100755 index 0000000..81377e3 Binary files /dev/null and b/example/configfile differ diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..d54803d --- /dev/null +++ b/example/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "io/ioutil" + "log" + "os" + + "google.golang.org/protobuf/proto" + + pb "go.wit.com/lib/protobuf/virtbuf" +) + +// +// saves entries in a config file +// + +func main() { + TestWriteCluster() + + in, err := ioutil.ReadFile("/tmp/testing4.protobuf") + if err != nil { + log.Fatalln("Error reading file:", err) + } + + var aCluster pb.Cluster + if err := proto.Unmarshal(in, &aCluster); err != nil { + log.Fatalln("Failed to parse droplet:", err) + } + + log.Println(aCluster.String()) + // show the droplets to STDOUT + for _, d := range aCluster.Droplets { + fmt.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor) + } + + // show the hypervisors to STDOUT + for _, h := range aCluster.Hypervisors { + fmt.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable()) + } + + json := aCluster.FormatJSON() + fmt.Println(json) + + data, _ := aCluster.MarshalJSON() + fmt.Println(string(data)) + + text := aCluster.FormatTEXT() + fmt.Println(text) +} + +func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) { + buf, err := proto.Marshal(c) + if err != nil { + log.Fatal("marshaling error: ", err) + } + tmp, err := myWriter.Write(buf) + myWriter.Flush() + log.Println("bufio.Write() tmp, err = ", tmp, err) + + buf, err = proto.Marshal(c) + tmp2, err := myWriter.Write(buf) + myWriter.Flush() + log.Println("bufio.Write() tmp2, err = ", tmp2, err) +} + +func TestWriteCluster() { + buf := new(bytes.Buffer) + + c := pb.CreateSampleCluster(7) + + got := buf.String() + log.Println(got) + + newfile, _ := os.Create("/tmp/testing4.protobuf") + myWriter := bufio.NewWriter(newfile) + marshalWriteToFile(myWriter, c) + + // marshalUnmarshal() +} + +func marshalUnmarshal() { + test := pb.CreateSampleCluster(7) + data, err := proto.Marshal(test) + if err != nil { + log.Fatal("marshaling error: ", err) + } + + newTest := &pb.Cluster{} + err = proto.Unmarshal(data, newTest) + if err != nil { + log.Fatal("unmarshaling error: ", err) + } else { + log.Println("proto.Marshal() and proto.Unmarshal() worked") + } +} diff --git a/helpers.go b/helpers.go index 8df7040..eb54261 100644 --- a/helpers.go +++ b/helpers.go @@ -4,8 +4,6 @@ import ( "fmt" "github.com/google/uuid" - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/encoding/prototext" ) // can the json protobuf output use a string and have a type handler @@ -27,6 +25,7 @@ func (x *Hypervisor) GetMemoryPrintable() string { return fmt.Sprintf("%d GB", i) } +/* func (c *Cluster) MarshalJSON() ([]byte, error) { return protojson.Marshal(c) } @@ -38,10 +37,12 @@ func (c *Cluster) FormatJSON() string { 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) } @@ -49,6 +50,7 @@ func (c *Cluster) FormatTEXT() string { func (d *Droplets) FormatTEXT() string { return prototext.Format(d) } +*/ func (c *Cluster) FindDroplet(name string) *Droplet { for _, d := range c.Droplets { diff --git a/hypervisor.proto b/hypervisor.proto index 3d2d87d..7e9d6be 100644 --- a/hypervisor.proto +++ b/hypervisor.proto @@ -1,6 +1,12 @@ syntax = "proto3"; package virtbuf; +message Hypervisors { + 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 + repeated Hypervisor hypervisors = 3; +} + message Hypervisor { string uuid = 1; string hostname = 2; -- cgit v1.2.3