forked from emqxarchive/emqx-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·202 lines (158 loc) · 6.35 KB
/
start.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh
## EMQ docker image start script
# Huang Rui <[email protected]>
# EMQ X Team <[email protected]>
## Shell setting
if [[ ! -z "$DEBUG" ]]; then
set -ex
fi
## Local IP address setting
LOCAL_IP=$(hostname -i |grep -E -oh '((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])'|head -n 1)
## EMQ Base settings and plugins setting
# Base settings in /opt/emqx/etc/emqx.conf
# Plugin settings in /opt/emqx/etc/plugins
_EMQX_HOME="/opt/emqx"
if [[ -z "$PLATFORM_ETC_DIR" ]]; then
export PLATFORM_ETC_DIR="$_EMQX_HOME/etc"
fi
if [[ -z "$PLATFORM_LOG_DIR" ]]; then
export PLATFORM_LOG_DIR="$_EMQX_HOME/log"
fi
if [[ -z "$EMQX_NAME" ]]; then
export EMQX_NAME="$(hostname)"
fi
if [[ -z "$EMQX_HOST" ]]; then
export EMQX_HOST="$LOCAL_IP"
fi
if [[ -z "$EMQX_WAIT_TIME" ]]; then
export EMQX_WAIT_TIME=5
fi
if [[ -z "$EMQX_NODE__NAME" ]]; then
export EMQX_NODE__NAME="$EMQX_NAME@$EMQX_HOST"
fi
# Set hosts to prevent cluster mode failed
# unset EMQX_NAME
# unset EMQX_HOST
if [[ -z "$EMQX_NODE__PROCESS_LIMIT" ]]; then
export EMQX_NODE__PROCESS_LIMIT=2097152
fi
if [[ -z "$EMQX_NODE__MAX_PORTS" ]]; then
export EMQX_NODE__MAX_PORTS=1048576
fi
if [[ -z "$EMQX_NODE__MAX_ETS_TABLES" ]]; then
export EMQX_NODE__MAX_ETS_TABLES=2097152
fi
if [[ -z "$EMQX__LOG_CONSOLE" ]]; then
export EMQX__LOG_CONSOLE="console"
fi
if [[ -z "$EMQX_LISTENER__TCP__EXTERNAL__ACCEPTORS" ]]; then
export EMQX_LISTENER__TCP__EXTERNAL__ACCEPTORS=64
fi
if [[ -z "$EMQX_LISTENER__TCP__EXTERNAL__MAX_CLIENTS" ]]; then
export EMQX_LISTENER__TCP__EXTERNAL__MAX_CLIENTS=1000000
fi
if [[ -z "$EMQX_LISTENER__SSL__EXTERNAL__ACCEPTORS" ]]; then
export EMQX_LISTENER__SSL__EXTERNAL__ACCEPTORS=32
fi
if [[ -z "$EMQX_LISTENER__SSL__EXTERNAL__MAX_CLIENTS" ]]; then
export EMQX_LISTENER__SSL__EXTERNAL__MAX_CLIENTS=500000
fi
if [[ -z "$EMQX_LISTENER__WS__EXTERNAL__ACCEPTORS" ]]; then
export EMQX_LISTENER__WS__EXTERNAL__ACCEPTORS=16
fi
if [[ -z "$EMQX_LISTENER__WS__EXTERNAL__MAX_CLIENTS" ]]; then
export EMQX_LISTENER__WS__EXTERNAL__MAX_CLIENTS=250000
fi
# Fix issue #42 - export env EMQX_DASHBOARD__DEFAULT_USER__PASSWORD to configure
# 'dashboard.default_user.password' in etc/plugins/emqx_dashboard.conf
if [[ ! -z "$EMQX_ADMIN_PASSWORD" ]]; then
export EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=$EMQX_ADMIN_PASSWORD
fi
# Catch all EMQX_ prefix environment variable and match it in configure file
CONFIG=/opt/emqx/etc/emqx.conf
CONFIG_PLUGINS=/opt/emqx/etc/plugins
for VAR in $(env)
do
# Config normal keys such like node.name = [email protected]
if [[ ! -z "$(echo $VAR | grep -E '^EMQX_')" ]]; then
VAR_NAME=$(echo "$VAR" | sed -r "s/EMQX_([^=]*)=.*/\1/g" | tr '[:upper:]' '[:lower:]' | sed -r "s/__/\./g")
VAR_FULL_NAME=$(echo "$VAR" | sed -r "s/([^=]*)=.*/\1/g")
# Config in emq.conf
if [[ ! -z "$(cat $CONFIG |grep -E "^(^|^#*|^#*\s*)$VAR_NAME")" ]]; then
echo "$VAR_NAME=$(eval echo \$$VAR_FULL_NAME)"
sed -r -i "s/(^#*\s*)($VAR_NAME)\s*=\s*(.*)/\2 = $(eval echo \$$VAR_FULL_NAME|sed -e 's/\//\\\//g')/g" $CONFIG
fi
# Config in plugins/*
if [[ ! -z "$(cat $CONFIG_PLUGINS/* |grep -E "^(^|^#*|^#*\s*)$VAR_NAME")" ]]; then
echo "$VAR_NAME=$(eval echo \$$VAR_FULL_NAME)"
sed -r -i "s/(^#*\s*)($VAR_NAME)\s*=\s*(.*)/\2 = $(eval echo \$$VAR_FULL_NAME|sed -e 's/\//\\\//g')/g" $(ls $CONFIG_PLUGINS/*)
fi
fi
# Config template such like {{ platform_etc_dir }}
if [[ ! -z "$(echo $VAR | grep -E '^PLATFORM_')" ]]; then
VAR_NAME=$(echo "$VAR" | sed -r "s/([^=]*)=.*/\1/g"| tr '[:upper:]' '[:lower:]')
VAR_FULL_NAME=$(echo "$VAR" | sed -r "s/([^=]*)=.*/\1/g")
sed -r -i "s@\{\{\s*$VAR_NAME\s*\}\}@$(eval echo \$$VAR_FULL_NAME|sed -e 's/\//\\\//g')@g" $CONFIG
fi
done
## EMQ Plugin load settings
# Plugins loaded by default
if [[ ! -z "$EMQX_LOADED_PLUGINS" ]]; then
echo "EMQX_LOADED_PLUGINS=$EMQX_LOADED_PLUGINS"
# First, remove special char at header
# Next, replace special char to ".\n" to fit emq loaded_plugins format
echo $(echo "$EMQX_LOADED_PLUGINS."|sed -e "s/^[^A-Za-z0-9_]\{1,\}//g"|sed -e "s/[^A-Za-z0-9_]\{1,\}/\. /g")|tr ' ' '\n' > /opt/emqx/data/loaded_plugins
fi
## EMQ Main script
# Start and run emqx, and when emqx crashed, this container will stop
/opt/emqx/bin/emqx start
tail -f /opt/emqx/log/erlang.log.1 &
# Wait and ensure emqx status is running
WAIT_TIME=0
while [[ -z "$(/opt/emqx/bin/emqx_ctl status |grep 'is running'|awk '{print $1}')" ]]
do
sleep 1
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:waiting emqx"
WAIT_TIME=$((WAIT_TIME+1))
if [[ $WAIT_TIME -gt $EMQX_WAIT_TIME ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:timeout error"
exit 1
fi
done
# Sleep 5 seconds to wait for the loaded plugins catch up.
sleep 5
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx start"
# Run cluster script
if [[ -x "./cluster.sh" ]]; then
./cluster.sh &
fi
# Join an exist cluster
if [[ ! -z "$EMQX_JOIN_CLUSTER" ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx try join $EMQX_JOIN_CLUSTER"
/opt/emqx/bin/emqx_ctl cluster join $EMQX_JOIN_CLUSTER &
fi
# Change admin password
if [[ ! -z "$EMQX_ADMIN_PASSWORD" ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:admin password changed to $EMQX_ADMIN_PASSWORD"
/opt/emqx/bin/emqx_ctl admins passwd admin $EMQX_ADMIN_PASSWORD &
fi
# monitor emqx is running, or the docker must stop to let docker PaaS know
# warning: never use infinite loops such as `` while true; do sleep 1000; done`` here
# you must let user know emqx crashed and stop this container,
# and docker dispatching system can known and restart this container.
IDLE_TIME=0
while [[ $IDLE_TIME -lt 5 ]]
do
IDLE_TIME=$((IDLE_TIME+1))
if [[ ! -z "$(/opt/emqx/bin/emqx_ctl status |grep 'is running'|awk '{print $1}')" ]]; then
IDLE_TIME=0
else
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds"
fi
sleep 5
done
# If running to here (the result 5 times not is running, thus in 25s emqx is not running), exit docker image
# Then the high level PaaS, e.g. docker swarm mode, will know and alert, rebanlance this service
# tail $(ls /opt/emqx/log/*)
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx exit abnormally"
exit 1