2024-10-11 23:01:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// remove '?' part and trailing '/'
|
|
|
|
func cleanURL(url string) string {
|
|
|
|
url = "/" + strings.Trim(url, "/")
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var tmp string
|
2024-10-15 11:02:54 -05:00
|
|
|
|
|
|
|
log.Info("Got URL Path: ", r.URL.Path)
|
|
|
|
log.Info("Got URL Query:", r.URL.Query().Get("start"))
|
|
|
|
|
2024-10-11 23:01:55 -05:00
|
|
|
tmp = cleanURL(r.URL.Path)
|
|
|
|
|
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))
|
|
|
|
log.Info("Got URL jcarr2 tmp:", tmp)
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/" {
|
|
|
|
fmt.Fprintln(w, "OK")
|
|
|
|
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-10-12 22:16:52 -05:00
|
|
|
if tmp == "/kill" {
|
|
|
|
log.Warn("KILLED")
|
|
|
|
fmt.Fprintln(w, "KILLED")
|
|
|
|
os.Exit(-1)
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
|
|
|
|
if tmp == "/vms" {
|
2024-10-26 21:05:16 -05:00
|
|
|
s := pollHypervisor(hv)
|
2024-10-11 23:18:45 -05:00
|
|
|
fmt.Fprint(w, s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-26 20:47:44 -05:00
|
|
|
if tmp == "/cluster" {
|
|
|
|
log.Info("/cluster jcarr actually doing START")
|
|
|
|
fmt.Fprintln(w, "/cluster jcarr actually doing START")
|
|
|
|
var c *pb.Cluster
|
|
|
|
c = new(pb.Cluster)
|
|
|
|
|
2024-10-26 20:56:35 -05:00
|
|
|
fmt.Fprintln(w, "cluster len(msg) =", len(msg))
|
2024-10-26 20:47:44 -05:00
|
|
|
err = c.UnmarshalJSON(msg)
|
|
|
|
if err != nil {
|
2024-10-26 20:56:35 -05:00
|
|
|
fmt.Fprintln(w, "cluster dirs failed")
|
2024-10-26 20:47:44 -05:00
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
}
|
|
|
|
for _, dir := range c.Dirs {
|
2024-10-26 21:48:42 -05:00
|
|
|
var found bool = false
|
|
|
|
for _, d := range me.dirs {
|
|
|
|
if d == dir {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
2024-10-27 02:55:36 -05:00
|
|
|
if found {
|
|
|
|
log.Info("dir already here", dir)
|
|
|
|
fmt.Fprintln(w, "dir already here", dir)
|
|
|
|
} else {
|
|
|
|
log.Info("append new dir", dir)
|
|
|
|
fmt.Fprintln(w, "append new dir", dir)
|
2024-10-26 21:48:42 -05:00
|
|
|
me.dirs = append(me.dirs, dir)
|
|
|
|
}
|
2024-10-26 20:47:44 -05:00
|
|
|
}
|
2024-10-26 21:48:42 -05:00
|
|
|
return
|
2024-10-26 20:47:44 -05:00
|
|
|
}
|
|
|
|
|
2024-10-14 15:26:11 -05:00
|
|
|
if tmp == "/start" {
|
2024-10-26 20:09:33 -05:00
|
|
|
log.Info("/start jcarr actually doing START")
|
2024-10-26 19:34:32 -05:00
|
|
|
fmt.Fprintln(w, "/start jcarr actually doing START")
|
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) {
|
|
|
|
fmt.Fprintln(w, "START FAILED")
|
|
|
|
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 {
|
|
|
|
fmt.Fprintln(w, "START FAILED")
|
|
|
|
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-26 20:47:44 -05:00
|
|
|
fmt.Fprintln(w, "START hostname is blank")
|
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 {
|
|
|
|
fmt.Fprintln(w, "START FAILED")
|
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := newStart(start, d); err != nil {
|
2024-10-26 12:32:37 -05:00
|
|
|
fmt.Fprintln(w, "START FAILED")
|
|
|
|
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
|
|
|
|
|
|
|
fmt.Fprintln(w, "Handling URL:", tmp, "start droplet")
|
2024-10-15 11:02:54 -05:00
|
|
|
log.Warn("cmd :", cmd)
|
2024-10-16 20:38:51 -05:00
|
|
|
|
|
|
|
fmt.Fprintln(w, "Handling URL:", tmp, "start droplet")
|
|
|
|
fmt.Fprintln(w, "cmd: ", cmd)
|
|
|
|
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 {
|
|
|
|
fmt.Fprintln(w, "START OK")
|
|
|
|
fmt.Fprintln(w, output)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "START FAILED")
|
|
|
|
fmt.Fprintln(w, "error =", err)
|
|
|
|
fmt.Fprintln(w, "output =", output)
|
|
|
|
}
|
2024-10-14 15:26:11 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/favicon.ico" {
|
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
if tmp == "/uptime" {
|
|
|
|
writeFile(w, "uptime.html")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// used for uptime monitor checking (like Kuma)
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/uptime" {
|
|
|
|
writeFile(w, "uptime.html")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("BAD URL =", tmp)
|
|
|
|
fmt.Fprintln(w, "BAD ZOOT")
|
|
|
|
// badurl(w, r.URL.String())
|
|
|
|
// fmt.Fprintln(w, "BAD", tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|