2024-10-30 02:28:53 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"os"
|
2024-10-30 09:14:20 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"errors"
|
2024-10-30 02:28:53 -05:00
|
|
|
|
|
|
|
"go.wit.com/dev/alexflint/arg"
|
2024-10-30 07:52:44 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-30 09:14:20 -05:00
|
|
|
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
|
|
|
|
|
2024-10-30 09:14:20 -05:00
|
|
|
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)
|
|
|
|
}
|
2024-10-30 07:52:44 -05:00
|
|
|
if argv.Dump != nil {
|
|
|
|
if argv.Dump.Droplets {
|
2024-10-30 09:14:20 -05:00
|
|
|
dumpDroplets()
|
2024-10-30 07:52:44 -05:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
log.Info("dump something here")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if argv.Create != nil {
|
2024-10-30 09:14:20 -05:00
|
|
|
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")
|
2024-10-30 07:52:44 -05:00
|
|
|
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
|
|
|
}
|
2024-10-30 09:14:20 -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
|
2024-10-30 09:30:05 -05:00
|
|
|
newDroplet.Disks = append(newDroplet.Disks, newDisk)
|
2024-10-30 09:14:20 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|