virtigo/windowDroplets.go

232 lines
5.4 KiB
Go
Raw Normal View History

2025-03-10 09:11:48 -05: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 (
2025-03-11 10:27:42 -05:00
"fmt"
2025-03-11 03:34:30 -05:00
"strings"
2025-03-10 09:11:48 -05:00
"sync"
2025-03-11 03:34:30 -05:00
"time"
2025-03-10 09:11:48 -05:00
"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
2025-03-11 03:34:30 -05:00
pb *virtpb.Droplets // the droplets protobuf
2025-03-10 09:11:48 -05:00
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()
}
2025-03-11 03:34:30 -05:00
func newDropletsWindow() *stdDropletTableWin {
2025-03-10 09:11:48 -05:00
dwin := new(stdDropletTableWin)
2025-03-11 03:34:30 -05:00
dwin.win = gadgets.NewGenericWindow("virtigo current droplets", "")
2025-03-10 09:11:48 -05:00
dwin.win.Custom = func() {
log.Info("test delete window here")
}
2025-03-11 03:34:30 -05:00
// make a box at the bottom of the window for the protobuf table
dwin.box = dwin.win.Bottom.Box().SetProgName("TBOX")
return dwin
}
/*
2025-03-11 03:34:30 -05:00
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")
}
2025-03-10 09:11:48 -05:00
// make a box at the bottom of the window for the protobuf table
dwin.box = dwin.win.Bottom.Box().SetProgName("TBOX")
2025-03-11 03:34:30 -05:00
dwin.doDropletsTable(pb)
2025-03-10 09:11:48 -05:00
return dwin
}
*/
2025-03-10 09:11:48 -05:00
/*
2025-03-10 09:11:48 -05:00
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
2025-03-11 03:34:30 -05:00
dwin.TB = addDropletsPB(dwin.box, currentDroplets)
2025-03-10 18:09:14 -05:00
f := func(e *virtpb.Droplet) {
2025-03-10 09:11:48 -05:00
log.Info("Triggered. do something here", e.Hostname)
// m.Enabled = true
}
dwin.TB.Custom(f)
}
2025-03-11 03:34:30 -05:00
func addDropletsPB(tbox *gui.Node, pb *virtpb.Droplets) *virtpb.DropletsTable {
2025-03-10 09:11:48 -05:00
t := pb.NewTable("DropletsPB")
t.NewUuid()
t.SetParent(tbox)
2025-03-11 03:34:30 -05:00
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)
}
2025-03-10 09:11:48 -05:00
t.AddHostname()
2025-03-11 03:34:30 -05:00
t.AddMemory()
t.AddCpus()
2025-03-10 09:11:48 -05:00
t.ShowTable()
return t
}
*/
2025-03-11 03:34:30 -05:00
// 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
})
*/
2025-03-11 03:34:30 -05:00
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) {
2025-03-11 08:03:54 -05:00
log.Printf("run %s: socat telnet somewhere %s:%d\n", d.Hostname, argv.Server, d.SpicePort)
log.Info("socat TCP-LISTEN:5000,reuseaddr,fork EXEC:\"virsh console myvm\"")
}
fb := t.AddButtonFunc("fb0 console", func(p *virtpb.Droplet) string {
return "remmina"
})
fb.Custom = func(d *virtpb.Droplet) {
2025-03-11 10:27:42 -05:00
log.Printf("connect to %s on %s: remmina spice://%s:%d\n", d.Hostname, d.Current.Hypervisor, argv.Server, 10000+d.SpicePort)
data, err := gusPost(fmt.Sprintf("%d", 10000+d.SpicePort), d.Current.Hypervisor)
log.Info("data", string(data), "err =", err)
}
2025-03-11 03:34:30 -05:00
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()
2025-03-11 08:03:54 -05:00
// log.Info("age", d.Hostname, virtpb.FormatDuration(time.Since(age)))
2025-03-11 03:34:30 -05:00
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)
}