Compare commits

..

3 Commits

Author SHA1 Message Date
Jeff Carr af7984a887 convert to httppb 2025-09-09 18:02:06 -05:00
Jeff Carr a29a774aa4 something, something, GUI codebase 2025-09-09 05:41:01 -05:00
Jeff Carr d00e96e354 cleanups 2025-09-08 23:14:04 -05:00
9 changed files with 161 additions and 205 deletions

View File

@ -7,8 +7,8 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
# REDOMOD = $(shell if [ -e go.mod ]; then echo go.mod; else echo no go mod; fi) # REDOMOD = $(shell if [ -e go.mod ]; then echo go.mod; else echo no go mod; fi)
REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi) REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi)
all: goimports gocui-debugging all: build
# ./zookeeper ./zookeeper list
vet: vet:
@GO111MODULE=off go vet @GO111MODULE=off go vet
@ -20,6 +20,9 @@ nogui:
gocui: build gocui: build
./zookeeper --gui gocui ./zookeeper --gui gocui
gocui-5000: build
./zookeeper --gui gocui --port 5000
gocui-debugging: build gocui-debugging: build
./zookeeper --gui gocui --gui-file ~/go/src/go.wit.com/toolkits/gocui/gocui.so ./zookeeper --gui gocui --gui-file ~/go/src/go.wit.com/toolkits/gocui/gocui.so
# ./zookeeper --gui gocui --gui-file ~/go/src/go.wit.com/toolkits/gocui/gocui.so >/tmp/forge.log 2>&1 # ./zookeeper --gui gocui --gui-file ~/go/src/go.wit.com/toolkits/gocui/gocui.so >/tmp/forge.log 2>&1

52
argv.go
View File

@ -9,16 +9,27 @@ package main
*/ */
import ( import (
"fmt"
"os"
"go.wit.com/log" "go.wit.com/log"
) )
var argv args var argv args
type args struct { type args struct {
Verbose bool `arg:"--verbose" default:"false" help:"talk more"` Gui *EmptyCmd `arg:"subcommand:gui" help:"open the gui"`
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` List *EmptyCmd `arg:"subcommand:list" help:"list the machines in your zoo"`
Port int `arg:"--port" default:"8080" help:"port to run on"` Upgrade *EmptyCmd `arg:"subcommand:upgrade" help:"upgrade the machines"`
NoPort bool `arg:"--no-port" help:"don't open socket"` Verbose bool `arg:"--verbose" default:"false" help:"talk more"`
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"`
Port int `arg:"--port" default:"8080" help:"port to run on"`
NoPort bool `arg:"--no-port" help:"don't open socket"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"`
}
type EmptyCmd struct {
} }
func (args) Version() string { func (args) Version() string {
@ -51,3 +62,36 @@ func init() {
ZOOD = log.NewFlag("ZOOD", false, full, short, "show reporting from zood") ZOOD = log.NewFlag("ZOOD", false, full, short, "show reporting from zood")
WARN = log.NewFlag("WARN", true, full, short, "bad things") WARN = log.NewFlag("WARN", true, full, short, "bad things")
} }
// prints help to STDERR // TODO: move everything below this to go-args
func (args) doBashHelp() {
if argv.BashAuto[1] != "''" {
// if this is not blank, then the user has typed something
return
}
if argv.BashAuto[0] != ARGNAME {
// if this is not the name of the command, the user already started doing something
return
}
if argv.BashAuto[0] == ARGNAME {
me.pp.WriteHelp(os.Stderr)
return
}
}
func (args) DoAutoComplete(argv []string) {
switch argv[0] {
case "list":
fmt.Println("")
case "verify":
fmt.Println("on")
case "upgrade":
fmt.Println("")
default:
if argv[0] == ARGNAME {
// list the subcommands here
fmt.Println("help list")
}
}
os.Exit(0)
}

View File

@ -7,9 +7,7 @@ package main
import ( import (
"os" "os"
"time"
"go.wit.com/gui"
"go.wit.com/lib/gadgets" "go.wit.com/lib/gadgets"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
@ -37,10 +35,6 @@ func refresh() {
} }
func doGui() { func doGui() {
me.myGui = gui.New()
me.myGui.InitEmbed(resources)
me.myGui.Default()
win := gadgets.RawBasicWindow("Zookeeper: (inventory your cluster)") win := gadgets.RawBasicWindow("Zookeeper: (inventory your cluster)")
win.Make() win.Make()
win.Show() win.Show()
@ -72,12 +66,6 @@ func doGui() {
grid.NewButton("ConfigSave()", func() { grid.NewButton("ConfigSave()", func() {
saveMachineState() saveMachineState()
}) })
// sit here forever refreshing the GUI
for {
refresh()
time.Sleep(90 * time.Second)
}
} }
func saveMachineState() { func saveMachineState() {

113
http.go
View File

@ -5,81 +5,90 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strings"
"time" "time"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/log" "go.wit.com/log"
) )
// remove '?' part and trailing '/' /*
func cleanURL(url string) string { if strings.HasPrefix(route, "/repos/") {
url = "/" + strings.Trim(url, "/") pb := gitpb.NewRepos()
return url if err := pb.Unmarshal(reqPB.ClientData); err == nil {
} reqPB.Log("Repos Unmarshal() len=%d", pb.Len())
} else {
func okHandler(w http.ResponseWriter, r *http.Request) { reqPB.Logf("Repos Unmarshal() err=%v", err)
// log.Info("Got URL Path: ", r.URL.Path) }
route := cleanURL(r.URL.Path) result := gitpb.NewRepos()
switch route {
hostname := r.URL.Query().Get("hostname") case "/repos/check":
// flag := r.URL.Query().Get("flag") result = addRequest(pb, reqPB)
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte case "/repos/pull":
if err != nil { result = pullRequest(pb, reqPB)
log.Info("ReadAll() error =", err) 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)
return 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 == "/" { if route == "/" {
return return
} }
if route == "/machine" { if route == "/machine" {
handleMachine(r, w, hostname, msg) handleMachine(w, reqPB)
return return
} }
if route == "/uptime" { if route == "/uptime" {
if me.zood == nil { doUptime(w)
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 return
} }
log.Warn("BAD URL =", route) log.Warn("BAD URL =", route)
} }
// starts and sits waiting for HTTP requests func doUptime(w io.Writer) {
func startHTTP() { if me.zood == nil {
http.HandleFunc("/", okHandler) fmt.Fprintf(w, "BAD zood == nil\n")
return
p := fmt.Sprintf(":%d", argv.Port) }
log.Println("zookeeper main startHTTP() Running on port", p) var count int
var bad int
err := http.ListenAndServe(p, nil) for m := range me.machines.IterAll() {
if err != nil { count += 1
log.Println("Error starting server:", err) if m.FindVersion("zood") != me.zood.version {
badExit(err) 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)
} }
} }

View File

@ -1,83 +0,0 @@
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))
}
}
*/
}

View File

@ -6,9 +6,9 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
@ -20,25 +20,27 @@ func rawGetHostname(data []byte) *zoopb.Machine {
return newm return newm
} }
func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) { func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
hostname = strings.TrimSpace(hostname) // hostname := strings.TrimSpace(reqPB.Hostname)
newm := rawGetHostname(data) newm := rawGetHostname(reqPB.ClientData)
if newm == nil { if newm == nil {
log.Info("unmarshal failed on data len =", len(data)) log.Info("unmarshal failed on data len =", len(reqPB.ClientData))
}
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 hostname == "" { if reqPB.Hostname == "" {
ua := dumpUserAgent(r) ua := reqPB.DumpUserAgent()
ra := dumpRemoteAddr(r) ra := reqPB.DumpRemoteAddr()
log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname) log.Info("hostname is blank even after unmarshal. data len =", len(reqPB.ClientData), ra, ua, newm.Cpus, newm.Hostname)
return return
} }
// log.Info("lookoing for", hostname) // log.Info("lookoing for", hostname)
m := me.machines.FindByHostname(hostname) m := me.machines.FindByHostname(reqPB.Hostname)
if m == nil { if m == nil {
am := new(zoopb.Machine) am := new(zoopb.Machine)
am.Hostname = newm.Hostname am.Hostname = newm.Hostname
@ -47,10 +49,10 @@ func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data
log.Info("new machine", am.Hostname, am.Memory) log.Info("new machine", am.Hostname, am.Memory)
return return
} }
ua := dumpUserAgent(r) ua := reqPB.DumpUserAgent()
ra := dumpRemoteAddr(r) ra := reqPB.DumpRemoteAddr()
if m.UserAgent != ua { if m.UserAgent != ua {
log.Info("hostname ua changed len =", len(data), ra, hostname, ua) log.Info("hostname ua changed len =", len(reqPB.ClientData), ra, reqPB.Hostname, ua)
m.UserAgent = ua m.UserAgent = ua
} }
if m.Upgrade { if m.Upgrade {
@ -65,7 +67,7 @@ func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data
} else { } else {
fmt.Fprintln(w, "good") fmt.Fprintln(w, "good")
} }
// log.Info("update machine protobuf", hostname) // log.Info("update machine protobuf", reqPB.hostname)
updateMachine(newm) updateMachine(newm)
} }

51
main.go
View File

@ -4,12 +4,12 @@
package main package main
import ( import (
"embed"
"os" "os"
"time" "time"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/gui" "go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/httppb"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -17,37 +17,19 @@ import (
var VERSION string var VERSION string
var BUILDTIME string var BUILDTIME string
//go:embed resources/* var ARGNAME string = "zookeeper"
var resources embed.FS
func main() { func main() {
log.Warn("zookeeper PID:", os.Getpid(), os.Args) me = new(mainType)
var pp *arg.Parser prep.Bash(ARGNAME, argv.DoAutoComplete) // this line should be: prep.Bash(argv)
gui.InitArg() // include the 'gui' package command line arguements me.myGui = prep.Gui() // prepares the GUI package for go-args
pp = arg.MustParse(&argv) // parse the command line me.pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
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 { if argv.Daemon {
// turn off timestamps for STDOUT (systemd adds them) // turn off timestamps for STDOUT (systemd adds them)
log.DaemonMode(true) log.DaemonMode(true)
} }
me = new(zookeep)
me.hostname, _ = os.Hostname() me.hostname, _ = os.Hostname()
me.pollDelay = time.Hour me.pollDelay = time.Hour
me.machines = zoopb.NewMachines() me.machines = zoopb.NewMachines()
@ -55,13 +37,22 @@ func main() {
log.Warn("load config failed", err) log.Warn("load config failed", err)
os.Exit(-1) os.Exit(-1)
} }
// me.upgrade = make(map[string]bool) // used to trigger upgrade attempts if argv.List != nil {
log.Info("do list here")
// log.Sleep(3) okExit("")
}
go NewWatchdog() go NewWatchdog()
if !argv.NoPort { if !argv.NoPort {
go startHTTP() 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)
} }
doGui()
} }

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -6,15 +6,18 @@ package main
import ( import (
"time" "time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/lib/gadgets" "go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
) )
var me *zookeep var me *mainType
// this app's variables // this app's variables
type zookeep struct { type mainType struct {
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
hostname string // my fqdn dns zookeeper hostname hostname string // my fqdn dns zookeeper hostname
pollDelay time.Duration // how often to report our status pollDelay time.Duration // how often to report our status
dog *time.Ticker // the watchdog timer dog *time.Ticker // the watchdog timer
@ -24,7 +27,7 @@ type zookeep struct {
machines *zoopb.Machines // every machine that has reported itself to the zookeeper machines *zoopb.Machines // every machine that has reported itself to the zookeeper
targets map[string]string // what versions the machines should be running targets map[string]string // what versions the machines should be running
upgrade map[string]bool // use this to trigger builds upgrade map[string]bool // use this to trigger builds
myGui *gui.Node // the gui toolkit handle myGui *prep.GuiPrep // the gui toolkit handle
machinesWin *gadgets.GenericWindow // the machines gui window machinesWin *gadgets.GenericWindow // the machines gui window
machinesBox *gui.Node // the machines gui parent box widget machinesBox *gui.Node // the machines gui parent box widget
machinesTB *zoopb.MachinesTable // the machines gui table buffer machinesTB *zoopb.MachinesTable // the machines gui table buffer