create sends a droplet protobuf
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
38bddac541
commit
97e8fb3fed
|
@ -0,0 +1,92 @@
|
|||
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
pb "go.wit.com/lib/protobuf/virtbuf"
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
url := urlbase + "/create"
|
||||
|
||||
body, err := postDropletJSON(url, newDroplet)
|
||||
if err != nil {
|
||||
log.Info("postDropletJSON() failed:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: try this and use this instead
|
||||
body, err = postDropletWIRE(url, newDroplet)
|
||||
if err != nil {
|
||||
log.Info("postDropletJSON() 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
|
||||
}
|
||||
|
||||
// send protobuf as wire data
|
||||
func postDropletWIRE(url string, d *pb.Droplet) (string, error) {
|
||||
var bytes []byte
|
||||
var err error
|
||||
// Automatically marshal to protobuf binary format
|
||||
bytes, err = proto.Marshal(d)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal: %v", err)
|
||||
return "", err
|
||||
}
|
||||
// fmt.Printf("Encoded data: %x\n", bytes)
|
||||
fmt.Printf("Encoded len(data): %d\n", len(bytes))
|
||||
return post(url, bytes)
|
||||
}
|
||||
|
||||
// send protobuf in json format
|
||||
func postDropletJSON(url string, d *pb.Droplet) (string, error) {
|
||||
// send protobuf as JSON
|
||||
bytes, err := d.MarshalJSON()
|
||||
if err != nil {
|
||||
log.Info("virtbuf.MarshalJson() failed:", err)
|
||||
return "", err
|
||||
}
|
||||
return post(url, bytes)
|
||||
}
|
||||
|
||||
func post(url string, bytes []byte) (string, error) {
|
||||
body, err := httpPost(url, bytes)
|
||||
if err != nil {
|
||||
log.Info("httpPost() failed:", err)
|
||||
return "", err
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
60
main.go
60
main.go
|
@ -7,11 +7,9 @@ import (
|
|||
"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
|
||||
|
@ -31,7 +29,20 @@ func main() {
|
|||
}
|
||||
if argv.Dump != nil {
|
||||
if argv.Dump.Droplets {
|
||||
dumpDroplets()
|
||||
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")
|
||||
|
@ -53,10 +64,15 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func dumpDroplets() error {
|
||||
func dumpDroplets(full bool) error {
|
||||
log.DaemonMode(true)
|
||||
log.Info("dump droplets here ==", argv.Dump.Droplets)
|
||||
url := urlbase + "/dumpdroplets"
|
||||
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)
|
||||
|
@ -71,41 +87,17 @@ func dumpDroplets() error {
|
|||
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)
|
||||
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))
|
||||
// log.Info("virtigo returned body:", test)
|
||||
for _, line := range strings.Split(test, "\n") {
|
||||
log.Info("GOT:", line)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue