From be5548cd4e24addacd8a7cfa8c61c2fad5ccbd65 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 18 Oct 2024 20:58:15 -0500 Subject: [PATCH] first go at it --- .gitignore | 5 +++ Makefile | 48 ++++++++++++++++++++++++++++ README.md | 6 ++++ configfile/Makefile | 8 +++++ configfile/main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ droplet.proto | 50 +++++++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 configfile/Makefile create mode 100644 configfile/main.go create mode 100644 droplet.proto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c741eed --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +go.* + +*.pb.go + +configfile/configfile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..292a050 --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +all: + # You must use the current protoc-gen-go + # protoc --version 3.6++ does not mean that protoc will generate version3 .go files + # + # apt remove golang-goprotobuf-dev + # apt install protobuf-compiler + # + # Then: + # go get -u github.com/golang/protobuf/protoc-gen-go + # cd ~/go/src/github.com/golang/protobuf/protoc-gen-go + # go install + # + # Then: + protoc --version + make droplet.pb.go + +redomod: + rm -f go.* + GO111MODULE= go mod init + GO111MODULE= go mod tidy + +clean: + rm -f *.pb.go + +droplet.pb.go: droplet.proto + protoc --go_out=. droplet.proto + +events.pb.go: events.proto + protoc --go_out=. events.proto + +account.pb.go: account.proto + protoc --go_out=. account.proto + +config.pb.go: config.proto + protoc --go_out=. config.proto + +compile: + protoc --go_out=. *.proto + +deps: + apt install golang-goprotobuf-dev + apt install protobuf-compiler + +push: + git pull + git add --all + git commit -a -s + git push diff --git a/README.md b/README.md new file mode 100644 index 0000000..06898f2 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +the libvirt xml files are just a bit much at this point + +They are what to use for starting droplets with virsh, +but for making a cluster or "home cloud" or something +like virtigo, I think it's going to be better to do +something simple. Anyway, this is that attmept. diff --git a/configfile/Makefile b/configfile/Makefile new file mode 100644 index 0000000..15d8b54 --- /dev/null +++ b/configfile/Makefile @@ -0,0 +1,8 @@ +build: + go build + +prep: + go get -v -t -u + +run: + go run *.go diff --git a/configfile/main.go b/configfile/main.go new file mode 100644 index 0000000..c012ba3 --- /dev/null +++ b/configfile/main.go @@ -0,0 +1,77 @@ +package main + +import "log" +import "bytes" +import "os" +import "bufio" +import "io/ioutil" + +import "github.com/golang/protobuf/proto" +import pb "git.wit.com/wit/virtbuf" + +// +// saves entries in a config file +// + +func main() { + TestWriteEvent() + + in, err := ioutil.ReadFile("/tmp/testing4.protobuf") + if err != nil { + log.Fatalln("Error reading file:", err) + } + allEvents := &pb.Event{} + if err := proto.Unmarshal(in, allEvents); err != nil { + log.Fatalln("Failed to parse events:", err) + } + + // listPeople(os.Stdout, allEvents) + // got := in.String() + log.Println(in) +} + +func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Event) { + buf, err := proto.Marshal(e) + 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(e) + tmp2, err := myWriter.Write(buf) + myWriter.Flush() + log.Println("bufio.Write() tmp2, err = ", tmp2, err) +} + +func TestWriteEvent() { + buf := new(bytes.Buffer) + + e := pb.CreateSampleEvent() + + got := buf.String() + log.Println(got) + + newfile, _ := os.Create("/tmp/testing4.protobuf") + myWriter := bufio.NewWriter(newfile) + marshalWriteToFile(myWriter, e) + + marshalUnmarshal() +} + +func marshalUnmarshal() { + test := pb.CreateSampleEvent() + data, err := proto.Marshal(test) + if err != nil { + log.Fatal("marshaling error: ", err) + } + + newTest := &pb.Event{} + 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 new file mode 100644 index 0000000..8c12664 --- /dev/null +++ b/droplet.proto @@ -0,0 +1,50 @@ +syntax = "proto3"; +package virtbuf; + +message Droplet { + string uuid = 1; + string name = 2; + + repeated Response results = 3; + repeated Network networks = 4; + repeated VM vms = 5; + + enum EventType { + ADD = 0; + DELETE = 1; + POWERON = 2; + POWEROFF = 3; + HIBERNATE = 4; + MIGRATE = 5; + DEMO = 6; + GET = 7; // request something + LOGIN = 8; // attempt to login + OK = 9; // everything is ok + FAIL = 10; // everything failed + } + + message Response { + EventType type = 1; + int32 id = 2; + string name = 3; + string error = 4; + repeated string snippets = 5; + } + + message Network { + string mac = 1; + string name = 2; + } + + message VM { + int64 id = 1; + string name = 2; + string hostname = 3; + int64 cpus = 4; + int64 memory = 5; + int64 disk = 6; + string IPv6 = 7; + string role = 8; + string baseImage = 9; + } +}