Skip to content

Instantly share code, notes, and snippets.

@mohit2152sharma
Created January 18, 2024 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohit2152sharma/e5c98c1fa09d82e71f0ac7ffc704e60e to your computer and use it in GitHub Desktop.
Save mohit2152sharma/e5c98c1fa09d82e71f0ac7ffc704e60e to your computer and use it in GitHub Desktop.
Running Redis Cluster on macos locally
#!/bin/sh
# when installing redis from brew, it doesn't come with `utils` folder
# and because of which it's rather difficult to run redis cluster locally
# (`utils` folder has `create-cluster` script files, which automate the creation
# startup, stopping of redis cluster)
# this script is a workaround for that, basically, it checks if `utils` folder is
# there or not, if not, it downloads it and makes the necessary changes
# it assumes, you are running this on macos, with zsh shell
# check if redis-cli is installed
if redis-cli --version; then
echo "Redis is already installed"
else
brew install redis
fi
# check if redis distribution contains the util script file or not
stringVersion=$(redis-cli --version)
versionArray=($stringVersion)
version=${versionArray[1]}
echo version of redis "$version"
echo check if utils directory was part of brew distribution
dirPath="/usr/local/Cellar/redis/${version}/utils"
if [ -d "$dirPath" ]; then
echo utils script exist
else
echo "utils script doesn't exist"
echo "downloading from utils from redis website"
wget https://download.redis.io/redis-stable.tar.gz -O redis-stable.tar.gz
tar -xzf redis-stable.tar.gz
cp -r redis-stable/utils /usr/local/Cellar/redis/${version}/
rm redis-stable.tar.gz
rm -r redis-stable
sed -i '' 's|BIN_PATH="$SCRIPT_DIR/../../src/"|BIN_PATH="$SCRIPT_DIR/../../bin/"|' /usr/local/Cellar/redis/"${version}"/utils/create-cluster/create-cluster
fi
echo testing by starting and stopping the cluster
curDir=$(pwd)
cd /usr/local/Cellar/redis/${version}/utils/create-cluster
./create-cluster create
if [ $? -eq 0 ]; then
echo "cluster created successfully"
./create-cluster start
redis-cli -c -p 30001 ping
./create-cluster stop
echo "cluster created successfully"
cd $curDir
echo "update zshrc with create-cluster aliases"
chmod +x /usr/local/Cellar/redis/${version}/utils/create-cluster/create-cluster
if [ $SHELL == "/bin/zsh" ]; then
echo "shell is zsh"
if [ -f "$HOME"/.zshrc ]; then
echo "zshrc already exists"
else
touch $HOME/.zshrc
fi
if grep -q "#### create-cluster-command ####" $HOME/.zshrc; then
echo "aliases already exist, overwriting"
replacement="alias create-cluster=\"/usr/local/Cellar/redis/"${version}"/utils/create-cluster/create-cluster\""
sed -i -z "s|\(#### create-cluster-command ####\n\).*\(\n#### end-create-cluster-commannd-alias ####\)|\1${replacement}\n\2|" $HOME/.zshrc
else
echo "adding aliases to zshrc"
echo "\n\n" >> $HOME/.zshrc
echo "#### create-cluster-command ####" >> $HOME/.zshrc
echo "alias create-cluster=/usr/local/Cellar/redis/${version}/utils/create-cluster/create-cluster" >> $HOME/.zshrc
echo "#### end-create-cluster-commannd-alias ####" >> $HOME/.zshrc
fi
else
ehco "not a macos shell"
fi
echo "restart your shell to run create cluster commands"
echo "To create a cluster run 'create-cluster create'"
echo "To start a cluster run 'create-cluster start'"
echo "To stop a cluster run 'create-cluster stop'"
echo "please note, the above commands will create log files in the directory from which you run them"
echo "Once the cluster is started, you can connect to it using 'redis-cli -c -p 30001'"
else
echo "cluster creation failed"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment