forked from codekitchen/dinghy-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a go binary for docker network joining
This gets rid of the brittle bash code that shells out to the docker cli.
- Loading branch information
1 parent
11414b0
commit 877e68c
Showing
7 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
join-networks.tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "vendor/github.com/fsouza/go-dockerclient"] | ||
path = vendor/github.com/fsouza/go-dockerclient | ||
url = https://github.com/codekitchen/go-dockerclient.git | ||
branch = add-network-id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
docker run --rm -v "$PWD":/go/src/github.com/codekitchen/dinghy-http-proxy -w /go/src/github.com/codekitchen/dinghy-http-proxy golang:1.6 go build -v -o join-networks | ||
tar czvf join-networks.tar.gz join-networks | ||
rm join-networks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
docker "github.com/fsouza/go-dockerclient" | ||
) | ||
|
||
var ( | ||
endpoint = "unix:///tmp/docker.sock" | ||
) | ||
|
||
func main() { | ||
var containerName = flag.String("container-name", "", "the name of this docker container") | ||
flag.Parse() | ||
|
||
client, err := docker.NewClient(endpoint) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
currentNetworks := getJoinedNetworks(client, *containerName) | ||
bridgeNetworks := getBridgeNetworks(client) | ||
|
||
for _, id := range bridgeNetworks { | ||
if !currentNetworks[id] { | ||
err := client.ConnectNetwork(id, docker.NetworkConnectionOptions{ | ||
Container: *containerName, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getJoinedNetworks(client *docker.Client, containerName string) (networks map[string]bool) { | ||
networks = make(map[string]bool) | ||
|
||
container, err := client.InspectContainer(containerName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, net := range container.NetworkSettings.Networks { | ||
networks[net.NetworkID] = true | ||
} | ||
|
||
return | ||
} | ||
|
||
func getBridgeNetworks(client *docker.Client) (ids []string) { | ||
networks, err := client.ListNetworks() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, net := range networks { | ||
if net.Driver == "bridge" { | ||
ids = append(ids, net.ID) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule go-dockerclient
added at
ddbe4d