forked from ieQu1/emqx-dev-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-cluster
executable file
·184 lines (156 loc) · 3.34 KB
/
dev-cluster
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
set -euo pipefail
[ -z ${LOG_LEVEL+x} ] && export LOG_LEVEL=notice
[ -z ${N_NODES+x} ] && export N_NODES=10
[ -z ${N_CORE_NODES+x} ] && export N_CORE_NODES=3
[ -z ${LB_STRATEGY+x} ] && export LB_STRATEGY=roundrobin
export STATS_PORT=8898
linux_distro() {
grep DISTRIB_ID /etc/lsb-release | cut -f2 -d'='
}
docker_image() {
case $(linux_distro) in
Arch)
echo "archlinux/archlinux:base-devel-20211019.0.37052"
;;
*)
echo "ubuntu:21.04"
;;
esac
}
assert_rel_built() {
if [ ! -d "_build/emqx/rel/emqx" ]
then
echo "Could not find release directory!"
echo 'Did you forget to call "make emqx-rel"?'
exit 1
fi
}
host() {
echo -n "n${1}.local"
}
node() {
echo -n "emqx@$(host $1)"
}
build() {
[ "${1}" -eq "1" ] && echo "build: ."
}
## Create a unique image name for a current directory
image-name() {
echo -n "$(basename $(pwd))-$(pwd | md5sum | head -c5)"
}
CORE_NODES=$(for i in $(seq 1 $N_CORE_NODES); do
node $i
[ $i -eq $N_CORE_NODES ] || echo -n ','
done)
container() {
cat <<EOF
emqx${1}:
$(build $1)
image: $(image-name)
hostname: $(host $1)
networks:
backplane:
aliases:
- $(host $1)
environment:
- "EMQX_LOG__CONSOLE_HANDLER__ENABLE=true"
- "EMQX_LOG__CONSOLE_HANDLER__LEVEL=${LOG_LEVEL}"
- "EMQX_NODE__NAME=$(node $1)"
- "EMQX_CLUSTER__DB_BACKEND=rlog"
- "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
- "EMQX_CLUSTER__STATIC__SEEDS=[${CORE_NODES}]"
EOF
}
core() {
container $1
}
replicant() {
container $1
cat <<EOF
- "EMQX_CLUSTER__RLOG__ROLE=replicant"
- "EMQX_CLUSTER__RLOG__CORE_NODES=${CORE_NODES}"
EOF
}
create_dockerfiles() {
[ -f ".dockerignore" ] || {
cat <<EOF > .dockerignore
_build/emqx/rel/emqx/data/mnesia
_build/emqx/rel/emqx/log
_build/emqx/rel/emqx/emqx-*.tar.gz
EOF
}
[ -f "Dockerfile" ] || {
assert_rel_built
cat <<EOF > Dockerfile
FROM $(docker_image)
COPY _build/emqx/rel/emqx/ /opt/emqx/
WORKDIR /opt/emqx
CMD /opt/emqx/bin/emqx foreground
EOF
}
# Haproxy dockerfile
mkdir -p haproxy
pushd haproxy
cat <<EOF > haproxy.cfg
defaults
log global
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen stats
bind *:${STATS_PORT}
mode http
stats enable
stats refresh 1s
stats show-node
stats uri /
listen mqtt
bind *:1883
mode tcp
option clitcpka # For TCP keep-alive
timeout client 3h
timeout server 3h
option tcplog
balance ${LB_STRATEGY}
$(for i in $(seq 1 $N_NODES); do
echo " server emqx$i emqx$i:1883 check"
done)
EOF
popd
}
create_dockercompose() {
cat <<EOF > docker-compose.yml
version: '3.3'
networks:
backplane:
services:
$(for i in $(seq 1 $N_CORE_NODES); do
core $i
done)
$(for i in $(seq $(($N_CORE_NODES+1)) $N_NODES); do
replicant $i
done)
haproxy:
image: haproxy:1.7
depends_on:
$(for i in $(seq 1 $N_NODES); do
echo " - emqx$i"
done)
ports:
- 1883:1883
- ${STATS_PORT}:${STATS_PORT}
networks:
backplane:
volumes:
- $(pwd)/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
EOF
}
create_dockerfiles
create_dockercompose
if [[ $# -lt 1 ]]; then
cat docker-compose.yml
else
docker-compose $@
fi