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:
@@ -0,0 +1 @@
|
||||
0.3.9
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
VERSION="0.3.8"
|
||||
VERSION="$(cat "$(dirname "$0")/VERSION")"
|
||||
APP_NAME="studioE5"
|
||||
BIN_NAME="studioE5-agent"
|
||||
LDFLAGS="-X main.version=${VERSION}"
|
||||
|
||||
@@ -71,6 +71,7 @@ func main() {
|
||||
}
|
||||
|
||||
go startWebSocket(cfg.Server, cfg.NodeID, *dataDir, cfg.HeadscaleURL, cfg.HeadscaleAuthKey)
|
||||
go updateCheckerLoop(*dataDir, cfg.Server)
|
||||
|
||||
shutdownCh := make(chan struct{})
|
||||
|
||||
|
||||
+32
@@ -56,6 +56,8 @@ func startUI(dataDir, nodeID, serverAddr string) {
|
||||
return
|
||||
}
|
||||
// Expose a merged view with the agent version for the UI.
|
||||
serverVersion := getServerAgentVersion()
|
||||
updateAvailable := serverVersion != "" && serverVersion != version
|
||||
response := map[string]interface{}{
|
||||
"server": cfg.Server,
|
||||
"headscale_url": cfg.HeadscaleURL,
|
||||
@@ -63,6 +65,8 @@ func startUI(dataDir, nodeID, serverAddr string) {
|
||||
"node_id": cfg.NodeID,
|
||||
"data_dir": cfg.DataDir,
|
||||
"version": version,
|
||||
"server_version": serverVersion,
|
||||
"update_available": updateAvailable,
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
case http.MethodPost:
|
||||
@@ -104,6 +108,34 @@ func startUI(dataDir, nodeID, serverAddr string) {
|
||||
}()
|
||||
})
|
||||
|
||||
http.HandleFunc("/api/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
cfg, _, err := loadOrCreateConfig(dataDir)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
broadcastUI(map[string]interface{}{
|
||||
"action": "update_progress",
|
||||
"percent": "10",
|
||||
"message": "Téléchargement de la mise à jour...",
|
||||
})
|
||||
if err := startAgentUpdate(dataDir, cfg.Server); err != nil {
|
||||
log.Printf("Agent update failed: %v", err)
|
||||
broadcastUI(map[string]interface{}{
|
||||
"action": "update_progress",
|
||||
"percent": "0",
|
||||
"message": "Échec de la mise à jour : " + err.Error(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -479,6 +479,8 @@
|
||||
<h1 id="welcome-text">Bienvenue</h1>
|
||||
<p class="subtitle">État de ton poste</p>
|
||||
|
||||
<div id="update-banner"></div>
|
||||
|
||||
<div class="services">
|
||||
<div class="service-item" id="svc-connection">
|
||||
<div class="service-icon pending">⏳</div>
|
||||
@@ -628,6 +630,7 @@
|
||||
: 'Bienvenue';
|
||||
showView('dashboard');
|
||||
ws.send(JSON.stringify({action: 'instances'}));
|
||||
loadSettings();
|
||||
break;
|
||||
|
||||
case 'activation_failed':
|
||||
@@ -671,6 +674,18 @@
|
||||
case 'config':
|
||||
fillSettings(msg.config);
|
||||
break;
|
||||
|
||||
case 'update_available':
|
||||
fillSettings({
|
||||
...getCurrentConfig(),
|
||||
server_version: msg.version,
|
||||
update_available: true
|
||||
});
|
||||
break;
|
||||
|
||||
case 'update_progress':
|
||||
showUpdateProgress(msg.percent, msg.message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -876,7 +891,14 @@
|
||||
}
|
||||
}
|
||||
|
||||
let currentConfig = {};
|
||||
|
||||
function getCurrentConfig() {
|
||||
return currentConfig;
|
||||
}
|
||||
|
||||
function fillSettings(cfg) {
|
||||
currentConfig = cfg;
|
||||
document.getElementById('cfg-server').value = cfg.server || '';
|
||||
document.getElementById('cfg-node').value = cfg.node_id || '';
|
||||
document.getElementById('cfg-headscale-url').value = cfg.headscale_url || '';
|
||||
@@ -885,6 +907,46 @@
|
||||
document.getElementById('detail-version').textContent = cfg.version || 'dev';
|
||||
document.getElementById('detail-nodeid').textContent = cfg.node_id || '-';
|
||||
document.getElementById('detail-server').textContent = cfg.server || '-';
|
||||
|
||||
const updateBanner = document.getElementById('update-banner');
|
||||
if (cfg.update_available) {
|
||||
updateBanner.innerHTML = `
|
||||
<div class="service-item" style="background: var(--warning-bg); border-radius: 8px; padding: 0.75rem; margin-bottom: 0.75rem;">
|
||||
<div class="service-icon" style="background: var(--warning);">⬆️</div>
|
||||
<div>
|
||||
<div class="service-text">Mise à jour disponible</div>
|
||||
<div class="service-detail">Version ${cfg.server_version} disponible. <button class="ghost" style="padding: 0; font-weight: 600;" onclick="startAgentUpdate()">Mettre à jour maintenant</button></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
updateBanner.innerHTML = '';
|
||||
}
|
||||
}
|
||||
|
||||
function showUpdateProgress(percent, message) {
|
||||
const banner = document.getElementById('update-banner');
|
||||
banner.innerHTML = `
|
||||
<div class="service-item" style="background: var(--info-bg); border-radius: 8px; padding: 0.75rem; margin-bottom: 0.75rem;">
|
||||
<div class="service-icon" style="background: var(--info);">⬇️</div>
|
||||
<div>
|
||||
<div class="service-text">Mise à jour en cours</div>
|
||||
<div class="service-detail">${escapeHtml(message || '')} (${percent || 0}%)</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
async function startAgentUpdate() {
|
||||
showUpdateProgress('10', 'Préparation de la mise à jour...');
|
||||
try {
|
||||
const res = await fetch('/api/update', {method: 'POST'});
|
||||
if (!res.ok) {
|
||||
showUpdateProgress('0', 'Erreur ' + res.status);
|
||||
}
|
||||
} catch (err) {
|
||||
showUpdateProgress('0', escapeHtml(err.message));
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSettings(event) {
|
||||
@@ -919,6 +981,7 @@
|
||||
function showDashboard() {
|
||||
showView('dashboard');
|
||||
ws.send(JSON.stringify({action: 'get_status'}));
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
function controlInstance(instanceId, action) {
|
||||
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const updateCheckInterval = 15 * time.Minute
|
||||
|
||||
// AgentVersionInfo matches the server's /api/agent/version response.
|
||||
type AgentVersionInfo struct {
|
||||
Version string `json:"version"`
|
||||
DownloadUrls struct {
|
||||
Windows string `json:"windows"`
|
||||
WindowsZip string `json:"windowsZip"`
|
||||
Linux string `json:"linux"`
|
||||
Mac string `json:"mac"`
|
||||
} `json:"downloadUrls"`
|
||||
}
|
||||
|
||||
// checkForUpdate fetches the latest agent version from the server and compares
|
||||
// it with the running binary's version.
|
||||
func checkForUpdate(serverBaseURL string) (*AgentVersionInfo, bool, error) {
|
||||
if serverBaseURL == "" {
|
||||
return nil, false, fmt.Errorf("no server URL configured")
|
||||
}
|
||||
wsURL := strings.TrimSuffix(serverBaseURL, "/api/websocket")
|
||||
url := wsURL + "/api/agent/version"
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, false, fmt.Errorf("server returned %d", resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var info AgentVersionInfo
|
||||
if err := json.Unmarshal(body, &info); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if info.Version == "" {
|
||||
return nil, false, fmt.Errorf("server returned empty version")
|
||||
}
|
||||
available := info.Version != version
|
||||
return &info, available, nil
|
||||
}
|
||||
|
||||
// downloadUpdate downloads the new agent binary to the update directory.
|
||||
func downloadUpdate(dataDir, downloadURL string) (string, error) {
|
||||
updateDir := filepath.Join(dataDir, "update")
|
||||
if err := os.MkdirAll(updateDir, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ext := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
ext = ".exe"
|
||||
}
|
||||
dest := filepath.Join(updateDir, "studioE5-agent-new"+ext)
|
||||
log.Printf("Downloading update from %s to %s", downloadURL, dest)
|
||||
|
||||
resp, err := http.Get(downloadURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("download returned %d", resp.StatusCode)
|
||||
}
|
||||
out, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer out.Close()
|
||||
if _, err := io.Copy(out, resp.Body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := out.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if runtime.GOOS != "windows" {
|
||||
if err := os.Chmod(dest, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return dest, nil
|
||||
}
|
||||
|
||||
// applyUpdate replaces the running binary with the downloaded one using an
|
||||
// external helper script, then exits the current process.
|
||||
func applyUpdate(currentPath, newPath, dataDir string) error {
|
||||
pid := os.Getpid()
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return applyUpdateWindows(currentPath, newPath, dataDir, pid)
|
||||
default:
|
||||
return applyUpdateUnix(currentPath, newPath, dataDir, pid)
|
||||
}
|
||||
}
|
||||
|
||||
func applyUpdateWindows(currentPath, newPath, dataDir string, pid int) error {
|
||||
scriptPath := filepath.Join(dataDir, "update", "apply-update.ps1")
|
||||
script := fmt.Sprintf(`$old = "%s"
|
||||
$new = "%s"
|
||||
$pid = %d
|
||||
$args = '-no-tray', '-data-dir', '%s'
|
||||
Wait-Process -Id $pid -ErrorAction SilentlyContinue
|
||||
Start-Sleep -Seconds 2
|
||||
Move-Item -Path $new -Destination $old -Force
|
||||
Start-Process -FilePath $old -ArgumentList $args -WindowStyle Hidden
|
||||
`, currentPath, newPath, pid, dataDir)
|
||||
if err := os.WriteFile(scriptPath, []byte(script), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command("powershell.exe", "-ExecutionPolicy", "Bypass", "-File", scriptPath)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Update helper started, exiting current process")
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyUpdateUnix(currentPath, newPath, dataDir string, pid int) error {
|
||||
scriptPath := filepath.Join(dataDir, "update", "apply-update.sh")
|
||||
script := fmt.Sprintf(`#!/bin/bash
|
||||
set -e
|
||||
old="%s"
|
||||
new="%s"
|
||||
pid=%d
|
||||
while kill -0 "$pid" 2>/dev/null; do sleep 1; done
|
||||
sleep 2
|
||||
mv "$new" "$old"
|
||||
chmod +x "$old"
|
||||
nohup "$old" -no-tray -data-dir "%s" >/dev/null 2>&1 &
|
||||
`, currentPath, newPath, pid, dataDir)
|
||||
if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command("/bin/bash", scriptPath)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Update helper started, exiting current process")
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
// startAgentUpdate performs the full update flow: download + replace + restart.
|
||||
func startAgentUpdate(dataDir, serverBaseURL string) error {
|
||||
info, available, err := checkForUpdate(serverBaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update check failed: %w", err)
|
||||
}
|
||||
if !available {
|
||||
return fmt.Errorf("no update available")
|
||||
}
|
||||
currentPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentPath, err = filepath.Abs(currentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var downloadURL string
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
downloadURL = info.DownloadUrls.Windows
|
||||
case "darwin":
|
||||
downloadURL = info.DownloadUrls.Mac
|
||||
default:
|
||||
downloadURL = info.DownloadUrls.Linux
|
||||
}
|
||||
if downloadURL == "" {
|
||||
return fmt.Errorf("no download URL for %s", runtime.GOOS)
|
||||
}
|
||||
newPath, err := downloadUpdate(dataDir, downloadURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("download failed: %w", err)
|
||||
}
|
||||
log.Printf("Applying update to version %s", info.Version)
|
||||
broadcastUI(map[string]interface{}{
|
||||
"action": "update_progress",
|
||||
"percent": "90",
|
||||
"message": "Redémarrage de l'agent...",
|
||||
})
|
||||
return applyUpdate(currentPath, newPath, dataDir)
|
||||
}
|
||||
|
||||
// updateCheckerLoop periodically checks for agent updates and notifies the UI.
|
||||
func updateCheckerLoop(dataDir, serverBaseURL string) {
|
||||
for {
|
||||
info, available, err := checkForUpdate(serverBaseURL)
|
||||
if err == nil && available && info != nil {
|
||||
log.Printf("Agent update available: %s (current: %s)", info.Version, version)
|
||||
setServerAgentVersion(info.Version)
|
||||
broadcastUI(map[string]interface{}{
|
||||
"action": "update_available",
|
||||
"version": info.Version,
|
||||
"update_available": true,
|
||||
})
|
||||
}
|
||||
time.Sleep(updateCheckInterval)
|
||||
}
|
||||
}
|
||||
+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