Skip to content

Instantly share code, notes, and snippets.

@jpe42
Created December 19, 2016 04:59
Show Gist options
  • Save jpe42/0d77e7696831d8774f18e54f010ebd50 to your computer and use it in GitHub Desktop.
Save jpe42/0d77e7696831d8774f18e54f010ebd50 to your computer and use it in GitHub Desktop.
Convenience Script for Executing CommandsAcross all Redis Swarm Services.
#!/bin/bash
#./forEach.sh 7001 5 redis-cluster-m- CLUSTER INFO
readonly STARTING_PORT=$1
readonly NUM_MASTERS=$2
readonly NAME_PREFIX=$3
readonly REDIS_CMD=("${@:4}")
readonly LOCAL_CONTAINER_ID=$(docker ps -f name="$NAME_PREFIX" -q | head -n 1)
readonly LOCAL_PORT=$(docker inspect --format='{{index .Config.Labels "com.docker.swarm.service.name"}}' "$LOCAL_CONTAINER_ID" | sed 's|.*-||')
for ((port = STARTING_PORT, endPort = port + NUM_MASTERS; port < endPort; port++)) do
host=$([ "$LOCAL_PORT" == $port ] && echo "127.0.0.1" || echo "$NAME_PREFIX$port")
printf "\n%s\n" "$NAME_PREFIX$port:~\$ ${REDIS_CMD[*]}"
docker exec -it "$LOCAL_CONTAINER_ID"\
redis-cli -h "$host" -p "$port"\
"${REDIS_CMD[@]}"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment