Selaa lähdekoodia

[release] v0.11.0-unstable3

Yann Stepienik 1 vuosi sitten
vanhempi
commit
22a25ee1e8
5 muutettua tiedostoa jossa 41 lisäystä ja 31 poistoa
  1. 1 1
      package.json
  2. 8 18
      src/docker/api_blueprint.go
  3. 4 2
      src/docker/docker.go
  4. 24 9
      src/docker/events.go
  5. 4 1
      src/docker/export.go

+ 1 - 1
package.json

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

+ 8 - 18
src/docker/api_blueprint.go

@@ -518,31 +518,21 @@ func CreateService(serviceRequest DockerServiceCreateRequest, OnLog func(string)
 			hostPorts := []string{}
 			hostPorts := []string{}
 			containerPorts := []string{}
 			containerPorts := []string{}
 
 
-			// if port contains ->, it's ip bound
-			if strings.Contains(port, "->") {
-				ports := strings.Split(port, "->")
-				hostPortSlice = strings.Split(ports[0], ":")[1]
-				containerPortSlice = strings.Split(ports[1], ":")[1]
-
-				hostPorts = []string{
-					hostPortSlice[len(hostPortSlice)-1],
-				}
+			ports := strings.Split(port, ":")
 
 
-				containerPorts = []string{
-					containerPortSlice[len(containerPortSlice)-1],
-				}				
-			} else {
-				ports := strings.Split(port, ":")
+			hostPorts = generatePorts(ports[len(ports)-2])
+			containerPorts = generatePorts(ports[len(ports)-1])
 
 
-				hostPorts = generatePorts(ports[0])
-				containerPorts = generatePorts(ports[1])
+			ipExposed := ""
+			if len(portStuff) > 2 {
+				ipExposed = strings.Join(portStuff[0:len(portStuff)-2], ":") + ":"
 			}
 			}
 
 
 			for i := 0; i < utils.Max(len(hostPorts), len(containerPorts)); i++ {
 			for i := 0; i < utils.Max(len(hostPorts), len(containerPorts)); i++ {
 				hostPort := hostPorts[i%len(hostPorts)]
 				hostPort := hostPorts[i%len(hostPorts)]
 				containerPort := containerPorts[i%len(containerPorts)]
 				containerPort := containerPorts[i%len(containerPorts)]
 				
 				
-				finalPorts = append(finalPorts, fmt.Sprintf("%s:%s/%s", hostPort, containerPort, protocol))
+				finalPorts = append(finalPorts, fmt.Sprintf("%s%s:%s/%s", ipExposed, hostPort, containerPort, protocol))
 			}
 			}
 		}
 		}
 
 
@@ -790,7 +780,7 @@ func CreateService(serviceRequest DockerServiceCreateRequest, OnLog func(string)
 				OnLog(utils.DoErr("Rolling back changes because of -- Network connection error: "+err.Error()))
 				OnLog(utils.DoErr("Rolling back changes because of -- Network connection error: "+err.Error()))
 				Rollback(rollbackActions, OnLog)
 				Rollback(rollbackActions, OnLog)
 				return err
 				return err
-			} else if strings.Contains(err.Error(), "already exists in network") {
+			} else if err != nil && strings.Contains(err.Error(), "already exists in network") {
 				utils.Warn("CreateService: Container " + container.Name + " already connected to network " + netName + ", skipping.")
 				utils.Warn("CreateService: Container " + container.Name + " already connected to network " + netName + ", skipping.")
 				OnLog(utils.DoWarn("Container %s already connected to network %s, skipping.", container.Name, netName))			
 				OnLog(utils.DoWarn("Container %s already connected to network %s, skipping.", container.Name, netName))			
 			}
 			}

+ 4 - 2
src/docker/docker.go

@@ -634,14 +634,14 @@ func SelfRecreate() error {
 }
 }
 
 
 func DockerPullImage(image string) (io.ReadCloser, error) {
 func DockerPullImage(image string) (io.ReadCloser, error) {
+	utils.Debug("DockerPull - Preparing Pulling image " + image)
+
 	options := types.ImagePullOptions{}
 	options := types.ImagePullOptions{}
 
 
 	configfile, err := config.Load(config.Dir())
 	configfile, err := config.Load(config.Dir())
 	if err != nil {
 	if err != nil {
 			utils.Error("DockerPull - Read config file error -", err)
 			utils.Error("DockerPull - Read config file error -", err)
 	} else {
 	} else {
-
-		
 		slashIndex := strings.Index(image, "/")
 		slashIndex := strings.Index(image, "/")
 		
 		
 		if slashIndex >= 1 && strings.ContainsAny(image[:slashIndex], ".:") {
 		if slashIndex >= 1 && strings.ContainsAny(image[:slashIndex], ".:") {
@@ -657,6 +657,8 @@ func DockerPullImage(image string) (io.ReadCloser, error) {
 		} 
 		} 
 	}
 	}
 	
 	
+	utils.Debug("DockerPull - Starting Pulling image " + image)
+
 	out, errPull := DockerClient.ImagePull(DockerContext, image, options)
 	out, errPull := DockerClient.ImagePull(DockerContext, image, options)
 
 
 	return out, errPull
 	return out, errPull

+ 24 - 9
src/docker/events.go

@@ -2,7 +2,6 @@ package docker
 
 
 import (
 import (
 	"context"
 	"context"
-	
 	"sync"
 	"sync"
 	"time"
 	"time"
 
 
@@ -71,6 +70,7 @@ var (
 	timer    *time.Timer
 	timer    *time.Timer
 	interval = 30000 * time.Millisecond
 	interval = 30000 * time.Millisecond
 	mu       sync.Mutex
 	mu       sync.Mutex
+	cancelFunc      context.CancelFunc
 )
 )
 
 
 func DebouncedExportDocker() {
 func DebouncedExportDocker() {
@@ -78,45 +78,60 @@ func DebouncedExportDocker() {
 	defer mu.Unlock()
 	defer mu.Unlock()
 
 
 	if timer != nil {
 	if timer != nil {
+		if cancelFunc != nil {
+			cancelFunc() // cancel the previous context
+		}
 		timer.Stop()
 		timer.Stop()
 	}
 	}
 
 
-	timer = time.AfterFunc(interval, ExportDocker)
+	// Create a new context and cancelFunc
+	ctx, newCancelFunc := context.WithCancel(context.Background())
+	cancelFunc = newCancelFunc
+
+	timer = time.AfterFunc(interval, func() {
+		select {
+		case <-ctx.Done():
+			// if the context was canceled, don't execute ExportDocker
+			return
+		default:
+			ExportDocker()
+		}
+	})
 }
 }
 
 
 func onDockerStarted(containerID string) {
 func onDockerStarted(containerID string) {
 	utils.Debug("onDockerStarted: " + containerID)
 	utils.Debug("onDockerStarted: " + containerID)
 	BootstrapContainerFromTags(containerID)
 	BootstrapContainerFromTags(containerID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onDockerDestroyed(containerID string) {
 func onDockerDestroyed(containerID string) {
 	utils.Debug("onDockerDestroyed: " + containerID)
 	utils.Debug("onDockerDestroyed: " + containerID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onNetworkDisconnect(networkID string) {
 func onNetworkDisconnect(networkID string) {
 	utils.Debug("onNetworkDisconnect: " + networkID)
 	utils.Debug("onNetworkDisconnect: " + networkID)
 	DebouncedNetworkCleanUp(networkID)
 	DebouncedNetworkCleanUp(networkID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onDockerCreated(containerID string) {
 func onDockerCreated(containerID string) {
 	utils.Debug("onDockerCreated: " + containerID)
 	utils.Debug("onDockerCreated: " + containerID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onNetworkDestroy(networkID string) {
 func onNetworkDestroy(networkID string) {
 	utils.Debug("onNetworkDestroy: " + networkID)
 	utils.Debug("onNetworkDestroy: " + networkID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onNetworkCreate(networkID string) {
 func onNetworkCreate(networkID string) {
 	utils.Debug("onNetworkCreate: " + networkID)
 	utils.Debug("onNetworkCreate: " + networkID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }
 
 
 func onNetworkConnect(networkID string) {
 func onNetworkConnect(networkID string) {
 	utils.Debug("onNetworkConnect: " + networkID)
 	utils.Debug("onNetworkConnect: " + networkID)
-	DebouncedExportDocker()
+	// DebouncedExportDocker()
 }
 }

+ 4 - 1
src/docker/export.go

@@ -73,17 +73,20 @@ func ExportDocker() {
 			CapDrop:          detailedInfo.HostConfig.CapDrop,
 			CapDrop:          detailedInfo.HostConfig.CapDrop,
 			SysctlsMap:       detailedInfo.HostConfig.Sysctls,
 			SysctlsMap:       detailedInfo.HostConfig.Sysctls,
 			Privileged:       detailedInfo.HostConfig.Privileged,
 			Privileged:       detailedInfo.HostConfig.Privileged,
+			
 			// StopGracePeriod:  int(detailedInfo.HostConfig.StopGracePeriod.Seconds()),
 			// StopGracePeriod:  int(detailedInfo.HostConfig.StopGracePeriod.Seconds()),
+			
 			// Ports
 			// Ports
 			Ports: func() []string {
 			Ports: func() []string {
 					ports := []string{}
 					ports := []string{}
 					for port, binding := range detailedInfo.NetworkSettings.Ports {
 					for port, binding := range detailedInfo.NetworkSettings.Ports {
 							for _, b := range binding {
 							for _, b := range binding {
-									ports = append(ports, fmt.Sprintf("%s:%s->%s/%s", b.HostIP, b.HostPort, port.Port(), port.Proto()))
+									ports = append(ports, fmt.Sprintf("%s:%s:%s/%s", b.HostIP, b.HostPort, port.Port(), port.Proto()))
 							}
 							}
 					}
 					}
 					return ports
 					return ports
 			}(),
 			}(),
+
 			// Volumes
 			// Volumes
 			Volumes: func() []mount.Mount {
 			Volumes: func() []mount.Mount {
 					mounts := []mount.Mount{}
 					mounts := []mount.Mount{}