zookeeper/windowZood.go

162 lines
3.6 KiB
Go
Raw Permalink Normal View History

2025-03-05 07:46:05 -06:00
// 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"
2025-03-12 13:16:05 -05:00
"sync"
2025-03-05 07:46:05 -06:00
"time"
"go.wit.com/gui"
2025-03-06 04:36:07 -06:00
"go.wit.com/lib/gadgets"
2025-03-05 07:46:05 -06:00
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
2025-03-12 13:16:05 -05:00
type stdTableWin struct {
sync.Mutex
win *gadgets.GenericWindow // the machines gui window
box *gui.Node // the machines gui parent box widget
TB *zoopb.MachinesTable // the machines gui table buffer
version string // the current zood version
versionL *gui.Node // label widget to display the current zood version
update bool // if the window should be updated
2025-03-12 13:16:05 -05:00
}
func (w *stdTableWin) Toggle() {
if w == nil {
return
}
if w.win == nil {
return
}
w.win.Toggle()
}
func makeZoodWin() *stdTableWin {
zood := new(stdTableWin)
zood.win = gadgets.NewGenericWindow("zood daemon versions", "todo: add global controls here")
zood.win.Custom = func() {
2025-03-06 04:36:07 -06:00
log.Info("test delete window here")
}
2025-03-12 13:16:05 -05:00
grid := zood.win.Group.RawGrid()
2025-03-06 04:47:18 -06:00
grid.NewButton("save machines.pb", func() {
2025-03-06 04:36:07 -06:00
saveMachineState()
})
grid.NewButton("show active", func() {
2025-03-12 13:16:05 -05:00
zood.doMachinesUpgradeTable(me.machines)
2025-03-06 04:47:18 -06:00
})
grid.NewButton("refresh", func() {
refresh()
})
zood.versionL = grid.NewLabel("scan")
grid.NewButton("show out of date", func() {
found := zoopb.NewMachines()
all := me.machines.All()
for all.Scan() {
m := all.Next()
if findVersion(m, "zood") != me.zood.version {
found.Append(m)
}
}
zood.doMachinesUpgradeTable(found)
})
2025-03-06 04:47:18 -06:00
// make a box at the bottom of the window for the protobuf table
2025-03-12 13:16:05 -05:00
zood.box = zood.win.Bottom.Box().SetProgName("TBOX")
zood.doMachinesUpgradeTable(me.machines)
return zood
2025-03-06 04:36:07 -06:00
}
2025-03-12 13:16:05 -05:00
func (zood *stdTableWin) doMachinesUpgradeTable(pb *zoopb.Machines) {
zood.Lock()
defer zood.Unlock()
if zood.TB != nil {
zood.TB.Delete()
zood.TB = nil
}
2025-03-05 07:46:05 -06:00
2025-03-12 13:16:05 -05:00
/*
found := zoopb.NewMachines()
all := pb.SortByHostname()
for all.Scan() {
m := all.Next()
found.Append(m)
}
*/
// display the protobuf
2025-03-12 13:16:05 -05:00
zood.TB = AddMachinesPB(zood.box, pb)
f := func(m *zoopb.Machine) {
2025-03-06 07:50:01 -06:00
log.Info("Triggering machine", m.Hostname, "to upgrade zood")
2025-03-06 05:16:00 -06:00
m.Upgrade = true
}
2025-03-12 13:16:05 -05:00
zood.TB.Custom(f)
log.Info("table has uuid", zood.TB.GetUuid())
2025-03-05 07:46:05 -06:00
}
func AddMachinesPB(tbox *gui.Node, pb *zoopb.Machines) *zoopb.MachinesTable {
t := pb.NewTable("MachinesPB")
t.NewUuid()
t.SetParent(tbox)
upbut := t.AddButtonFunc("upgrade", func(m *zoopb.Machine) string {
if me.zood != nil {
mver := findVersion(m, "zood")
if mver == me.zood.version {
return ""
} else {
// log.Info("machine mismatch", m.Hostname, mver, me.zood.version)
}
}
2025-03-12 13:16:05 -05:00
// log.Info("machine =", m.Hostname)
2025-03-06 07:50:01 -06:00
return "now"
})
upbut.Custom = func(m *zoopb.Machine) {
log.Info("Triggering machine", m.Hostname, "to upgrade zood")
m.Upgrade = true
2025-03-06 07:50:01 -06:00
}
2025-03-05 07:46:05 -06:00
t.AddHostname()
t.AddMemory()
t.AddCpus()
t.AddStringFunc("sMB", func(m *zoopb.Machine) string {
return fmt.Sprintf("%d mb", m.Memory/(1024*1024))
})
2025-03-06 04:36:07 -06:00
2025-03-05 07:46:05 -06:00
t.AddStringFunc("zood", func(m *zoopb.Machine) string {
return findVersion(m, "zood")
})
2025-03-12 13:16:05 -05:00
delf := func(m *zoopb.Machine) string {
return "delete"
}
t.AddButtonFunc("delete", delf)
2025-03-06 04:36:07 -06:00
2025-03-06 07:50:01 -06:00
/*
// show if the machine needs to be upgraded
t.AddStringFunc("triggered?", func(m *zoopb.Machine) string {
if m.Upgrade {
return "yes"
}
return ""
})
*/
2025-03-06 05:16:00 -06:00
2025-03-05 07:46:05 -06:00
t.AddTimeFunc("age", func(m *zoopb.Machine) time.Time {
return m.Laststamp.AsTime()
})
t.ShowTable()
return t
}
2025-03-06 04:36:07 -06:00
func findVersion(m *zoopb.Machine, pkgname string) string {
zood := m.Packages.FindByName(pkgname)
if zood == nil {
return "n/a"
}
return zood.Version
}