Merge pull request #388 from squaremo/remote_weaving

Move weave script into tools image with its deps
and thus enable weaving on a remote docker

Closes #312.
This commit is contained in:
rade committed 2015-03-20 15:17:09 +00:00
commit 8e65c7127d
18 files changed
+204 -201

No files matched your search

+105 -69
View File
@@ -2,6 +2,15 @@
set -e
SCRIPT_VERSION="(unreleased version)"
DOCKERHUB_USER=zettio
if [ "$SCRIPT_VERSION" = "(unreleased version)" ] ; then
IMAGE_VERSION=latest
else
IMAGE_VERSION=$SCRIPT_VERSION
fi
IMAGE_VERSION=${VERSION:-$IMAGE_VERSION}
usage() {
echo "Usage:"
@@ -27,27 +36,49 @@ usage() {
exit 1
}
[ $(id -u) = 0 ] || {
echo "weave must be run as 'root'" >&2
[ $# -gt 0 ] || usage
# These are potentially used for remote execs, and have defaults, so
# we introduce them here
DOCKER_BRIDGE=${DOCKER_BRIDGE:-docker0}
PROCFS=${PROCFS:-/proc}
BASE_IMAGE=$DOCKERHUB_USER/weave
BASE_DNS_IMAGE=$DOCKERHUB_USER/weavedns
BASE_EXEC_IMAGE=$DOCKERHUB_USER/weaveexec
IMAGE=$BASE_IMAGE:$IMAGE_VERSION
DNS_IMAGE=$BASE_DNS_IMAGE:$IMAGE_VERSION
EXEC_IMAGE=$BASE_EXEC_IMAGE:$IMAGE_VERSION
exec_remote() {
docker run -t --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PROCFS":/hostproc \
-e VERSION \
-e WEAVE_DOCKER_ARGS \
-e WEAVE_PASSWORD \
-e DOCKER_BRIDGE \
-e PROCFS=/hostproc --privileged --net=host \
$EXEC_IMAGE --local "$@"
}
# "run" is a special case because we want to use docker directly,
# rather than the docker in $EXEC_IMAGE remotely. That's because we
# are passing arbitrary arguments on to docker run, and we can't rely
# on our baked-in docker to support those arguments.
if [ "$1" = "--local" ]; then
LOCAL="$1"
shift 1
elif [ "$1" != "run" ]; then
exec_remote "$@"
exit $?
fi
[ $(id -u) = 0 ] || ([ "$1" = "run" ] && [ -z "$LOCAL" ]) || {
echo "weave must be run as 'root' when run locally" >&2
exit 1
}
[ $# -gt 0 ] || usage
if [ "$SCRIPT_VERSION" = "(unreleased version)" ] ; then
IMAGE_VERSION=latest
else
IMAGE_VERSION=$SCRIPT_VERSION
fi
IMAGE_VERSION=${VERSION:-$IMAGE_VERSION}
BASE_IMAGE=zettio/weave
BASE_DNS_IMAGE=zettio/weavedns
BASE_TOOLS_IMAGE=zettio/weavetools
IMAGE=$BASE_IMAGE:$IMAGE_VERSION
DNS_IMAGE=$BASE_DNS_IMAGE:$IMAGE_VERSION
TOOLS_IMAGE=$BASE_TOOLS_IMAGE:$IMAGE_VERSION
CONTAINER_NAME=weave
DNS_CONTAINER_NAME=weavedns
BRIDGE=weave
@@ -56,26 +87,42 @@ MTU=65535
PORT=6783
HTTP_PORT=6784
DNS_HTTP_PORT=6785
DOCKER_BRIDGE=${DOCKER_BRIDGE:-docker0}
PROCFS=${PROCFS:-/proc}
COMMAND=$1
shift 1
dns_args() {
if [ "$1" = "--with-dns" ] ; then
shift 1
if [ \( "$DOCKER_VERSION_MAJOR" -lt 1 \) -o \
\( "$DOCKER_VERSION_MAJOR" -eq 1 -a \
"$DOCKER_VERSION_MINOR" -lt 2 \) ] ; then
echo "ERROR: The '--with-dns' option requires Docker 1.2.0 or later; you are running $DOCKER_VERSION" >&2
exit 1
fi
docker_bridge_ip
DNS_ARG="--dns $DOCKER_BRIDGE_IP"
DNS_SEARCH_ARG="--dns-search=."
for arg in "$@"; do
case $arg in
--dns-search=*)
DNS_SEARCH_ARG=""
;;
*)
;;
esac;
done
fi
DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG"
}
# utility function to check whether a command can be executed by the shell
# see http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script
command_exists () {
command -v $1 >/dev/null 2>&1
}
run_tool() {
TOOL_NET=$1
TOOL_COMMAND=$2
shift 2
docker run --rm -t --privileged --net=$TOOL_NET $TOOLS_IMAGE /bin/$TOOL_COMMAND "$@"
}
is_cidr() {
# The regexp here is far from precise, but good enough.
echo "$1" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$' >/dev/null
@@ -164,11 +211,7 @@ create_bridge() {
add_iptables_rule nat POSTROUTING -j WEAVE
}
if [ ! "$1" = "--without-ethtool" ] ; then
# disable offloading - we do this even when $BRIDGE already exists
# simply because it has the desirable side-effect of ensuring we
# have the $TOOLS_IMAGE and thus do not incur downloading-induced
# delays in more time-sensitive sections elsewhere.
run_tool host ethtool -K $BRIDGE tx off >/dev/null
ethtool -K $BRIDGE tx off >/dev/null
fi
ip link set dev $BRIDGE up
@@ -253,7 +296,7 @@ connect_container_to_bridge() {
return 1
fi
if ! run_tool host ethtool -K $GUEST_IFNAME tx off >/dev/null ||
if ! ethtool -K $GUEST_IFNAME tx off >/dev/null ||
! ip link set $LOCAL_IFNAME master $BRIDGE ||
! ip link set $LOCAL_IFNAME up ||
! ip link set $GUEST_IFNAME netns $NETNS ; then
@@ -275,7 +318,7 @@ launch() {
return 1
fi
connect_container_to_bridge &&
run_tool container:$CONTAINER ethtool -K eth0 tx off >/dev/null &&
ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null &&
ip netns exec $NETNS ip link set $CONTAINER_IFNAME up
}
@@ -357,7 +400,7 @@ http_call_ip() {
http_verb="$3"
url="$4"
shift 4
run_tool host curl -s -X $http_verb "$@" http://$ip:$port$url
curl -s -X $http_verb "$@" http://$ip:$port$url
}
# Perform operation $1 on container ID $2 to local DNS database at address $3
@@ -483,7 +526,7 @@ DOCKER_VERSION_PATCH=$(echo "$DOCKER_VERSION" | cut -d. -f 3)
case "$COMMAND" in
setup)
for img in $IMAGE $DNS_IMAGE $TOOLS_IMAGE ; do
for img in $IMAGE $DNS_IMAGE $EXEC_IMAGE ; do
docker pull $img
done
;;
@@ -574,49 +617,42 @@ case "$COMMAND" in
echo weave script $SCRIPT_VERSION
ask_version $CONTAINER_NAME $IMAGE
ask_version $DNS_CONTAINER_NAME $DNS_IMAGE
if ! TOOLS_IMAGE_ID=$(docker inspect --format='{{.Id}}' $TOOLS_IMAGE 2>/dev/null) ; then
echo "Unable to find $TOOLS_IMAGE image." >&2
if ! EXEC_IMAGE_ID=$(docker inspect --format='{{.Id}}' $EXEC_IMAGE 2>/dev/null) ; then
echo "Unable to find $EXEC_IMAGE image." >&2
else
TOOLS_VERSION=$(docker images --no-trunc | grep $TOOLS_IMAGE_ID | grep -v latest | tr -s ' ' | cut -d ' ' -f 2 | tr "\n" ' ')
if [ -n "$TOOLS_VERSION" ] ; then
echo "weave tools $TOOLS_VERSION"
EXEC_VERSION=$(docker images --no-trunc | grep $EXEC_IMAGE_ID | grep -v latest | tr -s ' ' | cut -d ' ' -f 2 | tr "\n" ' ')
if [ -n "$EXEC_VERSION" ] ; then
echo "weave exec $EXEC_VERSION"
else
echo "weave tools (unreleased version)"
echo "weave exec (unreleased version)"
fi
fi
;;
run)
[ $# -gt 0 ] || usage
if [ "$1" = "--with-dns" ] ; then
shift 1
if [ \( "$DOCKER_VERSION_MAJOR" -lt 1 \) -o \
\( "$DOCKER_VERSION_MAJOR" -eq 1 -a \
"$DOCKER_VERSION_MINOR" -lt 2 \) ] ; then
echo "ERROR: The '--with-dns' option requires Docker 1.2.0 or later; you are running $DOCKER_VERSION" >&2
exit 1
fi
docker_bridge_ip
DNS_ARG="--dns $DOCKER_BRIDGE_IP"
DNS_SEARCH_ARG="--dns-search=."
for arg in "$@"; do
case $arg in
--dns-search=*)
DNS_SEARCH_ARG=""
;;
*)
;;
esac;
done
if [ "$LOCAL" = "--local" ]; then
dns_args "$@"
else
DNS_ARGS=$(exec_remote dns-args "$@")
[ "$1" = "--with-dns" ] && shift 1
fi
CIDR=$1
validate_cidr $CIDR
shift 1
create_bridge
CONTAINER=$(docker run $DNS_ARG $DNS_SEARCH_ARG -d "$@")
with_container_netns $CONTAINER attach $CIDR >/dev/null
tell_dns PUT $CONTAINER $CIDR
validate_cidr $CIDR
CONTAINER=$(docker run $DNS_ARGS -d "$@")
if [ "$LOCAL" = "--local" ]; then
create_bridge
with_container_netns $CONTAINER attach $CIDR >/dev/null
tell_dns PUT $CONTAINER $CIDR
else
exec_remote attach $CIDR $CONTAINER
fi
echo $CONTAINER
;;
dns-args)
dns_args "$@"
echo -n $DNS_ARGS
;;
start)
[ $# -eq 2 ] || usage
CIDR=$1
@@ -673,7 +709,7 @@ case "$COMMAND" in
echo "Weave is not running." >&2
fi
docker rm -f $CONTAINER_NAME >/dev/null 2>&1 || true
run_tool host conntrack -D -p udp --dport $PORT >/dev/null 2>&1 || true
conntrack -D -p udp --dport $PORT >/dev/null 2>&1 || true
;;
stop-dns)
[ $# -eq 0 ] || usage
@@ -688,7 +724,7 @@ case "$COMMAND" in
docker kill $DNS_CONTAINER_NAME >/dev/null 2>&1 || true
docker rm -f $CONTAINER_NAME >/dev/null 2>&1 || true
docker rm -f $DNS_CONTAINER_NAME >/dev/null 2>&1 || true
run_tool host conntrack -D -p udp --dport $PORT >/dev/null 2>&1 || true
conntrack -D -p udp --dport $PORT >/dev/null 2>&1 || true
destroy_bridge
for LOCAL_IFNAME in $(ip link show | grep v${CONTAINER_IFNAME}pl | cut -d ' ' -f 2 | tr -d ':') ; do
ip link del $LOCAL_IFNAME