Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refact docker-entrypoint.sh. #616

Merged
merged 6 commits into from
Dec 11, 2020
Merged

Refact docker-entrypoint.sh. #616

merged 6 commits into from
Dec 11, 2020

Conversation

PMExtra
Copy link
Contributor

@PMExtra PMExtra commented Nov 26, 2020

Related #612

zmstone and others added 2 commits November 26, 2020 16:40
The sed command substitution of bbmustache style template is stale
emqx no longer releases an unrendered config template
@@ -19,6 +18,9 @@ RUN apk add git \
libc-dev \
libstdc++

COPY tmp/qemu-$QEMU_ARCH-stati* /usr/bin/
COPY . /emqx-rel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort the build stages: run apk add first for more efficient cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @PMExtra sorry for the delay
We need qemu to help us compile other cpu architecture images on x86, so the COPY tmp/qemu-$QEMU_ARCH-stati* /usr/bin/ statement needs to be above, You can check https://github.com/multiarch/qemu-user-static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhanghongtong Thanks for your guidance. But I have some questions.

  1. Why don't we use qemu-user-static like:
FROM multiarch/qemu-user-static:${QEMU_ARCH} AS qemu
FROM ${BUILD_FROM} AS builder
COPY --from=qemu /usr/bin/qemu-$QEMU_ARCH-static /usr/bin
  1. Why should we copy qemu-$QEMU_ARCH-stati* rather than qemu-$QEMU_ARCH-static

Are these all designed to adapt x86_64? (copy nothing if QEMU_ARCH=x86_64)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @PMExtra

Why don't we use qemu-user-static like:

FROM multiarch/qemu-user-static:${QEMU_ARCH} AS qemu
FROM ${BUILD_FROM} AS builder
COPY --from=qemu /usr/bin/qemu-$QEMU_ARCH-static /usr/bin

The multiarch/qemu-user-static image requires the —privileged option, so we did not choose it

Why should we copy qemu-$QEMU_ARCH-stati* rather than qemu-$QEMU_ARCH-static

This is a historical code, it seems there is no difference between the two

Are these all designed to adapt x86_64?

Yes, we will build arm, arm64, 386 and s390 docker images on x86_64 machines, you can see them in GitHub actions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhanghongtong
抱歉我的英语可能没法准确表达我的意思,暂时中文沟通。
第一个问题我想问的是:目前我们应该是在make脚本中下载qemu的依赖到tmp目录,然后build时从tmp目录复制进去。但我们为什么不直接从multiarch/qemu-user-static复制依赖呢?README中也介绍过这种使用方法,这个应该和--privileged没有关系的,只是从其中复制文件,并不需要运行容器。
第二个问题我觉得差异在于,使用globs通配符,当文件不存在时会被忽略,不会报错。而直接写完整路径,如果文件不存在,会build失败。
想到这里我怀疑之所以有这两个设计,是不是为了把复制qemu依赖变成一个可选步骤。因为在x86_64上构建x86_64的镜像,其实是不需要qemu的,事实上,也没有multiarch/qemu-user-static:x86_64这样的镜像。
所以如果按照我的说法来做,就需要对 x86_64 和其它需要qemu的架构,分开成两个 Dockerfile 文件。而这两个看似有缺点的设计,却能够让所有架构包括x86_64自身都使用同样的Dockerfile来构建。
以上是我的想法,只是想核实一下我的理解是否正确。请不吝赐教。
另外,我已经提交commit把qemu的顺序提到 apk add 前面了,如果没有别的问题,这个PR应该可以合并了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhanghongtong Docker CI checks are all success. Please let me know if anything I can do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @PMExtra

第一个问题我想问的是:目前我们应该是在make脚本中下载qemu的依赖到tmp目录,然后build时从tmp目录复制进去。但我们为什么不直接从multiarch/qemu-user-static复制依赖呢?README中也介绍过这种使用方法,这个应该和--privileged没有关系的,只是从其中复制文件,并不需要运行容器。

I think this is a good idea, can you help us verify that this is work? If successful, we don’t need to download qemu anymore

第二个问题我觉得差异在于,使用globs通配符,当文件不存在时会被忽略,不会报错。而直接写完整路径,如果文件不存在,会build失败。

I have tried this point, even if wildcards are used, an error will still be reported when the file does not exist


RUN ln -s /opt/emqx/bin/* /usr/local/bin/
RUN apk add --no-cache curl ncurses-libs openssl sudo libstdc++
RUN ln -s /opt/emqx/bin/* /usr/local/bin/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort the build stages: run apk add first for more efficient cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI, @PMExtra
Just like above, the COPY must be before the RUN

@PMExtra PMExtra changed the title Refactor docker-entrypoint.sh. Refact docker-entrypoint.sh. Nov 27, 2020
## EMQ docker image start script
# Huang Rui <[email protected]>
# EMQ X Team <[email protected]>

## Shell setting
if [[ ! -z "$DEBUG" ]]; then
if [[ -n "$DEBUG" ]]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -ex
else
set -e
fi

shopt -s nullglob
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For globs loop.
It's used to enumerate plugins config, related https://github.com/koalaman/shellcheck/wiki/SC2045

## 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)
LOCAL_IP=$(hostname -i | grep -oE '((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])\.){3}(25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])' | head -n 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve ip regex. (https://stackoverflow.com/a/36760050/6401252)
The grep -h flag is redundant because it read input from stdin instead of file.


## 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"
_EMQX_HOME='/opt/emqx'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format: quote raw string in single quotes rather than double quotes.


if [[ -z "$EMQX_NAME" ]]; then
export EMQX_NAME="$(hostname)"
EMQX_NAME="$(hostname)"
export EMQX_NAME
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fi
export EMQX_HOST
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary variables, merge export at last.

# echo value of $VAR hiding secrets if any
# SYNOPSIS
# echo_value KEY VALUE
echo_value() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent use global variable across functions.
Explicit pass arguments to echo_value.

# get MASK_CONFIG
MASK_CONFIG_FILTER="$MASK_CONFIG_FILTER|password|passwd|key|token|secret"
FORMAT_MASK_CONFIG_FILTER=$(echo $MASK_CONFIG_FILTER |sed -e "s/^[^A-Za-z0-9_]\{1,\}//g"|sed -e "s/[^A-Za-z0-9_]\{1,\}/\|/g")

FORMAT_MASK_CONFIG_FILTER=$(echo "$MASK_CONFIG_FILTER" | sed -r -e 's/^[^A-Za-z0-9_]+//' -e 's/[^A-Za-z0-9_]+$//' -e 's/[^A-Za-z0-9_]+/|/g')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve regex and trim unexpected characters from both head and tail.

# check if contains sensitive value
if [ ! -z $(echo $(echo $VAR_NAME | tr '.' ' ') |grep -w -o -E "$FORMAT_MASK_CONFIG_FILTER") ]; then
echo "$VAR_NAME=***secret***"
if echo "$key" | grep -iqwE "$FORMAT_MASK_CONFIG_FILTER"; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PMExtra PMExtra marked this pull request as ready for review November 27, 2020 09:52
@PMExtra PMExtra closed this Nov 27, 2020
@PMExtra PMExtra reopened this Nov 27, 2020
@PMExtra
Copy link
Contributor Author

PMExtra commented Nov 27, 2020

I closed and reopened this pr for re-trigger GitHub actions.

@PMExtra PMExtra closed this Nov 30, 2020
@PMExtra PMExtra reopened this Nov 30, 2020
@PMExtra
Copy link
Contributor Author

PMExtra commented Nov 30, 2020

I've tried 3 times of CI check. But it would be cancelled after several minutes seems like randomly.
Seeing that there are some successful jobs of docker environment, and the most of cancelled jobs are cancelled in upload artifacts stage that already passed the check stage. So I think it should be regarded pass.

@PMExtra
Copy link
Contributor Author

PMExtra commented Nov 30, 2020

@zmstone Please review it.

@zmstone
Copy link
Member

zmstone commented Nov 30, 2020

Thanks. I'll have a look later.

Copy link
Member

@zmstone zmstone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job

@Rory-Z Rory-Z changed the base branch from master to dev/v4.3.0 December 1, 2020 03:44
@Rory-Z Rory-Z changed the base branch from dev/v4.3.0 to dev/v4.2.4 December 11, 2020 03:02
@Rory-Z Rory-Z merged commit 723727f into emqx:dev/v4.2.4 Dec 11, 2020
Rory-Z pushed a commit that referenced this pull request Dec 11, 2020
* chore(docker): Delete stale code

The sed command substitution of bbmustache style template is stale
emqx no longer releases an unrendered config template

* Refactor docker-entrypoint.sh.
Rory-Z pushed a commit that referenced this pull request Dec 11, 2020
* chore(docker): Delete stale code

The sed command substitution of bbmustache style template is stale
emqx no longer releases an unrendered config template

* Refactor docker-entrypoint.sh.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants