From 020de631c8da1f0dd674c0593acf5d814b713bf3 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 30 Oct 2024 09:14:20 -0500 Subject: [PATCH] try to send a create Signed-off-by: Jeff Carr --- Makefile | 12 ++++++++- main.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- post.go | 11 ++++----- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 04519de..93b43b1 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= all: build ./virtigoctl --version ./virtigoctl --help + make dump-droplets build: GO111MODULE=off go build -v -ldflags "-X main.Version=${VERSION} -X gui.GUIVERSION=${VERSION}" @@ -38,11 +39,20 @@ git-clone: go-clone --recursive --go-src --no-work go.wit.com/apps/gowebd go-clone --recursive --go-src --no-work go.wit.com/lib/daemons/virtigod +dump-uptime: + ./virtigoctl dump --uptime=true + dump-droplets: ./virtigoctl dump --droplets=true dump-droplets-full: - ./virtigoctl dump --droplets=alsdfkj + ./virtigoctl dump --droplets-full=true + +dump-hypervisors: + ./virtigoctl dump --hypervisors=true start: ./virtigoctl --start www.wit.com go.wit.com + +filename-nagios: + ./virtigoctl create --filename=/home/nfs1/node004/kvm/nagios.lab.wit.org.qcow2 diff --git a/main.go b/main.go index 005478a..45a6bb2 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,13 @@ package main import ( "embed" "os" + "path/filepath" + "strings" + "errors" "go.wit.com/dev/alexflint/arg" "go.wit.com/log" + pb "go.wit.com/lib/protobuf/virtbuf" ) var Version string @@ -15,6 +19,8 @@ var Version string //go:embed resources/* var resources embed.FS +var urlbase string = "http://localhost:8080" + func main() { var pp *arg.Parser pp = arg.MustParse(&argv) @@ -25,14 +31,20 @@ func main() { } if argv.Dump != nil { if argv.Dump.Droplets { - log.Info("dump droplets here ==", argv.Dump.Droplets) + dumpDroplets() os.Exit(0) } log.Info("dump something here") os.Exit(0) } if argv.Create != nil { - log.Info("create something here") + dir := filepath.Dir(argv.Create.Filename) + filename := filepath.Base(argv.Create.Filename) + if err := createFilename(dir, filename); err != nil { + log.Info("create failed", err) + os.Exit(-1) + } + log.Info("create worked") os.Exit(0) } if argv.Start != nil { @@ -40,3 +52,61 @@ func main() { os.Exit(0) } } + +func dumpDroplets() error { + log.DaemonMode(true) + log.Info("dump droplets here ==", argv.Dump.Droplets) + url := urlbase + "/dumpdroplets" + body, err := httpPost(url, nil) + if err != nil { + log.Info("httpPost() failed:", err) + return err + } + + test := strings.TrimSpace(string(body)) + // log.Info("virtigo returned body:", test) + for _, line := range strings.Split(test, "\n") { + log.Info("GOT:", line) + } + return nil +} + +func createFilename(dir string, filename string) error { + log.Info("dir ==", dir) + log.Info("filename ==", filename) + + filetype := filepath.Ext(filename) + if filetype != ".qcow2" { + log.Info("file type", filetype, "not supported") + return errors.New("only supporting qcow2 images for now") + } + hostname := strings.TrimSuffix(filename, filetype) + log.Info("hostname ==", hostname) + + var newDroplet *pb.Droplet + newDroplet = new(pb.Droplet) + newDroplet.Hostname = hostname + newDisk := new(pb.Disk) + newDisk.Filename = filename + newDisk.Filepath = dir + + bytes, err := newDroplet.MarshalJSON() + if err != nil { + log.Info("virtbuf.MarshalJson() failed:", err) + return err + } + + url := urlbase + "/create" + body, err := httpPost(url, bytes) + if err != nil { + log.Info("httpPost() failed:", err) + return err + } + + test := strings.TrimSpace(string(body)) + // log.Info("virtigo returned body:", test) + for _, line := range strings.Split(test, "\n") { + log.Info("GOT:", line) + } + return nil +} diff --git a/post.go b/post.go index 9537bc8..ccdfb11 100644 --- a/post.go +++ b/post.go @@ -6,7 +6,6 @@ import ( "net/http" "os" "os/user" - "strings" "go.wit.com/log" ) @@ -39,11 +38,11 @@ func httpPost(url string, data []byte) ([]byte, error) { return body, err } - test := strings.TrimSpace(string(body)) - log.Info("go.wit.com returned body:", test) - if test == "OK" { - return body, nil - } + // test := strings.TrimSpace(string(body)) + // log.Info("go.wit.com returned body:", test) + // if test == "OK" { + // return body, nil + // } return body, nil }