summaryrefslogtreecommitdiff
path: root/windowDroplets.go
blob: e94fec21deb72b23e12f23a58bd74eaa24034c18 (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
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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"strings"
	"sync"
	"time"

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

type stdDropletTableWin struct {
	sync.Mutex
	win    *gadgets.GenericWindow // the machines gui window
	box    *gui.Node              // the machines gui parent box widget
	pb     *virtpb.Droplets       // the droplets protobuf
	TB     *virtpb.DropletsTable  // the gui table buffer
	update bool                   // if the window should be updated
}

func (w *stdDropletTableWin) Toggle() {
	if w == nil {
		return
	}
	if w.win == nil {
		return
	}
	w.win.Toggle()
}

func newDropletsWindow() *stdDropletTableWin {
	dwin := new(stdDropletTableWin)
	dwin.win = gadgets.NewGenericWindow("virtigo current droplets", "")
	dwin.win.Custom = func() {
		log.Info("test delete window here")
	}

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

/*
func makeWindownDropletsPB(pb *virtpb.Droplets) *stdDropletTableWin {
	dwin := new(stdDropletTableWin)
	dwin.win = gadgets.NewGenericWindow("virtigo current droplets", "")
	dwin.win.Custom = func() {
		log.Info("test delete window here")
	}

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

	dwin.doDropletsTable(pb)

	return dwin
}
*/

/*
func (dwin *stdDropletTableWin) doDropletsTable(currentDroplets *virtpb.Droplets) {
	dwin.Lock()
	defer dwin.Unlock()
	if dwin.TB != nil {
		dwin.TB.Delete()
		dwin.TB = nil
	}

	// display the protobuf
	dwin.TB = addDropletsPB(dwin.box, currentDroplets)
	f := func(e *virtpb.Droplet) {
		log.Info("Triggered. do something here", e.Hostname)
		// m.Enabled = true
	}
	dwin.TB.Custom(f)
}

func addDropletsPB(tbox *gui.Node, pb *virtpb.Droplets) *virtpb.DropletsTable {
	t := pb.NewTable("DropletsPB")
	t.NewUuid()
	t.SetParent(tbox)

	vp := t.AddButtonFunc("start", func(p *virtpb.Droplet) string {
		return "poweron"
	})
	vp.Custom = func(d *virtpb.Droplet) {
		log.Info("power on the droplet here:", d.Hostname)
	}
	t.AddHostname()
	t.AddMemory()
	t.AddCpus()
	t.ShowTable()
	return t
}
*/

// default window for active running droplets
func (dw *stdDropletTableWin) doInactiveDroplets(pb *virtpb.Droplets) {
	dw.Lock()
	defer dw.Unlock()

	// erase the old table
	if dw.TB != nil {
		dw.TB.Delete()
		dw.TB = nil
	}

	// init the table
	dw.pb = pb
	t := dw.pb.NewTable("DropletsPB Off")
	t.NewUuid()
	t.SetParent(dw.box)

	// pick the columns
	// t.AddHostname()
	dropon := t.AddButtonFunc("Start", func(d *virtpb.Droplet) string {
		return "poweron"
	})
	dropon.Custom = func(d *virtpb.Droplet) {
		log.Info("start droplet here", d.Hostname)
	}
	/*
		t.AddHostname()
		t.AddStringFunc("location", func(d *virtpb.Droplet) string {
			return d.Current.Hypervisor
		})
	*/
	vp := t.AddButtonFunc("Configure Hostname", func(p *virtpb.Droplet) string {
		return p.Hostname
	})
	vp.Custom = func(d *virtpb.Droplet) {
		log.Info("open config window", d.Hostname)
	}
	/*
		t.AddHostname()
		t.AddStringFunc("location", func(d *virtpb.Droplet) string {
			return d.Current.Hypervisor
		})
	*/
	t.AddMemory()
	t.AddCpus()

	// final setup and display the table
	dw.TB = t
	f := func(e *virtpb.Droplet) {
		log.Info("Triggered. do something here", e.Hostname)
		// m.Enabled = true
	}
	dw.TB.Custom(f)
	dw.TB.ShowTable()
}

// default window for active running droplets
func (dw *stdDropletTableWin) doActiveDroplets(pb *virtpb.Droplets) {
	dw.Lock()
	defer dw.Unlock()
	if dw.TB != nil {
		dw.TB.Delete()
		dw.TB = nil
	}

	dw.pb = pb

	t := dw.pb.NewTable("DropletsPB On")
	t.NewUuid()

	t.SetParent(dw.box)

	serial := t.AddButtonFunc("serial", func(p *virtpb.Droplet) string {
		return "ttyS0"
	})
	serial.Custom = func(d *virtpb.Droplet) {
		log.Printf("run %s: remmina spice://%s:%d\n", d.Hostname, argv.Server, d.SpicePort)
	}

	fb := t.AddButtonFunc("fb0 console", func(p *virtpb.Droplet) string {
		return "remmina"
	})
	fb.Custom = func(d *virtpb.Droplet) {
		log.Printf("run %s: remmina spice://%s:%d\n", d.Hostname, argv.Server, d.SpicePort)
	}

	t.AddHostname()
	t.AddStringFunc("location", func(d *virtpb.Droplet) string {
		return d.Current.Hypervisor
	})
	t.AddMemory()
	t.AddCpus()
	t.AddSpicePort()
	t.AddTimeFunc("age", func(d *virtpb.Droplet) time.Time {
		age := d.Current.OnSince.AsTime()
		log.Info("age", d.Hostname, virtpb.FormatDuration(time.Since(age)))
		return age
	})
	t.AddStringFunc("State", func(d *virtpb.Droplet) string {
		if d.Current.State == virtpb.DropletState_ON {
			return "ON"
		}
		if d.Current.State == virtpb.DropletState_OFF {
			return "OFF"
		}
		return "UNKNOWN"
	})
	t.AddStringFunc("mac addr", func(d *virtpb.Droplet) string {
		var macs []string
		for _, n := range d.Networks {
			macs = append(macs, n.Mac)
		}
		tmp := strings.Join(macs, "\n")
		return strings.TrimSpace(tmp)
	})
	t.ShowTable()

	// display the protobuf
	dw.TB = t
	f := func(e *virtpb.Droplet) {
		log.Info("Triggered. do something here", e.Hostname)
		// m.Enabled = true
	}
	dw.TB.Custom(f)
}