From 9d265e0445164f6ef169bb490f3fe9d97a4ef473 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 15 Nov 2024 19:24:57 -0600 Subject: [PATCH] zood connects --- Makefile | 2 +- apt.go | 2 +- argv.go | 2 +- http.go | 55 +++++++++-------------------------------------------- main.go | 11 +---------- structs.go | 9 +++++---- watchdog.go | 13 ++++++++++--- 7 files changed, 28 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index a6631f5..f640dbf 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M) REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi) all: build - #./zookeeper ./zookeeper --version + ./zookeeper build: GO111MODULE=off go build \ diff --git a/apt.go b/apt.go index ddab8e4..9f88164 100644 --- a/apt.go +++ b/apt.go @@ -62,7 +62,7 @@ func updatePackages() { } } - footer := fmt.Sprintf("%s has distro %s with %d packages installed",me.hostname, me.distro, me.packages.Len()) + footer := fmt.Sprintf("%s has distro %s with %d packages installed", me.hostname, me.distro, me.packages.Len()) if changeCounter != 0 { footer += fmt.Sprintf(" (%d changed)", changeCounter) } diff --git a/argv.go b/argv.go index 9846438..85dcf75 100644 --- a/argv.go +++ b/argv.go @@ -13,7 +13,7 @@ var argv args type args struct { Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` - Port int `arg:"--port" default:"2521" help:"port to run on"` + Port int `arg:"--port" default:"8080" help:"port to run on"` } func (args) Version() string { diff --git a/http.go b/http.go index 6547f74..6ff3bb1 100644 --- a/http.go +++ b/http.go @@ -4,7 +4,6 @@ import ( "fmt" "io/ioutil" "net/http" - "os" "strings" "go.wit.com/log" @@ -20,67 +19,31 @@ func okHandler(w http.ResponseWriter, r *http.Request) { log.Info("Got URL Path: ", r.URL.Path) route := cleanURL(r.URL.Path) - domname := r.URL.Query().Get("domain") + hostname := r.URL.Query().Get("hostname") msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte if err != nil { - fmt.Fprintln(w, "ReadAll() error =", err) + log.Info("ReadAll() error =", err) return } - log.Info("Got URL msg:", string(msg)) if route == "/" { - fmt.Fprintln(w, "OK") return } - // exit the virtigo daemon & have systemd restart it - // this can happen & when it does, access to - // to libvirtd will hang (aka: virsh list will hang) - // One way to trigger this is to not properly close - // domain sockets opened from go-qemu/hypervisor - // it's a good idea in any case so leave it here - if route == "/kill" { - log.Warn("KILLED") - fmt.Fprintln(w, "KILLED") - os.Exit(-1) - return - } - - // curl http://localhost:2520/import?domain=foo.bar.com - if route == "/import" { - fmt.Fprint(w, "import domain:", domname) - - return - } - - if route == "/favicon.ico" { - writeFile(w, "ipv6.png") + if route == "/status" { + if hostname == "" { + // ignore junk + log.Info("hostname was blank") + return + } + log.Info("Got URL msg length:", len(msg)) return } log.Warn("BAD URL =", route) } -func writeFile(w http.ResponseWriter, filename string) { - // fmt.Fprintln(w, "GOT TEST?") - fullname := "resources/" + filename - pfile, err := resources.ReadFile(fullname) - if err != nil { - log.Println("ERROR:", err) - // w.Write(pfile) - return - } - - var repohtml string - repohtml = string(pfile) - if filename == "goReference.svg" { - w.Header().Set("Content-Type", "image/svg+xml") - } - fmt.Fprintln(w, repohtml) - log.Println("writeFile() found internal file:", filename) -} - // starts and sits waiting for HTTP requests func startHTTP() { http.HandleFunc("/", okHandler) diff --git a/main.go b/main.go index db3e42a..f540f6f 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,6 @@ import ( "time" "go.wit.com/dev/alexflint/arg" - "go.wit.com/lib/protobuf/zoopb" "go.wit.com/log" ) @@ -45,16 +44,8 @@ func main() { } me = new(stuff) - me.zookeeper = "zookeeper.wit.com" me.hostname, _ = os.Hostname() - me.pollDelay = 3 * time.Second - - // what OS? - me.distro = initDistro() - - // init the installed package list - me.packages = new(zoopb.Packages) - initPackages() + me.pollDelay = 10 * time.Second go NewWatchdog() diff --git a/structs.go b/structs.go index a5b726f..ba67457 100644 --- a/structs.go +++ b/structs.go @@ -10,10 +10,11 @@ var me *stuff // this app's variables type stuff struct { - hostname string // my hostname to send to zookeeper - zookeeper string // the dns name for the zookeeper - pollDelay time.Duration // how often to report our status - dog *time.Ticker // the watchdog timer + hostname string // my hostname to send to zookeeper + zookeeper string // the dns name for the zookeeper + pollDelay time.Duration // how often to report our status + dog *time.Ticker // the watchdog timer + dogchan chan bool distro string // debian,redhat,gentoo,macos,wincrap packages *zoopb.Packages // installed packages and versions } diff --git a/watchdog.go b/watchdog.go index d70ccbf..e685b7b 100644 --- a/watchdog.go +++ b/watchdog.go @@ -14,10 +14,18 @@ func TimeFunction(f func()) time.Duration { return time.Since(startTime) // Calculate the elapsed time } +func shutdownDog() { + // this example would exit/destroy the ticker in 10 seconds + go func() { + time.Sleep(10 * time.Second) + me.dogchan <- true + }() +} + func NewWatchdog() { me.dog = time.NewTicker(me.pollDelay) defer me.dog.Stop() - done := make(chan bool) + me.dogchan = make(chan bool) /* // this example would exit/destroy the ticker in 10 seconds go func() { @@ -27,12 +35,11 @@ func NewWatchdog() { */ for { select { - case <-done: + case <-me.dogchan: fmt.Println("Done!") return case t := <-me.dog.C: log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t) - updatePackages() // h.pollHypervisor() // h.Scan() }