159 lines
3.5 KiB
Go
159 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go.wit.com/lib/protobuf/zoopb"
|
|
"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)
|
|
|
|
hostname := r.URL.Query().Get("hostname")
|
|
flag := r.URL.Query().Get("flag")
|
|
packname := r.URL.Query().Get("package")
|
|
version := r.URL.Query().Get("version")
|
|
|
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
if err != nil {
|
|
log.Info("ReadAll() error =", err)
|
|
return
|
|
}
|
|
|
|
if route == "/" {
|
|
return
|
|
}
|
|
|
|
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))
|
|
log.Info("error =", err)
|
|
return
|
|
}
|
|
log.Log(INFO, "proto.Unmarshal() worked on wire message len", len(msg), "from", m.Hostname)
|
|
b := me.upgrade[m.Hostname]
|
|
switch updateMachine(m) {
|
|
case "upgrade":
|
|
if b {
|
|
fmt.Fprintln(w, "apt update")
|
|
me.upgrade[m.Hostname] = false
|
|
} else {
|
|
fmt.Fprintln(w, "upgrade")
|
|
}
|
|
default:
|
|
fmt.Fprintln(w, "notsure")
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/status" {
|
|
var packs *zoopb.Packages
|
|
packs = new(zoopb.Packages)
|
|
if err := packs.Unmarshal(msg); err != nil {
|
|
log.Info("/status proto.Unmarshal() failed on wire message len", len(msg), "from", hostname)
|
|
return
|
|
}
|
|
|
|
log.Info("/status Unmarshal worked with msg len", len(msg), "from", hostname)
|
|
log.Info("/status hostname", hostname, "has", packs.Len(), "packages installed")
|
|
fmt.Fprintln(w, "upgrade")
|
|
return
|
|
}
|
|
|
|
// list out the machines and thier version of zood
|
|
if route == "/list" {
|
|
log.HttpMode(w)
|
|
defer log.HttpMode(nil)
|
|
loop := me.machines.SortByHostname()
|
|
for loop.Scan() {
|
|
m := loop.Next()
|
|
zood := m.Packages.FindByName("zood")
|
|
v := me.targets["zood"] // this is the target version
|
|
if zood == nil {
|
|
log.Info("machine", m.Hostname, "does not have zood installed")
|
|
} else {
|
|
log.Info("zood version", zood.Version, "vs target version", v, "on machine", m.Hostname)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// save the config file
|
|
if route == "/save" {
|
|
log.HttpMode(w)
|
|
defer log.HttpMode(nil)
|
|
if err := me.machines.ConfigSave(); err == nil {
|
|
log.Log(NOW, "ConfigSave() ok")
|
|
} else {
|
|
log.Log(NOW, "ConfigSave() failed", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// flag a package to attempt to upgrade
|
|
if route == "/upgrade" {
|
|
log.HttpMode(w)
|
|
defer log.HttpMode(nil)
|
|
me.upgrade[hostname] = true
|
|
log.Log(NOW, "setting package ", packname, " to upgrade")
|
|
return
|
|
}
|
|
|
|
// set the target version for a package
|
|
if route == "/target" {
|
|
log.HttpMode(w)
|
|
defer log.HttpMode(nil)
|
|
me.targets[packname] = version
|
|
log.Log(NOW, "setting package/version to ", packname, " ", version)
|
|
return
|
|
}
|
|
|
|
// toggle logging flags
|
|
if route == "/flag" {
|
|
log.HttpMode(w)
|
|
defer log.HttpMode(nil)
|
|
log.Info("going to toggle flag:", flag)
|
|
switch flag {
|
|
case "ZOOD":
|
|
if ZOOD.Bool() {
|
|
log.Log(NOW, "toogle ZOOD false")
|
|
ZOOD.SetBool(false)
|
|
} else {
|
|
log.Log(NOW, "toogle ZOOD true")
|
|
ZOOD.SetBool(true)
|
|
}
|
|
default:
|
|
log.Info("unknown looging flag:", flag)
|
|
}
|
|
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)
|
|
}
|
|
}
|