From 9e4045f35e7b037f60b0a89a4368b9c4f77aa48d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 15 Nov 2024 18:11:52 -0600 Subject: [PATCH] use zoopb protobufs --- apt.go | 49 ++++++++++++++-------------------- apt_linux.go | 74 +++++++++++++++++++++++++++++----------------------- argv.go | 3 ++- distro.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 57 +++++++++------------------------------- structs.go | 17 +++++++----- watchdog.go | 2 +- 7 files changed, 156 insertions(+), 114 deletions(-) create mode 100644 distro.go diff --git a/apt.go b/apt.go index 7369d62..bdac375 100644 --- a/apt.go +++ b/apt.go @@ -1,41 +1,32 @@ package main import ( - "bufio" - "os/exec" - "strings" + "fmt" + + "go.wit.com/lib/protobuf/zoopb" + "go.wit.com/log" ) -func getInstalledPackages() (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() +// 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 { - return nil, err + fmt.Println("Error:", err) + return } - // 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 + // Print the installed packages and their versions + for pkg, version := range newP { + new1 := new(zoopb.Package) + new1.Name = pkg + new1.Version = version + if me.packages.Append(new1) { + // log.Info("added", new1.Name, "ok") + } else { + log.Info("added", new1.Name, "failed") } } - // Return the map with package names and versions - return installedPackages, scanner.Err() + log.Info(me.hostname, "has distro", me.distro, "with", me.packages.Len(), "packages installed") } diff --git a/apt_linux.go b/apt_linux.go index 5d6eb16..7e534f0 100644 --- a/apt_linux.go +++ b/apt_linux.go @@ -3,47 +3,20 @@ package main import ( "bufio" "fmt" - "os" "os/exec" - "runtime" "strings" + + "go.wit.com/log" ) -// detectDistro returns the Linux distribution name (if possible) -func detectDistro() string { - // Check if we're on Linux - if runtime.GOOS != "linux" { - return "" - } - - // 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 "" -} - // getPackageList returns the list of installed packages based on the distro -func getPackageList(distro string) ([]string, error) { +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": - cmd = exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n") + return dpkgQuery() case "fedora", "centos", "rhel": cmd = exec.Command("rpm", "-qa") case "arch", "manjaro": @@ -58,7 +31,42 @@ func getPackageList(distro string) ([]string, error) { return nil, fmt.Errorf("error running command: %v", err) } - // Split the output into lines and return + // todo: Split the output into lines and return lines := strings.Split(string(output), "\n") - return lines, nil + 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() } diff --git a/argv.go b/argv.go index 9f570e4..9846438 100644 --- a/argv.go +++ b/argv.go @@ -12,7 +12,8 @@ import ( var argv args type args struct { - Port int `arg:"--port" default:"2521" help:"port to run on"` + Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` + Port int `arg:"--port" default:"2521" help:"port to run on"` } func (args) Version() string { diff --git a/distro.go b/distro.go new file mode 100644 index 0000000..e6852b9 --- /dev/null +++ b/distro.go @@ -0,0 +1,68 @@ +// Copyright 2016 The go-qemu Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +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 "" +} diff --git a/main.go b/main.go index 7734a58..db3e42a 100644 --- a/main.go +++ b/main.go @@ -16,11 +16,11 @@ package main import ( "embed" - "fmt" "os" "time" "go.wit.com/dev/alexflint/arg" + "go.wit.com/lib/protobuf/zoopb" "go.wit.com/log" ) @@ -39,53 +39,22 @@ func main() { os.Exit(0) } + if argv.Daemon { + // turn off timestamps for STDOUT (systemd adds them) + log.DaemonMode(true) + } + me = new(stuff) - me.Zookeeper = "zookeeper.wit.com" - me.Hostname, _ = os.Hostname() + me.zookeeper = "zookeeper.wit.com" + me.hostname, _ = os.Hostname() me.pollDelay = 3 * time.Second - log.DaemonMode(true) + // what OS? + me.distro = initDistro() - installedPackages, err := getInstalledPackages() - if err != nil { - fmt.Println("Error:", err) - return - } - - log.Info("debian has", len(installedPackages), "installed") - /* - // Print the installed packages and their versions - for pkg, version := range installedPackages { - fmt.Printf("%s: %s\n", pkg, version) - } - */ - - // Detect the Linux distribution - distro := detectDistro() - if distro == "" { - fmt.Println("Unable to detect Linux distribution.") - return - } - - fmt.Printf("Detected distribution: %s\n", distro) - - // Get the list of installed packages for the detected distro - packages, err := getPackageList(distro) - if err != nil { - fmt.Println("Error:", err) - return - } - - /* - // Print the installed packages - fmt.Println("Installed Packages:") - for _, pkg := range packages { - if pkg != "" { - fmt.Println(pkg) - } - } - */ - log.Info(distro, "has", len(packages), "installed") + // init the installed package list + me.packages = new(zoopb.Packages) + initPackages() go NewWatchdog() diff --git a/structs.go b/structs.go index a8d7492..a5b726f 100644 --- a/structs.go +++ b/structs.go @@ -1,14 +1,19 @@ package main -import "time" +import ( + "time" + + "go.wit.com/lib/protobuf/zoopb" +) 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 - dirs []string + 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 + distro string // debian,redhat,gentoo,macos,wincrap + packages *zoopb.Packages // installed packages and versions } diff --git a/watchdog.go b/watchdog.go index 38b1959..46235a9 100644 --- a/watchdog.go +++ b/watchdog.go @@ -31,7 +31,7 @@ func NewWatchdog() { fmt.Println("Done!") return case t := <-me.dog.C: - log.Info("Watchdog() ticked", me.Zookeeper, "Current time: ", t) + log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t) // h.pollHypervisor() // h.Scan() }