forked from redis/redis-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
118 lines (99 loc) · 3.54 KB
/
makefile
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
113
114
115
116
117
118
REDIS_BRANCH ?= 6.0
TMP := tmp
BUILD_DIR := ${TMP}/cache/redis-${REDIS_BRANCH}
TARBALL := ${TMP}/redis-${REDIS_BRANCH}.tar.gz
BINARY := ${BUILD_DIR}/src/redis-server
REDIS_CLIENT := ${BUILD_DIR}/src/redis-cli
REDIS_TRIB := ${BUILD_DIR}/src/redis-trib.rb
PID_PATH := ${BUILD_DIR}/redis.pid
SOCKET_PATH := ${BUILD_DIR}/redis.sock
PORT := 6381
SLAVE_PORT := 6382
SLAVE_PID_PATH := ${BUILD_DIR}/redis_slave.pid
SLAVE_SOCKET_PATH := ${BUILD_DIR}/redis_slave.sock
HA_GROUP_NAME := master1
SENTINEL_PORTS := 6400 6401 6402
SENTINEL_PID_PATHS := $(addprefix ${TMP}/redis,$(addsuffix .pid,${SENTINEL_PORTS}))
CLUSTER_PORTS := 7000 7001 7002 7003 7004 7005
CLUSTER_PID_PATHS := $(addprefix ${TMP}/redis,$(addsuffix .pid,${CLUSTER_PORTS}))
CLUSTER_CONF_PATHS := $(addprefix ${TMP}/nodes,$(addsuffix .conf,${CLUSTER_PORTS}))
CLUSTER_ADDRS := $(addprefix 127.0.0.1:,${CLUSTER_PORTS})
define kill-redis
(ls $1 > /dev/null 2>&1 && kill $$(cat $1) && rm -f $1) || true
endef
all: start_all test stop_all
start_all: start start_slave start_sentinel wait_for_sentinel start_cluster create_cluster
stop_all: stop_sentinel stop_slave stop stop_cluster
${TMP}:
@mkdir -p $@
${BINARY}: ${TMP}
@bin/build ${REDIS_BRANCH} $<
test:
@env SOCKET_PATH=${SOCKET_PATH} bundle exec rake test
stop:
@$(call kill-redis,${PID_PATH})
start: ${BINARY}
@${BINARY}\
--daemonize yes\
--pidfile ${PID_PATH}\
--port ${PORT}\
--unixsocket ${SOCKET_PATH}
stop_slave:
@$(call kill-redis,${SLAVE_PID_PATH})
start_slave: ${BINARY}
@${BINARY}\
--daemonize yes\
--pidfile ${SLAVE_PID_PATH}\
--port ${SLAVE_PORT}\
--unixsocket ${SLAVE_SOCKET_PATH}\
--slaveof 127.0.0.1 ${PORT}
stop_sentinel:
@$(call kill-redis,${SENTINEL_PID_PATHS})
@rm -f ${TMP}/sentinel*.conf || true
start_sentinel: ${BINARY}
@for port in ${SENTINEL_PORTS}; do\
conf=${TMP}/sentinel$$port.conf;\
touch $$conf;\
echo '' > $$conf;\
echo 'sentinel monitor ${HA_GROUP_NAME} 127.0.0.1 ${PORT} 2' >> $$conf;\
echo 'sentinel down-after-milliseconds ${HA_GROUP_NAME} 5000' >> $$conf;\
echo 'sentinel failover-timeout ${HA_GROUP_NAME} 30000' >> $$conf;\
echo 'sentinel parallel-syncs ${HA_GROUP_NAME} 1' >> $$conf;\
${BINARY} $$conf\
--daemonize yes\
--pidfile ${TMP}/redis$$port.pid\
--port $$port\
--sentinel;\
done
wait_for_sentinel:
@for port in ${SENTINEL_PORTS}; do\
while : ; do\
if [ $$(${REDIS_CLIENT} -p $${port} SENTINEL SLAVES ${HA_GROUP_NAME} | wc -l) -gt 1 ]; then\
break;\
fi;\
echo 'Waiting for Redis sentinel to be ready...';\
sleep 1;\
done;\
done
stop_cluster:
@$(call kill-redis,${CLUSTER_PID_PATHS})
@rm -f appendonly.aof || true
@rm -f ${CLUSTER_CONF_PATHS} || true
start_cluster: ${BINARY}
@for port in ${CLUSTER_PORTS}; do\
${BINARY}\
--daemonize yes\
--appendonly yes\
--cluster-enabled yes\
--cluster-config-file ${TMP}/nodes$$port.conf\
--cluster-node-timeout 5000\
--pidfile ${TMP}/redis$$port.pid\
--port $$port\
--unixsocket ${TMP}/redis$$port.sock;\
done
create_cluster:
@bin/cluster_creator ${CLUSTER_ADDRS}
clean:
@(test -d ${BUILD_DIR} && cd ${BUILD_DIR}/src && make clean distclean) || true
.PHONY: all test stop start stop_slave start_slave stop_sentinel start_sentinel\
stop_cluster start_cluster create_cluster stop_all start_all clean