#!/bin/bash
set -e

# You may need to adjust
#    echo 4096 >/proc/sys/net/ipv4/neigh/default/gc_thresh3
# where the number is greater than NUM_WEAVES*(NUM_WEAVES-1) plus some
# fudge.
#
# Without that, some connections between the weaves may fail to get
# established, and networking on the host in general becomes
# broken. There will also be some "net_ratelimit: nnn callbacks
# suppressed" messages in the syslog.

START=${FIRST_WEAVE:-1}
COUNT=${NUM_WEAVES:-28}
FINISH=$((START+COUNT-1))

weavedir=$(dirname $0)/..

case "$1" in
    launch)
        shift 1
        for i in $(seq $START $FINISH); do
            WEAVE_CONTAINER_NAME=weave$i $weavedir/weave test-launch "$@"
        done
        if [ $START -ne 1 ] ; then
            status=$(docker inspect --format='{{.State.Running}} {{.NetworkSettings.IPAddress}}' weave$((START-1)))
            PREV="${status#true }"
        fi
        for i in $(seq $START $FINISH); do
            if [ -n "$PREV" ] ; then
                echo connecting weave$i to $PREV
                WEAVE_CONTAINER_NAME=weave$i $weavedir/weave connect $PREV
            fi
            status=$(docker inspect --format='{{.State.Running}} {{.NetworkSettings.IPAddress}}' weave$i)
            PREV="${status#true }"
        done
        ;;
    stop)
        for i in $(seq $START $FINISH); do
            echo Stopping weave$i
            WEAVE_CONTAINER_NAME=weave$i $weavedir/weave stop
        done
        ;;
esac
