2022-05-05 04:47:35 +08:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/layout"
|
|
|
|
"fyne.io/fyne/v2/theme"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
2022-05-05 21:48:02 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
2022-05-05 04:47:35 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/functions"
|
|
|
|
"github.com/gravitl/netmaker/netclient/gui/components"
|
2022-05-05 21:48:02 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
2022-05-05 04:47:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetNetworksView - displays the view of all networks
|
|
|
|
func GetNetworksView(networks []string) fyne.CanvasObject {
|
|
|
|
// renders := []fyne.CanvasObject{}
|
2022-06-17 21:35:08 +08:00
|
|
|
if len(networks) == 0 {
|
2022-05-05 04:47:35 +08:00
|
|
|
return container.NewCenter(widget.NewLabel("No networks present"))
|
|
|
|
}
|
|
|
|
grid := container.New(layout.NewGridLayout(4),
|
|
|
|
container.NewCenter(widget.NewLabel("Network Name")),
|
|
|
|
container.NewCenter(widget.NewLabel("Node Info")),
|
|
|
|
container.NewCenter(widget.NewLabel("Pull Latest")),
|
|
|
|
container.NewCenter(widget.NewLabel("Leave network")),
|
|
|
|
)
|
|
|
|
for i := range networks {
|
|
|
|
network := &networks[i]
|
2022-06-17 21:35:08 +08:00
|
|
|
grid.Add(
|
2022-05-05 04:47:35 +08:00
|
|
|
container.NewCenter(widget.NewLabel(*network)),
|
|
|
|
)
|
2022-06-17 21:35:08 +08:00
|
|
|
grid.Add(
|
2022-05-05 04:47:35 +08:00
|
|
|
components.ColoredIconButton("info", theme.InfoIcon(), func() {
|
|
|
|
RefreshComponent(NetDetails, GetSingleNetworkView(*network))
|
|
|
|
ShowView(NetDetails)
|
|
|
|
}, components.Gold_color),
|
|
|
|
)
|
2022-06-17 21:35:08 +08:00
|
|
|
grid.Add(
|
2022-05-05 04:47:35 +08:00
|
|
|
components.ColoredIconButton("pull", theme.DownloadIcon(), func() {
|
|
|
|
// TODO call pull with network name
|
|
|
|
pull(*network)
|
|
|
|
}, components.Blue_color),
|
|
|
|
)
|
2022-06-17 21:35:08 +08:00
|
|
|
grid.Add(
|
2022-05-05 04:47:35 +08:00
|
|
|
components.ColoredIconButton("leave", theme.DeleteIcon(), func() {
|
|
|
|
leave(*network)
|
|
|
|
}, components.Danger_color),
|
|
|
|
)
|
|
|
|
// renders = append(renders, container.NewCenter(netToolbar))
|
|
|
|
}
|
|
|
|
|
|
|
|
return container.NewCenter(grid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSingleNetworkView - returns details and option to pull a network
|
|
|
|
func GetSingleNetworkView(network string) fyne.CanvasObject {
|
2022-06-17 21:35:08 +08:00
|
|
|
if len(network) == 0 {
|
2022-05-05 04:47:35 +08:00
|
|
|
return container.NewCenter(widget.NewLabel("No valid network selected"))
|
|
|
|
}
|
2022-05-05 21:48:02 +08:00
|
|
|
|
|
|
|
// == read node values ==
|
2022-05-07 03:05:26 +08:00
|
|
|
LoadingNotify()
|
|
|
|
nets, err := functions.List(network)
|
|
|
|
if err != nil || len(nets) < 1 {
|
2022-05-23 22:02:28 +08:00
|
|
|
ClearNotification()
|
2022-05-07 03:05:26 +08:00
|
|
|
return container.NewCenter(widget.NewLabel("No data retrieved."))
|
|
|
|
}
|
2022-05-05 21:48:02 +08:00
|
|
|
var nodecfg config.ClientConfig
|
|
|
|
nodecfg.Network = network
|
|
|
|
nodecfg.ReadConfig()
|
|
|
|
nodeID := nodecfg.Node.ID
|
|
|
|
lastCheckInTime := time.Unix(nodecfg.Node.LastCheckIn, 0)
|
|
|
|
lastCheckIn := lastCheckInTime.Format("2006-01-02 15:04:05")
|
|
|
|
privateAddr := nodecfg.Node.Address
|
|
|
|
privateAddr6 := nodecfg.Node.Address6
|
|
|
|
endpoint := nodecfg.Node.Endpoint
|
|
|
|
health := " (HEALTHY)"
|
2022-05-23 22:02:28 +08:00
|
|
|
if time.Now().After(lastCheckInTime.Add(time.Minute * 30)) {
|
2022-05-05 21:48:02 +08:00
|
|
|
health = " (ERROR)"
|
2022-05-23 22:02:28 +08:00
|
|
|
} else if time.Now().After(lastCheckInTime.Add(time.Minute * 5)) {
|
|
|
|
health = " (WARNING)"
|
2022-05-05 21:48:02 +08:00
|
|
|
}
|
|
|
|
lastCheckIn += health
|
|
|
|
version := nodecfg.Node.Version
|
2022-05-07 03:28:07 +08:00
|
|
|
|
2022-05-05 04:47:35 +08:00
|
|
|
pullBtn := components.ColoredButton("pull "+network, func() { pull(network) }, components.Blue_color)
|
|
|
|
pullBtn.Resize(fyne.NewSize(pullBtn.Size().Width, 50))
|
2022-05-07 03:28:07 +08:00
|
|
|
|
|
|
|
view := container.NewGridWithColumns(1, widget.NewRichTextFromMarkdown(fmt.Sprintf(`### %s
|
2022-05-05 04:47:35 +08:00
|
|
|
- ID: %s
|
2022-05-05 21:48:02 +08:00
|
|
|
- Last Check In: %s
|
2022-05-05 04:47:35 +08:00
|
|
|
- Endpoint: %s
|
|
|
|
- Address (IPv4): %s
|
|
|
|
- Address6 (IPv6): %s
|
2022-05-05 21:48:02 +08:00
|
|
|
- Version: %s
|
2022-05-07 03:05:26 +08:00
|
|
|
### Peers
|
2022-05-07 03:28:07 +08:00
|
|
|
`, network, nodeID, lastCheckIn, endpoint, privateAddr, privateAddr6, version)),
|
|
|
|
)
|
|
|
|
netDetailsView := container.NewCenter(
|
|
|
|
view,
|
|
|
|
)
|
|
|
|
|
|
|
|
peerView := container.NewVBox()
|
|
|
|
|
|
|
|
for _, p := range nets[0].Peers {
|
2022-05-07 03:42:15 +08:00
|
|
|
peerString := ""
|
|
|
|
endpointEntry := widget.NewEntry()
|
|
|
|
endpointEntry.Text = fmt.Sprintf("Endpoint: %s", p.PublicEndpoint)
|
2022-05-07 03:45:05 +08:00
|
|
|
endpointEntry.Disable()
|
2022-05-07 03:28:07 +08:00
|
|
|
newEntry := widget.NewEntry()
|
|
|
|
for i, addr := range p.Addresses {
|
|
|
|
if i > 0 && i < len(p.Addresses) {
|
|
|
|
peerString += ", "
|
|
|
|
}
|
2022-06-17 21:35:08 +08:00
|
|
|
peerString += addr.IP
|
2022-05-07 03:28:07 +08:00
|
|
|
}
|
|
|
|
newEntry.Text = peerString
|
2022-05-07 03:45:05 +08:00
|
|
|
newEntry.Disable()
|
2022-06-17 21:35:08 +08:00
|
|
|
peerView.Add(widget.NewLabel(fmt.Sprintf("Peer: %s", p.PublicKey)))
|
|
|
|
peerView.Add(container.NewVBox(container.NewVBox(endpointEntry), container.NewVBox(newEntry)))
|
2022-05-07 03:28:07 +08:00
|
|
|
}
|
|
|
|
peerScroller := container.NewVScroll(peerView)
|
2022-06-17 21:35:08 +08:00
|
|
|
view.Add(peerScroller)
|
|
|
|
view.Add(container.NewVBox(pullBtn))
|
2022-05-07 03:28:07 +08:00
|
|
|
netDetailsView.Refresh()
|
2022-05-05 04:47:35 +08:00
|
|
|
ClearNotification()
|
|
|
|
return netDetailsView
|
|
|
|
}
|
|
|
|
|
|
|
|
// == private ==
|
|
|
|
func pull(network string) {
|
|
|
|
LoadingNotify()
|
|
|
|
_, err := functions.Pull(network, true)
|
|
|
|
if err != nil {
|
|
|
|
ErrorNotify("Failed to pull " + network + " : " + err.Error())
|
|
|
|
} else {
|
|
|
|
SuccessNotify("Pulled " + network + "!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func leave(network string) {
|
|
|
|
|
|
|
|
confirmView := GetConfirmation("Confirm leaving "+network+"?", func() {
|
|
|
|
ShowView(Networks)
|
|
|
|
}, func() {
|
|
|
|
LoadingNotify()
|
2022-06-20 22:37:31 +08:00
|
|
|
err := functions.LeaveNetwork(network)
|
2022-05-05 04:47:35 +08:00
|
|
|
if err != nil {
|
|
|
|
ErrorNotify("Failed to leave " + network + " : " + err.Error())
|
|
|
|
} else {
|
|
|
|
SuccessNotify("Left " + network)
|
|
|
|
}
|
2022-05-05 21:48:02 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
|
|
|
if err != nil {
|
|
|
|
networks = []string{}
|
|
|
|
ErrorNotify("Failed to read local networks!")
|
|
|
|
}
|
|
|
|
RefreshComponent(Networks, GetNetworksView(networks))
|
2022-05-05 04:47:35 +08:00
|
|
|
ShowView(Networks)
|
|
|
|
})
|
|
|
|
RefreshComponent(Confirm, confirmView)
|
|
|
|
ShowView(Confirm)
|
|
|
|
}
|