// 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" "sync" "time" "go.wit.com/gui" "go.wit.com/lib/gadgets" "go.wit.com/lib/protobuf/zoopb" "go.wit.com/log" ) 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 } 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() { log.Info("test delete window here") } grid := zood.win.Group.RawGrid() grid.NewButton("save machines.pb", func() { saveMachineState() }) grid.NewButton("show active", func() { zood.doMachinesUpgradeTable(me.machines) }) 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) }) // make a box at the bottom of the window for the protobuf table zood.box = zood.win.Bottom.Box().SetProgName("TBOX") zood.doMachinesUpgradeTable(me.machines) return zood } func (zood *stdTableWin) doMachinesUpgradeTable(pb *zoopb.Machines) { zood.Lock() defer zood.Unlock() if zood.TB != nil { zood.TB.Delete() zood.TB = nil } /* found := zoopb.NewMachines() all := pb.SortByHostname() for all.Scan() { m := all.Next() found.Append(m) } */ // display the protobuf zood.TB = AddMachinesPB(zood.box, pb) f := func(m *zoopb.Machine) { log.Info("Triggering machine", m.Hostname, "to upgrade zood") m.Upgrade = true } zood.TB.Custom(f) log.Info("table has uuid", zood.TB.GetUuid()) } 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) } } // log.Info("machine =", m.Hostname) return "now" }) upbut.Custom = func(m *zoopb.Machine) { log.Info("Triggering machine", m.Hostname, "to upgrade zood") m.Upgrade = true } t.AddHostname() t.AddMemory() t.AddCpus() t.AddStringFunc("sMB", func(m *zoopb.Machine) string { return fmt.Sprintf("%d mb", m.Memory/(1024*1024)) }) t.AddStringFunc("zood", func(m *zoopb.Machine) string { return findVersion(m, "zood") }) delf := func(m *zoopb.Machine) string { return "delete" } t.AddButtonFunc("delete", delf) /* // show if the machine needs to be upgraded t.AddStringFunc("triggered?", func(m *zoopb.Machine) string { if m.Upgrade { return "yes" } return "" }) */ t.AddTimeFunc("age", func(m *zoopb.Machine) time.Time { return m.Laststamp.AsTime() }) t.ShowTable() return t } func findVersion(m *zoopb.Machine, pkgname string) string { zood := m.Packages.FindByName(pkgname) if zood == nil { return "n/a" } return zood.Version }