Skip to content
This repository was archived by the owner on Aug 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ MAINTAINER Huang Rui <[email protected]>, Turtle <[email protected]>

ENV EMQ_VERSION=v2.3.9

COPY ./start.sh /start.sh

RUN set -ex \
# add build deps, remove after build
&& apk --no-cache add --virtual .build-deps \
Expand Down Expand Up @@ -83,26 +81,30 @@ RUN set -ex \
&& make \
&& mkdir -p /opt && mv /emqttd/_rel/emqttd /opt/emqttd \
&& cd / && rm -rf /emqttd \
&& mv /start.sh /opt/emqttd/start.sh \
&& chmod +x /opt/emqttd/start.sh \
&& ln -s /opt/emqttd/bin/* /usr/local/bin/ \
# removing fetch deps and build deps
&& apk --purge del .build-deps .fetch-deps \
&& rm -rf /var/cache/apk/*

WORKDIR /opt/emqttd
# set home so that any `--user` knows where to put the erlang cookie
ENV HOME /opt/emqttd
WORKDIR ${HOME}

COPY ./start.sh ./
RUN chmod +x ./start.sh

# start emqttd and initial environments
CMD ["/opt/emqttd/start.sh"]
CMD ["./start.sh"]

RUN adduser -D -u 1000 emqtt
RUN adduser -D -u 10001 emqtt

RUN chgrp -Rf root /opt/emqttd && chmod -Rf g+w /opt/emqttd \
&& chown -Rf emqtt /opt/emqttd
RUN chgrp -Rf root ${HOME} && chmod -Rf g+w ${HOME} \
&& chown -Rf emqtt ${HOME}
RUN chmod g=u /etc/passwd

USER emqtt
USER 10001

VOLUME ["/opt/emqttd/log", "/opt/emqttd/data", "/opt/emqttd/lib", "/opt/emqttd/etc"]
VOLUME ["${HOME}/log", "${HOME}/data", "${HOME}/lib", "${HOME}/etc"]

# emqttd will occupy these port:
# - 1883 port for MQTT
Expand Down
31 changes: 18 additions & 13 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ fi
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/emqttd/etc/emq.conf
# Plugin settings in /opt/emqttd/etc/plugins
# Base settings in $_EMQ_HOME/etc/emq.conf
# Plugin settings in $_EMQ_HOME/etc/plugins

_EMQ_HOME="/opt/emqtt"
_EMQ_HOME=$HOME

if ! whoami &> /dev/null; then
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-default}:x:$(id -u):0:${USER_NAME:-default} user:${_EMQ_HOME}:/sbin/nologin" >> /etc/passwd
fi
fi

if [[ -z "$PLATFORM_ETC_DIR" ]]; then
export PLATFORM_ETC_DIR="$_EMQ_HOME/etc"
Expand Down Expand Up @@ -97,8 +103,8 @@ if [[ ! -z "$EMQ_ADMIN_PASSWORD" ]]; then
fi

# Catch all EMQ_ prefix environment variable and match it in configure file
CONFIG=/opt/emqttd/etc/emq.conf
CONFIG_PLUGINS=/opt/emqttd/etc/plugins
CONFIG=$_EMQ_HOME/etc/emq.conf
CONFIG_PLUGINS=$_EMQ_HOME/etc/plugins
for VAR in $(env)
do
# Config normal keys such like node.name = [email protected]
Expand Down Expand Up @@ -131,19 +137,18 @@ if [[ ! -z "$EMQ_LOADED_PLUGINS" ]]; then
echo "EMQ_LOADED_PLUGINS=$EMQ_LOADED_PLUGINS"
# First, remove special char at header
# Next, replace special char to ".\n" to fit emq loaded_plugins format
echo $(echo "$EMQ_LOADED_PLUGINS."|sed -e "s/^[^A-Za-z0-9_]\{1,\}//g"|sed -e "s/[^A-Za-z0-9_]\{1,\}/\. /g")|tr ' ' '\n' > /opt/emqttd/data/loaded_plugins
echo $(echo "$EMQ_LOADED_PLUGINS."|sed -e "s/^[^A-Za-z0-9_]\{1,\}//g"|sed -e "s/[^A-Za-z0-9_]\{1,\}/\. /g")|tr ' ' '\n' > $_EMQ_HOME/data/loaded_plugins
fi

## EMQ Main script

# Start and run emqttd, and when emqttd crashed, this container will stop

/opt/emqttd/bin/emqttd start
tail -f /opt/emqttd/log/erlang.log.1 &
/opt/emqttd/bin/emqttd foreground &

# Wait and ensure emqttd status is running
WAIT_TIME=0
while [[ -z "$(/opt/emqttd/bin/emqttd_ctl status |grep 'is running'|awk '{print $1}')" ]]
while [[ -z "$($_EMQ_HOME/bin/emqttd_ctl status |grep 'is running'|awk '{print $1}')" ]]
do
sleep 1
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:waiting emqttd"
Expand All @@ -169,14 +174,14 @@ fi

if [[ ! -z "$EMQ_JOIN_CLUSTER" ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqttd try join $EMQ_JOIN_CLUSTER"
/opt/emqttd/bin/emqttd_ctl cluster join $EMQ_JOIN_CLUSTER &
$_EMQ_HOME/bin/emqttd_ctl cluster join $EMQ_JOIN_CLUSTER &
fi

# Change admin password

if [[ ! -z "$EMQ_ADMIN_PASSWORD" ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:admin password changed to $EMQ_ADMIN_PASSWORD"
/opt/emqttd/bin/emqttd_ctl admins passwd admin $EMQ_ADMIN_PASSWORD &
$_EMQ_HOME/bin/emqttd_ctl admins passwd admin $EMQ_ADMIN_PASSWORD &
fi

# monitor emqttd is running, or the docker must stop to let docker PaaS know
Expand All @@ -187,7 +192,7 @@ IDLE_TIME=0
while [[ $IDLE_TIME -lt 5 ]]
do
IDLE_TIME=$((IDLE_TIME+1))
if [[ ! -z "$(/opt/emqttd/bin/emqttd_ctl status |grep 'is running'|awk '{print $1}')" ]]; then
if [[ ! -z "$($_EMQ_HOME/bin/emqttd_ctl status |grep 'is running'|awk '{print $1}')" ]]; then
IDLE_TIME=0
else
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqttd not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds"
Expand All @@ -198,7 +203,7 @@ done
# If running to here (the result 5 times not is running, thus in 25s emq 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/emqttd/log/*)
# tail $(ls $_EMQ_HOME/log/*)

echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqttd exit abnormally"
exit 1