move common code to zoopb

This commit is contained in:
Jeff Carr 2024-11-21 19:48:09 -06:00
parent f5144e4b8a
commit 22d49578c9
7 changed files with 10 additions and 226 deletions

73
apt.go
View File

@ -1,73 +0,0 @@
package main
import (
"fmt"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
// init the installed package list
func initPackages() {
// Get the list of installed packages for the detected distro
newP, err := getPackageList(me.distro)
if err != nil {
fmt.Println("Error:", err)
return
}
// Print the installed packages and their versions
for pkg, version := range newP {
new1 := new(zoopb.Package)
new1.Name = pkg
new1.Version = version
if me.machine.Packages.Append(new1) {
// log.Info("added", new1.Name, "ok")
} else {
log.Info("added", new1.Name, "failed")
}
}
log.Info(me.hostname, "has distro", me.distro, "with", me.machine.Packages.Len(), "packages installed.")
}
func addNew(name string, version string) bool {
new1 := new(zoopb.Package)
new1.Name = name
new1.Version = version
return me.machine.Packages.Append(new1)
}
func updatePackages() string {
// Get the list of installed packages for the detected distro
newP, err := getPackageList(me.distro)
if err != nil {
fmt.Println("Error:", err)
return fmt.Sprintln("getPackageList()", err)
}
var newCounter, changeCounter int
// Print the installed packages and their versions
for pkg, version := range newP {
found := me.machine.Packages.FindByName(pkg)
if found == nil {
log.Info("adding new", pkg, version)
addNew(pkg, version)
newCounter += 1
} else {
found.Version = version
if me.machine.Packages.Update(found) {
changeCounter += 1
}
}
}
footer := fmt.Sprintf("%s has distro %s with %d packages installed", me.hostname, me.distro, me.machine.Packages.Len())
if changeCounter != 0 {
footer += fmt.Sprintf(" (%d changed)", changeCounter)
}
if newCounter != 0 {
footer += fmt.Sprintf(" (%d new)", newCounter)
}
return footer
}

View File

@ -1,72 +0,0 @@
package main
import (
"bufio"
"fmt"
"os/exec"
"strings"
"go.wit.com/log"
)
// getPackageList returns the list of installed packages based on the distro
func getPackageList(distro string) (map[string]string, error) {
var cmd *exec.Cmd
// Run the appropriate command based on the detected distribution
switch distro {
case "ubuntu", "debian":
return dpkgQuery()
case "fedora", "centos", "rhel":
cmd = exec.Command("rpm", "-qa")
case "arch", "manjaro":
cmd = exec.Command("pacman", "-Q")
default:
return nil, fmt.Errorf("unsupported distribution: %s", distro)
}
// Capture the command's output
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error running command: %v", err)
}
// todo: Split the output into lines and return
lines := strings.Split(string(output), "\n")
log.Info("output had", len(lines), "lines")
return nil, nil
}
func dpkgQuery() (map[string]string, error) {
// Run the dpkg-query command to list installed packages and versions
cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
// Start the command execution
if err := cmd.Start(); err != nil {
return nil, err
}
defer cmd.Wait()
// Create a map to store package names and versions
installedPackages := make(map[string]string)
// Use a scanner to read the output of the command line by line
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
// Split each line into package name and version
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 {
packageName := parts[0]
version := parts[1]
installedPackages[packageName] = version
}
}
// Return the map with package names and versions
return installedPackages, scanner.Err()
}

View File

@ -1,56 +0,0 @@
// Copyright 2024 WIT.COM Inc.
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
)
func initDistro() string {
switch runtime.GOOS {
case "windows":
return "windows"
case "macos":
return "macos"
case "linux":
// Detect the Linux distribution
distro := detectDistro()
if distro == "" {
fmt.Println("Unable to detect Linux distribution.")
distro = "fixme"
}
fmt.Printf("Detected distribution: %s\n", distro)
return distro
default:
return runtime.GOOS
}
}
// detectDistro returns the Linux distribution name (if possible)
func detectDistro() string {
// Check if we're on Linux
// Try to read /etc/os-release to determine the distro
file, err := os.Open("/etc/os-release")
if err != nil {
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "ID=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
return strings.Trim(parts[1], `"`)
}
}
}
return ""
}

14
main.go
View File

@ -8,7 +8,6 @@ import (
"time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
@ -35,21 +34,10 @@ func main() {
me = new(stuff)
me.urlbase = "http://zookeeper.grid.wit.com:8080"
me.hostname, _ = os.Hostname()
me.pollDelay = 3 * time.Second
me.failcountmax = 20 // die every minute if zookeeper can't be found
// what OS?
me.distro = initDistro()
// init my machine protobuf
me.machine = new(zoopb.Machine)
me.machine.Packages = new(zoopb.Packages)
me.machine.Hostname = me.hostname
// init the installed package list
// me.packages = new(zoopb.Packages)
initPackages()
me.machine.ConfigLoad()
go NewWatchdog()

View File

@ -12,7 +12,7 @@ import (
func pingStatus() error {
var url string
url = me.urlbase + "/status?hostname=" + me.hostname
url = me.urlbase + "/status?hostname=" + me.machine.Hostname
msg, err := me.machine.Packages.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
@ -68,7 +68,7 @@ func sendMachine(s string) error {
os.Exit(0)
} else {
log.Info(me.urlbase, "is maybe not working GOT:", line)
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.hostname)
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname)
}
}
return nil

View File

@ -10,13 +10,10 @@ var me *stuff
// this app's variables
type stuff struct {
hostname string // my hostname to send to zookeeper
urlbase string // the dns name for the zookeeper
pollDelay time.Duration // how often to report our status
dog *time.Ticker // the watchdog timer
distro string // debian,redhat,gentoo,macos,wincrap
machine *zoopb.Machine // my protobuf
machine zoopb.Machine // populated from protobuf based zoopb
failcount int // how many times we've failed to contact the zookeeper
failcountmax int // after this, exit and let systemd restart the daemon
// packages *zoopb.Packages // installed packages and versions
}

View File

@ -31,7 +31,7 @@ func NewWatchdog() {
return
case _ = <-me.dog.C:
// log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
s := updatePackages()
s := me.machine.UpdatePackages()
// pingStatus()
me.failcount += 1
sendMachine(s)