Compare commits

..

32 Commits

Author SHA1 Message Date
Jeff Carr 020dd37622 redo init() and config() 2025-09-22 16:58:31 -05:00
Jeff Carr 9e49784b9e print out a machine PB 2025-09-22 01:51:12 -05:00
Jeff Carr d656828a0f used this to fix autogenpb 'http' functions 2025-09-09 19:15:31 -05:00
Jeff Carr 1c479b61f6 fix FindVersion() 2025-03-23 11:43:55 -05:00
Jeff Carr 98ba8076ae add string 2025-03-23 09:32:50 -05:00
Jeff Carr 695ff26e82 stub in a upgrade string line 2025-03-23 09:32:50 -05:00
Jeff Carr 8ecc16f938 shortcut for last update time 2025-03-22 09:45:49 -05:00
Jeff Carr f87da2b041 now using the awesome golang 1.24 'iter' 2025-03-19 06:40:36 -05:00
Jeff Carr b587843836 quiet some output 2025-03-06 07:54:56 -06:00
Jeff Carr b963653bb6 bool for triggering upgrades 2025-03-06 05:34:26 -06:00
Jeff Carr cefb5b161e rm code now made by autogenpb 2025-03-05 22:12:05 -06:00
Jeff Carr 9ab1afbe14 code moved to autogenpb 2025-03-05 13:33:27 -06:00
Jeff Carr eea1747f44 next step, fix autogenpb 2025-03-05 12:42:50 -06:00
Jeff Carr 96b4c0b5a1 hand write table buttons for testing 2025-03-05 12:42:50 -06:00
Jeff Carr bf833ed92d need a uuid per machine 2025-02-22 14:33:26 -06:00
Jeff Carr 08400c8913 seperate package from forge 2025-02-22 07:17:57 -06:00
Jeff Carr 8f6f85e51c switch to autogenpb gui code 2025-02-20 09:39:58 -06:00
Jeff Carr 705800fb23 add int and time funcs 2025-02-20 03:23:14 -06:00
Jeff Carr a18ed55eaa string func worked 2025-02-20 03:23:14 -06:00
Jeff Carr 032d1b609a table week 1 2025-02-19 17:39:45 -06:00
Jeff Carr 8ba9c858f4 table v1 2025-02-19 17:39:45 -06:00
Jeff Carr d836e5ccbf string rows 2025-02-19 17:39:45 -06:00
Jeff Carr 78cc971daa table tests 2025-02-19 17:39:45 -06:00
Jeff Carr df91ac2cbd hack until I make a http.proto file 2025-02-17 13:31:57 -06:00
Jeff Carr 9c17d8b92f rm print to stdout 2025-02-15 12:21:39 -06:00
Jeff Carr 58790a760d minor 2025-02-15 12:21:37 -06:00
Jeff Carr 088a2f6515 memory and cpus 2025-02-15 07:28:49 -06:00
Jeff Carr 201ec576bc start something to make a Table() 2025-02-10 23:44:03 -06:00
Jeff Carr 2fc78f1c2a changes for autogenpb 2025-02-08 19:01:16 -06:00
Jeff Carr 812fd3eb50 old test code 2025-02-07 19:11:00 -06:00
Jeff Carr d8a2e56b88 updated proto files to conform with autogenpb 2025-02-07 18:23:05 -06:00
Jeff Carr 54f4c4be3d quiet output 2025-01-08 04:08:43 -06:00
12 changed files with 257 additions and 201 deletions

View File

@ -5,7 +5,7 @@
# go install
all: package.pb.go machine.pb.go vet
all: package.pb.go machine.pb.go goimports vet
vet:
@GO111MODULE=off go vet

View File

@ -1 +1,6 @@
protobuf definition files for zookeeper
* experimental. at this point, it's just stubbed in
* zookeeper is similar to the apache zookeeper project
* you can use it to maintain the systems in your cluster
* zookeeper works with virtigo to maintain your private cloud

42
apt.go
View File

@ -2,8 +2,9 @@ package zoopb
import (
"fmt"
"runtime"
"go.wit.com/log"
"golang.org/x/sys/unix"
)
// init the installed package list
@ -24,17 +25,40 @@ func (me *Machine) initPackages() {
new1 := new(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")
}
}
me.Packages.Append(new1)
// log.Info("added", new1.Name, "failed")
}
func (me *Machine) addNew(name string, version string) bool {
me.getMemory()
}
func (me *Machine) addNew(name string, version string) {
new1 := new(Package)
new1.Name = name
new1.Version = version
return me.Packages.Append(new1)
me.Packages.Append(new1)
}
// simple memory and cpu count
func (me *Machine) getMemory() {
// 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)
}

View File

@ -71,11 +71,13 @@ func dpkgQuery() (map[string]string, error) {
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)
@ -94,7 +96,7 @@ func (me *Machine) UpdatePackages() string {
newCounter += 1
} else {
found.Version = version
panic("redo this. broken after autogenpb. was never right anyway")
// panic("redo this. broken after autogenpb. was never right anyway")
//if me.Packages.Update(found) {
// changeCounter += 1
//}
@ -110,4 +112,3 @@ func (me *Machine) UpdatePackages() string {
}
return footer
}
*/

157
config.go
View File

@ -4,157 +4,20 @@ package zoopb
// data to and from config files
import (
"errors"
"os"
"path/filepath"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
"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() error {
data, err := m.Marshal()
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("proto.Marshal() failed len", len(data), err)
return err
// log.Info("zoopb.ConfigLoad() failed", err, fullname)
}
log.Info("proto.Marshal() worked len", len(data))
configWrite(data)
return nil
}
// when running on a single machine, save the file in forge/
// as <hostname>.pb
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
func (m *Machine) ConfigSave() error {
if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_HOME", fullpath)
}
data, err := m.Marshal()
if err != nil {
log.Info("proto.Marshal() failed len", len(data), err)
return err
}
log.Info("ConfigSave() proto.Marshal() worked len", len(data))
hostname, _ := os.Hostname()
fname := hostname + ".pb"
return m.configWrite(fname, data)
}
func ConfigSaveRaw(data []byte) error {
configWrite(data)
return nil
}
func (m *Machines) ConfigLoad() error {
if m == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil ?")
}
if data, err := loadFile("zookeeper.pb"); err == nil {
if err = proto.Unmarshal(data, m); err != nil {
log.Warn("broken zookeeper.pb config file")
return err
}
} else {
return err
}
return nil
}
func (m *Machine) ConfigLoad() error {
if m == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil ?")
}
if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_HOME", fullpath)
}
hostname, _ := os.Hostname()
fname := hostname + ".pb"
var data []byte
var err error
if data, err = loadFile(fname); err != nil {
// something went wrong loading the file
return err
}
if data != nil {
if err = proto.Unmarshal(data, m); err != nil {
log.Warn("broken zookeeper.pb config file", fname)
return err
}
return nil
}
m.Hostname = hostname
m.Distro = detectDistro()
m.initPackages()
log.Info("zoopb.ConfigLoad()", m.Hostname, "runs", m.Distro, "with", m.Packages.Len(), "packages")
return nil
}
func loadFile(filename string) ([]byte, error) {
homeDir, err := os.UserHomeDir()
p := filepath.Join(homeDir, ".config/zookeeper")
fullname := filepath.Join(p, filename)
data, err := os.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {
// if file does not exist, just return nil. this
// will cause ConfigLoad() to try the next config file like "forge.text"
// because the user might want to edit the .config by hand
return nil, nil
}
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
return data, nil
}
func (m *Machine) loadFile(fname string) ([]byte, error) {
fullname := filepath.Join(os.Getenv("FORGE_HOME"), fname)
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
return data, nil
}
func configWrite(data []byte) error {
homeDir, err := os.UserHomeDir()
p := filepath.Join(homeDir, ".config/zookeeper")
fname := filepath.Join(p, "zookeeper.pb")
cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer cfgfile.Close()
if err != nil {
log.Warn("open config file :", err)
return err
}
cfgfile.Write(data)
return nil
}
func (m *Machine) configWrite(fname string, data []byte) error {
fullname := filepath.Join(os.Getenv("FORGE_HOME"), fname)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer cfgfile.Close()
if err != nil {
log.Warn("open config file :", err)
return err
}
cfgfile.Write(data)
return nil
fullname, err = config.LoadPB(m, "zookeeper", "machines")
return fullname, err
}

75
http.go Normal file
View File

@ -0,0 +1,75 @@
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0
package zoopb
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os/user"
"go.wit.com/log"
)
func (m *Machine) HttpPostMachine(url string) ([]byte, error) {
if m == nil {
// run f.InitMachine() here?
log.Info("you must run f.InitMachine()")
return nil, fmt.Errorf("you must run f.InitMachine()")
}
if m.Hostname == "" {
log.Info("WTF. hostname is blank")
} else {
// log.Info("GOOD. hostname is set to", m.Hostname)
}
// log.Info("GOOD2. hostname is set to", m.Hostname)
msg, err := m.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return nil, err
}
// log.Info("GOOD3. hostname is set to", m.Hostname)
check := new(Machine)
check.Unmarshal(msg)
if check == nil {
log.Info("WTF. check == nil")
}
// log.Info("good? check.hostname =", m.Hostname)
return m.HttpPostOld(url, msg)
}
func (m *Machine) HttpPostOld(url string, data []byte) ([]byte, error) {
var err error
var req *http.Request
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
// log.Info("httpPost() with len", len(data), "url", url)
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
// if f.Machine == nil {
// // run f.InitMachine() here?
// log.Info("you must run f.InitMachine()")
// return nil, fmt.Errorf("you must run f.InitMachine()")
// }
req.Header.Set("hostname", m.Hostname)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error(err)
return []byte("client.Do(req) error"), err
}
defer resp.Body.Close()
// log.Info("httpPost() with len", len(data))
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return body, err
}
return body, nil
}

49
init.go Normal file
View File

@ -0,0 +1,49 @@
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.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
}

16
log.go Normal file
View File

@ -0,0 +1,16 @@
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")
}

View File

@ -7,8 +7,8 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time
// global settings for autogenpb `autogenpb:mutex`
message Machine { // `autogenpb:marshal`
string hostname = 1; // `autogenpb:unique`
message Machine { // `autogenpb:marshal` `autogenpb:http`
string hostname = 1; // `autogenpb:unique` `autogenpb:sort`
int64 memory = 2;
int64 cpus = 3;
string distro = 4;
@ -17,10 +17,14 @@ message Machine { // `autogenpb:marshal`
Packages installed = 7; // packages that are installed
Packages available = 8; // packages that are available
Packages wit = 9; // packages that are available from mirrors.wit.com
string userAgent = 10;
string uuid = 11;
bool upgrade = 12; // upgrade is needed
string upgradeCmd = 13; // upgrade cmd
}
message Machines { // `autogenpb:marshal`
string uuid = 1; // I guess why not just have this on each file
string version = 2; // maybe can be used for protobuf schema change violations
message Machines { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:http`
string uuid = 1; // `autogenpb:uuid:b57e7fac-a8fc-4949-9d50-fa38312dec87`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Machine machines = 3;
}

View File

@ -8,7 +8,7 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time
// global settings for autogenpb `autogenpb:mutex`
message Package {
string name = 1; // `autogenpb:unique` // name: zookeeper-go
string name = 1; // `autogenpb:unique` `autogenpb:sort`
string version = 2; // version: 0.0.3
google.protobuf.Timestamp laststamp = 3; // the last time this package was seen (used to timeout entries)
string srcPath = 4; // path to the sources (go.wit.com/apps/zookeeper)
@ -17,7 +17,7 @@ message Package {
}
message Packages { // `autogenpb:marshal`
string uuid = 1; // I guess why not just have this on each file
string version = 2; // maybe can be used for protobuf schema change violations
string uuid = 1; // `autogenpb:uuid:2f26cc03-ea30-4481-a333-ad0acc86e1d3`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Package packages = 3;
}

13
print.go Normal file
View File

@ -0,0 +1,13 @@
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)
}

42
wit.go
View File

@ -6,14 +6,8 @@ import (
"strings"
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
func (m *Machine) IsInstalled(name string) bool {
loop := m.Packages.SortByName()
for loop.Scan() {
p := loop.Next()
for p := range m.Packages.IterByName() {
if name == p.Name {
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
return true
@ -25,9 +19,13 @@ func (m *Machine) IsInstalled(name string) bool {
}
func (m *Machine) FindInstalledByName(name string) *Package {
loop := m.Packages.SortByName()
for loop.Scan() {
p := loop.Next()
if m == nil {
panic("m == nil")
}
if m.Packages == nil {
panic("m.Packages == nil")
}
for p := range m.Packages.IterByName() {
if name == p.Name {
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
return p
@ -40,11 +38,21 @@ func (m *Machine) FindInstalledByName(name string) *Package {
// looks to see if any package matches a name and version
// if version == "", return the first name found
func (m *Machine) FindVersion(name string, version string) *Package {
func (m *Machine) FindVersion(name string) string {
// first check all installed versions
loop := m.Packages.SortByName()
for loop.Scan() {
p := loop.Next()
for p := range m.Packages.IterByName() {
if name == p.Name {
return p.Version
}
}
return ""
}
// looks to see if any package matches a name and version
// if version == "", return the first name found
func (m *Machine) FindByVersion(name string, version string) *Package {
// first check all installed versions
for p := range m.Packages.IterByName() {
if name == p.Name {
if version == "" {
return p
@ -57,9 +65,7 @@ func (m *Machine) FindVersion(name string, version string) *Package {
}
// check all wit packages
loop = m.Wit.SortByName()
for loop.Scan() {
p := loop.Next()
for p := range m.Wit.IterByName() {
if name == p.Name {
if version == "" {
return p
@ -77,7 +83,7 @@ func (m *Machine) FindVersion(name string, version string) *Package {
// read the package list file from mirrors.wit.com
// obviously a hack at this point
func (m *Machine) InitWit() error {
func (m *Machine) InitWitMirrors() error {
if m.Wit == nil {
m.Wit = new(Packages)
}