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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  | 
// 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
	outOfDate *gui.Node              // checkbox to only show out of date droplets
	showAll   *gui.Node              // show everything in the zoo
	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 {
	stdw := new(stdTableWin)
	stdw.win = gadgets.NewGenericWindow("zood daemon versions", "todo: add global controls here")
	stdw.win.Custom = func() {
		log.Info("test delete window here")
	}
	grid := stdw.win.Group.RawGrid()
	grid.NewButton("save machines.pb", func() {
		saveMachineState()
	})
	grid.NewButton("show active", func() {
		stdw.doMachinesUpgradeTable(me.machines)
	})
	grid.NewButton("refresh", func() {
		stdw.refresh()
	})
	stdw.versionL = grid.NewLabel("scan")
	stdw.outOfDate = grid.NewCheckbox("out of date")
	stdw.showAll = grid.NewCheckbox("all")
	grid.NewButton("upgrade 10", func() {
		sendUpgrade(10)
	})
	grid.NewButton("upgrade all", func() {
		sendUpgrade(-1)
	})
	// make a box at the bottom of the window for the protobuf table
	stdw.box = stdw.win.Bottom.Box().SetProgName("TBOX")
	stdw.doMachinesUpgradeTable(me.machines)
	return stdw
}
func sendUpgrade(i int) {
	var count int
	all := me.machines.All()
	for all.Scan() {
		m := all.Next()
		mtime := m.Laststamp.AsTime()
		if time.Since(mtime) > 10*time.Hour {
			continue
		}
		if m.FindVersion("zood") != me.zood.version {
			count += 1
			m.Upgrade = true
			log.Info("upgrade", m.Hostname, count)
		}
		if i == -1 {
			continue
		}
		if count > i {
			return
		}
	}
}
func (stdw *stdTableWin) refresh() {
	if stdw.outOfDate.Checked() {
		log.Info("refresh() showing out of date zoo")
		found := zoopb.NewMachines()
		all := me.machines.All()
		for all.Scan() {
			m := all.Next()
			if !stdw.showAll.Checked() {
				// skip non-active zoo members
				mtime := m.Laststamp.AsTime()
				if time.Since(mtime) > 10*time.Hour {
					continue
				}
			}
			if m.FindVersion("zood") != me.zood.version {
				found.Append(m)
			}
		}
		stdw.doMachinesUpgradeTable(found)
		return
	}
	if stdw.showAll.Checked() {
		log.Info("refresh() showing everything in zoo")
		stdw.doMachinesUpgradeTable(me.machines)
		return
	}
	log.Info("refresh() only show active zoo")
	found := zoopb.NewMachines()
	all := me.machines.All()
	for all.Scan() {
		m := all.Next()
		mtime := m.Laststamp.AsTime()
		// now := time.Now()
		if time.Since(mtime) > 10*time.Hour {
			continue
		}
		found.Append(m)
	}
	stdw.doMachinesUpgradeTable(found)
}
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 := m.FindVersion("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 {
		if m.Memory/(1024*1024) > 10000 {
			return fmt.Sprintf("%4d G", m.Memory/(1024*1024*1024))
		}
		return fmt.Sprintf("%4d M", m.Memory/(1024*1024))
	})
	t.AddStringFunc("zood", func(m *zoopb.Machine) string {
		return m.FindVersion("zood")
	})
	virtbut := t.AddButtonFunc("virtigod", func(m *zoopb.Machine) string {
		ver := m.FindVersion("virtigod")
		if ver == "n/a" {
			return ""
		}
		return ver
	})
	virtbut.Custom = func(m *zoopb.Machine) {
		log.Info("Triggering machine", m.Hostname, "to upgrade virtigod")
		m.Upgrade = true
		m.UpgradeCmd = "apt install virtigod"
	}
	forgebut := t.AddButtonFunc("forge", func(m *zoopb.Machine) string {
		ver := m.FindVersion("forge")
		if ver == "n/a" {
			return ""
		}
		return ver
	})
	forgebut.Custom = func(m *zoopb.Machine) {
		log.Info("Triggering machine", m.Hostname, "to upgrade forge")
		m.Upgrade = true
		m.UpgradeCmd = "apt install forge"
	}
	delf := func(m *zoopb.Machine) string {
		return "delete"
	}
	delbut := t.AddButtonFunc("delete", delf)
	delbut.Custom = func(m *zoopb.Machine) {
		log.Info("Need to delete the protobuf record here", m.Hostname)
	}
	/*
		// 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
}
  |