zookeeper/machine.go

77 lines
1.8 KiB
Go
Raw Normal View History

2024-11-15 21:51:25 -06:00
package main
import (
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
// someone sent machine 'u' is it new?
// if not, update the record of it
func updateMachine(u *zoopb.Machine) string {
if u == nil {
return "nil"
}
2024-12-04 02:25:46 -06:00
m := me.machines.FindByHostname(u.Hostname)
2024-11-15 21:51:25 -06:00
if m == nil {
log.Info("adding new machine", u.Hostname)
2024-11-15 21:51:25 -06:00
me.machines.Append(u)
return "new"
}
// log.Info("updating machine", m.Hostname)
2024-11-15 21:51:25 -06:00
// did the # of cpus change?
if m.Cpus != u.Cpus {
m.Cpus = u.Cpus
log.Info("cpus changed to", m.Cpus)
}
// did the memory change?
if m.Memory != u.Memory {
m.Memory = u.Memory
log.Info("memory changed to", m.Memory)
}
// init these if nil
if m.Packages == nil {
m.Packages = new(zoopb.Packages)
}
if u.Packages == nil {
u.Packages = new(zoopb.Packages)
}
2024-12-04 02:25:46 -06:00
if zood := m.Packages.FindByName("zood"); zood != nil {
log.Log(INFO, m.Hostname, "has zood version", zood.Version)
}
2024-11-15 21:51:25 -06:00
updatePackages(m, u.Packages)
2024-11-15 21:51:25 -06:00
return "upgrade"
}
// looks to see if any packages:
// changed versions
// were newly installed
// were uninstalled
func updatePackages(m *zoopb.Machine, newp *zoopb.Packages) bool {
2024-11-15 21:51:25 -06:00
var changed bool = false
loop := newp.SortByName()
for loop.Scan() {
2024-12-04 02:25:46 -06:00
p := loop.Next()
if p.Name == "zood" {
2024-12-04 02:25:46 -06:00
if pold := m.Packages.FindByName("zood"); pold == nil {
changed = true
log.Log(ZOOD, "updatePackages() new package", p.Name , "version", p.Version, "machine", m.Hostname)
m.Packages.Append(p)
} else {
if p.Version == pold.Version {
log.Log(ZOOD, "updatePackages() unchanged", p.Version, "machine", m.Hostname)
} else {
changed = true
log.Log(NOW, "updatePackages() package", p.Name , "version changed", pold.Version, "to", p.Version, "machine", m.Hostname)
pold.Version = p.Version
}
}
}
}
2024-11-15 21:51:25 -06:00
return changed
}