Merge pull request #1901 from weaveworks/1897-weaveutil-plugin-network

move plugin network creation/removal into weaveutil
This commit is contained in:
Bryan Boreham committed 2016-01-15 11:07:38 +00:00
commit efd4fc4704
5 files changed
+84 -72

No files matched your search

+6 -59
View File
@@ -5,12 +5,8 @@ import (
"fmt"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/docker/libnetwork/ipamapi"
go_docker "github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/docker"
ipamplugin "github.com/weaveworks/weave/plugin/ipam"
@@ -27,9 +23,7 @@ func main() {
nameserver string
meshAddress string
logLevel string
meshNetworkName string
noMulticastRoute bool
removeNetwork bool
)
flag.BoolVar(&justVersion, "version", false, "print version and exit")
@@ -37,9 +31,7 @@ func main() {
flag.StringVar(&address, "socket", "/run/docker/plugins/weave.sock", "socket on which to listen")
flag.StringVar(&nameserver, "nameserver", "", "nameserver to provide to containers")
flag.StringVar(&meshAddress, "meshsocket", "/run/docker/plugins/weavemesh.sock", "socket on which to listen in mesh mode")
flag.StringVar(&meshNetworkName, "mesh-network-name", "weave", "network name to create in mesh mode")
flag.BoolVar(&noMulticastRoute, "no-multicast-route", false, "do not add a multicast route to network endpoints")
flag.BoolVar(&removeNetwork, "remove-network", false, "remove mesh network and exit")
flag.Parse()
@@ -56,16 +48,6 @@ func main() {
Log.Fatalf("unable to connect to docker: %s", err)
}
if removeNetwork {
if _, err = dockerClient.Client.NetworkInfo(meshNetworkName); err == nil {
err = dockerClient.Client.RemoveNetwork(meshNetworkName)
if err != nil {
Log.Fatalf("unable to remove network: %s", err)
}
}
os.Exit(0)
}
Log.Println("Weave plugin", version, "Command line options:", os.Args[1:])
Log.Info(dockerClient.Info())
@@ -86,26 +68,12 @@ func main() {
defer meshListener.Close()
}
if meshNetworkName != "" {
createNetwork(dockerClient, meshNetworkName, meshAddress)
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, os.Kill, syscall.SIGTERM)
select {
case sig := <-sigChan:
Log.Debugf("Caught signal %s; shutting down", sig)
if err := dockerClient.Client.RemoveNetwork(meshNetworkName); err != nil {
Log.Fatal(err)
}
case err := <-endChan:
if err != nil {
Log.Errorf("Error from listener: %s", err)
globalListener.Close()
meshListener.Close()
os.Exit(1)
}
err = <-endChan
if err != nil {
Log.Errorf("Error from listener: %s", err)
globalListener.Close()
meshListener.Close()
os.Exit(1)
}
}
@@ -140,24 +108,3 @@ func listenAndServe(dockerClient *docker.Client, address, nameserver string, noM
return listener, nil
}
func createNetwork(dockerClient *docker.Client, networkName, address string) {
if _, err := dockerClient.Client.NetworkInfo(networkName); err == nil {
Log.Printf("Docker network '%s' already exists", networkName)
} else if _, ok := err.(*go_docker.NoSuchNetwork); ok {
driverName := strings.TrimSuffix(address, ".sock")
if i := strings.LastIndex(driverName, "/"); i >= 0 {
driverName = driverName[i+1:]
}
options := go_docker.CreateNetworkOptions{
Name: networkName,
CheckDuplicate: true,
Driver: driverName,
IPAM: go_docker.IPAMOptions{Driver: driverName},
}
_, err := dockerClient.Client.CreateNetwork(options)
if err != nil {
Log.Fatalf("Error creating network: %s", err)
}
}
}
+2
View File
@@ -16,6 +16,8 @@ func init() {
"create-datapath": createDatapath,
"delete-datapath": deleteDatapath,
"add-datapath-interface": addDatapathInterface,
"create-plugin-network": createPluginNetwork,
"remove-plugin-network": removePluginNetwork,
}
}
+67
View File
@@ -0,0 +1,67 @@
/* 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.CreateNetwork(
docker.CreateNetworkOptions{
Name: networkName,
CheckDuplicate: true,
Driver: driverName,
IPAM: docker.IPAMOptions{Driver: driverName},
})
if err != docker.ErrNetworkAlreadyExists && err != nil {
// Despite appearances to the contrary, CreateNetwork does
// sometimes(always?) *not* return ErrNetworkAlreadyExists
// when the network already exists. Hence we need to check for
// this explicitly.
if _, err2 := d.NetworkInfo(networkName); err2 != 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.RemoveNetwork(networkName)
if _, ok := err.(*docker.NoSuchNetwork); !ok && 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
}
-2
View File
@@ -73,8 +73,6 @@ The plugin command-line arguments are:
* `--log-level=debug|info|warning|error`, which tells the plugin
how much information to emit for debugging.
* `--mesh-network-name=<name>`: set it to blank to disable creation
of a default network, or a name of your own choice.
* `--no-multicast-route`: stop weave adding a static IP route for
multicast traffic on its interface
+9 -11
View File
@@ -448,7 +448,8 @@ util_op() {
if command_exists weaveutil ; then
weaveutil "$@"
else
docker run --rm --privileged --net=host -v /proc:/hostproc -e PROCFS=/hostproc \
docker run --rm --privileged --net=host $(docker_sock_options) \
-v /proc:/hostproc -e PROCFS=/hostproc \
--entrypoint=/usr/bin/weaveutil $EXEC_IMAGE "$@"
fi
}
@@ -1682,26 +1683,23 @@ launch_plugin_if_not_running() {
# Any other kind of error code from check_not_running is a failure.
[ $retval -gt 0 ] && return $retval
PLUGIN_CONTAINER=$(docker run --privileged -d --name=$PLUGIN_CONTAINER_NAME \
if ! PLUGIN_CONTAINER=$(docker run --privileged -d --name=$PLUGIN_CONTAINER_NAME \
--restart=always \
--net=host \
$(docker_sock_options) \
-v /run/docker/plugins:/run/docker/plugins \
$PLUGIN_IMAGE "$@")
$PLUGIN_IMAGE "$@") ; then
return 1
fi
util_op create-plugin-network weave weavemesh
}
plugin_disabled() {
[ -n "$WEAVE_NO_PLUGIN" ] || ! check_docker_server_api_version 1.21
}
remove_plugin_network() {
docker run --rm --net=host \
-v /var/run/docker.sock:/var/run/docker.sock \
$PLUGIN_IMAGE --remove-network
}
stop_plugin() {
remove_plugin_network
util_op remove-plugin-network weave
stop $PLUGIN_CONTAINER_NAME "Plugin"
}
@@ -2137,7 +2135,7 @@ EOF
;;
reset)
[ $# -eq 0 ] || usage
plugin_disabled || remove_plugin_network || \
plugin_disabled || util_op remove-plugin-network weave || \
echo "WARNING: docker may hang in this state; re-launch weave and remove attached containers before shutting down" >&2
warn_if_stopping_proxy_in_env
call_weave DELETE /peer >/dev/null 2>&1 || true