Do "docker plugin inspect" inside weaveutil

We cannot do it from weaveexec, because the latter is distributed
with an old version of Docker client which does not support
Plugins API.
This commit is contained in:
Martynas Pumputis authored and Bryan Boreham committed 2017-04-12 13:05:40 +00:00
1 parent b3866ebd94
commit 0ff37d3492
3 files changed
+79 -44

No files matched your search

+26 -25
View File
@@ -14,31 +14,32 @@ var commands map[string]func([]string) error
func init() { func init() {
commands = map[string]func([]string) error{ commands = map[string]func([]string) error{
"help": help, "help": help,
"netcheck": netcheck, "netcheck": netcheck,
"docker-tls-args": dockerTLSArgs, "docker-tls-args": dockerTLSArgs,
"create-datapath": createDatapath, "create-datapath": createDatapath,
"delete-datapath": deleteDatapath, "delete-datapath": deleteDatapath,
"check-datapath": checkDatapath, "check-datapath": checkDatapath,
"add-datapath-interface": addDatapathInterface, "add-datapath-interface": addDatapathInterface,
"create-plugin-network": createPluginNetwork, "create-plugin-network": createPluginNetwork,
"remove-plugin-network": removePluginNetwork, "remove-plugin-network": removePluginNetwork,
"container-addrs": containerAddrs, "container-addrs": containerAddrs,
"process-addrs": processAddrs, "process-addrs": processAddrs,
"attach-container": attach, "attach-container": attach,
"detach-container": detach, "detach-container": detach,
"configure-arp": configureARP, "configure-arp": configureARP,
"check-iface": checkIface, "check-iface": checkIface,
"del-iface": delIface, "del-iface": delIface,
"setup-iface": setupIface, "setup-iface": setupIface,
"list-netdevs": listNetDevs, "list-netdevs": listNetDevs,
"cni-net": cniNet, "cni-net": cniNet,
"cni-ipam": cniIPAM, "cni-ipam": cniIPAM,
"expose-nat": exposeNAT, "expose-nat": exposeNAT,
"bridge-ip": bridgeIP, "bridge-ip": bridgeIP,
"unique-id": uniqueID, "unique-id": uniqueID,
"swarm-manager-peers": swarmManagerPeers, "swarm-manager-peers": swarmManagerPeers,
"is-swarm-manager": isSwarmManager, "is-swarm-manager": isSwarmManager,
"is-docker-plugin-enabled": isDockerPluginEnabled,
} }
} }
+31
View File
@@ -2,8 +2,10 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/docker/docker/client"
docker "github.com/fsouza/go-dockerclient" docker "github.com/fsouza/go-dockerclient"
"github.com/weaveworks/weave/plugin/net" "github.com/weaveworks/weave/plugin/net"
@@ -71,6 +73,34 @@ while Weave is not running`, networkName, containers)
return nil return nil
} }
// Exits with 0 if the given plugin (v2) is enabled.
func isDockerPluginEnabled(args []string) error {
if len(args) != 1 {
cmdUsage("is-docker-plugin-enabled", "<plugin-name>")
}
pluginName := args[0]
// This is messed up: we are using docker/docker/client instead of
// fsouza/go-dockerclient because the latter does not support plugins.
c, err := client.NewEnvClient()
if err != nil {
return fmt.Errorf("unable to connect to docker: %s", err)
}
ctx := context.Background()
p, _, err := c.PluginInspectWithRaw(ctx, pluginName)
if err != nil {
return err
}
if !p.Enabled {
return fmt.Errorf("plugin %q is disabled", pluginName)
}
return nil
}
func newDockerClient() (*docker.Client, error) { func newDockerClient() (*docker.Client, error) {
// API 1.21 is the first version that supports docker network // API 1.21 is the first version that supports docker network
// commands // commands
@@ -82,5 +112,6 @@ func newDockerClient() (*docker.Client, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to connect to docker: %s", err) return nil, fmt.Errorf("unable to connect to docker: %s", err)
} }
return c, nil return c, nil
} }
+22 -19
View File
@@ -883,6 +883,24 @@ attach() {
util_op attach-container $ATTACH_ARGS $CONTAINER $BRIDGE $MTU "$@" util_op attach-container $ATTACH_ARGS $CONTAINER $BRIDGE $MTU "$@"
} }
check_not_running_v2_plugin() {
if is_docker_plugin_enabled $PLUGIN_V2_NAME; then
echo "Docker plugin \"$PLUGIN_V2_NAME\" is already running." >&2
return 1
fi
}
# Docker Plugin V2
is_docker_plugin_enabled() {
util_op is-docker-plugin-enabled $PLUGIN_V2_NAME 2>/dev/null
}
# Docker Plugin (part of weaver process, so V2 or V1).
# Assumes that plugin is running inside Docker container.
is_plugin_enabled() {
docker inspect -f '{{.Config.Cmd}}' $CONTAINER_NAME | grep -q -- "--plugin"
}
###################################################################### ######################################################################
# functions for interacting with containers # functions for interacting with containers
###################################################################### ######################################################################
@@ -1206,21 +1224,6 @@ check_running() {
fi fi
} }
check_not_running_v2_plugin() {
is_docker_plugin_enabled "$1" && echo "$1 is already running." >&2 && return 1
}
# Docker Plugin V2
is_docker_plugin_enabled() {
[ "$(docker plugin inspect -f '{{ .Enabled }}' "$1" 2>/dev/null)" = "true" ]
}
# Docker Plugin (part of weaver process, so V2 or V1).
# Assumes that plugin is running inside Docker container.
is_plugin_enabled() {
docker inspect -f '{{.Config.Cmd}}' $CONTAINER_NAME | grep -q -- "--plugin"
}
# Execute $@ only if the weave container is running # Execute $@ only if the weave container is running
when_weave_running() { when_weave_running() {
! check_running $CONTAINER_NAME 2>/dev/null || "$@" ! check_running $CONTAINER_NAME 2>/dev/null || "$@"
@@ -1981,7 +1984,7 @@ EOF
deprecation_warnings "$@" deprecation_warnings "$@"
check_not_running router $CONTAINER_NAME $BASE_IMAGE check_not_running router $CONTAINER_NAME $BASE_IMAGE
check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE
check_not_running_v2_plugin "$PLUGIN_V2_NAME" check_not_running_v2_plugin
COMMON_ARGS=$(common_launch_args "$@") COMMON_ARGS=$(common_launch_args "$@")
launch_router --plugin "$@" launch_router --plugin "$@"
launch_proxy $COMMON_ARGS launch_proxy $COMMON_ARGS
@@ -1989,7 +1992,7 @@ EOF
launch-router) launch-router)
deprecation_warnings "$@" deprecation_warnings "$@"
check_not_running router $CONTAINER_NAME $BASE_IMAGE check_not_running router $CONTAINER_NAME $BASE_IMAGE
check_not_running_v2_plugin "$PLUGIN_V2_NAME" check_not_running_v2_plugin
launch_router "$@" launch_router "$@"
echo $ROUTER_CONTAINER echo $ROUTER_CONTAINER
;; ;;
@@ -2007,7 +2010,7 @@ EOF
launch-proxy) launch-proxy)
deprecation_warnings "$@" deprecation_warnings "$@"
check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE
check_not_running_v2_plugin "$PLUGIN_V2_NAME" check_not_running_v2_plugin
launch_proxy "$@" launch_proxy "$@"
echo $PROXY_CONTAINER echo $PROXY_CONTAINER
;; ;;
@@ -2052,7 +2055,7 @@ EOF
echo " Service: proxy" echo " Service: proxy"
echo " Address: $PROXY_ADDRS" echo " Address: $PROXY_ADDRS"
fi fi
if [ -z "$SUB_STATUS" ] && ( is_docker_plugin_enabled "$PLUGIN_V2_NAME" || is_plugin_enabled ) ; then if [ -z "$SUB_STATUS" ] && ( is_docker_plugin_enabled || is_plugin_enabled ) ; then
echo echo
echo " Service: plugin" echo " Service: plugin"
echo " DriverName: weave" echo " DriverName: weave"