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)
all: build
#./zookeeper
./zookeeper --version
./zookeeper
build:
GO111MODULE=off go build \

2
apt.go
View File

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

View File

@ -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 {

55
http.go
View File

@ -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)

11
main.go
View File

@ -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()

View File

@ -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
}

View File

@ -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()
}