<commit_before># NB only to be sourced

set -e

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Protect against being sourced multiple times to prevent
# overwriting assert.sh global state
if ! [ -z "$SOURCED_CONFIG_SH" ]; then
    return
fi
SOURCED_CONFIG_SH=true

# these ought to match what is in Vagrantfile
N_MACHINES=${N_MACHINES:-3}
IP_PREFIX=${IP_PREFIX:-192.168.48}
IP_SUFFIX_BASE=${IP_SUFFIX_BASE:-10}

if [ -z "$HOSTS" ] ; then
    for i in $(seq 1 $N_MACHINES); do
        IP="${IP_PREFIX}.$((${IP_SUFFIX_BASE}+$i))"
        HOSTS="$HOSTS $IP"
    done
fi

# these are used by the tests
HOST1=$(echo $HOSTS | cut -f 1 -d ' ')
HOST2=$(echo $HOSTS | cut -f 2 -d ' ')
HOST3=$(echo $HOSTS | cut -f 3 -d ' ')

. "$DIR/assert.sh"

SSH_DIR=${SSH_DIR:-$DIR}
SSH_OPTS=${SSH_OPTS:-"-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes -o LogLevel=ERROR"}
SSH=${SSH:-ssh -l vagrant -i "$SSH_DIR/insecure_private_key" $SSH_OPTS}

SMALL_IMAGE="alpine"
DNS_IMAGE="aanand/docker-dnsutils"
TEST_IMAGES="$SMALL_IMAGE $DNS_IMAGE"

PING="ping -nq -W 2 -c 1"
CHECK_ETHWE_UP="grep ^1$ /sys/class/net/ethwe/carrier"
CHECK_ETHWE_MISSING="test ! -d /sys/class/net/ethwe"

DOCKER_PORT=2375

CHECKPOINT_DISABLE=true

# The regexp here is far from precise, but good enough. (taken from weave script)
IP_REGEXP="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
CIDR_REGEXP="(ip:|net:)?$IP_REGEXP/[0-9]{1,2}"

is_cidr() {
    [ "$1" = "net:default" ] && return
    echo "$1" | grep -E "^$CIDR_REGEXP$" >/dev/null
}

upload_executable() {
    host=$1
    file=$2
    target=${3:-/usr/local/bin/$(basename "$file")}
    dir=$(dirname "$target")
    run_on $host "[ -e '$dir' ] || sudo mkdir -p '$dir'"
    [ -z "$DEBUG" ] || greyly echo "Uploading to $host: $file -> $target" >&2
    <"$file" remote $host $SSH $host sh -c "cat | sudo tee $target >/dev/null"
    run_on $host "sudo chmod a+x $target"
}

remote() {
    rem=$1
    shift 1
    "$@" > >(while read line; do echo -e $'\e[0;34m'"$rem>"$'\e[0m'" $line"; done)
}

colourise() {
    [ -t 0 ] && echo -ne $'\e['$1'm' || true
    shift
    # It's important that we don't do this in a subshell, as some
    # commands we execute need to modify global state
    "$@"
    [ -t 0 ] && echo -ne $'\e[0m' || true
}

whitely() {
    colourise '1;37' "$@"
}

greyly () {
    colourise '0;37' "$@"
}

redly() {
    colourise '1;31' "$@"
}

greenly() {
    colourise '1;32' "$@"
}

run_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Running on $host: $@" >&2
    remote $host $SSH $host "$@"
}

docker_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Docker on $host:$DOCKER_PORT: $@" >&2
    docker -H tcp://$host:$DOCKER_PORT "$@"
}

docker_api_on() {
    host=$1
    method=$2
    url=$3
    data=$4
    shift 4
    [ -z "$DEBUG" ] || greyly echo "Docker (API) on $host:$DOCKER_PORT: $method $url" >&2
    echo -n "$data" | curl -s -f -X "$method" -H Content-Type:application/json "http://$host:$DOCKER_PORT/v1.15$url" -d @-
}

proxy() {
    DOCKER_PORT=12375 "$@"
}

weave_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Weave on $host:$DOCKER_PORT: $@" >&2<commit_msg>Add test function to recover clean output from command run on remote host<commit_after># NB only to be sourced

set -e

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Protect against being sourced multiple times to prevent
# overwriting assert.sh global state
if ! [ -z "$SOURCED_CONFIG_SH" ]; then
    return
fi
SOURCED_CONFIG_SH=true

# these ought to match what is in Vagrantfile
N_MACHINES=${N_MACHINES:-3}
IP_PREFIX=${IP_PREFIX:-192.168.48}
IP_SUFFIX_BASE=${IP_SUFFIX_BASE:-10}

if [ -z "$HOSTS" ] ; then
    for i in $(seq 1 $N_MACHINES); do
        IP="${IP_PREFIX}.$((${IP_SUFFIX_BASE}+$i))"
        HOSTS="$HOSTS $IP"
    done
fi

# these are used by the tests
HOST1=$(echo $HOSTS | cut -f 1 -d ' ')
HOST2=$(echo $HOSTS | cut -f 2 -d ' ')
HOST3=$(echo $HOSTS | cut -f 3 -d ' ')

. "$DIR/assert.sh"

SSH_DIR=${SSH_DIR:-$DIR}
SSH_OPTS=${SSH_OPTS:-"-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes -o LogLevel=ERROR"}
SSH=${SSH:-ssh -l vagrant -i "$SSH_DIR/insecure_private_key" $SSH_OPTS}

SMALL_IMAGE="alpine"
DNS_IMAGE="aanand/docker-dnsutils"
TEST_IMAGES="$SMALL_IMAGE $DNS_IMAGE"

PING="ping -nq -W 2 -c 1"
CHECK_ETHWE_UP="grep ^1$ /sys/class/net/ethwe/carrier"
CHECK_ETHWE_MISSING="test ! -d /sys/class/net/ethwe"

DOCKER_PORT=2375

CHECKPOINT_DISABLE=true

# The regexp here is far from precise, but good enough. (taken from weave script)
IP_REGEXP="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
CIDR_REGEXP="(ip:|net:)?$IP_REGEXP/[0-9]{1,2}"

is_cidr() {
    [ "$1" = "net:default" ] && return
    echo "$1" | grep -E "^$CIDR_REGEXP$" >/dev/null
}

upload_executable() {
    host=$1
    file=$2
    target=${3:-/usr/local/bin/$(basename "$file")}
    dir=$(dirname "$target")
    run_on $host "[ -e '$dir' ] || sudo mkdir -p '$dir'"
    [ -z "$DEBUG" ] || greyly echo "Uploading to $host: $file -> $target" >&2
    <"$file" remote $host $SSH $host sh -c "cat | sudo tee $target >/dev/null"
    run_on $host "sudo chmod a+x $target"
}

remote() {
    rem=$1
    shift 1
    "$@" > >(while read line; do echo -e $'\e[0;34m'"$rem>"$'\e[0m'" $line"; done)
}

colourise() {
    [ -t 0 ] && echo -ne $'\e['$1'm' || true
    shift
    # It's important that we don't do this in a subshell, as some
    # commands we execute need to modify global state
    "$@"
    [ -t 0 ] && echo -ne $'\e[0m' || true
}

whitely() {
    colourise '1;37' "$@"
}

greyly () {
    colourise '0;37' "$@"
}

redly() {
    colourise '1;31' "$@"
}

greenly() {
    colourise '1;32' "$@"
}

run_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Running on $host: $@" >&2
    remote $host $SSH $host "$@"
}

get_command_output_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Running on $host: $@" >&2
    $SSH $host "$@"
}

docker_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Docker on $host:$DOCKER_PORT: $@" >&2
    docker -H tcp://$host:$DOCKER_PORT "$@"
}

docker_api_on() {
    host=$1
    method=$2
    url=$3
    data=$4
    shift 4
    [ -z "$DEBUG" ] || greyly echo "Docker (API) on $host:$DOCKER_PORT: $method $url" >&2
    echo -n "$data" | curl -s -f -X "$method" -H Content-Type:application/json "http://$host:$DOCKER_PORT/v1.15$url" -d @-
}

proxy() {
    DOCKER_PORT=12375 "$@"
}

weave_on() {
    host=$1
    shift 1
    [ -z "$DEBUG" ] || greyly echo "Weave on $host:$DOCKER_PORT: $@" >&2
{'fraction_non_alphanumeric': 0.2012351257719536, 'fraction_numerical': 0.029673143545714716, 'mean_word_length': 3.30609597924773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7099441', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2757, 'n_words': 667}