2024-10-12 00:17:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-10-26 20:09:59 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-26 04:36:38 -05:00
|
|
|
"go.wit.com/lib/virtigoxml"
|
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-12 12:45:43 -05:00
|
|
|
// is the cluster running what it should?
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/droplets" {
|
2024-10-26 08:54:28 -05:00
|
|
|
for _, d := range me.cluster.Droplets {
|
2024-10-26 13:05:05 -05:00
|
|
|
var msg string
|
|
|
|
if d.StartState == pb.DropletState_ON {
|
|
|
|
msg = "(should be on)"
|
|
|
|
}
|
|
|
|
switch d.CurrentState {
|
|
|
|
case pb.DropletState_ON:
|
|
|
|
fmt.Fprintln(w, d.Hostname, "ON")
|
|
|
|
case pb.DropletState_OFF:
|
|
|
|
fmt.Fprintln(w, d.Hostname, msg)
|
|
|
|
default:
|
|
|
|
fmt.Fprintln(w, d.Hostname, "? state:", d.CurrentState, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// show only what droplets should be running but are missing
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/missing" {
|
2024-10-26 13:05:05 -05:00
|
|
|
var count int
|
|
|
|
var missing int
|
|
|
|
for _, d := range me.cluster.Droplets {
|
|
|
|
if d.StartState != pb.DropletState_ON {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
count += 1
|
|
|
|
if d.CurrentState == pb.DropletState_ON {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
missing += 1
|
|
|
|
fmt.Fprintln(w, d.Hostname, "current state:", d.CurrentState)
|
|
|
|
}
|
|
|
|
if missing == 0 {
|
|
|
|
fmt.Fprintln(w, "all", count, "droplets set to run are running")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, missing, "droplets missing")
|
|
|
|
fmt.Fprintln(w, count, "droplets should be running")
|
2024-10-12 12:45:43 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/favicon.ico" {
|
2024-10-22 19:51:01 -05:00
|
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
2024-10-12 00:17:26 -05:00
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-10-22 19:51:01 -05:00
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/goReference.svg" {
|
2024-10-22 19:51:01 -05:00
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
writeFile(w, "goReference.svg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/dumplibvirtxml" {
|
2024-10-26 04:36:38 -05:00
|
|
|
virtigoxml.DumpLibvirtxmlDomainNames()
|
2024-10-23 18:06:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 02:29:45 -05:00
|
|
|
if route == "/uptime" {
|
|
|
|
ok, s := clusterHealthy()
|
|
|
|
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 02:29:45 -05:00
|
|
|
b, result := Start(hostname)
|
2024-10-16 20:43:01 -05:00
|
|
|
log.Warn("Start returned =", b, "result =", result)
|
|
|
|
fmt.Fprintln(w, "Start() returned", b)
|
|
|
|
fmt.Fprintln(w, "result:", result)
|
2024-10-13 03:49:54 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|