######################################################################
#### function: install redis
####author:danial_chen
####create date:2019-11-07
####modify date:2019-11-07
######################################################################
#/bin/bash
#useage: sh installredis.sh amount role master_ip redis_password
#eg. sh installredis.sh 3 master 192.168.1.11 *****

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ];then
    echo "Useage : sh $0 amount [master|slave] master_ip redis_port sentinel_port {redis_password} "
    exit 1
fi

set -e
## ENV ##
source /etc/profile
pd=$PWD
amount=$1
role=$2
master_redis_ip=$3
redis_port=$4
sentinel_port=$5
redis_password="`echo ${6}|sed -e 's/&/\\\\\\&/g' -e 's/\//\\\\\\//g'`"
if [ -z "$redis_password" ];then
  echo "redis redis_password is empty"
  exit 10
fi
redis_version="redis-5.0.13"
redis_dataDir=$pd/data
#export redis_port=${redis_port:-6379}
#export sentinel_port=${sentinel_port:-7505}
export redis_password=${redis_password}

## install Redis ##
if [ -d "$pd/$redis_version" ]; then
    rm -rf $redis_version
fi
tar -zxf $redis_version.tar.gz
chmod -R 755 $redis_version conf
cd $pd/$redis_version
make && make install

## Modify config file ##
mkdir -p $redis_dataDir
if [ $role = "master" ];then
	/bin/cp $pd/conf/redis_master_template.conf $pd/conf/redis-$redis_port.conf
else
	/bin/cp $pd/conf/redis_slave_template.conf $pd/conf/redis-$redis_port.conf
fi
sed -i "s/redis_port/$redis_port/g" $pd/conf/redis-$redis_port.conf
sed -i "s/master_redis_ip/$master_redis_ip/g" $pd/conf/redis-$redis_port.conf
sed -i "s#redis_dataDir#$redis_dataDir#g" $pd/conf/redis-$redis_port.conf
sed -i "s/redis_password/$redis_password/g" $pd/conf/redis-$redis_port.conf

systemctl stop sentinel-$sentinel_port || echo
systemctl stop redis-$redis_port || echo
## register systemd redis-port.service ##
cat > /usr/lib/systemd/system/redis-$redis_port.service <<EOF
[Unit]
Description=redis-$redis_port
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server $pd/conf/redis-$redis_port.conf
ExecReload=/bin/kill -s HUP \$MAINPID 
ExecStop=/usr/local/bin/redis-cli -p $redis_port shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable redis-$redis_port
systemctl start redis-$redis_port
	
if [ $amount -gt 1 ];then
	/bin/cp $pd/conf/sentinel_template.conf $pd/conf/sentinel-$sentinel_port.conf
    sed -i "s/sentinel_port/$sentinel_port/g" $pd/conf/sentinel-$sentinel_port.conf		
    sed -i "s#redis_dataDir#$redis_dataDir#g" $pd/conf/sentinel-$sentinel_port.conf
    sed -i "s/master_redis_ip/$master_redis_ip/g" $pd/conf/sentinel-$sentinel_port.conf
    sed -i "s/redis_port/$redis_port/g" $pd/conf/sentinel-$sentinel_port.conf
	sed -i "s/redis_password/$redis_password/g" $pd/conf/sentinel-$sentinel_port.conf

## register systemd sentinel-port.service ##	
cat > /usr/lib/systemd/system/sentinel-$sentinel_port.service <<EOF
[Unit]
Description=sentinel-$sentinel_port
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-sentinel $pd/conf/sentinel-$sentinel_port.conf
ExecReload=/bin/kill -s HUP \$MAINPID 
ExecStop=/usr/local/bin/redis-cli -p $sentinel_port shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable sentinel-$sentinel_port	
systemctl start sentinel-$sentinel_port		
fi

## echo install resault ##
rm -rf $pd/conf/*_template.conf
echo "install redis success!"
