Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update config file using environment variable while docker create
  • Loading branch information
kernelai authored Mar 24, 2020
1 parent e05d2c0 commit abe1dde
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 9 deletions.
26 changes: 17 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
FROM centos:centos7
MAINTAINER left2right <[email protected]>
FROM centos:7

RUN rpm -ivh https://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm && \
LABEL maintainer="[email protected], [email protected]"

ENV PIKA /pika
ENV PIKA_BUILD_DIR /tmp/pika
ENV PATH ${PIKA}:${PIKA}/bin:${PATH}

COPY . ${PIKA_BUILD_DIR}

WORKDIR ${PIKA_BUILD_DIR}

RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm && \
yum -y makecache && \
yum -y install snappy-devel && \
yum -y install protobuf-devel && \
Expand All @@ -15,17 +24,16 @@ RUN rpm -ivh https://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm &
yum -y install make && \
yum -y install which && \
yum -y install git && \
make && \
make -j24 && \
cp -r ${PIKA_BUILD_DIR}/output ${PIKA} && \
cp -r ${PIKA_BUILD_DIR}/entrypoint.sh ${PIKA} && \
yum -y remove gcc-c++ && \
yum -y remove make && \
yum -y remove which && \
yum -y remove git && \
yum -y clean all && \
rm -rf /var/cache/yum && \
rm -rf .git && \
rm -rf ${PIKA_BUILD_DIR}
yum -y clean all

WORKDIR ${PIKA}

CMD ["sh", "-c", "${PIKA}/bin/pika -c ${PIKA}/conf/pika.conf"]
ENTRYPOINT ["/pika/entrypoint.sh"]
CMD ["/pika/bin/pika", "-c", "/pika/conf/pika.conf"]
126 changes: 126 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/sh
#
# CONFIG(path1;path2)
# You can add the path of configuration file or folder to the CONFIG
# environment variable, and use semicolon to separate them. This script
# will catch all SUNKAISENS_ prefix environment variable and match it
# in configuration files. e.g. 'SUNKAISENS_HELLO__ZHANG_XIANG_LONG' may
# be converted to 'hello.zhang_xiang_long'. This is borrowed from the
# emqx's docker-entrypoint.sh.
#
# VAR_CASE(CamelCase,snake_case,spinal-case,ignore-case,raw-case)
# We use 'raw-case' by default, if you set the VAR_CASE environment
# variable to 'CamelCase', that will be converted to 'hello.zhangXiangLong'.
# Of course, we also support case-insensitive variable name matching by
# setting VAR_CASE to 'ignore-case', but it is not recommended because you
# need to ensure that the variable name is unique when the case is ignored.
#
# VAR_SEPARATOR('=' or ':')
# VAR_SEPARATOR environment variable is used to separate the name and
# value of variables, we use '=' by default, you can also use ':'.
#
# WAIT_FOR_SERVICE(IP1[:Port1];IP2[:Port2])
# In addition, you can add services that you want wait for to the WAIT_FOR_SERVICE
# environment variable, such as database service. This script will wait
# for these services until they are available.
#
# ZhangXiangLong <[email protected]>

set -eo pipefail

CONFIG=/pika/conf/pika.conf
VAR_CASE=spinal-case
VAR_SEPARATOR=":"

case $VAR_SEPARATOR in
:)
;;
=)
;;
*)
VAR_SEPARATOR="=";;
esac

array=(${CONFIG//;/ })
for path in ${array[@]}; do
if [[ -f $path ]]; then
file_list=$file_list";"$path
elif [[ -d $path ]]; then
for var in $(ls $path); do
if [[ -f $path/$var ]]; then
file_list=$file_list";"$path/$var
fi
done
fi
done
file_array=(${file_list//;/ })

var_replace() {
for file in ${file_array[@]}; do
if [[ -n "$(cat $file | grep -E "^(^|^#*|^#*\s*)$var_name")" ]]; then
echo "$var_name${VAR_SEPARATOR}$(eval echo \$$var_full_name)"
echo "$(sed -r "s/(^#*\s*)($var_name)\s*${VAR_SEPARATOR}\s*(.*)/\2${VAR_SEPARATOR}$(eval echo \$$var_full_name | sed 's/[[:punct:]]/\\&/g')/g" $file)" > $file
fi
done
}

var_replace_i() {
for file in ${file_array[@]}; do
if [[ -n "$(cat $file | grep -i -E "^(^|^#*|^#*\s*)$var_name")" ]]; then
echo "$var_name${VAR_SEPARATOR}$(eval echo \$$var_full_name)"
echo "$(sed -r "s/(^#*\s*)($var_name)\s*${VAR_SEPARATOR}\s*(.*)/\2${VAR_SEPARATOR}$(eval echo \$$var_full_name | sed 's/[[:punct:]]/\\&/g')/gI" $file)" > $file
fi
done
}

for var in $(env); do
if [[ -n "$(echo $var | grep -E "^PIKA_")" ]]; then
var_name=$(echo "$var" | sed -r "s/PIKA_([^=]*)=.*/\1/g" | sed -r "s/__/\./g")
var_full_name=$(echo "$var" | sed -r "s/([^=]*)=.*/\1/g")
case $VAR_CASE in
CamelCase)
var_name=$(echo "$var_name" | tr '[:upper:]' '[:lower:]' | sed -r "s/_([^_])/\u\1/g")
var_replace;;
snake_case)
var_name=$(echo "$var_name" | tr '[:upper:]' '[:lower:]')
var_replace;;
spinal-case)
var_name=$(echo "$var_name" | tr '[:upper:]' '[:lower:]' | tr '_' '-')
var_replace;;
ignore-case)
var_name=$(echo "$var_name" | tr '[:upper:]' '[:lower:]')
var_replace_i;;
raw-case | *)
var_replace;;
esac
fi
done

# https://docs.docker.com/compose/startup-order/
# https://github.com/vishnubob/wait-for-it
if [[ -n "$WAIT_FOR_SERVICE" ]]; then
serv_array=(${WAIT_FOR_SERVICE//;/ })
for serv in ${serv_array[@]}; do
serv_addr=(${serv//:/ })
serv_host=${serv_addr[0]}
serv_port=${serv_addr[1]}
case ${#serv_addr[*]} in
1)
until host $serv_host; do
sleep 3
done
echo "$serv_host OK.";;
2)
until host $serv_host; do
sleep 3
done
echo "$serv_host OK."

wait-for-it.sh -t 0 $serv;;
*)
;;
esac
done
fi

exec "$@"

0 comments on commit abe1dde

Please sign in to comment.