2024-11-15 18:49:52 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-11-15 19:49:48 -06:00
|
|
|
"go.wit.com/lib/protobuf/zoopb"
|
2024-11-15 18:49:52 -06: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) {
|
|
|
|
log.Info("Got URL Path: ", r.URL.Path)
|
|
|
|
route := cleanURL(r.URL.Path)
|
|
|
|
|
2024-11-15 19:24:57 -06:00
|
|
|
hostname := r.URL.Query().Get("hostname")
|
2024-11-15 18:49:52 -06:00
|
|
|
|
|
|
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
|
|
if err != nil {
|
2024-11-15 19:24:57 -06:00
|
|
|
log.Info("ReadAll() error =", err)
|
2024-11-15 18:49:52 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if route == "/" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-15 20:59:23 -06:00
|
|
|
if route == "/machine" {
|
|
|
|
var m *zoopb.Machine
|
|
|
|
m = new(zoopb.Machine)
|
|
|
|
if err := m.Unmarshal(msg); err != nil {
|
|
|
|
log.Info("proto.Unmarshal() failed on wire message len", len(msg), "from", hostname)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Packages == nil {
|
|
|
|
log.Info("Unmarshal worked with msg len", len(msg), "from", m.Hostname)
|
|
|
|
log.Info(m.Hostname, "sent machine")
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Info("Unmarshal worked with msg len", len(msg), "from", m.Hostname)
|
|
|
|
log.Info(m.Hostname, "has", m.Packages.Len(), "packages installed")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-15 19:24:57 -06:00
|
|
|
if route == "/status" {
|
|
|
|
if hostname == "" {
|
|
|
|
// ignore junk
|
|
|
|
log.Info("hostname was blank")
|
|
|
|
return
|
|
|
|
}
|
2024-11-15 19:49:48 -06:00
|
|
|
var packs *zoopb.Packages
|
|
|
|
packs = new(zoopb.Packages)
|
|
|
|
if err := packs.Unmarshal(msg); err != nil {
|
|
|
|
log.Info("proto.Unmarshal() failed on wire message len", len(msg), "from", hostname)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Unmarshal worked with msg len", len(msg), "from", hostname)
|
|
|
|
log.Info(hostname, "has", packs.Len(), "packages installed")
|
2024-11-15 20:59:23 -06:00
|
|
|
fmt.Fprintln(w, "upgrade")
|
|
|
|
|
|
|
|
m := me.machines.FindByName(hostname)
|
|
|
|
if m == nil {
|
|
|
|
log.Info("did not find", hostname)
|
|
|
|
} else {
|
|
|
|
log.Info("found", hostname)
|
|
|
|
}
|
2024-11-15 18:49:52 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("BAD URL =", route)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|