diff options
| author | Jeff Carr <[email protected]> | 2025-10-07 09:01:54 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-07 09:01:54 -0500 |
| commit | d110888f81e357010b71a0a0286ba298fcd452c5 (patch) | |
| tree | 1905eb28f1da7217b6e7217713bf97dc71b0b90f | |
| parent | 920eb20a447e1b6c08f433ac1857263dc21da41d (diff) | |
moved to go.wit.com/lib/debian
| -rw-r--r-- | apt.go | 37 | ||||
| -rw-r--r-- | apt_darwin.go | 11 | ||||
| -rw-r--r-- | apt_linux.go | 114 | ||||
| -rw-r--r-- | apt_windows.go | 11 | ||||
| -rw-r--r-- | common.go | 15 | ||||
| -rw-r--r-- | config.go | 23 | ||||
| -rw-r--r-- | distro.go | 61 | ||||
| -rw-r--r-- | hw.go | 6 | ||||
| -rw-r--r-- | hw_darwin.go | 7 | ||||
| -rw-r--r-- | hw_linux.go | 32 | ||||
| -rw-r--r-- | hw_windows.go | 7 | ||||
| -rw-r--r-- | init.go | 50 | ||||
| -rw-r--r-- | log.go | 16 | ||||
| -rw-r--r-- | print.go | 13 | ||||
| -rw-r--r-- | wit.go | 129 |
15 files changed, 15 insertions, 517 deletions
@@ -1,37 +0,0 @@ -package zoopb - -import ( - "fmt" -) - -// init the installed package list -func (me *Machine) initPackages() { - // Get the list of installed packages for the detected distro - newP, err := getPackageList(me.Distro) - if err != nil { - fmt.Println("Error:", err) - return - } - - if me.Packages == nil { - me.Packages = new(Packages) - } - - // Print the installed packages and their versions - for pkg, version := range newP { - new1 := new(Package) - new1.Name = pkg - new1.Version = version - me.Packages.Append(new1) - // log.Info("added", new1.Name, "failed") - } - - me.getMemory() -} - -func (me *Machine) addNew(name string, version string) { - new1 := new(Package) - new1.Name = name - new1.Version = version - me.Packages.Append(new1) -} diff --git a/apt_darwin.go b/apt_darwin.go deleted file mode 100644 index 0fc8e0f..0000000 --- a/apt_darwin.go +++ /dev/null @@ -1,11 +0,0 @@ -package zoopb - -import ( - "go.wit.com/log" -) - -// getPackageList returns the list of installed packages based on the distro -func getPackageList(distro string) (map[string]string, error) { - log.Info("zoopb: have not done macos yet, skipping okay") - return nil, nil -} diff --git a/apt_linux.go b/apt_linux.go deleted file mode 100644 index 6c7391c..0000000 --- a/apt_linux.go +++ /dev/null @@ -1,114 +0,0 @@ -package zoopb - -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() -} - -/* -func (me *Machine) UpdatePackages() string { - log.Info("fixme. broken after move to autogenpb") - return "" -} -*/ - -func (me *Machine) 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.Packages.FindByName(pkg) - if found == nil { - log.Info("adding new", pkg, version) - me.addNew(pkg, version) - newCounter += 1 - } else { - found.Version = version - // panic("redo this. broken after autogenpb. was never right anyway") - //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) - } - return footer -} diff --git a/apt_windows.go b/apt_windows.go deleted file mode 100644 index feec104..0000000 --- a/apt_windows.go +++ /dev/null @@ -1,11 +0,0 @@ -package zoopb - -import ( - "go.wit.com/log" -) - -// getPackageList returns the list of installed packages based on the distro -func getPackageList(distro string) (map[string]string, error) { - log.Info("zoopb: have not done windows yet, skipping okay") - return nil, nil -} diff --git a/common.go b/common.go new file mode 100644 index 0000000..54bc045 --- /dev/null +++ b/common.go @@ -0,0 +1,15 @@ +// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0 + +package zoopb + +import "go.wit.com/lib/config" + +var pbfile string = "/var/cache/zookeeper/machine" + +func (pb *Machine) Save() error { + return config.SavePB(pb, pbfile) +} + +func (pb *Machine) Load() error { + return config.LoadFile(pb, pbfile) +} diff --git a/config.go b/config.go deleted file mode 100644 index 808722c..0000000 --- a/config.go +++ /dev/null @@ -1,23 +0,0 @@ -package zoopb - -// functions to import and export the protobuf -// data to and from config files - -import ( - "go.wit.com/lib/config" -) - -// writes out the cluster information it seperate files -// to make it humanly possible to hand edit things as needed -func (m *Machines) ConfigSave(fullname string) error { - return config.SavePB(m, fullname) -} - -func (m *Machines) ConfigLoad() (string, error) { - fullname, err := config.LoadPB(m, "/var/lib", "machines") - if err != nil { - // log.Info("zoopb.ConfigLoad() failed", err, fullname) - } - fullname, err = config.LoadPB(m, "zookeeper", "machines") - return fullname, err -} diff --git a/distro.go b/distro.go deleted file mode 100644 index bbbdda3..0000000 --- a/distro.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2024 WIT.COM Inc. - -package zoopb - -// simple stab at making a human readable distro name -// this is for displaying in a table in the zookeeper app -// it's just so you can easily see what machines in your grid are -// doing what - -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 "" -} @@ -1,6 +0,0 @@ -package zoopb - -// init the installed package list -func (me *Machine) getMemory() { - me.osGetMemory() -} diff --git a/hw_darwin.go b/hw_darwin.go deleted file mode 100644 index ab1aef8..0000000 --- a/hw_darwin.go +++ /dev/null @@ -1,7 +0,0 @@ -package zoopb - -// simple memory and cpu count -func (me *Machine) osGetMemory() { - me.Memory = int64(1024 * 1024) - me.Cpus = int64(1) -} diff --git a/hw_linux.go b/hw_linux.go deleted file mode 100644 index d3cb116..0000000 --- a/hw_linux.go +++ /dev/null @@ -1,32 +0,0 @@ -package zoopb - -import ( - "fmt" - "runtime" - - "golang.org/x/sys/unix" -) - -// simple memory and cpu count -func (me *Machine) osGetMemory() { - // Get number of CPUs - numCPUs := runtime.NumCPU() - - // Get total system memory - var sysInfo unix.Sysinfo_t - err := unix.Sysinfo(&sysInfo) - if err != nil { - fmt.Println("Error getting system info:", err) - return - } - - // Convert memory from bytes to GB - m := float64(sysInfo.Totalram) * float64(sysInfo.Unit) - me.Memory = int64(m) - me.Cpus = int64(numCPUs) - - // totalMemGB := float64(sysInfo.Totalram) * float64(sysInfo.Unit) / (1024 * 1024 * 1024) - // Print results - // fmt.Printf("Total Memory: %.2f GB\n", totalMemGB) - // fmt.Printf("Number of CPUs: %d\n", numCPUs) -} diff --git a/hw_windows.go b/hw_windows.go deleted file mode 100644 index ab1aef8..0000000 --- a/hw_windows.go +++ /dev/null @@ -1,7 +0,0 @@ -package zoopb - -// simple memory and cpu count -func (me *Machine) osGetMemory() { - me.Memory = int64(1024 * 1024) - me.Cpus = int64(1) -} diff --git a/init.go b/init.go deleted file mode 100644 index 2b89ff5..0000000 --- a/init.go +++ /dev/null @@ -1,50 +0,0 @@ -package zoopb - -import ( - "os" - "time" - - "go.wit.com/lib/config" - "go.wit.com/log" -) - -// sent via -ldflags -var VERSION string -var BUILDTIME string - -func (m *Machine) SinceLastUpdate() time.Duration { - age := m.Laststamp.AsTime() - return time.Since(age) -} - -func InitMachine() (*Machine, string) { - var fullname string - var err error - m := new(Machine) - if fullname, err = config.LoadPB(m, "forge", "machine"); err != nil { - log.Info("zoopb.ConfigLoad() failed", err) - } - hostname, _ := os.Hostname() - m.Hostname = hostname - m.Distro = detectDistro() - m.Packages = NewPackages() - m.initPackages() - - m.InitWitMirrors() - config.SavePB(m, fullname) - - return m, fullname -} - -func InitDaemon() (*Machine, string) { - var fullname string - var err error - machine := new(Machine) - if fullname, err = config.LoadPB(machine, "/etc/zookeeper", "machine"); err != nil { - log.Info("zoopb.ConfigLoad() failed", err) - } - machine.InitWitMirrors() - config.SavePB(machine, fullname) - - return machine, fullname -} @@ -1,16 +0,0 @@ -package zoopb - -import ( - "go.wit.com/log" -) - -var INFO *log.LogFlag -var WARN *log.LogFlag - -func init() { - full := "go.wit.com/lib/protobuf/zoopb" - short := "zoopb" - - INFO = log.NewFlag("INFO", false, full, short, "general forgepb things") - WARN = log.NewFlag("WARN", true, full, short, "zoopb warnings") -} diff --git a/print.go b/print.go deleted file mode 100644 index a5f0092..0000000 --- a/print.go +++ /dev/null @@ -1,13 +0,0 @@ -package zoopb - -import "go.wit.com/log" - -// init the installed package list -func (m *Machine) Dump() { - log.Infof("mem=%d cpus=%d\n", m.Memory, m.Cpus) - - // totalMemGB := float64(sysInfo.Totalram) * float64(sysInfo.Unit) / (1024 * 1024 * 1024) - // Print results - // fmt.Printf("Total Memory: %.2f GB\n", totalMemGB) - // fmt.Printf("Number of CPUs: %d\n", numCPUs) -} @@ -1,11 +1,7 @@ package zoopb import ( - "bufio" - "os" "strings" - - "go.wit.com/lib/fhelp" ) func (m *Machine) IsInstalled(name string) bool { @@ -113,128 +109,3 @@ func (m *Machine) FindByVersion(name string, version string) *Package { // log.Info("package not-installed:", name) return nil } - -// obviously a hack at this point -// read the package list file from mirrors.wit.com -func (m *Machine) InitWitMirrors() error { - m.Wit = NewPackages() - err := m.scanPackageListFile("/var/lib/apt/lists/mirrors.wit.com_wit_dists_sid_main_binary-amd64_Packages") - return err -} - -// adds the package if the name isn't there -// if it's newer, remove the old package and use the new one -func (all *Packages) AddIfNewer(p *Package) { - check := all.FindByName(p.Name) - if check == nil { - all.Append(p) - return - } - v1, _ := fhelp.NewDebVersion(check.Version) - v2, _ := fhelp.NewDebVersion(p.Version) - if v1.Equal(v2) { - // log.Info("do nothing", v1, v2) - return - } - if v1.LessThan(v2) { - // log.Info("removing", v1, "using", v2) - all.Delete(check) - all.Append(p) - } else { - // log.Info("keeping", v1, "got:", v2) - } -} - -// breaks up the apt list file into sections -// then sends each section to be processed -// and added to zoopb.Machine.Wit -func (m *Machine) scanPackageListFile(filename string) error { - file, err := os.Open(filename) - if err != nil { - return err - } - defer file.Close() - scanner := bufio.NewScanner(file) - var debInfo string - for scanner.Scan() { - line := scanner.Text() - if line == "" { - p := parsePackageInfo(debInfo) - m.Wit.AddIfNewer(p) - debInfo = "" - } - debInfo += line + "\n" - } - p := parsePackageInfo(debInfo) - m.Wit.Append(p) - return nil -} - -/* -func (m *Machine) addPackage(name string, version string, filename string) { - if name == "" { - return - } - // log.Info("addPackage:", name, version, filename) - var deb *DebPackage - var ok bool - deb, ok = allp[name] - if !ok { - deb = NewPackage() - deb.name.SetLabel(name) - allp[name] = deb - } - newversion := new(Version) - newversion.v = version - newversion.file = filename - deb.versions = append(deb.versions, newversion) -} -*/ - -// parses dpkg -s foo.deb -func parsePackageInfo(lines string) *Package { - var name string - var version string - var filename string - var gopath string - - for _, line := range strings.Split(lines, "\n") { - if line == "" { - continue - } - if strings.HasPrefix(line, " ") { - // these are usually Description: lines - continue - } - if strings.HasPrefix(line, "#") { - // skip comment lines. (probably doesn't happen in debian list files - continue - } - line = strings.TrimSpace(line) - parts := strings.Split(line, " ") - if len(parts) < 2 { - continue - } - if parts[0] == "Package:" { - name = parts[1] - } - if parts[0] == "Version:" { - version = parts[1] - } - if parts[0] == "Filename:" { - filename = strings.Join(parts[1:], " ") - } - if parts[0] == "GoPath:" { - gopath = parts[1] - } - } - - p := Package{ - Name: name, - Version: version, - PkgName: filename, - SrcPath: gopath, - } - - return &p -} |
