zookeeper/machine.go

161 lines
3.8 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"net/http"
"strings"
"time"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
func rawGetHostname(data []byte) *zoopb.Machine {
newm := new(zoopb.Machine)
newm.Unmarshal(data)
return newm
}
func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) {
hostname = strings.TrimSpace(hostname)
newm := rawGetHostname(data)
if newm == nil {
log.Info("unmarshal failed on data len =", len(data))
}
if hostname != newm.Hostname {
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
hostname = newm.Hostname
}
if hostname == "" {
ua := dumpUserAgent(r)
ra := dumpRemoteAddr(r)
log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname)
return
}
// log.Info("lookoing for", hostname)
m := me.machines.FindByHostname(hostname)
if m == nil {
am := new(zoopb.Machine)
am.Hostname = newm.Hostname
am.Memory = newm.Memory
me.machines.Append(newm)
log.Info("new machine", am.Hostname, am.Memory)
return
}
ua := dumpUserAgent(r)
ra := dumpRemoteAddr(r)
if m.UserAgent != ua {
log.Info("hostname ua changed len =", len(data), ra, hostname, ua)
m.UserAgent = ua
}
if m.Upgrade {
log.Info(m.Hostname, "was told to upgrade zood")
if m.UpgradeCmd == "" {
fmt.Fprintln(w, "apt update")
} else {
fmt.Fprintln(w, m.UpgradeCmd)
}
m.UpgradeCmd = ""
m.Upgrade = false
} else {
fmt.Fprintln(w, "good")
}
// log.Info("update machine protobuf", hostname)
updateMachine(newm)
}
// 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"
}
m := me.machines.FindByHostname(u.Hostname)
if m == nil {
log.Info("adding new machine", u.Hostname)
me.machines.Append(u)
if me.zood == nil {
// do nothing. window has not been opened
} else {
me.zood.doMachinesUpgradeTable(me.machines)
}
saveMachineState()
return "new"
}
// log.Info("updating machine", m.Hostname)
// 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)
}
if zood := m.Packages.FindByName("zood"); zood != nil {
log.Log(INFO, m.Hostname, "has zood version", zood.Version)
}
m.Laststamp = timestamppb.New(time.Now())
if updatePackages(m, u.Packages) {
// trigger save pb
}
return "upgrade"
}
// looks to see if any packages:
// changed versions
// were newly installed
// were uninstalled
func updatePackages(m *zoopb.Machine, newp *zoopb.Packages) bool {
var changed bool = false
loop := newp.SortByName()
for loop.Scan() {
p := loop.Next()
if p.Name == "zood" {
if updatePackageVersion(m, p) {
changed = true
}
}
if p.Name == "virtigod" {
if updatePackageVersion(m, p) {
changed = true
}
}
}
return changed
}
func updatePackageVersion(m *zoopb.Machine, pnew *zoopb.Package) bool {
pold := m.Packages.FindByName(pnew.Name)
if pold == nil {
log.Log(NOW, "updatePackages() new package", pnew.Name, "version", pnew.Version, "machine", m.Hostname)
m.Packages.Append(pnew)
return true
}
if pold.Version == pnew.Version {
log.Log(ZOOD, "updatePackages() unchanged", pold.Version, "machine", m.Hostname)
return false
}
log.Log(NOW, "updatePackages() package", pnew.Name, "version changed", pold.Version, "to", pnew.Version, "machine", m.Hostname)
pold.Version = pnew.Version
return true
}