zood connects

This commit is contained in:
Jeff Carr 2024-11-15 19:24:57 -06:00
parent fbd638aec0
commit 9d265e0445
7 changed files with 28 additions and 66 deletions

View File

@ -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) REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi)
all: build all: build
#./zookeeper
./zookeeper --version ./zookeeper --version
./zookeeper
build: build:
GO111MODULE=off go build \ GO111MODULE=off go build \

View File

@ -13,7 +13,7 @@ var argv args
type args struct { type args struct {
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:"2521" help:"port to run on"` Port int `arg:"--port" default:"8080" help:"port to run on"`
} }
func (args) Version() string { func (args) Version() string {

51
http.go
View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"go.wit.com/log" "go.wit.com/log"
@ -20,67 +19,31 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Got URL Path: ", r.URL.Path) log.Info("Got URL Path: ", r.URL.Path)
route := cleanURL(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 msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
if err != nil { if err != nil {
fmt.Fprintln(w, "ReadAll() error =", err) log.Info("ReadAll() error =", err)
return return
} }
log.Info("Got URL msg:", string(msg))
if route == "/" { if route == "/" {
fmt.Fprintln(w, "OK")
return return
} }
// exit the virtigo daemon & have systemd restart it if route == "/status" {
// this can happen & when it does, access to if hostname == "" {
// to libvirtd will hang (aka: virsh list will hang) // ignore junk
// One way to trigger this is to not properly close log.Info("hostname was blank")
// 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 return
} }
log.Info("Got URL msg length:", len(msg))
// 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")
return return
} }
log.Warn("BAD URL =", route) 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 // starts and sits waiting for HTTP requests
func startHTTP() { func startHTTP() {
http.HandleFunc("/", okHandler) http.HandleFunc("/", okHandler)

11
main.go
View File

@ -20,7 +20,6 @@ import (
"time" "time"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -45,16 +44,8 @@ func main() {
} }
me = new(stuff) me = new(stuff)
me.zookeeper = "zookeeper.wit.com"
me.hostname, _ = os.Hostname() me.hostname, _ = os.Hostname()
me.pollDelay = 3 * time.Second me.pollDelay = 10 * time.Second
// what OS?
me.distro = initDistro()
// init the installed package list
me.packages = new(zoopb.Packages)
initPackages()
go NewWatchdog() go NewWatchdog()

View File

@ -14,6 +14,7 @@ type stuff struct {
zookeeper string // the dns name for the zookeeper zookeeper string // the dns name for the zookeeper
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
dogchan chan bool
distro string // debian,redhat,gentoo,macos,wincrap distro string // debian,redhat,gentoo,macos,wincrap
packages *zoopb.Packages // installed packages and versions packages *zoopb.Packages // installed packages and versions
} }

View File

@ -14,10 +14,18 @@ func TimeFunction(f func()) time.Duration {
return time.Since(startTime) // Calculate the elapsed time 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() { func NewWatchdog() {
me.dog = time.NewTicker(me.pollDelay) me.dog = time.NewTicker(me.pollDelay)
defer me.dog.Stop() defer me.dog.Stop()
done := make(chan bool) me.dogchan = make(chan bool)
/* /*
// this example would exit/destroy the ticker in 10 seconds // this example would exit/destroy the ticker in 10 seconds
go func() { go func() {
@ -27,12 +35,11 @@ func NewWatchdog() {
*/ */
for { for {
select { select {
case <-done: case <-me.dogchan:
fmt.Println("Done!") fmt.Println("Done!")
return return
case t := <-me.dog.C: case t := <-me.dog.C:
log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t) log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
updatePackages()
// h.pollHypervisor() // h.pollHypervisor()
// h.Scan() // h.Scan()
} }