Compare commits
No commits in common. "master" and "v0.0.67" have entirely different histories.
6
argv.go
6
argv.go
|
@ -79,8 +79,8 @@ func (args) doBashHelp() {
|
|||
}
|
||||
}
|
||||
|
||||
func (args) DoAutoComplete(argv []string) {
|
||||
switch argv[0] {
|
||||
func (args) doBashAuto() {
|
||||
switch argv.BashAuto[0] {
|
||||
case "list":
|
||||
fmt.Println("")
|
||||
case "verify":
|
||||
|
@ -88,7 +88,7 @@ func (args) DoAutoComplete(argv []string) {
|
|||
case "upgrade":
|
||||
fmt.Println("")
|
||||
default:
|
||||
if argv[0] == ARGNAME {
|
||||
if argv.BashAuto[0] == ARGNAME {
|
||||
// list the subcommands here
|
||||
fmt.Println("help list")
|
||||
}
|
||||
|
|
24
doGui.go
24
doGui.go
|
@ -7,7 +7,10 @@ package main
|
|||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.wit.com/gui"
|
||||
"go.wit.com/lib/fhelp"
|
||||
"go.wit.com/lib/gadgets"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
|
@ -35,6 +38,21 @@ func refresh() {
|
|||
}
|
||||
|
||||
func doGui() {
|
||||
me.myGui = gui.New()
|
||||
me.myGui.SetAppDefaultPlugin("gocui") // sets the default GUI plugin to use
|
||||
if pname, err := me.myGui.Default(); err != nil {
|
||||
if !fhelp.BuildPlugin("gocui") {
|
||||
log.Info("You can't run the forge GUI since the plugins did not build", pname)
|
||||
okExit("")
|
||||
} else {
|
||||
if err := me.myGui.LoadToolkitNew("gocui"); err != nil {
|
||||
log.Info("The plugins built, but still failed to load", pname)
|
||||
badExit(err)
|
||||
}
|
||||
log.Info("The plugins built and loaded!", pname)
|
||||
}
|
||||
}
|
||||
|
||||
win := gadgets.RawBasicWindow("Zookeeper: (inventory your cluster)")
|
||||
win.Make()
|
||||
win.Show()
|
||||
|
@ -66,6 +84,12 @@ func doGui() {
|
|||
grid.NewButton("ConfigSave()", func() {
|
||||
saveMachineState()
|
||||
})
|
||||
|
||||
// sit here forever refreshing the GUI
|
||||
for {
|
||||
refresh()
|
||||
time.Sleep(90 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func saveMachineState() {
|
||||
|
|
113
http.go
113
http.go
|
@ -5,90 +5,81 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
/*
|
||||
if strings.HasPrefix(route, "/repos/") {
|
||||
pb := gitpb.NewRepos()
|
||||
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
||||
reqPB.Log("Repos Unmarshal() len=%d", pb.Len())
|
||||
} else {
|
||||
reqPB.Logf("Repos Unmarshal() err=%v", err)
|
||||
}
|
||||
result := gitpb.NewRepos()
|
||||
switch route {
|
||||
case "/repos/check":
|
||||
result = addRequest(pb, reqPB)
|
||||
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
|
||||
case "/repos/pull":
|
||||
result = pullRequest(pb, reqPB)
|
||||
case "/repos/add":
|
||||
result = addRequest(pb, reqPB)
|
||||
default:
|
||||
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
|
||||
log.Info("repos", route, "unknown")
|
||||
}
|
||||
if err := result.SendReply(w, reqPB); err != nil {
|
||||
reqPB.Logf("Oh well, Send to client failed. err=%v", err)
|
||||
}
|
||||
// todo: logReq(reqPB)
|
||||
logReqPB(reqPB)
|
||||
// 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")
|
||||
|
||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
||||
if err != nil {
|
||||
log.Info("ReadAll() error =", err)
|
||||
return
|
||||
}
|
||||
*/
|
||||
func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||
reqPB, err := httppb.ReqToPB(r)
|
||||
reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
|
||||
if err != nil {
|
||||
reqPB.Logf("httppb err %v", err)
|
||||
}
|
||||
|
||||
route := reqPB.Route
|
||||
|
||||
if route == "/" {
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/machine" {
|
||||
handleMachine(w, reqPB)
|
||||
handleMachine(r, w, hostname, msg)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/uptime" {
|
||||
doUptime(w)
|
||||
if me.zood == nil {
|
||||
fmt.Fprintf(w, "BAD zood == nil\n")
|
||||
return
|
||||
}
|
||||
var count int
|
||||
var bad int
|
||||
for m := range me.machines.IterAll() {
|
||||
count += 1
|
||||
if m.FindVersion("zood") != me.zood.version {
|
||||
if m.SinceLastUpdate() > 10*time.Minute {
|
||||
// skip machines that have not been updated in the last 10 minutes
|
||||
log.Info("ignoring old machine", m.Hostname)
|
||||
continue
|
||||
}
|
||||
bad += 1
|
||||
}
|
||||
}
|
||||
if bad == 0 {
|
||||
fmt.Fprintf(w, "GOOD machine count=(%d) all machines are version %s\n", count, me.zood.version)
|
||||
} else {
|
||||
fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.Warn("BAD URL =", route)
|
||||
}
|
||||
|
||||
func doUptime(w io.Writer) {
|
||||
if me.zood == nil {
|
||||
fmt.Fprintf(w, "BAD zood == nil\n")
|
||||
return
|
||||
}
|
||||
var count int
|
||||
var bad int
|
||||
for m := range me.machines.IterAll() {
|
||||
count += 1
|
||||
if m.FindVersion("zood") != me.zood.version {
|
||||
if m.SinceLastUpdate() > 10*time.Minute {
|
||||
// skip machines that have not been updated in the last 10 minutes
|
||||
log.Info("ignoring old machine", m.Hostname)
|
||||
continue
|
||||
}
|
||||
bad += 1
|
||||
}
|
||||
}
|
||||
if bad == 0 {
|
||||
fmt.Fprintf(w, "GOOD machine count=(%d) all machines are version %s\n", count, me.zood.version)
|
||||
} else {
|
||||
fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
|
||||
// starts and sits waiting for HTTP requests
|
||||
func startHTTP() {
|
||||
http.HandleFunc("/", okHandler)
|
||||
|
||||
p := fmt.Sprintf(":%d", argv.Port)
|
||||
log.Println("zookeeper main startHTTP() Running on port", p)
|
||||
|
||||
err := http.ListenAndServe(p, nil)
|
||||
if err != nil {
|
||||
log.Println("Error starting server:", err)
|
||||
badExit(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func dumpRemoteAddr(r *http.Request) string {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
func dumpUserAgent(r *http.Request) string {
|
||||
var all string
|
||||
for param, values := range r.URL.Query() {
|
||||
for _, value := range values {
|
||||
all += fmt.Sprint(" Query:", param, value)
|
||||
}
|
||||
}
|
||||
// hostname := r.URL.Query().Get("hostname")
|
||||
return r.UserAgent() + all
|
||||
}
|
||||
|
||||
func dumpClient(r *http.Request) {
|
||||
/*
|
||||
var host, url, proto, addr, agent string
|
||||
|
||||
host = r.Host
|
||||
url = r.URL.String()
|
||||
proto = r.Proto
|
||||
addr = r.RemoteAddr
|
||||
agent = r.UserAgent()
|
||||
|
||||
log.Warn(host, proto, addr, url, agent)
|
||||
|
||||
fmt.Fprintln(accessf, time.Now(), host, proto, addr, url, agent)
|
||||
// return
|
||||
|
||||
fmt.Fprintln(clientf)
|
||||
fmt.Fprintln(clientf, time.Now())
|
||||
// Basic request information
|
||||
fmt.Fprintln(clientf, "Method:", r.Method)
|
||||
fmt.Fprintln(clientf, "URL:", r.URL)
|
||||
fmt.Fprintln(clientf, "Protocol:", r.Proto)
|
||||
fmt.Fprintln(clientf, "Host:", r.Host)
|
||||
fmt.Fprintln(clientf, "Remote Address:", r.RemoteAddr)
|
||||
|
||||
// Headers
|
||||
fmt.Fprintln(clientf, "Headers:")
|
||||
for name, values := range r.Header {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Headers:", name, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Query parameters
|
||||
fmt.Fprintln(clientf, "Query Parameters:")
|
||||
for param, values := range r.URL.Query() {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Query:", param, value)
|
||||
}
|
||||
}
|
||||
|
||||
// User-Agent
|
||||
fmt.Fprintln(clientf, "User-Agent:", r.UserAgent())
|
||||
|
||||
// Content Length
|
||||
fmt.Fprintln(clientf, "Content Length:", r.ContentLength)
|
||||
|
||||
// Cookies
|
||||
fmt.Fprintln(clientf, "Cookies:")
|
||||
for _, cookie := range r.Cookies() {
|
||||
fmt.Fprintln(clientf, cookie.Name, cookie.Value)
|
||||
}
|
||||
|
||||
// Request Body (if applicable)
|
||||
if r.Body != nil {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err == nil {
|
||||
fmt.Fprintln(clientf, "Body:", string(body))
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
38
machine.go
38
machine.go
|
@ -6,9 +6,9 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
@ -20,27 +20,25 @@ func rawGetHostname(data []byte) *zoopb.Machine {
|
|||
return newm
|
||||
}
|
||||
|
||||
func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
|
||||
// hostname := strings.TrimSpace(reqPB.Hostname)
|
||||
newm := rawGetHostname(reqPB.ClientData)
|
||||
func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) {
|
||||
hostname = strings.TrimSpace(hostname)
|
||||
newm := rawGetHostname(data)
|
||||
if newm == nil {
|
||||
log.Info("unmarshal failed on data len =", len(reqPB.ClientData))
|
||||
log.Info("unmarshal failed on data len =", len(data))
|
||||
}
|
||||
if hostname != newm.Hostname {
|
||||
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
||||
hostname = newm.Hostname
|
||||
}
|
||||
/*
|
||||
if reqPB.Hostname != newm.Hostname {
|
||||
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
||||
hostname = newm.Hostname
|
||||
}
|
||||
*/
|
||||
|
||||
if reqPB.Hostname == "" {
|
||||
ua := reqPB.DumpUserAgent()
|
||||
ra := reqPB.DumpRemoteAddr()
|
||||
log.Info("hostname is blank even after unmarshal. data len =", len(reqPB.ClientData), ra, ua, newm.Cpus, newm.Hostname)
|
||||
if hostname == "" {
|
||||
ua := dumpUserAgent(r)
|
||||
ra := dumpRemoteAddr(r)
|
||||
log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname)
|
||||
return
|
||||
}
|
||||
// log.Info("lookoing for", hostname)
|
||||
m := me.machines.FindByHostname(reqPB.Hostname)
|
||||
m := me.machines.FindByHostname(hostname)
|
||||
if m == nil {
|
||||
am := new(zoopb.Machine)
|
||||
am.Hostname = newm.Hostname
|
||||
|
@ -49,10 +47,10 @@ func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
|
|||
log.Info("new machine", am.Hostname, am.Memory)
|
||||
return
|
||||
}
|
||||
ua := reqPB.DumpUserAgent()
|
||||
ra := reqPB.DumpRemoteAddr()
|
||||
ua := dumpUserAgent(r)
|
||||
ra := dumpRemoteAddr(r)
|
||||
if m.UserAgent != ua {
|
||||
log.Info("hostname ua changed len =", len(reqPB.ClientData), ra, reqPB.Hostname, ua)
|
||||
log.Info("hostname ua changed len =", len(data), ra, hostname, ua)
|
||||
m.UserAgent = ua
|
||||
}
|
||||
if m.Upgrade {
|
||||
|
@ -67,7 +65,7 @@ func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
|
|||
} else {
|
||||
fmt.Fprintln(w, "good")
|
||||
}
|
||||
// log.Info("update machine protobuf", reqPB.hostname)
|
||||
// log.Info("update machine protobuf", hostname)
|
||||
updateMachine(newm)
|
||||
}
|
||||
|
||||
|
|
39
main.go
39
main.go
|
@ -8,8 +8,8 @@ import (
|
|||
"time"
|
||||
|
||||
"go.wit.com/dev/alexflint/arg"
|
||||
"go.wit.com/lib/gui/prep"
|
||||
"go.wit.com/lib/protobuf/httppb"
|
||||
"go.wit.com/gui"
|
||||
"go.wit.com/lib/fhelp"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
@ -21,10 +21,29 @@ var ARGNAME string = "zookeeper"
|
|||
|
||||
func main() {
|
||||
me = new(mainType)
|
||||
prep.Bash(ARGNAME, argv.DoAutoComplete) // this line should be: prep.Bash(argv)
|
||||
me.myGui = prep.Gui() // prepares the GUI package for go-args
|
||||
gui.InitArg()
|
||||
me.pp = arg.MustParse(&argv)
|
||||
|
||||
if argv.Bash {
|
||||
fhelp.DoBash(ARGNAME)
|
||||
os.Exit(0)
|
||||
}
|
||||
if len(argv.BashAuto) != 0 {
|
||||
argv.doBashAuto()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// check if the binary is being called to test
|
||||
// if the plugin can actually loaded. This is a hack
|
||||
// around needing a Test() plugin load function in GO
|
||||
if gui.IsGoPluginTestHack() {
|
||||
argv.NoPort = true
|
||||
gui.CheckPlugin()
|
||||
os.Exit(0)
|
||||
// if argv.GuiCheck != "" {
|
||||
// }
|
||||
}
|
||||
|
||||
if argv.Daemon {
|
||||
// turn off timestamps for STDOUT (systemd adds them)
|
||||
log.DaemonMode(true)
|
||||
|
@ -44,15 +63,7 @@ func main() {
|
|||
|
||||
go NewWatchdog()
|
||||
if !argv.NoPort {
|
||||
go httppb.StartHTTP(okHandler, argv.Port)
|
||||
}
|
||||
|
||||
me.myGui.Start() // loads the GUI toolkit
|
||||
doGui() // start making our forge GUI
|
||||
|
||||
// sit here forever refreshing the GUI
|
||||
for {
|
||||
refresh()
|
||||
time.Sleep(90 * time.Second)
|
||||
go startHTTP()
|
||||
}
|
||||
doGui()
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"go.wit.com/dev/alexflint/arg"
|
||||
"go.wit.com/gui"
|
||||
"go.wit.com/lib/gadgets"
|
||||
"go.wit.com/lib/gui/prep"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
)
|
||||
|
||||
|
@ -27,7 +26,7 @@ type mainType struct {
|
|||
machines *zoopb.Machines // every machine that has reported itself to the zookeeper
|
||||
targets map[string]string // what versions the machines should be running
|
||||
upgrade map[string]bool // use this to trigger builds
|
||||
myGui *prep.GuiPrep // the gui toolkit handle
|
||||
myGui *gui.Node // the gui toolkit handle
|
||||
machinesWin *gadgets.GenericWindow // the machines gui window
|
||||
machinesBox *gui.Node // the machines gui parent box widget
|
||||
machinesTB *zoopb.MachinesTable // the machines gui table buffer
|
||||
|
|
Loading…
Reference in New Issue