feat(agent): v0.3.9 sync, UI details, self-update, centralized version
- Add agent/server startup sync (sync/sync_response) - Centralize agent version in agent/VERSION + expose /api/agent/version - Display agent version, nodeId and server version in local UI - Add agent self-update detection/download/restart via helper scripts - Run start/stop/delete/reset handlers in goroutines to avoid WebSocket blocking - Update dashboard download links and SUIVI_VPN_ONDEMAND.md - Document Podman stays installer-managed, not agent-updated
This commit is contained in:
+138
-35
@@ -10,22 +10,35 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type SyncInstanceInfo struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Port int `json:"port"`
|
||||
ComposeConfig string `json:"composeConfig,omitempty"`
|
||||
InitScript string `json:"initScript,omitempty"`
|
||||
}
|
||||
|
||||
type WSMessage struct {
|
||||
Action string `json:"action"`
|
||||
NodeID string `json:"nodeId,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
InstanceID string `json:"instanceId,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
ComposeConfig string `json:"composeConfig,omitempty"`
|
||||
InitScript string `json:"initScript,omitempty"`
|
||||
StudentId string `json:"studentId,omitempty"`
|
||||
StudentName string `json:"studentName,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
TailscaleIP string `json:"tailscaleIp,omitempty"`
|
||||
HeadscaleURL string `json:"headscaleUrl,omitempty"`
|
||||
HeadscaleAuthKey string `json:"headscaleAuthKey,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
Action string `json:"action"`
|
||||
NodeID string `json:"nodeId,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
InstanceID string `json:"instanceId,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
ComposeConfig string `json:"composeConfig,omitempty"`
|
||||
InitScript string `json:"initScript,omitempty"`
|
||||
StudentId string `json:"studentId,omitempty"`
|
||||
StudentName string `json:"studentName,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
TailscaleIP string `json:"tailscaleIp,omitempty"`
|
||||
HeadscaleURL string `json:"headscaleUrl,omitempty"`
|
||||
HeadscaleAuthKey string `json:"headscaleAuthKey,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
ServerVersion string `json:"serverVersion,omitempty"`
|
||||
Instances []InstanceInfo `json:"instances"`
|
||||
ToStart []SyncInstanceInfo `json:"toStart"`
|
||||
ToDelete []string `json:"toDelete"`
|
||||
ToStop []string `json:"toStop"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -54,6 +67,25 @@ func getHeadscaleConfig() (string, string) {
|
||||
return currentHeadscaleURL, currentHeadscaleAuthKey
|
||||
}
|
||||
|
||||
// serverAgentVersion holds the agent version expected by the server. It is used
|
||||
// to notify the user when an update is available.
|
||||
var (
|
||||
serverAgentVersion string
|
||||
serverAgentVersionMu sync.RWMutex
|
||||
)
|
||||
|
||||
func setServerAgentVersion(v string) {
|
||||
serverAgentVersionMu.Lock()
|
||||
serverAgentVersion = v
|
||||
serverAgentVersionMu.Unlock()
|
||||
}
|
||||
|
||||
func getServerAgentVersion() string {
|
||||
serverAgentVersionMu.RLock()
|
||||
defer serverAgentVersionMu.RUnlock()
|
||||
return serverAgentVersion
|
||||
}
|
||||
|
||||
func sendMessage(msg WSMessage) error {
|
||||
mainConnMu.Lock()
|
||||
defer mainConnMu.Unlock()
|
||||
@@ -63,9 +95,33 @@ func sendMessage(msg WSMessage) error {
|
||||
if msg.Action != "heartbeat" {
|
||||
log.Printf("sendMessage: sending %+v", msg)
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("PANIC in sendMessage: %v", r)
|
||||
}
|
||||
}()
|
||||
return mainConn.WriteJSON(msg)
|
||||
}
|
||||
|
||||
// sendSyncMessage sends the local instance list to the server so it can
|
||||
// reconcile any differences (instances created/deleted while offline).
|
||||
func sendSyncMessage(dataDir, nodeID string) {
|
||||
inst, err := loadInstances(dataDir)
|
||||
if err != nil {
|
||||
log.Printf("sendSyncMessage: loadInstances error: %v", err)
|
||||
return
|
||||
}
|
||||
list := make([]InstanceInfo, 0, len(inst))
|
||||
for _, info := range inst {
|
||||
list = append(list, *info)
|
||||
}
|
||||
if err := sendMessage(WSMessage{Action: "sync", NodeID: nodeID, Instances: list}); err != nil {
|
||||
log.Printf("sendSyncMessage error: %v", err)
|
||||
} else {
|
||||
log.Printf("sendSyncMessage: sent %d local instances", len(list))
|
||||
}
|
||||
}
|
||||
|
||||
// UI notifier system: broadcast activation results to all connected UI clients
|
||||
type uiNotifier func(msg map[string]interface{})
|
||||
|
||||
@@ -258,7 +314,15 @@ func handleMessage(conn *websocket.Conn, msg WSMessage, dataDir, nodeID string)
|
||||
"studentName": msg.StudentName,
|
||||
})
|
||||
case "registered":
|
||||
// Server acknowledged our register message; nothing to do.
|
||||
if msg.ServerVersion != "" {
|
||||
setServerAgentVersion(msg.ServerVersion)
|
||||
log.Printf("Server agent version: %s", msg.ServerVersion)
|
||||
}
|
||||
// After registration, send a sync request with our local instances so
|
||||
// the server can reconcile any changes that happened while offline.
|
||||
if act, err := loadActivation(dataDir); err == nil && act.Activated {
|
||||
go sendSyncMessage(dataDir, nodeID)
|
||||
}
|
||||
return
|
||||
case "start_vpn":
|
||||
log.Printf("Server requested VPN start")
|
||||
@@ -327,27 +391,66 @@ func handleMessage(conn *websocket.Conn, msg WSMessage, dataDir, nodeID string)
|
||||
}()
|
||||
case "stop":
|
||||
log.Printf("Stop instance %s", msg.InstanceID)
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
removeTailscaleServe(inst[msg.InstanceID].Port)
|
||||
}
|
||||
if err := dockerComposeStop(dataDir, msg.InstanceID); err != nil {
|
||||
log.Printf("dockerComposeStop error: %v", err)
|
||||
}
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
inst[msg.InstanceID].Status = "stopped"
|
||||
_ = saveInstances(dataDir, inst)
|
||||
}
|
||||
go sendMessage(WSMessage{Action: "instance_stopped", InstanceID: msg.InstanceID})
|
||||
notifyUI(map[string]interface{}{"action": "instances_updated"})
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("PANIC in stop goroutine instance=%s: %v", msg.InstanceID, r)
|
||||
}
|
||||
}()
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
removeTailscaleServe(inst[msg.InstanceID].Port)
|
||||
}
|
||||
if err := dockerComposeStop(dataDir, msg.InstanceID); err != nil {
|
||||
log.Printf("dockerComposeStop error: %v", err)
|
||||
}
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
inst[msg.InstanceID].Status = "stopped"
|
||||
_ = saveInstances(dataDir, inst)
|
||||
}
|
||||
go sendMessage(WSMessage{Action: "instance_stopped", InstanceID: msg.InstanceID})
|
||||
notifyUI(map[string]interface{}{"action": "instances_updated"})
|
||||
}()
|
||||
case "delete":
|
||||
log.Printf("Delete instance %s", msg.InstanceID)
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
removeTailscaleServe(inst[msg.InstanceID].Port)
|
||||
}
|
||||
dockerComposeRm(dataDir, msg.InstanceID)
|
||||
removeInstance(dataDir, msg.InstanceID)
|
||||
go sendMessage(WSMessage{Action: "instance_deleted", InstanceID: msg.InstanceID})
|
||||
notifyUI(map[string]interface{}{"action": "instances_updated"})
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("PANIC in delete goroutine instance=%s: %v", msg.InstanceID, r)
|
||||
}
|
||||
}()
|
||||
if inst, _ := loadInstances(dataDir); inst[msg.InstanceID] != nil {
|
||||
removeTailscaleServe(inst[msg.InstanceID].Port)
|
||||
}
|
||||
dockerComposeRm(dataDir, msg.InstanceID)
|
||||
removeInstance(dataDir, msg.InstanceID)
|
||||
go sendMessage(WSMessage{Action: "instance_deleted", InstanceID: msg.InstanceID})
|
||||
notifyUI(map[string]interface{}{"action": "instances_updated"})
|
||||
}()
|
||||
case "sync_response":
|
||||
log.Printf("Sync response received: start=%d delete=%d stop=%d", len(msg.ToStart), len(msg.ToDelete), len(msg.ToStop))
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("PANIC in sync_response goroutine: %v", r)
|
||||
}
|
||||
}()
|
||||
for _, id := range msg.ToDelete {
|
||||
handleMessage(mainConn, WSMessage{Action: "delete", InstanceID: id}, dataDir, nodeID)
|
||||
}
|
||||
for _, id := range msg.ToStop {
|
||||
handleMessage(mainConn, WSMessage{Action: "stop", InstanceID: id}, dataDir, nodeID)
|
||||
}
|
||||
for _, info := range msg.ToStart {
|
||||
handleMessage(mainConn, WSMessage{
|
||||
Action: "start",
|
||||
InstanceID: info.ID,
|
||||
Type: info.Type,
|
||||
Port: info.Port,
|
||||
ComposeConfig: info.ComposeConfig,
|
||||
InitScript: info.InitScript,
|
||||
}, dataDir, nodeID)
|
||||
}
|
||||
}()
|
||||
case "reset":
|
||||
log.Printf("Reset instance %s", msg.InstanceID)
|
||||
dockerComposeRm(dataDir, msg.InstanceID)
|
||||
|
||||
Reference in New Issue
Block a user