Changed Orborus network detection

This commit is contained in:
frikky
2020-07-09 06:04:35 +02:00
parent ab804a3fdc
commit 82f08e3c6b
+26 -20
View File
@@ -63,42 +63,47 @@ func init() {
panic(fmt.Sprintf("Unable to create docker client: %s", err)) panic(fmt.Sprintf("Unable to create docker client: %s", err))
} }
// BElow: // FIXME: Move this to global variables?
// Checking if orborus is running on docker within a specific network containerIdentifier := "orborus"
ctx := context.Background() networkIdentifier := "shuffle"
networkName := ""
dockerNetworks, err := dockercli.NetworkList(ctx, types.NetworkListOptions{})
for _, item := range dockerNetworks {
if strings.Contains(strings.ToLower(item.Name), "shuffle") {
networkName = item.Name
break
}
}
if len(networkName) > 0 { ctx := context.Background()
containers, err := dockercli.ContainerList(ctx, types.ContainerListOptions{ containers, err := dockercli.ContainerList(ctx, types.ContainerListOptions{
All: true, All: true,
}) })
if err != nil { if err != nil {
log.Printf("Failed getting containers during init - running without network check: %s", err) log.Printf("Failed getting containers during init - running without network check: %s", err)
} }
_ = networkName
// Skip random containers. Only handle things related to Shuffle. // Skip random containers. Only handle things related to Shuffle.
for _, container := range containers { for _, container := range containers {
for key, value := range container.NetworkSettings.Networks { found := false
_ = value //log.Printf("Running? %#v", container)
if key == networkName { if container.State != "running" {
continue
}
for _, name := range container.Names { for _, name := range container.Names {
if strings.Contains(strings.ToLower(name), "orborus") { if !strings.Contains(strings.ToLower(name), containerIdentifier) {
// BEING HERE MEANS THAT ORBORUS HAS BEEN FOUND IN THE SPECIFIED NETWORK found = true
shuffleNetwork = networkName continue
}
}
if found {
for key, _ := range container.NetworkSettings.Networks {
if strings.Contains(strings.ToLower(key), networkIdentifier) {
shuffleNetwork = key
break break
} }
} }
} }
} }
}
if len(shuffleNetwork) > 0 {
log.Printf("Found shuffle network \"%s\" for container %s", shuffleNetwork, containerIdentifier)
} else {
log.Printf("Running Shuffle without a docker network")
} }
} }
@@ -133,6 +138,7 @@ func deployWorker(image string, identifier string, env []string) {
}, },
}, },
} }
env = append(env, fmt.Sprintf("DOCKER_NETWORK", shuffleNetwork)) env = append(env, fmt.Sprintf("DOCKER_NETWORK", shuffleNetwork))
} }