|
@@ -3,6 +3,9 @@ package docker
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
|
|
|
|
|
|
+ "sync"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
"github.com/azukaar/cosmos-server/src/utils"
|
|
"github.com/azukaar/cosmos-server/src/utils"
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types"
|
|
@@ -35,15 +38,28 @@ func DockerListenEvents() error {
|
|
case msg := <-msgs:
|
|
case msg := <-msgs:
|
|
utils.Debug("Docker Event: " + msg.Type + " " + msg.Action + " " + msg.Actor.ID)
|
|
utils.Debug("Docker Event: " + msg.Type + " " + msg.Action + " " + msg.Actor.ID)
|
|
if msg.Type == "container" && msg.Action == "start" {
|
|
if msg.Type == "container" && msg.Action == "start" {
|
|
- onDockerCreated(msg.Actor.ID)
|
|
|
|
|
|
+ onDockerStarted(msg.Actor.ID)
|
|
}
|
|
}
|
|
|
|
+
|
|
// on container destroy and network disconnect
|
|
// on container destroy and network disconnect
|
|
if msg.Type == "container" && msg.Action == "destroy" {
|
|
if msg.Type == "container" && msg.Action == "destroy" {
|
|
onDockerDestroyed(msg.Actor.ID)
|
|
onDockerDestroyed(msg.Actor.ID)
|
|
}
|
|
}
|
|
|
|
+ if msg.Type == "container" && msg.Action == "create" {
|
|
|
|
+ onDockerCreated(msg.Actor.ID)
|
|
|
|
+ }
|
|
if msg.Type == "network" && msg.Action == "disconnect" {
|
|
if msg.Type == "network" && msg.Action == "disconnect" {
|
|
onNetworkDisconnect(msg.Actor.ID)
|
|
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
|
|
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)
|
|
BootstrapContainerFromTags(containerID)
|
|
|
|
+ DebouncedExportDocker()
|
|
}
|
|
}
|
|
|
|
|
|
func onDockerDestroyed(containerID string) {
|
|
func onDockerDestroyed(containerID string) {
|
|
utils.Debug("onDockerDestroyed: " + containerID)
|
|
utils.Debug("onDockerDestroyed: " + containerID)
|
|
|
|
+ DebouncedExportDocker()
|
|
}
|
|
}
|
|
|
|
|
|
func onNetworkDisconnect(networkID string) {
|
|
func onNetworkDisconnect(networkID string) {
|
|
utils.Debug("onNetworkDisconnect: " + networkID)
|
|
utils.Debug("onNetworkDisconnect: " + networkID)
|
|
DebouncedNetworkCleanUp(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()
|
|
|
|
+}
|