summaryrefslogtreecommitdiff
path: root/windowZood.go
blob: f62da2c5e6342b9da87d1c98ee9ffecbe7720a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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"
	"time"

	"go.wit.com/gui"
	"go.wit.com/lib/gadgets"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
)

func makeZoodWin() {
	me.zood = new(stdTableWin)
	me.zood.win = gadgets.NewGenericWindow("zood daemon versions", "todo: add global controls here")
	me.zood.win.Custom = func() {
		log.Info("test delete window here")
	}
	grid := me.zood.win.Group.RawGrid()
	grid.NewButton("save machines.pb", func() {
		saveMachineState()
	})
	grid.NewCheckbox("hide active")
	grid.NewButton("update", func() {
		doMachinesUpgradeTable()
	})

	// make a box at the bottom of the window for the protobuf table
	me.zood.box = me.zood.win.Bottom.Box().SetProgName("TBOX")
	doMachinesUpgradeTable()
}

func doMachinesUpgradeTable() {
	me.zood.Lock()
	defer me.zood.Unlock()
	if me.zood.TB != nil {
		me.zood.TB.Delete()
		me.zood.TB = nil
	}

	// display the protobuf
	me.zood.TB = AddMachinesPB(me.zood.box, me.machines)
	f := func(m *zoopb.Machine) {
		log.Info("upgrade machine", m.Hostname, "memory", m.Memory/(1024*1024*1024))
		log.Info("ADD THE CODE TO TRIGGER AN UPGRADE HERE")
		log.Info("ADD THE CODE TO TRIGGER AN UPGRADE HERE")
		m.Upgrade = true
	}
	me.zood.TB.Custom(f)
	log.Info("table has uuid", me.zood.TB.GetUuid())
}

func AddMachinesPB(tbox *gui.Node, pb *zoopb.Machines) *zoopb.MachinesTable {
	t := pb.NewTable("MachinesPB")
	t.NewUuid()
	t.SetParent(tbox)

	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")
	})

	// 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()
	})

	f := func(m *zoopb.Machine) string {
		log.Info("machine =", m.Hostname)
		return m.Hostname
	}
	t.AddButtonFunc("upgrade", f)
	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
}

func saveMachineState() {
	cur := zoopb.NewMachines()

	all := me.machines.SortByHostname()
	for all.Scan() {
		m := all.Next()
		log.Info("have machine:", m.Hostname)
		cur.Append(m)
	}
	cur.ConfigSave()
}