try to send a create

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-30 09:14:20 -05:00
parent 7685f0b5eb
commit 020de631c8
3 changed files with 88 additions and 9 deletions

View File

@ -7,6 +7,7 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE=
all: build all: build
./virtigoctl --version ./virtigoctl --version
./virtigoctl --help ./virtigoctl --help
make dump-droplets
build: build:
GO111MODULE=off go build -v -ldflags "-X main.Version=${VERSION} -X gui.GUIVERSION=${VERSION}" 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/apps/gowebd
go-clone --recursive --go-src --no-work go.wit.com/lib/daemons/virtigod go-clone --recursive --go-src --no-work go.wit.com/lib/daemons/virtigod
dump-uptime:
./virtigoctl dump --uptime=true
dump-droplets: dump-droplets:
./virtigoctl dump --droplets=true ./virtigoctl dump --droplets=true
dump-droplets-full: dump-droplets-full:
./virtigoctl dump --droplets=alsdfkj ./virtigoctl dump --droplets-full=true
dump-hypervisors:
./virtigoctl dump --hypervisors=true
start: start:
./virtigoctl --start www.wit.com go.wit.com ./virtigoctl --start www.wit.com go.wit.com
filename-nagios:
./virtigoctl create --filename=/home/nfs1/node004/kvm/nagios.lab.wit.org.qcow2

74
main.go
View File

@ -5,9 +5,13 @@ package main
import ( import (
"embed" "embed"
"os" "os"
"path/filepath"
"strings"
"errors"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/log" "go.wit.com/log"
pb "go.wit.com/lib/protobuf/virtbuf"
) )
var Version string var Version string
@ -15,6 +19,8 @@ var Version string
//go:embed resources/* //go:embed resources/*
var resources embed.FS var resources embed.FS
var urlbase string = "http://localhost:8080"
func main() { func main() {
var pp *arg.Parser var pp *arg.Parser
pp = arg.MustParse(&argv) pp = arg.MustParse(&argv)
@ -25,14 +31,20 @@ func main() {
} }
if argv.Dump != nil { if argv.Dump != nil {
if argv.Dump.Droplets { if argv.Dump.Droplets {
log.Info("dump droplets here ==", argv.Dump.Droplets) dumpDroplets()
os.Exit(0) os.Exit(0)
} }
log.Info("dump something here") log.Info("dump something here")
os.Exit(0) os.Exit(0)
} }
if argv.Create != nil { 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) os.Exit(0)
} }
if argv.Start != nil { if argv.Start != nil {
@ -40,3 +52,61 @@ func main() {
os.Exit(0) 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
}

11
post.go
View File

@ -6,7 +6,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/user" "os/user"
"strings"
"go.wit.com/log" "go.wit.com/log"
) )
@ -39,11 +38,11 @@ func httpPost(url string, data []byte) ([]byte, error) {
return body, err return body, err
} }
test := strings.TrimSpace(string(body)) // test := strings.TrimSpace(string(body))
log.Info("go.wit.com returned body:", test) // log.Info("go.wit.com returned body:", test)
if test == "OK" { // if test == "OK" {
return body, nil // return body, nil
} // }
return body, nil return body, nil
} }