#!/usr/bin/env bash
set -e
set -x
REDISVERSION=4.0.1
WORKSPACE=/tmp
if [ $# -eq 1 ]; then
echo 'Redis version unspecified, use default: 4.0.1'
REDISVERSION=$1
fi
apt-get update
apt-get install -y tcl build-essential
#add redis user
groupadd redis
useradd redis -g redis -s /usr/sbin/nologin
#workspace
mkdir -p /etc/redis /data/redis /var/log/redis
#install
wget http://download.redis.io/releases/redis-$REDISVERSION.tar.gz -O $WORKSPACE/redis-$REDISVERSION.tar.gz
tar xf $WORKSPACE/redis-$REDISVERSION.tar.gz -C $WORKSPACE
cd $WORKSPACE/redis-$REDISVERSION && make && make test && make install
cp $WORKSPACE/redis-$REDISVERSION/*.conf /etc/redis
#configuration
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/redis.conf
sed -i '/^logfile/c logfile "/var/log/redis/redis.log"' /etc/redis/redis.conf
sed -i '/^dir .\//c dir \/data\/redis\/' /etc/redis/redis.conf
cat << EOF > /lib/systemd/system/redis.service
[Unit]
Description=Redis Server
After=network.target