diff --git a/Makefile b/Makefile index e3e5f21e..44754692 100644 --- a/Makefile +++ b/Makefile @@ -93,38 +93,35 @@ WEAVEWAIT_EXE=prog/weavewait/weavewait WEAVEWAIT_NOOP_EXE=prog/weavewait/weavewait_noop WEAVEWAIT_NOMCAST_EXE=prog/weavewait/weavewait_nomcast WEAVEUTIL_EXE=prog/weaveutil/weaveutil -PLUGIN_EXE=prog/plugin/plugin RUNNER_EXE=tools/runner/runner MANIFEST_TOOL_DIR=vendor/github.com/estesp/manifest-tool MANIFEST_TOOL_EXE=$(MANIFEST_TOOL_DIR)/manifest-tool TEST_TLS_EXE=test/tls/tls # All binaries together in a list -EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(KUBEPEERS_EXE) $(WEAVENPC_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(WEAVEUTIL_EXE) $(PLUGIN_EXE) $(RUNNER_EXE) $(TEST_TLS_EXE) $(MANIFEST_TOOL_EXE) +EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(KUBEPEERS_EXE) $(WEAVENPC_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(WEAVEUTIL_EXE) $(RUNNER_EXE) $(TEST_TLS_EXE) $(MANIFEST_TOOL_EXE) # These stamp files are used to mark the current state of the build; whether an image has been built or not BUILD_UPTODATE=.build.uptodate WEAVER_UPTODATE=.weaver$(ARCH_EXT).uptodate WEAVEEXEC_UPTODATE=.weaveexec$(ARCH_EXT).uptodate -PLUGIN_UPTODATE=.plugin$(ARCH_EXT).uptodate PLUGINV2_UPTODATE=.pluginv2$(ARCH_EXT).uptodate WEAVEKUBE_UPTODATE=.weavekube$(ARCH_EXT).uptodate WEAVENPC_UPTODATE=.weavenpc$(ARCH_EXT).uptodate WEAVEDB_UPTODATE=.weavedb.uptodate -IMAGES_UPTODATE=$(WEAVER_UPTODATE) $(WEAVEEXEC_UPTODATE) $(PLUGIN_UPTODATE) $(WEAVEKUBE_UPTODATE) $(WEAVENPC_UPTODATE) +IMAGES_UPTODATE=$(WEAVER_UPTODATE) $(WEAVEEXEC_UPTODATE) $(WEAVEKUBE_UPTODATE) $(WEAVENPC_UPTODATE) # The names of the images. Note that the images for other architectures than amd64 have a suffix in the image name. WEAVER_IMAGE=$(DOCKERHUB_USER)/weave$(ARCH_EXT) WEAVEEXEC_IMAGE=$(DOCKERHUB_USER)/weaveexec$(ARCH_EXT) -PLUGIN_IMAGE=$(DOCKERHUB_USER)/plugin$(ARCH_EXT) WEAVEKUBE_IMAGE=$(DOCKERHUB_USER)/weave-kube$(ARCH_EXT) WEAVENPC_IMAGE=$(DOCKERHUB_USER)/weave-npc$(ARCH_EXT) BUILD_IMAGE=weaveworks/weavebuild WEAVEDB_IMAGE=$(DOCKERHUB_USER)/weavedb PLUGINV2_IMAGE=$(DOCKERHUB_USER)/pluginv2$(ARCH_EXT) -IMAGES=$(WEAVER_IMAGE) $(WEAVEEXEC_IMAGE) $(PLUGIN_IMAGE) $(WEAVEKUBE_IMAGE) $(WEAVENPC_IMAGE) $(WEAVEDB_IMAGE) +IMAGES=$(WEAVER_IMAGE) $(WEAVEEXEC_IMAGE) $(WEAVEKUBE_IMAGE) $(WEAVENPC_IMAGE) $(WEAVEDB_IMAGE) PLUGINV2_WORK_DIR="prog/plugin-v2/rootfs" @@ -156,12 +153,12 @@ testrunner: $(RUNNER_EXE) $(TEST_TLS_EXE) $(WEAVER_EXE) $(WEAVEPROXY_EXE) $(WEAVEUTIL_EXE): common/*.go common/*/*.go net/*.go net/*/*.go $(WEAVER_EXE): router/*.go ipam/*.go ipam/*/*.go db/*.go nameserver/*.go prog/weaver/*.go +$(WEAVER_EXE): api/*.go plugin/*.go plugin/*/* $(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go $(WEAVEUTIL_EXE): prog/weaveutil/*.go net/*.go plugin/net/*.go plugin/ipam/*.go db/*.go $(SIGPROXY_EXE): prog/sigproxy/*.go $(KUBEPEERS_EXE): prog/kube-peers/*.go $(WEAVENPC_EXE): prog/weave-npc/*.go npc/*.go npc/*/*.go -$(PLUGIN_EXE): prog/plugin/*.go plugin/*/*.go api/*.go common/*.go common/docker/*.go net/*.go $(TEST_TLS_EXE): test/tls/*.go $(RUNNER_EXE): tools/runner/*.go $(MANIFEST_TOOL_EXE): $(MANIFEST_TOOL_DIR)/*.go @@ -191,7 +188,7 @@ else exes: $(EXES) -$(WEAVER_EXE) $(WEAVEPROXY_EXE) $(PLUGIN_EXE): +$(WEAVER_EXE) $(WEAVEPROXY_EXE): ifeq ($(COVERAGE),true) $(eval COVERAGE_MODULES := $(shell (go list ./$(@D); go list -f '{{join .Deps "\n"}}' ./$(@D) | grep "^$(PACKAGE_BASE)/") | grep -v "^$(PACKAGE_BASE)/vendor/" | paste -s -d,)) go test -c -o ./$@ $(BUILD_FLAGS) -v -covermode=atomic -coverpkg $(COVERAGE_MODULES) ./$(@D)/ @@ -268,14 +265,10 @@ $(WEAVEEXEC_UPTODATE): prog/weaveexec/Dockerfile.$(DOCKERHUB_USER) prog/weaveexe $(SUDO) DOCKER_HOST=$(DOCKER_HOST) docker build -f prog/weaveexec/Dockerfile.$(DOCKERHUB_USER) -t $(WEAVEEXEC_IMAGE) prog/weaveexec touch $@ -$(PLUGIN_UPTODATE): prog/plugin/Dockerfile.$(DOCKERHUB_USER) $(PLUGIN_EXE) $(WEAVER_UPTODATE) - $(SUDO) docker build -f prog/plugin/Dockerfile.$(DOCKERHUB_USER) -t $(PLUGIN_IMAGE) prog/plugin - touch $@ - # TODO(mp) cleanup! -pluginv2: prog/plugin-v2/launch.sh prog/plugin-v2/config.json $(PLUGIN_UPTODATE) +pluginv2: prog/plugin-v2/launch.sh prog/plugin-v2/config.json $(WEAVER_UPTODATE) -$(SUDO) docker rm -f buildpluginv2 - $(SUDO) docker create --name=buildpluginv2 $(PLUGIN_IMAGE) true + $(SUDO) docker create --name=buildpluginv2 $(WEAVER_IMAGE) true rm -rf $(PLUGINV2_WORK_DIR) mkdir $(PLUGINV2_WORK_DIR) docker export buildpluginv2 | tar -x -C $(PLUGINV2_WORK_DIR) diff --git a/api/weaveapi.go b/api/weaveapi.go index 210618a8..19a567a2 100644 --- a/api/weaveapi.go +++ b/api/weaveapi.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "strings" + "time" ) const ( @@ -76,6 +77,20 @@ func (client *Client) Connect(remote string) error { return err } +// IsReady returns true if the API server is up and running +func (client *Client) IsReady() bool { + _, err := client.httpVerb("GET", "/status", nil) + + return err == nil +} + +// WaitAPIServer waits until the API server is ready to serve. +func (client *Client) WaitAPIServer(n int) { + for i := 0; i < n && !client.IsReady(); i++ { + time.Sleep(2 * time.Second) + } +} + type Logger interface { Infof(string, ...interface{}) Debugf(string, ...interface{}) diff --git a/prog/plugin/main.go b/plugin/plugin.go similarity index 60% rename from prog/plugin/main.go rename to plugin/plugin.go index 3084f219..70876c04 100644 --- a/prog/plugin/main.go +++ b/plugin/plugin.go @@ -1,15 +1,12 @@ -package main +package plugin import ( - "flag" "fmt" "net" "net/http" "os" - "os/signal" "path" "strings" - "syscall" "github.com/docker/libnetwork/ipamapi" weaveapi "github.com/weaveworks/weave/api" @@ -21,55 +18,21 @@ import ( "github.com/weaveworks/weave/plugin/skel" ) -var version = "unreleased" - var Log = common.Log -func main() { - var ( - justVersion bool - dockerAPI string - address string - meshAddress string - logLevel string - noMulticastRoute bool - - defaultDockerHost = "unix:///var/run/docker.sock" - ) - - if val := os.Getenv("DOCKER_HOST"); val != "" { - defaultDockerHost = val - } - - flag.BoolVar(&justVersion, "version", false, "print version and exit") - flag.StringVar(&logLevel, "log-level", "info", "logging level (debug, info, warning, error)") - flag.StringVar(&dockerAPI, "docker-api", defaultDockerHost, "Docker API endpoint") - flag.StringVar(&address, "socket", "/run/docker/plugins/weave.sock", "socket on which to listen") - flag.StringVar(&meshAddress, "meshsocket", "/run/docker/plugins/weavemesh.sock", "socket on which to listen in mesh mode") - flag.BoolVar(&noMulticastRoute, "no-multicast-route", false, "deprecated (this is now the default)") - - flag.Parse() - - if justVersion { - fmt.Printf("weave plugin %s\n", version) - os.Exit(0) - } - - common.SetLogLevel(logLevel) - - weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log) +func Start(weaveAPIAddr string, dockerAPIAddr string, address string, meshAddress string, noMulticastRoute bool) { + weave := weaveapi.NewClient(weaveAPIAddr, Log) var dockerClient *docker.Client var err error - if dockerAPI != "" { + if dockerAPIAddr != "" { // API 1.21 is the first version that supports docker network commands - dockerClient, err = docker.NewVersionedClient(dockerAPI, "1.21") + dockerClient, err = docker.NewVersionedClient(dockerAPIAddr, "1.21") if err != nil { Log.Fatalf("unable to connect to docker: %s", err) } } - Log.Println("Weave plugin", version, "Command line options:", os.Args[1:]) if noMulticastRoute { Log.Warning("--no-multicast-route option has been removed; multicast is off by default") } @@ -79,6 +42,10 @@ func main() { Log.Info(dockerClient.Info()) } + Log.Info("Waiting for Weave API Server...") + weave.WaitAPIServer(30) + Log.Info("Finished waiting for Weave API Server") + err = run(dockerClient, weave, address, meshAddress) if err != nil { Log.Fatal(err) @@ -87,6 +54,7 @@ func main() { func run(dockerClient *docker.Client, weave *weaveapi.Client, address, meshAddress string) error { endChan := make(chan error, 1) + if address != "" { globalListener, err := listenAndServe(dockerClient, weave, address, endChan, "global", false) if err != nil { @@ -104,21 +72,13 @@ func run(dockerClient *docker.Client, weave *weaveapi.Client, address, meshAddre defer meshListener.Close() } - statusListener, err := weavenet.ListenUnixSocket("/home/weave/status.sock") + statusListener, err := weavenet.ListenUnixSocket("/home/weave/plugin-status.sock") if err != nil { return err } go serveStatus(statusListener) - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) - select { - case sig := <-sigChan: - Log.Debugf("Caught signal %s; shutting down", sig) - return nil - case err := <-endChan: - return err - } + return <-endChan } func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, endChan chan<- error, scope string, withIpam bool) (net.Listener, error) { diff --git a/prog/plugin-v2/launch.sh b/prog/plugin-v2/launch.sh index c5bfc3b3..28decd7f 100755 --- a/prog/plugin-v2/launch.sh +++ b/prog/plugin-v2/launch.sh @@ -8,25 +8,23 @@ HTTP_ADDR=${WEAVE_HTTP_ADDR:-127.0.0.1:6784} STATUS_ADDR=${WEAVE_STATUS_ADDR:-0.0.0.0:6782} HOST_ROOT=${HOST_ROOT:-/host} WEAVE_DIR="/host/var/lib/weave" -LOG_FILE_WEAVER="$WEAVE_DIR/weaver.log" -LOG_FILE_PLUGIN="$WEAVE_DIR/plugin.log" mkdir $WEAVE_DIR || true -echo "Starting launch.sh" >>$LOG_FILE_WEAVER +echo "Starting launch.sh" # Check if the IP range overlaps anything existing on the host /usr/bin/weaveutil netcheck $IPALLOC_RANGE weave -SWARM_PEERS=$(/usr/bin/weaveutil swarm-manager-peers 2>>$LOG_FILE_WEAVER) -! /usr/bin/weaveutil is-swarm-manager 2>>$LOG_FILE_WEAVER || IS_SWARM_MANAGER=1 +SWARM_MANAGER_PEERS=$(/usr/bin/weaveutil swarm-manager-peers) +! /usr/bin/weaveutil is-swarm-manager || IS_SWARM_MANAGER=1 # Prevent from restoring from a persisted peers list rm -f "/restart.sentinel" /home/weave/weave --local create-bridge \ - --proc-path=/host/proc \ - --weavedb-dir-path=$WEAVE_DIR \ - --force >>$LOG_FILE_WEAVER 2>&1 + --proc-path=/host/proc \ + --weavedb-dir-path=$WEAVE_DIR \ + --force BRIDGE_OPTIONS="--datapath=datapath" if [ "$(/home/weave/weave --local bridge-type)" = "bridge" ]; then @@ -42,36 +40,18 @@ fi if [ -z "$IPALLOC_INIT" ]; then if [ $IS_SWARM_MANAGER == "1" ]; then - IPALLOC_INIT="consensus=$(echo $SWARM_PEERS | wc -l)" + IPALLOC_INIT="consensus=$(echo $SWARM_MANAGER_PEERS | wc -l)" else IPALLOC_INIT="observer" fi fi -/home/weave/weaver $EXTRA_ARGS --port=6783 $BRIDGE_OPTIONS \ +exec /home/weave/weaver $EXTRA_ARGS --port=6783 $BRIDGE_OPTIONS \ --http-addr=$HTTP_ADDR --status-addr=$STATUS_ADDR --docker-api='' --no-dns \ --ipalloc-range=$IPALLOC_RANGE \ --ipalloc-init $IPALLOC_INIT \ --nickname "$(hostname)" \ --log-level=debug \ --db-prefix="$WEAVE_DIR/weave" \ - $(echo $SWARM_PEERS | tr '\n' ' ') \ - >>$LOG_FILE_WEAVER 2>&1 & -WEAVE_PID=$! - -# Wait for weave process to become responsive -while true; do - curl $HTTP_ADDR/status >/dev/null 2>&1 && break - if ! kill -0 $WEAVE_PID >/dev/null 2>&1; then - echo Weave process has died >&2 - exit 1 - fi - sleep 1 -done - -# TODO(mp) docker api? -/home/weave/plugin $EXTRA_ARGS --log-level=debug --meshsocket='' --docker-api='' >>$LOG_FILE_PLUGIN 2>&1 & - -echo "End of launch.sh" >>$LOG_FILE_WEAVER - -wait $WEAVE_PID + --plugin \ + $(echo $SWARM_MANAGER_PEERS | tr '\n' ' ') diff --git a/prog/plugin/Dockerfile.template b/prog/plugin/Dockerfile.template deleted file mode 100644 index 4de1c580..00000000 --- a/prog/plugin/Dockerfile.template +++ /dev/null @@ -1,3 +0,0 @@ -FROM DOCKERHUB_USER/weaveARCH_EXT -ADD ./plugin /home/weave/ -ENTRYPOINT ["/home/weave/plugin"] diff --git a/prog/plugin/main_test.go b/prog/plugin/main_test.go deleted file mode 100644 index 1873f4a3..00000000 --- a/prog/plugin/main_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "testing" - - weavetest "github.com/weaveworks/weave/testing" -) - -func TestMain(t *testing.T) { - weavetest.TrimTestArgs() - main() -} diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 8b077da3..d1b1515b 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -25,6 +25,7 @@ import ( "github.com/weaveworks/weave/nameserver" weavenet "github.com/weaveworks/weave/net" "github.com/weaveworks/weave/net/address" + "github.com/weaveworks/weave/plugin" weave "github.com/weaveworks/weave/router" ) @@ -150,6 +151,10 @@ func main() { discoveryEndpoint string token string advertiseAddress string + pluginSocket string + pluginMeshSocket string + noMulticastRoute bool + enablePlugin bool defaultDockerHost = "unix:///var/run/docker.sock" ) @@ -195,6 +200,11 @@ func main() { mflag.StringVar(&token, []string{"-token"}, "", "token for peer discovery") mflag.StringVar(&advertiseAddress, []string{"-advertise-address"}, "", "address to advertise for peer discovery") + mflag.BoolVar(&enablePlugin, []string{"-plugin"}, false, "enable Docker plugin") + mflag.StringVar(&pluginSocket, []string{"-plugin-socket"}, "/run/docker/plugins/weave.sock", "plugin socket on which to listen") + mflag.StringVar(&pluginMeshSocket, []string{"-plugin-mesh-socket"}, "/run/docker/plugins/weavemesh.sock", "plugin socket on which to listen in mesh mode") + mflag.BoolVar(&noMulticastRoute, []string{"-no-multicast-route"}, false, "deprecated (this is now the default)") + // crude way of detecting that we probably have been started in a // container, with `weave launch` --> suppress misleading paths in // mflags error messages. @@ -396,6 +406,11 @@ func main() { go listenAndServeHTTP(statusAddr, statusMux) } + if enablePlugin { + // TODO(mp) do not pass pluginMeshSocket if plugin-v2 is in use + go plugin.Start(httpAddr, dockerAPI, pluginSocket, pluginMeshSocket, noMulticastRoute) + } + signals.SignalHandlerLoop(common.Log, router) } diff --git a/test/090_docker_restart_policy_2_test.sh b/test/090_docker_restart_policy_2_test.sh index b4cc76cd..e1630bc9 100755 --- a/test/090_docker_restart_policy_2_test.sh +++ b/test/090_docker_restart_policy_2_test.sh @@ -6,22 +6,20 @@ start_suite "Test docker restart policy" weave_on $HOST1 launch -assert "docker_on $HOST1 inspect -f '{{.HostConfig.RestartPolicy.Name}}' weave weaveproxy weaveplugin" "always\nalways\nalways" +assert "docker_on $HOST1 inspect -f '{{.HostConfig.RestartPolicy.Name}}' weave weaveproxy" "always\nalways" assert_raises "check_restart $HOST1 weave" assert_raises "check_restart $HOST1 weaveproxy" -assert_raises "check_restart $HOST1 weaveplugin" # stop + launch tests that restart policy changes result # in the old containers being removed and new ones created weave_on $HOST1 stop weave_on $HOST1 launch --no-restart -assert "docker_on $HOST1 inspect -f '{{.HostConfig.RestartPolicy.Name}}' weave weaveproxy weaveplugin" "no\nno\nno" +assert "docker_on $HOST1 inspect -f '{{.HostConfig.RestartPolicy.Name}}' weave weaveproxy" "no\nno" assert_raises "! check_restart $HOST1 weave" assert_raises "! check_restart $HOST1 weaveproxy" -assert_raises "! check_restart $HOST1 weaveplugin" # Relaunch to prevent the `weave stop` in `end_suite` # timing out trying to remove the plugin network diff --git a/test/195_no_multicast_route_1_test.sh b/test/195_no_multicast_route_1_test.sh index 740be6d6..2342582b 100755 --- a/test/195_no_multicast_route_1_test.sh +++ b/test/195_no_multicast_route_1_test.sh @@ -12,9 +12,8 @@ start_suite "--no-multicast-route operation" $SSH $HOST1 docker network rm testmcasttrue testmcastfalse >/dev/null 2>&1 || true # Ensure containers run either way have no multicast route -weave_on $HOST1 launch-router +weave_on $HOST1 launch-router --plugin weave_on $HOST1 launch-proxy --no-multicast-route -weave_on $HOST1 launch-plugin # plugin defaults to multicast off except for 'net=weave' start_container $HOST1 --no-multicast-route --name c1 proxy_start_container $HOST1 --name c2 diff --git a/test/850_plugin_v2_cross_hosts_2_test.sh b/test/850_plugin_v2_cross_hosts_2_test.sh index 3c8bab54..e5db1c2b 100755 --- a/test/850_plugin_v2_cross_hosts_2_test.sh +++ b/test/850_plugin_v2_cross_hosts_2_test.sh @@ -23,7 +23,7 @@ setup_master() { WORK_DIR=$(mktemp -d) mkdir -p \${WORK_DIR}/rootfs - docker create --name=$build_plugin_img weaveworks/plugin true + docker create --name=$build_plugin_img weaveworks/weave true docker export $build_plugin_img | tar -x -C \${WORK_DIR}/rootfs cp \${HOME}/plugin-v2/launch.sh \${WORK_DIR}/rootfs/home/weave/launch.sh cp \${HOME}/plugin-v2/config.json \${WORK_DIR} @@ -38,8 +38,6 @@ setup_master() { [ -n "$COVERAGE" ] && docker plugin set $PLUGIN_NAME EXTRA_ARGS="-test.coverprofile=/home/weave/cover.prof --" #docker plugin set $PLUGIN_NAME WEAVE_PASSWORD="foobar" docker plugin enable $PLUGIN_NAME - ps aux | grep weave - cat /var/lib/weave/weaver.log EOF } @@ -98,6 +96,9 @@ wait_for_service() { start_suite "Test Docker plugin-v2" +#cleanup $HOST1 $HOST2 +#exit 1 + setup_master #sleep 20 # registry seems to be async :( setup_worker $($SSH $HOST1 docker swarm join-token --quiet worker) diff --git a/weave b/weave index aaf830a6..0553197d 100755 --- a/weave +++ b/weave @@ -49,6 +49,7 @@ weave launch [--no-restart] [--resume] [--no-discovery] [--no-dns] [--ipalloc-init ] [--ipalloc-range [--ipalloc-default-subnet ]] + [--plugin [--no-multicast-route]] [--log-level=debug|info|warning|error] ... launch-proxy [-H ] [--without-dns] [--no-multicast-route] @@ -58,8 +59,6 @@ weave launch [--hostname-replacement ] [--rewrite-inspect] [--log-level=debug|info|warning|error] - launch-plugin [--no-restart] [--no-multicast-route] - [--log-level=debug|info|warning|error] weave prime @@ -93,7 +92,6 @@ weave status [targets | connections | peers | dns | ipam] weave stop stop-router stop-proxy - stop-plugin weave reset [--force] rmpeer ... @@ -166,7 +164,6 @@ exec_remote() { -e WEAVE_MTU \ -e WEAVE_NO_FASTDP \ -e WEAVE_NO_BRIDGED_FASTDP \ - -e WEAVE_NO_PLUGIN \ -e DOCKER_BRIDGE \ -e DOCKER_CLIENT_HOST="$DOCKER_CLIENT_HOST" \ -e DOCKER_CLIENT_ARGS \ @@ -332,15 +329,12 @@ fi # main (remote and --local) - settings ###################################################################### -# Default restart policy for router/proxy/plugin +# Default restart policy for router/proxy RESTART_POLICY="--restart=always" BASE_IMAGE=$DOCKERHUB_USER/weave IMAGE=$BASE_IMAGE:$IMAGE_VERSION CONTAINER_NAME=${WEAVE_CONTAINER_NAME:-weave} -BASE_PLUGIN_IMAGE=$DOCKERHUB_USER/plugin -PLUGIN_IMAGE=$BASE_PLUGIN_IMAGE:$IMAGE_VERSION -PLUGIN_CONTAINER_NAME=weaveplugin PLUGIN_V2_NAME="$DOCKERHUB_USER/pluginv2" CNI_PLUGIN_NAME="weave-plugin-$IMAGE_VERSION" CNI_PLUGIN_DIR=${WEAVE_CNI_PLUGIN_DIR:-$HOST_ROOT/opt/cni/bin} @@ -1212,10 +1206,17 @@ check_running() { fi } +# 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 when_weave_running() { ! check_running $CONTAINER_NAME 2>/dev/null || "$@" @@ -1621,6 +1622,10 @@ launch_router() { AWSVPC_ARGS="--awsvpc" AWSVPC=1 ;; + --plugin) + PLUGIN_ARGS="--plugin" + ENABLE_PLUGIN=1 + ;; *) ARGS="$ARGS '$(echo "$1" | sed "s|'|'\"'\"'|g")'" ;; @@ -1683,6 +1688,7 @@ launch_router() { --pid=host \ --volumes-from $DB_CONTAINER_NAME \ -v $RESOLV_CONF_DIR:/var/run/weave/etc \ + -v /run/docker/plugins:/run/docker/plugins \ -e WEAVE_PASSWORD \ -e CHECKPOINT_DISABLE \ $WEAVE_DOCKER_ARGS $IMAGE $COVERAGE_ARGS \ @@ -1692,6 +1698,7 @@ launch_router() { --dns-effective-listen-address $DOCKER_BRIDGE_IP \ $DNS_ROUTER_OPTS $NO_DNS_OPT \ $AWSVPC_ARGS \ + $PLUGIN_ARGS \ --http-addr $HTTP_ADDR \ --status-addr $STATUS_ADDR \ --resolv-conf "/var/run/weave/etc/$RESOLV_CONF_BASE" \ @@ -1699,6 +1706,11 @@ launch_router() { wait_for_status $CONTAINER_NAME http_call $HTTP_ADDR setup_awsvpc populate_router + + if [ -n "$ENABLE_PLUGIN" ]; then + wait_for_status $CONTAINER_NAME http_call_unix $CONTAINER_NAME plugin-status.sock + util_op create-plugin-network weave weavemesh $IPRANGE + fi } setup_awsvpc() { @@ -1733,6 +1745,7 @@ populate_router() { } stop_router() { + ! is_plugin_enabled || util_op remove-plugin-network weave || true stop $CONTAINER_NAME "Weave" conntrack -D -p udp --dport $PORT >/dev/null 2>&1 || true } @@ -1774,53 +1787,9 @@ stop_proxy() { stop $PROXY_CONTAINER_NAME "Proxy" } -launch_plugin_if_not_running() { - while [ $# -gt 0 ]; do - case "$1" in - --no-restart) - RESTART_POLICY= - ;; - *) - break - ;; - esac - shift - done - - retval=0 - check_not_running plugin $PLUGIN_CONTAINER_NAME $BASE_PLUGIN_IMAGE || retval=$? - # If an existing plugin is running (we start it with restart=always), return its ID - [ $retval = 1 ] && PLUGIN_CONTAINER=$(container_id $PLUGIN_CONTAINER_NAME) && return 0 - # Any other kind of error code from check_not_running is a failure. - [ $retval -gt 0 ] && return $retval - - if ! PLUGIN_CONTAINER=$(docker run -d --name=$PLUGIN_CONTAINER_NAME \ - $(docker_run_options) \ - $RESTART_POLICY \ - --pid=host \ - -v /run/docker/plugins:/run/docker/plugins \ - -e WEAVE_HTTP_ADDR \ - $WEAVEPLUGIN_DOCKER_ARGS $PLUGIN_IMAGE $COVERAGE_ARGS \ - "$@") ; then - return 1 - fi - wait_for_status $PLUGIN_CONTAINER_NAME http_call_unix $PLUGIN_CONTAINER_NAME status.sock - WEAVE_IPAM_SUBNET=$(call_weave GET /ipinfo/defaultsubnet) - util_op create-plugin-network weave weavemesh $WEAVE_IPAM_SUBNET -} - -plugin_disabled() { - [ -n "$WEAVE_NO_PLUGIN" ] || ! check_docker_server_api_version 1.21 -} - -stop_plugin() { - util_op remove-plugin-network weave || true - stop $PLUGIN_CONTAINER_NAME "Plugin" -} - protect_against_docker_hang() { # If the plugin is not running, remove its socket so Docker doesn't try to talk to it - if ! check_running $PLUGIN_CONTAINER_NAME 2>/dev/null ; then + if ! is_docker_plugin_enabled 2>/dev/null ; then rm -f /run/docker/plugins/weave.sock /run/docker/plugins/weavemesh.sock fi } @@ -1936,7 +1905,7 @@ handle_help_arg "$1" case "$COMMAND" in setup) - for img in $IMAGE $EXEC_IMAGE $PLUGIN_IMAGE $WEAVEDB_IMAGE ; do + for img in $IMAGE $EXEC_IMAGE $WEAVEDB_IMAGE ; do docker pull $img done setup_cni @@ -1948,7 +1917,6 @@ case "$COMMAND" in [ $# -eq 0 ] || usage ask_version $CONTAINER_NAME $IMAGE || true ask_version $PROXY_CONTAINER_NAME $EXEC_IMAGE --entrypoint=/home/weave/weaveproxy || true - ask_version $PLUGIN_CONTAINER_NAME $PLUGIN_IMAGE || true ;; # intentionally undocumented since it assumes knowledge of weave # internals @@ -2009,11 +1977,9 @@ EOF deprecation_warnings "$@" check_not_running router $CONTAINER_NAME $BASE_IMAGE check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE - check_not_running plugin $PLUGIN_CONTAINER_NAME $BASE_PLUGIN_IMAGE COMMON_ARGS=$(common_launch_args "$@") - launch_router "$@" + launch_router --plugin "$@" launch_proxy $COMMON_ARGS - plugin_disabled || launch_plugin_if_not_running $COMMON_ARGS ;; launch-router) deprecation_warnings "$@" @@ -2038,14 +2004,6 @@ EOF launch_proxy "$@" echo $PROXY_CONTAINER ;; - launch-plugin) - if ! check_running $CONTAINER_NAME 2>/dev/null ; then - echo "ERROR:" $CONTAINER_NAME "container must be running before plugin can be launched" >&2 - exit 1 - fi - launch_plugin_if_not_running "$@" - echo $PLUGIN_CONTAINER - ;; env|proxy-env) [ "$COMMAND" = "env" ] || deprecation_warning "$COMMAND" "'weave env'" if PROXY_ADDR=$(proxy_addr) ; then @@ -2087,7 +2045,7 @@ EOF echo " Service: proxy" echo " Address: $PROXY_ADDRS" fi - if [ -z "$SUB_STATUS" ] && ( is_docker_plugin_enabled "$PLUGIN_V2_NAME" || check_running $PLUGIN_CONTAINER_NAME 2>/dev/null ) ; then + if [ -z "$SUB_STATUS" ] && ( is_docker_plugin_enabled "$PLUGIN_V2_NAME" || is_plugin_enabled ) ; then echo echo " Service: plugin" echo " DriverName: weave" @@ -2276,7 +2234,6 @@ EOF ;; stop) [ $# -eq 0 ] || usage - plugin_disabled || stop_plugin stop_router stop_proxy ;; @@ -2288,13 +2245,9 @@ EOF [ $# -eq 0 ] || usage stop_proxy ;; - stop-plugin) - [ $# -eq 0 ] || usage - stop_plugin - ;; reset) [ $# -eq 0 ] || [ $# -eq 1 -a "$1" = "--force" ] || usage - plugin_disabled || util_op remove-plugin-network weave || true + ! is_plugin_enabled || util_op remove-plugin-network weave || true warn_if_stopping_proxy_in_env res=0 [ "$1" = "--force" ] || check_running $CONTAINER_NAME 2>/dev/null || res=$? @@ -2312,7 +2265,7 @@ EOF exit 1 ;; esac - for NAME in $PLUGIN_CONTAINER_NAME $CONTAINER_NAME $PROXY_CONTAINER_NAME ; do + for NAME in $CONTAINER_NAME $PROXY_CONTAINER_NAME ; do docker stop $NAME >/dev/null 2>&1 || true docker rm -f $NAME >/dev/null 2>&1 || true done