Compare commits
No commits in common. "master" and "v0.0.26" have entirely different histories.
6
apt.go
6
apt.go
|
@ -53,12 +53,12 @@ func (me *Machine) getMemory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert memory from bytes to GB
|
// Convert memory from bytes to GB
|
||||||
|
totalMemGB := float64(sysInfo.Totalram) * float64(sysInfo.Unit) / (1024 * 1024 * 1024)
|
||||||
m := float64(sysInfo.Totalram) * float64(sysInfo.Unit)
|
m := float64(sysInfo.Totalram) * float64(sysInfo.Unit)
|
||||||
me.Memory = int64(m)
|
me.Memory = int64(m)
|
||||||
me.Cpus = int64(numCPUs)
|
me.Cpus = int64(numCPUs)
|
||||||
|
|
||||||
// totalMemGB := float64(sysInfo.Totalram) * float64(sysInfo.Unit) / (1024 * 1024 * 1024)
|
|
||||||
// Print results
|
// Print results
|
||||||
// fmt.Printf("Total Memory: %.2f GB\n", totalMemGB)
|
fmt.Printf("Total Memory: %.2f GB\n", totalMemGB)
|
||||||
// fmt.Printf("Number of CPUs: %d\n", numCPUs)
|
fmt.Printf("Number of CPUs: %d\n", numCPUs)
|
||||||
}
|
}
|
||||||
|
|
155
config.go
155
config.go
|
@ -4,20 +4,157 @@ package zoopb
|
||||||
// data to and from config files
|
// data to and from config files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.wit.com/lib/config"
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"go.wit.com/log"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// writes out the cluster information it seperate files
|
// writes out the cluster information it seperate files
|
||||||
// to make it humanly possible to hand edit things as needed
|
// to make it humanly possible to hand edit things as needed
|
||||||
func (m *Machines) ConfigSave(fullname string) error {
|
func (m *Machines) ConfigSave() error {
|
||||||
return config.SavePB(m, fullname)
|
data, err := m.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
log.Info("proto.Marshal() failed len", len(data), err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Info("proto.Marshal() worked len", len(data))
|
||||||
|
configWrite(data)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machines) ConfigLoad() (string, error) {
|
// when running on a single machine, save the file in forge/
|
||||||
fullname, err := config.LoadPB(m, "/var/lib", "machines")
|
// as <hostname>.pb
|
||||||
if err != nil {
|
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
|
||||||
// log.Info("zoopb.ConfigLoad() failed", err, fullname)
|
func (m *Machine) ConfigSave() error {
|
||||||
|
if os.Getenv("FORGE_HOME") == "" {
|
||||||
|
homeDir, _ := os.UserHomeDir()
|
||||||
|
fullpath := filepath.Join(homeDir, ".config/forge")
|
||||||
|
os.Setenv("FORGE_HOME", fullpath)
|
||||||
}
|
}
|
||||||
fullname, err = config.LoadPB(m, "zookeeper", "machines")
|
data, err := m.Marshal()
|
||||||
return fullname, err
|
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.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
|
||||||
}
|
}
|
||||||
|
|
75
http.go
75
http.go
|
@ -1,75 +0,0 @@
|
||||||
// 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
49
init.go
|
@ -1,49 +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.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
|
|
||||||
}
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package zoopb
|
||||||
|
|
||||||
|
/*
|
||||||
|
type RepoTable struct {
|
||||||
|
n *gui.Node
|
||||||
|
x *Repos
|
||||||
|
FullPath *repoTableFullPath
|
||||||
|
columns []*gui.NodeColumn
|
||||||
|
order []*gui.NodeColumn
|
||||||
|
}
|
||||||
|
|
||||||
|
type RepoTableRow struct {
|
||||||
|
nodes []*gui.Node
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type repoTableFullPath struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *repoTableFullPath) Add(s string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *RepoTable) FullPath() *RepoTableFullPath {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Repos) NewTable() *RepoTable {
|
||||||
|
t := new(RepoTable)
|
||||||
|
t.n = gui.NewTable()
|
||||||
|
return *RepoTable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *RepoTable) AddFullPath() {
|
||||||
|
x.order = append(x.order, x.n.AddStringFunc( f() string {
|
||||||
|
return t.x.getRepoFullPath(rx *RepoTableRow)
|
||||||
|
}))
|
||||||
|
return *RepoTableRow
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *RepoTable) Show() {
|
||||||
|
x.order.f()
|
||||||
|
ZZ
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *RepoTable) AddMasterVersion() {
|
||||||
|
|
||||||
|
all := x.x.SortByFullPath()
|
||||||
|
for all.Scan() {
|
||||||
|
repo := all.Next()
|
||||||
|
for x.order
|
||||||
|
|
||||||
|
|
||||||
|
return *RepoTableRow
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *RepoTable) NewColumn() *RepoRow {
|
||||||
|
t := new(RepoTableRow)
|
||||||
|
return *RepoTableRow
|
||||||
|
}
|
||||||
|
*/
|
|
@ -7,24 +7,20 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time
|
||||||
|
|
||||||
// global settings for autogenpb `autogenpb:mutex`
|
// global settings for autogenpb `autogenpb:mutex`
|
||||||
|
|
||||||
message Machine { // `autogenpb:marshal` `autogenpb:http`
|
message Machine { // `autogenpb:marshal`
|
||||||
string hostname = 1; // `autogenpb:unique` `autogenpb:sort`
|
string hostname = 1; // `autogenpb:unique` `autogenpb:sort`
|
||||||
int64 memory = 2;
|
int64 memory = 2;
|
||||||
int64 cpus = 3;
|
int64 cpus = 3;
|
||||||
string distro = 4;
|
string distro = 4;
|
||||||
Packages packages = 5;
|
Packages packages = 5;
|
||||||
google.protobuf.Timestamp laststamp = 6; // the last time we heard anything from this machine
|
google.protobuf.Timestamp laststamp = 6; // the last time we heard anything from this machine
|
||||||
Packages installed = 7; // packages that are installed
|
Packages installed = 7; // packages that are installed
|
||||||
Packages available = 8; // packages that are available
|
Packages available = 8; // packages that are available
|
||||||
Packages wit = 9; // packages that are available from mirrors.wit.com
|
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` `autogenpb:gui` `autogenpb:http`
|
message Machines { // `autogenpb:marshal`
|
||||||
string uuid = 1; // `autogenpb:uuid:b57e7fac-a8fc-4949-9d50-fa38312dec87`
|
string uuid = 1; // `autogenpb:uuid:b57e7fac-a8fc-4949-9d50-fa38312dec87`
|
||||||
string version = 2; // `autogenpb:version:v0.0.1`
|
string version = 2; // `autogenpb:version:v0.0.1`
|
||||||
repeated Machine machines = 3;
|
repeated Machine machines = 3;
|
||||||
}
|
}
|
||||||
|
|
13
print.go
13
print.go
|
@ -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)
|
|
||||||
}
|
|
42
wit.go
42
wit.go
|
@ -6,8 +6,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// sent via -ldflags
|
||||||
|
var VERSION string
|
||||||
|
var BUILDTIME string
|
||||||
|
|
||||||
func (m *Machine) IsInstalled(name string) bool {
|
func (m *Machine) IsInstalled(name string) bool {
|
||||||
for p := range m.Packages.IterByName() {
|
loop := m.Packages.SortByName()
|
||||||
|
for loop.Scan() {
|
||||||
|
p := loop.Next()
|
||||||
if name == p.Name {
|
if name == p.Name {
|
||||||
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
||||||
return true
|
return true
|
||||||
|
@ -19,13 +25,9 @@ func (m *Machine) IsInstalled(name string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) FindInstalledByName(name string) *Package {
|
func (m *Machine) FindInstalledByName(name string) *Package {
|
||||||
if m == nil {
|
loop := m.Packages.SortByName()
|
||||||
panic("m == nil")
|
for loop.Scan() {
|
||||||
}
|
p := loop.Next()
|
||||||
if m.Packages == nil {
|
|
||||||
panic("m.Packages == nil")
|
|
||||||
}
|
|
||||||
for p := range m.Packages.IterByName() {
|
|
||||||
if name == p.Name {
|
if name == p.Name {
|
||||||
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
||||||
return p
|
return p
|
||||||
|
@ -38,21 +40,11 @@ func (m *Machine) FindInstalledByName(name string) *Package {
|
||||||
|
|
||||||
// looks to see if any package matches a name and version
|
// looks to see if any package matches a name and version
|
||||||
// if version == "", return the first name found
|
// if version == "", return the first name found
|
||||||
func (m *Machine) FindVersion(name string) string {
|
func (m *Machine) FindVersion(name string, version string) *Package {
|
||||||
// first check all installed versions
|
// first check all installed versions
|
||||||
for p := range m.Packages.IterByName() {
|
loop := m.Packages.SortByName()
|
||||||
if name == p.Name {
|
for loop.Scan() {
|
||||||
return p.Version
|
p := loop.Next()
|
||||||
}
|
|
||||||
}
|
|
||||||
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 name == p.Name {
|
||||||
if version == "" {
|
if version == "" {
|
||||||
return p
|
return p
|
||||||
|
@ -65,7 +57,9 @@ func (m *Machine) FindByVersion(name string, version string) *Package {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check all wit packages
|
// check all wit packages
|
||||||
for p := range m.Wit.IterByName() {
|
loop = m.Wit.SortByName()
|
||||||
|
for loop.Scan() {
|
||||||
|
p := loop.Next()
|
||||||
if name == p.Name {
|
if name == p.Name {
|
||||||
if version == "" {
|
if version == "" {
|
||||||
return p
|
return p
|
||||||
|
@ -83,7 +77,7 @@ func (m *Machine) FindByVersion(name string, version string) *Package {
|
||||||
|
|
||||||
// read the package list file from mirrors.wit.com
|
// read the package list file from mirrors.wit.com
|
||||||
// obviously a hack at this point
|
// obviously a hack at this point
|
||||||
func (m *Machine) InitWitMirrors() error {
|
func (m *Machine) InitWit() error {
|
||||||
if m.Wit == nil {
|
if m.Wit == nil {
|
||||||
m.Wit = new(Packages)
|
m.Wit = new(Packages)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue