Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nestoru/c8fbc94b068e430a9041 to your computer and use it in GitHub Desktop.
Save nestoru/c8fbc94b068e430a9041 to your computer and use it in GitHub Desktop.
Plain Old Bash (POB) idempotent recipe to install redis
# install redis
cd /tmp
rm -fr redis-stable
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make install
# prepare necessary directories
rm -fr /etc/redis
rm -fr /var/redis
mkdir /etc/redis
mkdir /var/redis
mkdir -p /var/log/redis
mkdir -p /var/run/redis
# copy init script and change it to allow running and stopping (without asking for password) as current user
redis_init=/etc/init.d/redis_6379
cp /tmp/redis-stable/utils/redis_init_script $redis_init
user=`logname`
sed -i "s#\$EXEC \$CONF#su - $user -c \"\$EXEC \$CONF\"#" $redis_init
sed -i "s#\$CLIEXEC -p \$REDISPORT shutdown#su - $user -c \"kill \$PID\"#" $redis_init
sed -i "s#PIDFILE=/var/run#PIDFILE=/var/run/redis#" $redis_init
# copy configuration
cp redis.conf /etc/redis/6379.conf
# create database directory
mkdir /var/redis/6379
chown -R $user:$user /etc/redis
chown -R $user:$user /var/redis
chown -R $user:$user /var/log/redis
chown -R $user:$user /var/run/redis
# kill any redis-server process
set +e
killall -9 redis-server
set -e
# remove any pid file left behind
rm -fr /var/run/redis/*
# for non production environments we use a simple common password
requirepass=secret
# for production requirepass is supposed to be defined in redis-vars.properties which you could host in a subversion directory for example
if [ "$environment" == "production" ]; then
svn export https://svn.sample.com/protected/production/path/redis-vars.properties /tmp
. /tmp/redis-vars.properties
rm /tmp/redis-vars.properties
fi
# modify configuration file to use proper password, pid path, log file and database path. Start it as a daemon
redis_conf_file=/etc/redis/6379.conf
sed -i "s/.*requirepass.*/requirepass $requirepass/" $redis_conf_file
sed -i "s#^pidfile.*#pidfile /var/run/redis/redis_6379.pid#" $redis_conf_file
sed -i "s/^daemonize.*/daemonize yes/" $redis_conf_file
sed -i "s#.*logfile.*#logfile /var/log/redis/redis_6379.log#" $redis_conf_file
sed -i "s#^dir .*#dir /var/redis/6379#" $redis_conf_file
# make sure the service starts after system startup
update-rc.d -f redis_6379 defaults
# start the service
/etc/init.d/redis_6379 start
# test the service
echo "Testing redis replies with pong after authentication ..."
echo -e "auth $requirepass\nping" | redis-cli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment