emqx-docker
emqx-docker copied to clipboard
Environment variable to configure container failed when value include "http://" like
when I use docker-compose yml configure container like this:
version: '2.0'
services:
mongo:
image: emqttd:master
hostname: emqttd
ports:
- "18083:18083"
- "1883:1883"
- "8083:8083"
- "8883:8883"
environment:
- EMQ_CLUSTER__DISCOVERY=k8s
- EMQ_CLUSTER__K8S__APISERVER=http://172.16.1.225:8080
- EMQ_CLUSTER__K8S__SERVICE_NAME=ekka
- EMQ_CLUSTER__K8S__ADDRESS_TYPE=ip
- EMQ_CLUSTER__K8S__APP_NAME=ekka
It cause set option error, the error line in start.sh is:
sed -r -i "s/(^#*\s*)($VAR_NAME)\s*=\s*(.*)/\2 = $(eval echo \$$VAR_FULL_NAME)/g" $CONFIG
it replaced as
sed -r -i "s/(^#*\s*)(cluster.k8s.apiserver)\s*=\s*(.*)/\2 = http://172.16.1.225:8080/g"
The format is wrong in sed, “http://“ must be "http:\/\/",so can not use $eval directly. It works when I modified start.sh like this:
if [[ ! -z "$(cat $CONFIG |grep -E "^(^|^#*|^#*\s*)$VAR_NAME")" ]]; then
echo "emq.conf: $VAR_NAME=$(eval echo \$$VAR_FULL_NAME)"
VAR_VALUE=$(eval echo \$$VAR_FULL_NAME)
VAR_VALUE_R=${VAR_VALUE//\//\\\/}
sed -r -i "s/(^#*\s*)($VAR_NAME)\s*=\s*(.*)/\2 = $VAR_VALUE_R/g" $CONFIG
Changes must be done in all the lines include sed with eval.
@eastpav duplicate #21
I overcame the issue with /'s embedded and the problem with = in the value (ie. 'EMQ_AUTH__HTTP__SUPER_REQ__PARAMS=access_token=%u,clientid=%c,username=%u' ) with the following code in start.sh:
for VAR in $(env)
do
# Config normal keys such like node.name = [email protected]
if [[ ! -z "$(echo $VAR | grep -E '^EMQ_')" ]]; then
VAR_NAME=$(echo "$VAR" | sed -r "s/EMQ_([^=]*)=.*/\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
It uses [^=] as the non-greedy substitute for .* since sed doesn't support non greedy pattern matches and uses $(eval echo \$$VAR_FULL_NAME|sed -e 's/\//\\\//g') to inline replace forward slashes.
@turtleDeng Yes, add '\' is ok. I think write URL as URL to config file is more natural .
@phanimahesh @eastpav Thanks, the problem will be fixed in V2.3.7 https://github.com/emqtt/emq-docker/pull/53.