#!/bin/bash

exit 0

# Set up Redis
apt-get install -t stretch-backports redis redis-sentinel

# XXX
# probably IPv6 ?
Setting up redis-sentinel (5:4.0.10-1~bpo9+1) ...
Created symlink /etc/systemd/system/sentinel.service → /lib/systemd/system/redis-sentinel.service.
Created symlink /etc/systemd/system/multi-user.target.wants/redis-sentinel.service → /lib/systemd/system/redis-sentinel.service.
Setting up redis-server (5:4.0.10-1~bpo9+1) ...
Job for redis-server.service failed because of unavailable resources or another system error.
See "systemctl status redis-server.service" and "journalctl -xe" for details.
invoke-rc.d: initscript redis-server, action "start" failed.
● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: resources) since Sat 2018-07-14 13:50:33 MDT; 6ms ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 1018 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)

Jul 14 13:50:33 redis3 systemd[1]: Failed to start Advanced key-value store.
Jul 14 13:50:33 redis3 systemd[1]: redis-server.service: Unit entered faile…ate.
Jul 14 13:50:33 redis3 systemd[1]: redis-server.service: Failed with result…es
Hint: Some lines were ellipsized, use -l to show in full.
dpkg: error processing package redis-server (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of redis:
 redis depends on redis-server (<< 5:4.0.10-1~bpo9+1.1~); however:
  Package redis-server is not configured yet.
 redis depends on redis-server (>= 5:4.0.10-1~bpo9+1); however:
  Package redis-server is not configured yet.

dpkg: error processing package redis (--configure):
 dependency problems - leaving unconfigured
Processing triggers for systemd (232-25+deb9u4) ...
Errors were encountered while processing:
 redis-server
 redis
E: Sub-process /usr/bin/dpkg returned an error code (1)

# XXX

cd /etc ; git add . ; git commit -a -m "Install redis"

vim /etc/redis/redis.conf
# Disable IPv6
-bind 127.0.0.1 ::1
+bind 127.0.0.1


# Set up firewall:
# Redis Access Ports
-A INPUT -p tcp --dport  6379 -j ACCEPT
-A INPUT -p tcp --dport 16379 -j ACCEPT
# Sentinel
-A INPUT -p tcp --dport 26379 -j ACCEPT
# Cluster
-A INPUT -p tcp --dport 30001 -j ACCEPT

cd /etc ; git add . ; git commit -a -m "Setup firewall for redis"

# Change /etc/redis/redis.conf like this (using appropriate IP/name):
bind 10.62.1.41
cluster-enabled yes
cluster-config-file redis1.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 0
cluster-migration-barrier 1
cluster-require-full-coverage no


# Change /etc/redis/sentinel.conf like this (using appropriate IP):
bind 10.62.1.41
protected-mode no
sentinel monitor mymaster 10.62.1.41 6379 2


cd /etc ; git add . ; git commit -a -m "Configure redis cluster"

# Install ruby deps for cluster creation script:
apt install ruby
gem install redis
cd /etc ; git add . ; git commit -a -m "Install ruby"

# XXX create cluster, run on one node, redis1:
/usr/share/doc/redis-tools/examples/redis-trib.rb create --replicas 1 10.62.1.41:6379 10.62.1.42:6379 \
10.62.1.43:6379 10.62.1.44:6379 10.62.1.45:6379 10.62.1.46:6379

# If creation fails with:
[ERR] Node 10.62.1.41:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
# Run:
systemctl stop redis-sentinel
systemctl stop redis-server
rm /var/lib/redis/dump.rdb
rm /var/lib/redis/redis*.conf
systemctl start redis-server
systemctl start redis-sentinel
# flushdb if necessary

############ System tweaks ######################
# XXX set in sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled

475:M 18 Jul 20:32:45.616 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

475:M 18 Jul 20:32:45.616 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.


