simplify local portion of weave run
pull all the local `weave run` execution into one place, thus dispensing with the need to have lots of tests for whether we are executing in the context of "--local". Also, we only remote exec dns-args if "--with-dns" is supplied, which is more efficient in the common case (and also more obvious). NB: we need to retain the docker version check for `run` since the specific breakage it guards against does affect the code we execute locally for `run`.
This commit is contained in:
1 parent
0a2f341c6b
commit
53e145bea7
1 file changed
+81
-77
@@ -56,41 +56,17 @@ exec_remote() {
|
|||||||
$EXEC_IMAGE --local "$@"
|
$EXEC_IMAGE --local "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# "run" is a special case because we want to use docker directly,
|
check_docker_version() {
|
||||||
# rather than the docker in $EXEC_IMAGE remotely. That's because we
|
if ! DOCKER_VERSION=$(docker -v | sed -n -e 's|^Docker version \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*|\1|p') || [ -z "$DOCKER_VERSION" ] ; then
|
||||||
# are passing arbitrary arguments on to docker run, and we can't rely
|
echo "ERROR: Unable to parse docker version" >&2
|
||||||
# on our baked-in docker to support those arguments.
|
exit 1
|
||||||
if [ "$1" = "--local" ]; then
|
fi
|
||||||
LOCAL="$1"
|
|
||||||
shift 1
|
|
||||||
elif [ "$1" != "run" ]; then
|
|
||||||
exec_remote "$@"
|
|
||||||
exit $?
|
|
||||||
fi
|
|
||||||
|
|
||||||
BASE_IMAGE=$DOCKERHUB_USER/weave
|
# guard against https://github.com/docker/docker/issues/8632
|
||||||
BASE_DNS_IMAGE=$DOCKERHUB_USER/weavedns
|
if [ "$DOCKER_VERSION" = "1.3.0" ] ; then
|
||||||
IMAGE=$BASE_IMAGE:$IMAGE_VERSION
|
echo "You are running Docker version $DOCKER_VERSION, which contains a bug that prevents weave from working properly. Please upgrade." >&2
|
||||||
DNS_IMAGE=$BASE_DNS_IMAGE:$IMAGE_VERSION
|
exit 1
|
||||||
|
fi
|
||||||
DOCKER_BRIDGE=${DOCKER_BRIDGE:-docker0}
|
|
||||||
CONTAINER_NAME=${WEAVE_CONTAINER_NAME:-weave}
|
|
||||||
DNS_CONTAINER_NAME=weavedns
|
|
||||||
BRIDGE=weave
|
|
||||||
CONTAINER_IFNAME=ethwe
|
|
||||||
MTU=65535
|
|
||||||
PORT=${WEAVE_PORT:-6783}
|
|
||||||
HTTP_PORT=6784
|
|
||||||
DNS_HTTP_PORT=6785
|
|
||||||
|
|
||||||
######################################################################
|
|
||||||
# general helpers; independent of docker and weave
|
|
||||||
######################################################################
|
|
||||||
|
|
||||||
# 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
is_cidr() {
|
is_cidr() {
|
||||||
@@ -119,6 +95,55 @@ collect_cidr_args() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# "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" = "run" ]; then
|
||||||
|
shift 1
|
||||||
|
check_docker_version
|
||||||
|
if [ "$1" = "--with-dns" ]; then
|
||||||
|
DNS_ARGS=$(exec_remote dns-args "$@")
|
||||||
|
shift 1
|
||||||
|
fi
|
||||||
|
collect_cidr_args "$@"
|
||||||
|
shift $CIDR_COUNT
|
||||||
|
CONTAINER=$(docker run $DNS_ARGS -d "$@")
|
||||||
|
exec_remote attach $CIDR_ARGS $CONTAINER
|
||||||
|
echo $CONTAINER
|
||||||
|
exit 0
|
||||||
|
elif [ "$1" != "--local" ]; then
|
||||||
|
exec_remote "$@"
|
||||||
|
exit $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
shift 1
|
||||||
|
|
||||||
|
BASE_IMAGE=$DOCKERHUB_USER/weave
|
||||||
|
BASE_DNS_IMAGE=$DOCKERHUB_USER/weavedns
|
||||||
|
IMAGE=$BASE_IMAGE:$IMAGE_VERSION
|
||||||
|
DNS_IMAGE=$BASE_DNS_IMAGE:$IMAGE_VERSION
|
||||||
|
|
||||||
|
DOCKER_BRIDGE=${DOCKER_BRIDGE:-docker0}
|
||||||
|
CONTAINER_NAME=${WEAVE_CONTAINER_NAME:-weave}
|
||||||
|
DNS_CONTAINER_NAME=weavedns
|
||||||
|
BRIDGE=weave
|
||||||
|
CONTAINER_IFNAME=ethwe
|
||||||
|
MTU=65535
|
||||||
|
PORT=${WEAVE_PORT:-6783}
|
||||||
|
HTTP_PORT=6784
|
||||||
|
DNS_HTTP_PORT=6785
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# general helpers; independent of docker and weave
|
||||||
|
######################################################################
|
||||||
|
|
||||||
|
# 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_iptables() {
|
run_iptables() {
|
||||||
# -w is recent addition to iptables
|
# -w is recent addition to iptables
|
||||||
if [ -z "$CHECKED_IPTABLES_W" ] ; then
|
if [ -z "$CHECKED_IPTABLES_W" ] ; then
|
||||||
@@ -551,51 +576,39 @@ populate_dns() {
|
|||||||
|
|
||||||
##########################################################################################
|
##########################################################################################
|
||||||
|
|
||||||
if [ -n "$LOCAL" ]; then
|
[ $(id -u) = 0 ] || {
|
||||||
[ $(id -u) = 0 ] || {
|
echo "weave must be run as 'root' when run locally" >&2
|
||||||
echo "weave must be run as 'root' when run locally" >&2
|
exit 1
|
||||||
exit 1
|
}
|
||||||
}
|
|
||||||
|
|
||||||
uname -s -r | sed -n -e 's|^\([^ ]*\) \([0-9][0-9]*\)\.\([0-9][0-9]*\).*|\1 \2 \3|p' | {
|
uname -s -r | sed -n -e 's|^\([^ ]*\) \([0-9][0-9]*\)\.\([0-9][0-9]*\).*|\1 \2 \3|p' | {
|
||||||
if ! read sys maj min ; then
|
if ! read sys maj min ; then
|
||||||
echo "ERROR: Unable to parse operating system version $(uname -s -r)" >&2
|
echo "ERROR: Unable to parse operating system version $(uname -s -r)" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$sys" != 'Linux' ] ; then
|
if [ "$sys" != 'Linux' ] ; then
|
||||||
echo "ERROR: Operating systems other than Linux are not supported (you have $(uname -s -r))" >&2
|
echo "ERROR: Operating systems other than Linux are not supported (you have $(uname -s -r))" >&2
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ! [ \( "$maj" -eq 3 -a "$min" -ge 5 \) -o "$maj" -gt 3 ] ; then
|
|
||||||
echo "WARNING: Linux kernel version 3.5 or newer is required (you have ${maj}.${min})" >&2
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
if ! command_exists ip ; then
|
|
||||||
echo "ERROR: ip utility is missing. Please install it." >&2
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! ip netns list >/dev/null 2>&1 ; then
|
if ! [ \( "$maj" -eq 3 -a "$min" -ge 5 \) -o "$maj" -gt 3 ] ; then
|
||||||
echo "ERROR: $(ip -V) does not support network namespaces." >&2
|
echo "WARNING: Linux kernel version 3.5 or newer is required (you have ${maj}.${min})" >&2
|
||||||
echo " Please install iproute2-ss111010 or later." >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
fi
|
}
|
||||||
|
|
||||||
if ! DOCKER_VERSION=$(docker -v | sed -n -e 's|^Docker version \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*|\1|p') || [ -z "$DOCKER_VERSION" ] ; then
|
if ! command_exists ip ; then
|
||||||
echo "ERROR: Unable to parse docker version" >&2
|
echo "ERROR: ip utility is missing. Please install it." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# guard against https://github.com/docker/docker/issues/8632
|
if ! ip netns list >/dev/null 2>&1 ; then
|
||||||
if [ "$DOCKER_VERSION" = "1.3.0" ] ; then
|
echo "ERROR: $(ip -V) does not support network namespaces." >&2
|
||||||
echo "You are running Docker version $DOCKER_VERSION, which contains a bug that prevents weave from working properly. Please upgrade." >&2
|
echo " Please install iproute2-ss111010 or later." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
check_docker_version
|
||||||
DOCKER_VERSION_MAJOR=$(echo "$DOCKER_VERSION" | cut -d. -f 1)
|
DOCKER_VERSION_MAJOR=$(echo "$DOCKER_VERSION" | cut -d. -f 1)
|
||||||
DOCKER_VERSION_MINOR=$(echo "$DOCKER_VERSION" | cut -d. -f 2)
|
DOCKER_VERSION_MINOR=$(echo "$DOCKER_VERSION" | cut -d. -f 2)
|
||||||
DOCKER_VERSION_PATCH=$(echo "$DOCKER_VERSION" | cut -d. -f 3)
|
DOCKER_VERSION_PATCH=$(echo "$DOCKER_VERSION" | cut -d. -f 3)
|
||||||
@@ -746,22 +759,13 @@ case "$COMMAND" in
|
|||||||
;;
|
;;
|
||||||
run)
|
run)
|
||||||
[ $# -gt 0 ] || usage
|
[ $# -gt 0 ] || usage
|
||||||
if [ -n "$LOCAL" ]; then
|
dns_args "$@"
|
||||||
dns_args "$@"
|
|
||||||
else
|
|
||||||
DNS_ARGS=$(exec_remote dns-args "$@")
|
|
||||||
[ "$1" = "--with-dns" ] && shift 1
|
|
||||||
fi
|
|
||||||
collect_cidr_args "$@"
|
collect_cidr_args "$@"
|
||||||
shift $CIDR_COUNT
|
shift $CIDR_COUNT
|
||||||
CONTAINER=$(docker run $DNS_ARGS -d "$@")
|
CONTAINER=$(docker run $DNS_ARGS -d "$@")
|
||||||
if [ -n "$LOCAL" ]; then
|
create_bridge
|
||||||
create_bridge
|
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
||||||
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
tell_dns PUT $CONTAINER $CIDR_ARGS
|
||||||
tell_dns PUT $CONTAINER $CIDR_ARGS
|
|
||||||
else
|
|
||||||
exec_remote attach $CIDR_ARGS $CONTAINER
|
|
||||||
fi
|
|
||||||
echo $CONTAINER
|
echo $CONTAINER
|
||||||
;;
|
;;
|
||||||
dns-args)
|
dns-args)
|
||||||
|
|||||||
Reference in new issue
Block a user