forked from taybenlor/dinghy-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
join-networks.go
119 lines (96 loc) · 2.75 KB
/
join-networks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"flag"
"fmt"
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)
}
// get actual container ID, in case name is passed in
container, err := client.InspectContainer(*containerName)
if err != nil {
panic(err)
}
containerID := container.ID
currentNetworks := getJoinedNetworks(client, containerID)
bridgeNetworks := getActiveBridgeNetworks(client, containerID)
toJoin := getNetworksToJoin(currentNetworks, bridgeNetworks)
toLeave := getNetworksToLeave(currentNetworks, bridgeNetworks)
fmt.Printf("currently in %d networks, found %d bridge networks, %d to join, %d to leave\n", len(currentNetworks), len(bridgeNetworks), len(toJoin), len(toLeave))
for _, id := range toLeave {
fmt.Printf("leaving network %s\n", id)
err := client.DisconnectNetwork(id, docker.NetworkConnectionOptions{
Container: *containerName,
})
if err != nil {
panic(err)
}
}
for _, id := range toJoin {
fmt.Printf("joining network %s\n", 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 getActiveBridgeNetworks(client *docker.Client, containerName string) (networks map[string]bool) {
networks = make(map[string]bool)
allNetworks, err := client.ListNetworks()
if err != nil {
panic(err)
}
for _, netOverview := range allNetworks {
if netOverview.Driver == "bridge" {
// grab the network details (including the list of containers)
net, err := client.NetworkInfo(netOverview.ID)
if err != nil {
panic(err)
}
_, containsSelf := net.Containers[containerName]
if net.Options["com.docker.network.bridge.default_bridge"] == "true" ||
len(net.Containers) > 1 ||
(len(net.Containers) == 1 && !containsSelf) {
networks[net.ID] = true
}
}
}
return
}
func getNetworksToJoin(currentNetworks map[string]bool, bridgeNetworks map[string]bool) (ids []string) {
for id := range bridgeNetworks {
if !currentNetworks[id] {
ids = append(ids, id)
}
}
return
}
func getNetworksToLeave(currentNetworks map[string]bool, bridgeNetworks map[string]bool) (ids []string) {
for id := range currentNetworks {
if !bridgeNetworks[id] {
ids = append(ids, id)
}
}
return
}