#!/bin/sh

# CONTEXT: GUEST during CONSTRUCTION as ROOT
# PURPOSE: Install controller base required packages

set -ex

export DEBIAN_FRONTEND=noninteractive

cat > "/etc/sysctl.d/10-redis-performance.conf" << _EOF_
# See 'http://redis.io/topics/admin' for best practices.
# Make sure to set the Linux kernel overcommit memory setting to 1.
vm.overcommit_memory=1

# Linux kernel will silently truncate 'tcp-backlog' to the value of
# '/proc/sys/net/core/somaxconn' so make sure to raise both the value of
# 'somaxconn' and 'tcp_max_syn_backlog' in order to get the desired effect.
net.ipv4.tcp_max_syn_backlog=1024
net.core.somaxconn=1024

_EOF_

cat > "/etc/rc.local" << _EOF_
#!/bin/bash

# Make sure to disable Linux kernel feature transparent huge pages,
# it will affect greatly both memory usage and latency in a negative way.
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
  echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
  echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

_EOF_

# Install Redis from scratch
ARCH_SAV=$ARCH
unset ARCH
apt-get update
apt-get --allow-unauthenticated install -y build-essential tcl curl
cd /tmp
REDIS_VERSION=3.2.6
REDIS_NAME=redis-$REDIS_VERSION
REDIS_ARCHIVE=$REDIS_NAME.tar.gz
curl -O http://download.redis.io/releases/$REDIS_ARCHIVE
tar xzvf $REDIS_ARCHIVE
cd $REDIS_NAME
make distclean
make
make install
export ARCH=$ARCH_SAV

adduser --system --group --no-create-home redis
# Create the data dir
REDIS_DATA_DIR=/var/lib/redis
REDIS_LOG_DIR=/var/log/redis
REDIS_LOG=$REDIS_LOG_DIR/redis.log
REDIS_RUN_DIR=/var/run/redis
REDIS_PID=$REDIS_RUN_DIR/redis-server.pid
mkdir -p $REDIS_DATA_DIR
chown redis:redis $REDIS_DATA_DIR
chmod 770 $REDIS_DATA_DIR
mkdir -p $REDIS_LOG_DIR
chown redis:redis $REDIS_LOG_DIR
chmod 775 $REDIS_LOG_DIR
mkdir -p $REDIS_RUN_DIR
chown redis:redis $REDIS_RUN_DIR
# Set up a proper conf file to start
REDIS_CONF_NAME=redis.conf
REDIS_CONF_DIR=/etc/redis
REDIS_CONF=$REDIS_CONF_DIR/$REDIS_CONF_NAME
mkdir $REDIS_CONF_DIR
cp /tmp/$REDIS_NAME/$REDIS_CONF_NAME $REDIS_CONF_DIR
chown redis:redis $REDIS_CONF
sed -i "s#dir .*#dir $REDIS_DATA_DIR#" $REDIS_CONF
sed -i "s#pidfile .*#pidfile $REDIS_PID#" $REDIS_CONF
sed -i "s#logfile .*#logfile $REDIS_LOG#" $REDIS_CONF
sed -i "s/supervised no/supervised systemd/" $REDIS_CONF
sed -i "s/daemonize no/daemonize yes/" $REDIS_CONF
sed -i "s/protected-mode yes/protected-mode no/" $REDIS_CONF

cat > "/etc/systemd/system/redis-server.service" << _EOF_
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
PIDFile=$REDIS_PID
User=redis
Group=redis

Environment=statedir=$REDIS_RUN_DIR
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p \${statedir}
ExecStartPre=/bin/chown -R redis:redis \${statedir}
ExecStart=/usr/local/bin/redis-server $REDIS_CONF
ExecReload=/bin/kill -USR2 \$MAINPID
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
_EOF_

cat > "/etc/default/redis-server" << _EOF_
# Call ulimit -n with this argument prior to invoking Redis itself.
# This may be required for high-concurrency environments. Redis itself cannot
# alter its limits as it is not being run as root.
ULIMIT=65536

_EOF_

# Install Python driver for Redis ('redis-py').
pip3 install redis
