about summary refs log tree commit diff
path: root/src/network.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-09-07 14:04:58 +0200
committermaride <maride@darknebu.la>2018-09-07 14:04:58 +0200
commit5dde5e766c0178a80416e3ec8f3ffc55a2691ed2 (patch)
tree0f38dcfa29ce4205a8204880247594f97119793f /src/network.go
parent2f94d8b6cdf325f23a0e40db1097ebbcd7f7c0e1 (diff)
Cleanup, react to signals (\^C), tidy up and sort code
Diffstat (limited to 'src/network.go')
-rw-r--r--src/network.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/network.go b/src/network.go
new file mode 100644
index 0000000..93abaca
--- /dev/null
+++ b/src/network.go
@@ -0,0 +1,82 @@
+package main
+
+import (
+	"github.com/docker/docker/api/types"
+	"time"
+)
+
+
+const(
+	vpnHostNetworkName = "vpnhostnet"
+)
+
+var (
+	containerNetworkID string
+	vpnHostNetworkID string
+)
+
+func setupNetwork(networkName string, internalNetwork bool) (string, error) {
+	setupContext()
+	setupDockerCLI()
+
+	// Cleanup old network(s) with that name
+	err := deleteNetwork(networkName)
+	if err != nil {
+		return "", err
+	}
+
+	// create our new network
+	response, err := dockerCli.NetworkCreate(dockerCtx, networkName, types.NetworkCreate{
+		Internal: internalNetwork,
+	})
+
+	if err != nil {
+		return "", err
+	}
+
+	return response.ID, nil
+}
+
+func deleteNetwork(networkName string) (error) {
+	// try to cleanup old instances of that network
+	// BUT we need to get the ID used on the old network, and we only have the name of the network
+	// so we get a list of all networks and skip through that array...:
+	networks, err := dockerCli.NetworkList(dockerCtx, types.NetworkListOptions{})
+	// TODO: we sure as hell can filter that with NetworkListOptions!
+	if err != nil {
+		return err
+	}
+
+	// Iterate over all networks
+	for _, network := range networks {
+		if network.Name == networkName {
+			// Found a network with that name
+			// We need to stop all containers attached to it before we can remove the network
+
+			// Get all containers attached to the network
+			resource, err := dockerCli.NetworkInspect(dockerCtx, network.ID, types.NetworkInspectOptions{})
+			if err != nil {
+				return err
+			}
+
+			// Loop over containers attached to that network and stop them
+			timeout := time.Second
+			for _, container := range resource.Containers {
+				// Stop container
+				dockerCli.ContainerStop(dockerCtx, container.EndpointID, &timeout)
+				time.Sleep(timeout)
+				// We ignore the err return variable here.
+				// It may happen that this container is already stopped, but is still part of the network
+				// TODO: Inspect the container to check that - we don't want to ignore errors!
+			}
+
+			// Now we can remove the network
+			err = dockerCli.NetworkRemove(dockerCtx, network.ID)
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
\ No newline at end of file