virtigoctl/main.go

106 lines
2.0 KiB
Go
Raw Normal View History

2024-10-30 02:28:53 -05:00
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
package main
import (
"embed"
"os"
"path/filepath"
"strings"
2024-10-30 02:28:53 -05:00
"go.wit.com/dev/alexflint/arg"
"go.wit.com/log"
2024-10-30 02:28:53 -05:00
)
var Version string
//go:embed resources/*
var resources embed.FS
var urlbase string = "http://localhost:8080"
2024-10-30 02:28:53 -05:00
func main() {
var pp *arg.Parser
pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
if argv.Dump != nil {
if argv.Dump.Droplets {
dumpDroplets(false)
os.Exit(0)
}
if argv.Dump.DropletsFull {
dumpStdout("/dumpdropletsfull")
dumpDroplets(true)
os.Exit(0)
}
if argv.Dump.Uptime {
dumpStdout("/uptime")
os.Exit(0)
}
if argv.Dump.Hypervisors {
dumpStdout("/dumphypervisors")
os.Exit(0)
}
log.Info("dump something here")
os.Exit(0)
}
if argv.Create != nil {
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 {
log.Info("start the vm's here:", argv.Start)
os.Exit(0)
}
2024-10-30 02:28:53 -05:00
}
func dumpDroplets(full bool) error {
log.DaemonMode(true)
log.Info("dump droplets here ==", argv.Dump.Droplets)
var url string
if full {
url = urlbase + "/dumpdropletsfull"
} else {
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 dumpStdout(route string) error {
log.DaemonMode(true)
url := urlbase + route
log.Info("dump url here ==", url)
body, err := httpPost(url, nil)
if err != nil {
log.Info("httpPost() failed:", err)
return err
}
test := strings.TrimSpace(string(body))
for _, line := range strings.Split(test, "\n") {
log.Info("GOT:", line)
}
return nil
}