2024-10-11 23:01:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-31 06:50:17 -05:00
|
|
|
"errors"
|
2024-10-11 23:01:55 -05:00
|
|
|
"fmt"
|
2024-10-26 19:34:32 -05:00
|
|
|
"io/ioutil"
|
2024-10-11 23:01:55 -05:00
|
|
|
"net/http"
|
2024-10-12 22:16:52 -05:00
|
|
|
"os"
|
2024-10-11 23:01:55 -05:00
|
|
|
"strings"
|
|
|
|
|
2024-10-15 11:02:54 -05:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-10-31 06:50:17 -05:00
|
|
|
"go.wit.com/lib/virtigolib"
|
2024-10-26 19:34:32 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-11 23:01:55 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-31 06:50:17 -05:00
|
|
|
"libvirt.org/go/libvirtxml"
|
2024-10-11 23:01:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// remove '?' part and trailing '/'
|
|
|
|
func cleanURL(url string) string {
|
|
|
|
url = "/" + strings.Trim(url, "/")
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-15 11:02:54 -05:00
|
|
|
log.Info("Got URL Path: ", r.URL.Path)
|
2024-11-01 02:53:12 -05:00
|
|
|
route := cleanURL(r.URL.Path)
|
2024-10-11 23:01:55 -05:00
|
|
|
|
2024-10-26 19:34:32 -05:00
|
|
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "ReadAll() error =", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Got URL msg:", string(msg))
|
2024-11-01 02:53:12 -05:00
|
|
|
if route == "/" {
|
2024-10-11 23:01:55 -05:00
|
|
|
fmt.Fprintln(w, "OK")
|
|
|
|
return
|
|
|
|
}
|
2024-10-26 10:34:04 -05:00
|
|
|
|
2024-11-01 02:53:12 -05:00
|
|
|
// curl http://localhost:2520/dumpdomain?domain=foo.bar.com
|
|
|
|
if route == "/dumpdomain" {
|
|
|
|
domname := r.URL.Query().Get("domain")
|
|
|
|
fmt.Fprint(w, "import domain:", domname)
|
|
|
|
|
|
|
|
xmldoc, err := virshDumpXML(w, r, domname)
|
|
|
|
|
|
|
|
domcfg := &libvirtxml.Domain{}
|
2024-11-01 04:10:14 -05:00
|
|
|
fmt.Fprintln(w, "domcfg.Unmarshal()")
|
|
|
|
fmt.Fprintln(w, "domcfg.Unmarshal()")
|
|
|
|
fmt.Fprintln(w, "domcfg.Unmarshal()")
|
|
|
|
log.Warn("domcfg.Unmarshal()")
|
2024-11-01 02:53:12 -05:00
|
|
|
err = domcfg.Unmarshal(xmldoc)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "domain.Unmarshal XML failed")
|
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
return
|
|
|
|
}
|
2024-11-01 04:10:14 -05:00
|
|
|
fmt.Fprintln(w, "About to virtigolib.ImportXML(domcfg)")
|
|
|
|
fmt.Fprintln(w, "About to virtigolib.ImportXML(domcfg)")
|
|
|
|
fmt.Fprintln(w, "About to virtigolib.ImportXML(domcfg)")
|
|
|
|
log.Warn("About to virtigolib.ImportXML(domcfg)")
|
|
|
|
log.Warn("domcfg.Uuid", domcfg.UUID)
|
|
|
|
log.Warn("domcfg.Name", domcfg.Name)
|
2024-11-01 02:53:12 -05:00
|
|
|
d, _, err := virtigolib.ImportXML(domcfg)
|
2024-11-01 04:10:14 -05:00
|
|
|
fmt.Fprintln(w, "Finished virtigolib.ImportXML(domcfg)")
|
|
|
|
fmt.Fprintln(w, "Finished virtigolib.ImportXML(domcfg)")
|
|
|
|
fmt.Fprintln(w, "Finished virtigolib.ImportXML(domcfg)")
|
|
|
|
log.Warn("Finished virtigolib.ImportXML(domcfg)")
|
2024-11-01 02:53:12 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "ImportXML failed for", domname, err)
|
|
|
|
return
|
|
|
|
}
|
2024-11-01 04:10:14 -05:00
|
|
|
if d == nil {
|
|
|
|
fmt.Fprintln(w, "ImportXML failed d == nil", err)
|
|
|
|
return
|
|
|
|
}
|
2024-11-01 02:53:12 -05:00
|
|
|
fmt.Fprintln(w, "ImportXML worked for", domname)
|
|
|
|
fmt.Fprintln(w, "should send the protobuf to virtigo here", domname)
|
|
|
|
d.Current.FullXml = xmldoc
|
2024-11-01 04:10:14 -05:00
|
|
|
log.Warn("About to DumpDroplet()")
|
2024-11-01 02:53:12 -05:00
|
|
|
d.DumpDroplet(w, r)
|
2024-11-01 04:10:14 -05:00
|
|
|
fmt.Fprintln(w, "DUMP DOMAIN DONE OK FOR", domname)
|
2024-11-01 02:53:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/vms" {
|
|
|
|
s := pollHypervisor(me.hv)
|
|
|
|
fmt.Fprint(w, s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-26 10:34:04 -05:00
|
|
|
// exit the virtigo daemon & have systemd restart it
|
|
|
|
// this can happen & when it does, access to
|
|
|
|
// to libvirtd will hang (aka: virsh list will hang)
|
|
|
|
// One way to trigger this is to not properly close
|
|
|
|
// domain sockets opened from go-qemu/hypervisor
|
|
|
|
// it's a good idea in any case so leave it here
|
2024-11-01 02:53:12 -05:00
|
|
|
if route == "/kill" {
|
2024-10-12 22:16:52 -05:00
|
|
|
log.Warn("KILLED")
|
|
|
|
fmt.Fprintln(w, "KILLED")
|
|
|
|
os.Exit(-1)
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
|
2024-11-01 02:53:12 -05:00
|
|
|
// curl http://localhost:2520/import?domain=foo.bar.com
|
|
|
|
if route == "/import" {
|
|
|
|
domname := r.URL.Query().Get("domain")
|
2024-10-31 06:50:17 -05:00
|
|
|
fmt.Fprint(w, "import domain:", domname)
|
|
|
|
|
|
|
|
xmldoc, err := virshDumpXML(w, r, domname)
|
|
|
|
|
|
|
|
domcfg := &libvirtxml.Domain{}
|
|
|
|
err = domcfg.Unmarshal(xmldoc)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "domain.Unmarshal XML failed")
|
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// importDomain(w, r, domcfg)
|
|
|
|
// importDomain(w http.ResponseWriter, r *http.Request, dom *libvirtxml.Domain) {
|
|
|
|
d, _, err := virtigolib.ImportXML(domcfg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "ImportXML failed for", domname, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "ImportXML worked for", domname)
|
|
|
|
fmt.Fprintln(w, "should send the protobuf to virtigo here", domname)
|
|
|
|
d.Current.FullXml = xmldoc
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-01 02:53:12 -05:00
|
|
|
if route == "/start" {
|
2024-10-27 05:43:01 -05:00
|
|
|
log.Info("/start jcarr actually doing START", me.Hostname)
|
|
|
|
fmt.Fprintln(w, "/start jcarr actually doing START", me.Hostname)
|
2024-10-14 15:26:11 -05:00
|
|
|
start := r.URL.Query().Get("start")
|
2024-10-26 12:32:37 -05:00
|
|
|
xml := "/tmp/" + start + ".xml"
|
|
|
|
if shell.Unlink(xml) {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
2024-10-26 12:32:37 -05:00
|
|
|
fmt.Fprintln(w, "error =", "could not remove existing xml file", xml)
|
|
|
|
fmt.Fprintln(w, "error =", "file still exists after unlink")
|
|
|
|
return
|
|
|
|
}
|
2024-10-26 19:34:32 -05:00
|
|
|
// fmt.Fprintln(w, "HTTP:", r.Body)
|
|
|
|
var d *pb.Droplet
|
2024-10-26 20:09:33 -05:00
|
|
|
d = new(pb.Droplet)
|
2024-10-26 19:34:32 -05:00
|
|
|
// msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
|
|
fmt.Fprintln(w, "/start ReadAll() START")
|
|
|
|
fmt.Fprintln(w, "msg =", string(msg))
|
|
|
|
fmt.Fprintln(w, "/start ReadAll() END")
|
|
|
|
if err != nil {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
2024-10-26 19:34:32 -05:00
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "START len(msg) =", len(msg))
|
2024-10-26 20:09:33 -05:00
|
|
|
err = d.UnmarshalJSON(msg)
|
|
|
|
if d.Hostname == "" {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START hostname is blank", me.Hostname)
|
2024-10-26 20:09:33 -05:00
|
|
|
log.Info("START hostname is blank")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-26 19:34:32 -05:00
|
|
|
if err != nil {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
2024-10-26 19:34:32 -05:00
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := newStart(start, d); err != nil {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
2024-10-26 12:32:37 -05:00
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-15 11:02:54 -05:00
|
|
|
cmd := []string{"virsh", "create", xml}
|
2024-10-16 20:38:51 -05:00
|
|
|
|
2024-10-15 11:02:54 -05:00
|
|
|
log.Warn("cmd :", cmd)
|
2024-11-01 02:53:12 -05:00
|
|
|
fmt.Fprintln(w, "Handling start droplet", cmd)
|
2024-10-16 20:38:51 -05:00
|
|
|
|
|
|
|
err, ok, output := shell.RunCmd("/home/", cmd)
|
2024-10-15 11:02:54 -05:00
|
|
|
shell.Run(cmd)
|
2024-10-16 20:38:51 -05:00
|
|
|
if ok {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START OK", me.Hostname)
|
2024-10-16 20:38:51 -05:00
|
|
|
fmt.Fprintln(w, output)
|
|
|
|
} else {
|
2024-10-27 05:43:01 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
2024-10-16 20:38:51 -05:00
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
fmt.Fprintln(w, "output =", output)
|
|
|
|
}
|
2024-10-14 15:26:11 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-01 02:53:12 -05:00
|
|
|
if route == "/favicon.ico" {
|
2024-10-11 23:01:55 -05:00
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
|
2024-11-01 02:53:12 -05:00
|
|
|
log.Warn("BAD URL =", route)
|
|
|
|
fmt.Fprintln(w, "BAD URL =", route)
|
2024-10-11 23:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
|
|
// fmt.Fprintln(w, "GOT TEST?")
|
|
|
|
fullname := "resources/" + filename
|
|
|
|
pfile, err := resources.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
// w.Write(pfile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var repohtml string
|
|
|
|
repohtml = string(pfile)
|
|
|
|
if filename == "goReference.svg" {
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, repohtml)
|
|
|
|
log.Println("writeFile() found internal file:", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts and sits waiting for HTTP requests
|
|
|
|
func startHTTP() {
|
|
|
|
http.HandleFunc("/", okHandler)
|
|
|
|
|
|
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
|
|
log.Println("Running on port", p)
|
|
|
|
|
|
|
|
err := http.ListenAndServe(p, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error starting server:", err)
|
|
|
|
}
|
|
|
|
}
|
2024-10-31 06:50:17 -05:00
|
|
|
|
|
|
|
func virshDumpXML(w http.ResponseWriter, r *http.Request, name string) (string, error) {
|
|
|
|
cmd := []string{"virsh", "dumpxml", "--security-info", name}
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "virsh dumpxml", name)
|
|
|
|
log.Warn("cmd :", cmd)
|
|
|
|
fmt.Fprintln(w, "cmd: ", cmd)
|
|
|
|
err, ok, output := shell.RunCmd("/home/", cmd)
|
|
|
|
shell.Run(cmd)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(w, "START FAILED", me.Hostname)
|
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
result := fmt.Sprintln("virsh dumpxml failed:", err)
|
|
|
|
return output, errors.New(result)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "START OK", me.Hostname)
|
|
|
|
fmt.Fprintln(w, output)
|
|
|
|
return output, nil
|
|
|
|
}
|