-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstart_redis.sh
executable file
·112 lines (91 loc) · 3.72 KB
/
start_redis.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Path to the redis binary
#
REDIS=/usr/local/bin/redis-server
DEFAULT_CONFIG=/etc/redis/redis.conf
SENTINEL_CONFIG=/etc/redis/sentinel.conf
REDIS_PORT=6379
SENTINEL_PORT=26379
: ${REDIS_RUN_USER:='redis'}
SU="su $REDIS_RUN_USER sh -c"
: ${EXTRA_OPTS:=''}
: ${ENABLE_REDIS:=true}
: ${ENABLE_SENTINEL:=false}
: ${SENTINEL_CLUSTER_NAME:='redis-cluster'}
: ${SENTINEL_CLUSTER_QUORUM:=2}
: ${SENTINEL_DOWN_AFTER_MILLISECONDS:=5000}
: ${SENTINEL_PARALLEL_SYNCS:=1}
: ${SENTINEL_FAILOVER_TIMEOUT:=10000}
: ${REDIS_MASTER_IP:=''}
# Allow both REDIS_PASSWORD and REDIS_PASS as env variables to maintain
# backwards compatibility
: ${REDIS_PASSWORD:=''}
: ${REDIS_PASS:=$REDIS_PASSWORD}
: ${IS_SLAVE:=false}
if [ -n "${RANCHER_SENTINEL_SERVICE}" -a $ENABLE_SENTINEL = true ]; then
# Get my Rancher IP address
MY_RANCHER_IP=$(curl http://rancher-metadata/2015-12-19/self/container/primary_ip)
# If the ip is not found, it probably means we're using the host network stack
# so use the host's ip instead
if [[ $MY_RANCHER_IP = \Not* ]] ; then
MY_RANCHER_IP=$(curl http://rancher-metadata/2015-12-19/self/host/agent_ip)
fi
echo "My IP is: $MY_RANCHER_IP"
# Get the ips of any instances already in the given service
service_ips=$(dig +short $RANCHER_SENTINEL_SERVICE | awk -v ORS=, '{print $1}' | sed 's/,$//')
IFS=, read -ra ips <<<"$service_ips"
# If we only one found IP, then we're the master
if [ ${#ips[@]} -eq 1 ]; then
REDIS_MASTER_IP=$MY_RANCHER_IP
else
# If there's more than one ip, connect to one of the other services and
# get the master's ip address.
for ip in ${ips[@]} ; do
if [ $ip = $MY_RANCHER_IP ]; then
continue
fi
REDIS_MASTER_IP=$(redis-cli -h $ip -p $SENTINEL_PORT sentinel get-master-addr-by-name redis-cluster | awk '{print $1; exit}')
IS_SLAVE=true
break
done
fi
fi
if [ $ENABLE_REDIS = true ]; then
if [ -n "$REDIS_PASS" ]; then
sed -i "s/^# requirepass .*/requirepass $REDIS_PASS/" $DEFAULT_CONFIG
sed -i "s/^# masterauth .*/masterauth $REDIS_PASS/" $DEFAULT_CONFIG
fi
if [ $IS_SLAVE = true ]; then
sed -i "s/^# slaveof <masterip> <masterport>/slaveof $REDIS_MASTER_IP $REDIS_PORT/" $DEFAULT_CONFIG
fi
# Ensure proper ownership on /var/lib/redis directory
chown -R redis:redis /var/lib/redis
fi
if [ $ENABLE_SENTINEL = true ]; then
echo "The master's ip is: $REDIS_MASTER_IP"
# Create the sentinel config file
echo "bind 0.0.0.0" > $SENTINEL_CONFIG
echo "port $SENTINEL_PORT" >> $SENTINEL_CONFIG
echo "dir /tmp" >> $SENTINEL_CONFIG
echo "sentinel monitor $SENTINEL_CLUSTER_NAME $REDIS_MASTER_IP $REDIS_PORT ${SENTINEL_CLUSTER_QUORUM}" >> $SENTINEL_CONFIG
echo "sentinel down-after-milliseconds $SENTINEL_CLUSTER_NAME $SENTINEL_DOWN_AFTER_MILLISECONDS" >> $SENTINEL_CONFIG
echo "sentinel parallel-syncs $SENTINEL_CLUSTER_NAME $SENTINEL_PARALLEL_SYNCS" >> $SENTINEL_CONFIG
echo "sentinel failover-timeout $SENTINEL_CLUSTER_NAME $SENTINEL_FAILOVER_TIMEOUT" >> $SENTINEL_CONFIG
if [ -n $REDIS_PASS ]; then
echo "sentinel auth-pass $SENTINEL_CLUSTER_NAME $REDIS_PASS" >> $SENTINEL_CONFIG
fi
chown $REDIS_RUN_USER:$REDIS_RUN_USER $SENTINEL_CONFIG
fi
# Set backup schedule if we're given a cron time and redis is enabled
if [ -n "${CRON_TIME}" -a $ENABLE_REDIS = true ]; then
exec /enable_backups.sh
fi
# Start redis if enabled
if [ $ENABLE_REDIS = true -a $ENABLE_SENTINEL = true ]; then
$SU "$REDIS $DEFAULT_CONFIG $EXTRA_OPTS &"
$SU "$REDIS $SENTINEL_CONFIG --sentinel"
elif [ $ENABLE_REDIS = false -a $ENABLE_SENTINEL = true ]; then
$SU "$REDIS $SENTINEL_CONFIG --sentinel"
elif [ $ENABLE_REDIS = true -a $ENABLE_SENTINEL = false ]; then
$SU "$REDIS $DEFAULT_CONFIG $EXTRA_OPTS"
fi