function get_docker_bridge_ip() { echo $(docker run --rm -it busybox ip route show | grep default | cut -d" " -f3) } function inst_id2port() { echo $((41000+${1}-1)) } function dnet_container_name() { echo dnet-$1-$2 } function dnet_container_ip() { docker inspect --format '{{.NetworkSettings.IPAddress}}' dnet-$1-$2 } function get_sbox_id() { local line line=$(dnet_cmd $(inst_id2port ${1}) service ls | grep ${2}) echo ${line} | cut -d" " -f5 } function net_connect() { local al gl if [ -n "$4" ]; then if [ "${4}" != ":" ]; then al="--alias=${4}" fi fi if [ -n "$5" ]; then gl="--alias=${5}" fi dnet_cmd $(inst_id2port ${1}) service publish $gl ${2}.${3} dnet_cmd $(inst_id2port ${1}) service attach $al ${2} ${2}.${3} } function net_disconnect() { dnet_cmd $(inst_id2port ${1}) service detach ${2} ${2}.${3} dnet_cmd $(inst_id2port ${1}) service unpublish ${2}.${3} } function start_consul() { stop_consul docker run -d \ --name=pr_consul \ -p 8500:8500 \ -p 8300-8302:8300-8302/tcp \ -p 8300-8302:8300-8302/udp \ -h consul \ progrium/consul -server -bootstrap sleep 2 } function stop_consul() { echo "consul started" docker stop pr_consul || true # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci if [ -z "$CIRCLECI" ]; then docker rm -f pr_consul || true fi } hrun() { local e E T oldIFS [[ ! "$-" =~ e ]] || e=1 [[ ! "$-" =~ E ]] || E=1 [[ ! "$-" =~ T ]] || T=1 set +e set +E set +T output="$("$@" 2>&1)" status="$?" oldIFS=$IFS IFS=$'\n' lines=($output) [ -z "$e" ] || set -e [ -z "$E" ] || set -E [ -z "$T" ] || set -T IFS=$oldIFS } function wait_for_dnet() { local hport hport=$1 echo "waiting on dnet to come up ..." for i in `seq 1 10`; do hrun ./bin/dnet -H tcp://127.0.0.1:${hport} network ls echo ${output} if [ "$status" -eq 0 ]; then return fi if [[ "${lines[1]}" =~ .*EOF.* ]] then docker logs ${2} fi echo "still waiting after ${i} seconds" sleep 1 done } function parse_discovery_str() { local d provider address discovery=$1 provider=$(echo ${discovery} | cut -d":" -f1) address=$(echo ${discovery} | cut -d":" -f2):$(echo ${discovery} | cut -d":" -f3) address=${address:2} echo "${discovery} ${provider} ${address}" } function start_dnet() { local inst suffix name hport cport hopt store bridge_ip labels tomlfile nip local discovery provider address inst=$1 shift suffix=$1 shift store=$(echo $suffix | cut -d":" -f1) nip=$(echo $suffix | cut -s -d":" -f2) stop_dnet ${inst} ${store} name=$(dnet_container_name ${inst} ${store}) hport=$((41000+${inst}-1)) cport=2385 hopt="" while [ -n "$1" ] do if [[ "$1" =~ ^[0-9]+$ ]] then hport=$1 cport=$1 hopt="-H tcp://0.0.0.0:${cport}" else store=$1 fi shift done bridge_ip=$(get_docker_bridge_ip) echo "start_dnet parsed values: " ${inst} ${suffix} ${name} ${hport} ${cport} ${hopt} ${store} mkdir -p /tmp/dnet/${name} tomlfile="/tmp/dnet/${name}/libnetwork.toml" # Try discovery URLs with or without path neigh_ip="" neighbors="" if [ "$store" = "zookeeper" ]; then read discovery provider address < <(parse_discovery_str zk://${bridge_ip}:2182) elif [ "$store" = "etcd" ]; then read discovery provider address < <(parse_discovery_str etcd://${bridge_ip}:42000/custom_prefix) elif [ "$store" = "consul" ]; then read discovery provider address < <(parse_discovery_str consul://${bridge_ip}:8500/custom_prefix) else if [ "$nip" != "" ]; then neighbors=${nip} fi discovery="" provider="" address="" fi if [ "$discovery" != "" ]; then cat > ${tomlfile} < ${tomlfile} <