use zoopb protobufs
This commit is contained in:
parent
76db985cf1
commit
9e4045f35e
49
apt.go
49
apt.go
|
@ -1,41 +1,32 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"fmt"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getInstalledPackages() (map[string]string, error) {
|
// init the installed package list
|
||||||
// Run the dpkg-query command to list installed packages and versions
|
func initPackages() {
|
||||||
cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
|
// Get the list of installed packages for the detected distro
|
||||||
stdout, err := cmd.StdoutPipe()
|
newP, err := getPackageList(me.distro)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the command execution
|
// Print the installed packages and their versions
|
||||||
if err := cmd.Start(); err != nil {
|
for pkg, version := range newP {
|
||||||
return nil, err
|
new1 := new(zoopb.Package)
|
||||||
}
|
new1.Name = pkg
|
||||||
defer cmd.Wait()
|
new1.Version = version
|
||||||
|
if me.packages.Append(new1) {
|
||||||
// Create a map to store package names and versions
|
// log.Info("added", new1.Name, "ok")
|
||||||
installedPackages := make(map[string]string)
|
} else {
|
||||||
|
log.Info("added", new1.Name, "failed")
|
||||||
// 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
|
log.Info(me.hostname, "has distro", me.distro, "with", me.packages.Len(), "packages installed")
|
||||||
return installedPackages, scanner.Err()
|
|
||||||
}
|
}
|
||||||
|
|
74
apt_linux.go
74
apt_linux.go
|
@ -3,47 +3,20 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"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
|
// 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
|
var cmd *exec.Cmd
|
||||||
|
|
||||||
// Run the appropriate command based on the detected distribution
|
// Run the appropriate command based on the detected distribution
|
||||||
switch distro {
|
switch distro {
|
||||||
case "ubuntu", "debian":
|
case "ubuntu", "debian":
|
||||||
cmd = exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
|
return dpkgQuery()
|
||||||
case "fedora", "centos", "rhel":
|
case "fedora", "centos", "rhel":
|
||||||
cmd = exec.Command("rpm", "-qa")
|
cmd = exec.Command("rpm", "-qa")
|
||||||
case "arch", "manjaro":
|
case "arch", "manjaro":
|
||||||
|
@ -58,7 +31,42 @@ func getPackageList(distro string) ([]string, error) {
|
||||||
return nil, fmt.Errorf("error running command: %v", err)
|
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")
|
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()
|
||||||
}
|
}
|
||||||
|
|
1
argv.go
1
argv.go
|
@ -12,6 +12,7 @@ import (
|
||||||
var argv args
|
var argv args
|
||||||
|
|
||||||
type args struct {
|
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:"2521" help:"port to run on"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
57
main.go
|
@ -16,11 +16,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.wit.com/dev/alexflint/arg"
|
"go.wit.com/dev/alexflint/arg"
|
||||||
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -39,53 +39,22 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if argv.Daemon {
|
||||||
|
// turn off timestamps for STDOUT (systemd adds them)
|
||||||
|
log.DaemonMode(true)
|
||||||
|
}
|
||||||
|
|
||||||
me = new(stuff)
|
me = new(stuff)
|
||||||
me.Zookeeper = "zookeeper.wit.com"
|
me.zookeeper = "zookeeper.wit.com"
|
||||||
me.Hostname, _ = os.Hostname()
|
me.hostname, _ = os.Hostname()
|
||||||
me.pollDelay = 3 * time.Second
|
me.pollDelay = 3 * time.Second
|
||||||
|
|
||||||
log.DaemonMode(true)
|
// what OS?
|
||||||
|
me.distro = initDistro()
|
||||||
|
|
||||||
installedPackages, err := getInstalledPackages()
|
// init the installed package list
|
||||||
if err != nil {
|
me.packages = new(zoopb.Packages)
|
||||||
fmt.Println("Error:", err)
|
initPackages()
|
||||||
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")
|
|
||||||
|
|
||||||
go NewWatchdog()
|
go NewWatchdog()
|
||||||
|
|
||||||
|
|
13
structs.go
13
structs.go
|
@ -1,14 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
|
)
|
||||||
|
|
||||||
var me *stuff
|
var me *stuff
|
||||||
|
|
||||||
// this app's variables
|
// this app's variables
|
||||||
type stuff struct {
|
type stuff struct {
|
||||||
Hostname string // my hostname to send to zookeeper
|
hostname string // my hostname to send to zookeeper
|
||||||
Zookeeper string // the dns name for the zookeeper
|
zookeeper string // the dns name for the zookeeper
|
||||||
pollDelay time.Duration // how often to report our status
|
pollDelay time.Duration // how often to report our status
|
||||||
dog *time.Ticker // the watchdog timer
|
dog *time.Ticker // the watchdog timer
|
||||||
dirs []string
|
distro string // debian,redhat,gentoo,macos,wincrap
|
||||||
|
packages *zoopb.Packages // installed packages and versions
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func NewWatchdog() {
|
||||||
fmt.Println("Done!")
|
fmt.Println("Done!")
|
||||||
return
|
return
|
||||||
case t := <-me.dog.C:
|
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.pollHypervisor()
|
||||||
// h.Scan()
|
// h.Scan()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue