virtigoctl/main.go

114 lines
2.4 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"
"errors"
2024-10-30 02:28:53 -05:00
"go.wit.com/dev/alexflint/arg"
"go.wit.com/log"
pb "go.wit.com/lib/protobuf/virtbuf"
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()
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() 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
newDroplet.Disks = append(newDroplet.Disks, newDisk)
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
}