2024-10-12 00:17:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-10-28 08:08:27 -05:00
|
|
|
"os"
|
2024-10-12 00:17:26 -05:00
|
|
|
"strings"
|
|
|
|
|
2024-10-31 06:50:57 -05:00
|
|
|
"go.wit.com/lib/virtigolib"
|
2024-10-26 05:17:51 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-12 00:17:26 -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-27 02:29:45 -05:00
|
|
|
var route string
|
|
|
|
route = cleanURL(r.URL.Path)
|
2024-10-12 00:17:26 -05:00
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/uptime" {
|
2024-10-27 11:02:50 -05:00
|
|
|
ok, s := uptimeCheck()
|
2024-10-27 02:29:45 -05:00
|
|
|
if ok {
|
|
|
|
log.Info("Handling URL:", route, "cluster is ok", s)
|
2024-10-13 01:13:19 -05:00
|
|
|
fmt.Fprintln(w, s)
|
2024-10-12 13:01:31 -05:00
|
|
|
} else {
|
2024-10-27 02:29:45 -05:00
|
|
|
log.Info("Handling URL:", route, "cluster is not right yet", s)
|
2024-10-13 01:13:19 -05:00
|
|
|
fmt.Fprintln(w, s)
|
2024-10-12 13:01:31 -05:00
|
|
|
}
|
2024-10-12 00:17:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/start" {
|
|
|
|
hostname := r.URL.Query().Get("hostname")
|
|
|
|
if hostname == "" {
|
|
|
|
log.Warn("start failed. hostname is blank", cleanURL(r.URL.Path))
|
|
|
|
fmt.Fprintln(w, "start failed. hostname is blank", cleanURL(r.URL.Path))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("hostname is", hostname)
|
|
|
|
fmt.Fprintln(w, "hostname is", hostname)
|
|
|
|
|
2024-10-16 20:43:01 -05:00
|
|
|
// log.Warn("Handling URL:", tmp, "start droplet", start)
|
2024-10-27 04:53:39 -05:00
|
|
|
result, err := Start(hostname)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintln(w, result)
|
2024-10-27 08:10:19 -05:00
|
|
|
fmt.Fprintln(w, hostname, "started output ok")
|
|
|
|
fmt.Fprintln(w, hostname, "need to parse the output here")
|
|
|
|
fmt.Fprintln(w, hostname, "todo: switch to protobuf here")
|
2024-10-27 02:55:08 -05:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, result)
|
2024-10-27 04:53:39 -05:00
|
|
|
fmt.Fprintln(w, err)
|
2024-10-27 02:55:08 -05:00
|
|
|
fmt.Fprintln(w, hostname, "start failed")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:15:36 -05:00
|
|
|
if route == "/import" {
|
|
|
|
log.Info("virtigo import starts here")
|
|
|
|
fmt.Fprintln(w, "virtigo import starts here")
|
2024-10-31 15:43:25 -05:00
|
|
|
result, err := importDomain(w, r)
|
2024-10-30 18:09:54 -05:00
|
|
|
if err != nil {
|
2024-10-31 14:15:36 -05:00
|
|
|
log.Info("virtigo import failed")
|
2024-10-30 18:09:54 -05:00
|
|
|
log.Info(result)
|
2024-10-31 14:15:36 -05:00
|
|
|
fmt.Fprintln(w, "virtigo import failed")
|
2024-10-30 18:09:54 -05:00
|
|
|
fmt.Fprintln(w, result)
|
|
|
|
return
|
|
|
|
}
|
2024-11-01 11:17:52 -05:00
|
|
|
log.Info("virtigo import worked")
|
|
|
|
fmt.Fprintln(w, "virtigo import worked")
|
2024-10-30 12:16:08 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 11:02:50 -05:00
|
|
|
// toggle poll logging
|
|
|
|
if route == "/poll" {
|
|
|
|
if POLL.Get() {
|
|
|
|
fmt.Fprintln(w, "POLL is true")
|
|
|
|
POLL.SetBool(false)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "POLL is false")
|
|
|
|
POLL.SetBool(true)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:55:08 -05:00
|
|
|
if route == "/dumpcluster" {
|
|
|
|
dumpCluster(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-01 02:00:46 -05:00
|
|
|
if route == "/dumpdroplet" {
|
2024-11-01 05:50:42 -05:00
|
|
|
me.cluster.DumpDroplet(w, r)
|
2024-11-01 02:00:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 03:17:34 -05:00
|
|
|
if route == "/dumpdroplets" {
|
2024-10-29 09:00:40 -05:00
|
|
|
dumpDroplets(w, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/dumpdropletsfull" {
|
|
|
|
dumpDroplets(w, true)
|
2024-10-27 03:17:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 11:02:50 -05:00
|
|
|
if route == "/dumphypervisors" {
|
|
|
|
dumpHypervisors(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:55:08 -05:00
|
|
|
if route == "/dumplibvirtxml" {
|
2024-10-31 06:50:57 -05:00
|
|
|
virtigolib.DumpLibvirtxmlDomainNames()
|
2024-10-13 03:49:54 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-28 08:08:27 -05:00
|
|
|
if route == "/quit" {
|
|
|
|
log.Warn("writing out config file and exiting virtigo")
|
|
|
|
fmt.Fprintln(w, "writing out config file and exiting virtigo")
|
|
|
|
if err := me.cluster.ConfigSave(); err != nil {
|
|
|
|
log.Info("configsave error", err)
|
|
|
|
} else {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-29 09:02:15 -05:00
|
|
|
if route == "/favicon.ico" {
|
|
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/goReference.svg" {
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
writeFile(w, "goReference.svg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
log.Warn("BAD URL =", route)
|
|
|
|
fmt.Fprintln(w, "BAD URL tmp =", route)
|
2024-10-12 00:17:26 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 12:32:17 -05:00
|
|
|
// write a file out to the http socket
|
2024-10-12 00:17:26 -05:00
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|