use zoopb protobufs

This commit is contained in:
Jeff Carr 2024-11-15 18:11:52 -06:00
parent 76db985cf1
commit 9e4045f35e
7 changed files with 156 additions and 114 deletions

49
apt.go
View File

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

View File

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

View File

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

68
distro.go Normal file
View File

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

57
main.go
View File

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

View File

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

View File

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