Compare commits

..

No commits in common. "master" and "v0.0.58" have entirely different histories.

11 changed files with 202 additions and 193 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: build all: goimports gocui-debugging
./zookeeper list # ./zookeeper
vet: vet:
@GO111MODULE=off go vet @GO111MODULE=off go vet
@ -20,12 +20,8 @@ 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 >/tmp/forge.log 2>&1
# ./zookeeper --gui gocui --gui-file ~/go/src/go.wit.com/toolkits/gocui/gocui.so >/tmp/forge.log 2>&1
build: goimports vet build: goimports vet
GO111MODULE=off go build -v -x \ GO111MODULE=off go build -v -x \
@ -55,7 +51,7 @@ redomod:
clean: clean:
rm -f go.* rm -f go.*
rm -f zookeeper rm -f zookeeper
go-mod-clean purge go-mod-clean --purge
# git clone the sources and all the golang dependancies into ~/go/src # git clone the sources and all the golang dependancies into ~/go/src
# if you don't have go-clone, you can get it from http://go.wit.com/ # if you don't have go-clone, you can get it from http://go.wit.com/

45
argv.go
View File

@ -9,27 +9,15 @@ 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 {
Gui *EmptyCmd `arg:"subcommand:gui" help:"open the gui"`
List *EmptyCmd `arg:"subcommand:list" help:"list the machines in your zoo"`
Upgrade *EmptyCmd `arg:"subcommand:upgrade" help:"upgrade the machines"`
Verbose bool `arg:"--verbose" default:"false" help:"talk more"` Verbose bool `arg:"--verbose" default:"false" help:"talk more"`
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"`
Port int `arg:"--port" default:"8080" help:"port to run on"` 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 {
@ -62,36 +50,3 @@ 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

@ -8,7 +8,7 @@ Maintainer: Jeff Carr <jcarr@wit.com>
Architecture: amd64 Architecture: amd64
Depends: Depends:
URL: https://go.wit.com/apps/zookeeper URL: https://go.wit.com/apps/zookeeper
Description: manage your homelab cluster Description: zookeeper for homelab grids
keeps track of things in a grid. Maybe keeps track of things in a grid. Maybe
this is similar to the apache project by this is similar to the apache project by
the same name, but in any case, this is the same name, but in any case, this is

View File

@ -7,7 +7,9 @@ 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"
@ -35,6 +37,10 @@ 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()
@ -63,9 +69,11 @@ func doGui() {
log.Info("todo: start a list here!") log.Info("todo: start a list here!")
}) })
grid.NewButton("ConfigSave()", func() { // sit here forever refreshing the GUI
saveMachineState() for {
}) refresh()
time.Sleep(90 * time.Second)
}
} }
func saveMachineState() { func saveMachineState() {

83
http.go
View File

@ -5,70 +5,43 @@ package main
import ( import (
"fmt" "fmt"
"io" "io/ioutil"
"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 '/'
if strings.HasPrefix(route, "/repos/") { func cleanURL(url string) string {
pb := gitpb.NewRepos() url = "/" + strings.Trim(url, "/")
if err := pb.Unmarshal(reqPB.ClientData); err == nil { return url
reqPB.Log("Repos Unmarshal() len=%d", pb.Len()) }
} else {
reqPB.Logf("Repos Unmarshal() err=%v", err) func okHandler(w http.ResponseWriter, r *http.Request) {
} // log.Info("Got URL Path: ", r.URL.Path)
result := gitpb.NewRepos() route := cleanURL(r.URL.Path)
switch route {
case "/repos/check": hostname := r.URL.Query().Get("hostname")
result = addRequest(pb, reqPB) // flag := r.URL.Query().Get("flag")
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
case "/repos/pull": msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
result = pullRequest(pb, reqPB) if err != nil {
case "/repos/add": log.Info("ReadAll() error =", err)
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(w, reqPB) handleMachine(r, w, hostname, msg)
return return
} }
if route == "/uptime" { if route == "/uptime" {
doUptime(w)
return
}
log.Warn("BAD URL =", route)
}
func doUptime(w io.Writer) {
if me.zood == nil { if me.zood == nil {
fmt.Fprintf(w, "BAD zood == nil\n") fmt.Fprintf(w, "BAD zood == nil\n")
return return
@ -91,4 +64,22 @@ func doUptime(w io.Writer) {
} else { } else {
fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version) fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
} }
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)
badExit(err)
}
} }

83
httpDump.go Normal file
View File

@ -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))
}
}
*/
}

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

37
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/lib/gui/prep" "go.wit.com/gui"
"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,19 +17,25 @@ import (
var VERSION string var VERSION string
var BUILDTIME string var BUILDTIME string
var ARGNAME string = "zookeeper" //go:embed resources/*
var resources embed.FS
func main() { func main() {
me = new(mainType) var pp *arg.Parser
prep.Bash(ARGNAME, argv.DoAutoComplete) // this line should be: prep.Bash(argv) gui.InitArg()
me.myGui = prep.Gui() // prepares the GUI package for go-args pp = arg.MustParse(&argv)
me.pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
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()
@ -37,22 +43,11 @@ func main() {
log.Warn("load config failed", err) log.Warn("load config failed", err)
os.Exit(-1) os.Exit(-1)
} }
if argv.List != nil { // me.upgrade = make(map[string]bool) // used to trigger upgrade attempts
log.Info("do list here")
okExit("")
}
go NewWatchdog() go NewWatchdog()
if !argv.NoPort {
go httppb.StartHTTP(okHandler, argv.Port)
}
me.myGui.Start() // loads the GUI toolkit go startHTTP()
doGui() // start making our forge GUI
// sit here forever refreshing the GUI doGui()
for {
refresh()
time.Sleep(90 * time.Second)
}
} }

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -6,18 +6,15 @@ 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 *mainType var me *zookeep
// this app's variables // this app's variables
type mainType struct { type zookeep 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
@ -27,7 +24,7 @@ type mainType 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 *prep.GuiPrep // the gui toolkit handle myGui *gui.Node // 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

View File

@ -59,11 +59,16 @@ func makeZoodWin() *stdTableWin {
stdw.versionL = grid.NewLabel("scan") stdw.versionL = grid.NewLabel("scan")
stdw.outOfDate = grid.NewCheckbox("out of date") stdw.outOfDate = grid.NewCheckbox("out of date")
stdw.showAll = grid.NewCheckbox("all") stdw.showAll = grid.NewCheckbox("all")
grid.NewButton("upgrade 10", func() { grid.NewButton("show out of date", func() {
sendUpgrade(10) found := zoopb.NewMachines()
}) all := me.machines.All()
grid.NewButton("upgrade all", func() { for all.Scan() {
sendUpgrade(-1) m := all.Next()
if m.FindVersion("zood") != me.zood.version {
found.Append(m)
}
}
stdw.doMachinesUpgradeTable(found)
}) })
// make a box at the bottom of the window for the protobuf table // make a box at the bottom of the window for the protobuf table
@ -73,26 +78,6 @@ func makeZoodWin() *stdTableWin {
return stdw return stdw
} }
func sendUpgrade(i int) {
var count int
all := me.machines.All()
for all.Scan() {
m := all.Next()
mtime := m.Laststamp.AsTime()
if time.Since(mtime) > 10*time.Hour {
continue
}
if m.FindVersion("zood") != me.zood.version {
count += 1
m.Upgrade = true
log.Info("upgrade", m.Hostname, count)
}
if i == -1 || count > i {
return
}
}
}
func (stdw *stdTableWin) refresh() { func (stdw *stdTableWin) refresh() {
if stdw.outOfDate.Checked() { if stdw.outOfDate.Checked() {
log.Info("refresh() showing out of date zoo") log.Info("refresh() showing out of date zoo")