瀏覽代碼

[release] v0.11.0-unstable2

Yann Stepienik 1 年之前
父節點
當前提交
a854cd9bd7
共有 3 個文件被更改,包括 78 次插入8 次删除
  1. 1 1
      package.json
  2. 60 4
      src/docker/events.go
  3. 17 3
      src/docker/export.go

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "cosmos-server",
-  "version": "0.11.0-unstable",
+  "version": "0.11.0-unstable2",
   "description": "",
   "main": "test-server.js",
   "bugs": {

+ 60 - 4
src/docker/events.go

@@ -3,6 +3,9 @@ package docker
 import (
 	"context"
 	
+	"sync"
+	"time"
+
 	"github.com/azukaar/cosmos-server/src/utils" 
 
 	"github.com/docker/docker/api/types"
@@ -35,15 +38,28 @@ func DockerListenEvents() error {
 				case msg := <-msgs:
 					utils.Debug("Docker Event: " + msg.Type + " " + msg.Action + " " + msg.Actor.ID)
 					if msg.Type == "container" && msg.Action == "start" {
-						onDockerCreated(msg.Actor.ID)
+						onDockerStarted(msg.Actor.ID)
 					}
+
 					// on container destroy and network disconnect
 					if msg.Type == "container" && msg.Action == "destroy" {
 						onDockerDestroyed(msg.Actor.ID)
 					}
+					if msg.Type == "container" && msg.Action == "create" {
+						onDockerCreated(msg.Actor.ID)
+					}
 					if msg.Type == "network" && msg.Action == "disconnect" {
 						onNetworkDisconnect(msg.Actor.ID)
 					}
+					if msg.Type == "network" && msg.Action == "destroy" {
+						onNetworkDestroy(msg.Actor.ID)
+					}
+					if msg.Type == "network" && msg.Action == "create" {
+						onNetworkCreate(msg.Actor.ID)
+					}
+					if msg.Type == "network" && msg.Action == "connect" {
+						onNetworkConnect(msg.Actor.ID)
+					}
 			}
 		}
 	}()
@@ -51,16 +67,56 @@ func DockerListenEvents() error {
 	return nil
 }
 
-func onDockerCreated(containerID string) {
-	utils.Debug("onDockerCreated: " + containerID)
+var (
+	timer    *time.Timer
+	interval = 30000 * time.Millisecond
+	mu       sync.Mutex
+)
+
+func DebouncedExportDocker() {
+	mu.Lock()
+	defer mu.Unlock()
+
+	if timer != nil {
+		timer.Stop()
+	}
+
+	timer = time.AfterFunc(interval, ExportDocker)
+}
+
+func onDockerStarted(containerID string) {
+	utils.Debug("onDockerStarted: " + containerID)
 	BootstrapContainerFromTags(containerID)
+	DebouncedExportDocker()
 }
 
 func onDockerDestroyed(containerID string) {
 	utils.Debug("onDockerDestroyed: " + containerID)
+	DebouncedExportDocker()
 }
 
 func onNetworkDisconnect(networkID string) {
 	utils.Debug("onNetworkDisconnect: " + networkID)
 	DebouncedNetworkCleanUp(networkID)
-}
+	DebouncedExportDocker()
+}
+
+func onDockerCreated(containerID string) {
+	utils.Debug("onDockerCreated: " + containerID)
+	DebouncedExportDocker()
+}
+
+func onNetworkDestroy(networkID string) {
+	utils.Debug("onNetworkDestroy: " + networkID)
+	DebouncedExportDocker()
+}
+
+func onNetworkCreate(networkID string) {
+	utils.Debug("onNetworkCreate: " + networkID)
+	DebouncedExportDocker()
+}
+
+func onNetworkConnect(networkID string) {
+	utils.Debug("onNetworkConnect: " + networkID)
+	DebouncedExportDocker()
+}

+ 17 - 3
src/docker/export.go

@@ -6,6 +6,7 @@ import (
 	"io/ioutil"
 	"strconv"
 	"strings"
+	"bytes"
 
 	"github.com/azukaar/cosmos-server/src/utils"
 	"github.com/docker/docker/api/types"
@@ -209,13 +210,26 @@ func ExportDocker() {
 	// Convert the services map to your finalBackup struct
 	finalBackup.Services = services
 
+	// Create a buffer to hold the JSON output
+	var buf bytes.Buffer
 
-	// Convert the finalBackup struct to JSON
-	jsonData, err := json.MarshalIndent(finalBackup, "", "  ")
+	// Create a new JSON encoder that writes to the buffer
+	encoder := json.NewEncoder(&buf)
+
+	// Set escape HTML to false to avoid escaping special characters
+	encoder.SetEscapeHTML(false)
+	//format
+	encoder.SetIndent("", "  ")
+
+	// Use the encoder to write the structured data to the buffer
+	err = encoder.Encode(finalBackup)
 	if err != nil {
-		utils.Error("Cannot marshal docker backup", err)
+			utils.Error("Cannot marshal docker backup", err)
 	}
 
+	// The JSON data is now in buf.Bytes()
+	jsonData := buf.Bytes()
+
 	// Write the JSON data to a file
 	err = ioutil.WriteFile(utils.CONFIGFOLDER + "backup.cosmos-compose.json", jsonData, 0644)
 	if err != nil {