From 1e9ee55a8e098ada869fd5870c2eed9938421485 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 15 Nov 2024 19:25:24 -0600 Subject: [PATCH] connects to zookeeper --- Makefile | 14 +++++++++++--- apt.go | 41 +++++++++++++++++++++++++++++++++++++++++ post.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ send.go | 31 +++++++++++++++++++++++++++++++ watchdog.go | 2 ++ 5 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 post.go create mode 100644 send.go diff --git a/Makefile b/Makefile index 2ef1093..ef166c0 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +.PHONY: build + VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d_%H%M) @@ -5,11 +7,17 @@ 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.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi) -all: +all: build + ./zood --version + ./zood + +build: GO111MODULE=off go build \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" - #./zood - ./zood --version + +install: + GO111MODULE=off go install \ + -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" # this is for release builds using the go.mod files release-build: diff --git a/apt.go b/apt.go index bdac375..9f88164 100644 --- a/apt.go +++ b/apt.go @@ -30,3 +30,44 @@ func initPackages() { log.Info(me.hostname, "has distro", me.distro, "with", me.packages.Len(), "packages installed") } + +func addNew(name string, version string) bool { + new1 := new(zoopb.Package) + new1.Name = name + new1.Version = version + return me.packages.Append(new1) +} + +func updatePackages() { + // Get the list of installed packages for the detected distro + newP, err := getPackageList(me.distro) + if err != nil { + fmt.Println("Error:", err) + return + } + + var newCounter, changeCounter int + // Print the installed packages and their versions + for pkg, version := range newP { + found := me.packages.FindByName(pkg) + if found == nil { + log.Info("adding new", pkg, version) + addNew(pkg, version) + newCounter += 1 + } else { + found.Version = version + if me.packages.Update(found) { + changeCounter += 1 + } + } + } + + 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) + } + if newCounter != 0 { + footer += fmt.Sprintf(" (%d new)", newCounter) + } + log.Info(footer) +} diff --git a/post.go b/post.go new file mode 100644 index 0000000..08c487d --- /dev/null +++ b/post.go @@ -0,0 +1,48 @@ +package main + +import ( + "bytes" + "io/ioutil" + "net/http" + "os" + "os/user" + + "go.wit.com/log" +) + +func httpPost(url string, data []byte) ([]byte, error) { + var err error + var req *http.Request + + // data := []byte("some junk") + // url := "https://go.wit.com/register/" + + req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + + usr, _ := user.Current() + req.Header.Set("author", usr.Username) + hostname, _ := os.Hostname() + req.Header.Set("hostname", hostname) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Error(err) + return []byte("client.Do(req) error"), err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + return body, err + } + + // test := strings.TrimSpace(string(body)) + // log.Info("go.wit.com returned body:", test) + // if test == "OK" { + // return body, nil + // } + + return body, nil +} diff --git a/send.go b/send.go new file mode 100644 index 0000000..74640e6 --- /dev/null +++ b/send.go @@ -0,0 +1,31 @@ +// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 + +package main + +import ( + "strings" + + "go.wit.com/log" +) + +var urlbase string = "http://localhost:8080" + +func send() { +} + +func pingStatus() error { + var url string + url = urlbase + "/status" + body, err := httpPost(url, nil) + if err != nil { + log.Info("httpPost() failed:", err) + return err + } + + test := strings.TrimSpace(string(body)) + // log.Info("virtigo returned body:", test) + for _, line := range strings.Split(test, "\n") { + log.Info("GOT:", line) + } + return nil +} diff --git a/watchdog.go b/watchdog.go index 46235a9..75ac1cb 100644 --- a/watchdog.go +++ b/watchdog.go @@ -32,6 +32,8 @@ func NewWatchdog() { return case t := <-me.dog.C: log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t) + updatePackages() + pingStatus() // h.pollHypervisor() // h.Scan() }