Files
weave/prog/weaveutil/plugin_network.go
T
Matthias Radestock 31b303a419 move plugin network creation into weaveutil
Note that we remove the ability to set the weavemesh network name, or
disabling it altogether. This was always poorly support, since the
removal operation only ever operated on the default name. The option
was also not listed in the usage, though it was in the docs.
2016-01-15 08:52:57 +00:00

74 lines
1.6 KiB
Go

/* various weave docker network plugin operations */
package main
import (
"fmt"
"github.com/fsouza/go-dockerclient"
)
func createPluginNetwork(args []string) error {
if len(args) != 2 {
cmdUsage("create-plugin-network", "<network-name> <driver-name>")
}
networkName := args[0]
driverName := args[1]
d, err := newDockerClient()
if err != nil {
return err
}
_, err = d.NetworkInfo(networkName)
if err == nil {
return nil
}
if _, ok := err.(*docker.NoSuchNetwork); !ok {
return nil // TODO is this right?
}
_, err = d.CreateNetwork(
docker.CreateNetworkOptions{
Name: networkName,
CheckDuplicate: true,
Driver: driverName,
IPAM: docker.IPAMOptions{Driver: driverName},
})
if err != nil {
return fmt.Errorf("unable to create network: %s", err)
}
return nil
}
func removePluginNetwork(args []string) error {
if len(args) != 1 {
cmdUsage("remove-plugin-network", "<network-name>")
}
networkName := args[0]
d, err := newDockerClient()
if err != nil {
return err
}
_, err = d.NetworkInfo(networkName)
if err != nil {
// network probably doesn't exist; TODO check this better
return nil
}
err = d.RemoveNetwork(networkName)
if err != nil {
return fmt.Errorf("unable to remove network: %s", err)
}
return nil
}
func newDockerClient() (*docker.Client, error) {
// API 1.21 is the first version that supports docker network
// commands
c, err := docker.NewVersionedClientFromEnv("1.21")
if err != nil {
return nil, fmt.Errorf("unable to connect to docker: %s", err)
}
_, err = c.Version()
if err != nil {
return nil, fmt.Errorf("unable to connect to docker: %s", err)
}
return c, nil
}