[fix:5.0]修正无法启用密码问题;优化脚本;Dockerfile中CMD参数启用变量支持

This commit is contained in:
2020-07-15 17:56:48 +08:00
parent 38e534aed4
commit ec4ce66bbe
12 changed files with 381 additions and 219 deletions
+58 -23
View File
@@ -1,15 +1,22 @@
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 指定原始系统镜像,常用镜像为 colovu/ubuntu:18.04、colovu/debian:10-buster、colovu/alpine:3.11、colovu/openjdk:8u252-jre
FROM colovu/ubuntu:18.04
# 外部指定应用版本信息,如 "--build-arg app_ver=6.0.0"
ARG app_ver=5.0.8
#ARG LOCAL_SERVER=http://172.29.14.108/dist-files/
ARG LOCAL_SERVER=
# 编译镜像时指定本地服务器地址,如 "--build-arg local_url=http://172.29.14.108/dist-files/"
ARG local_url=""
# 定义应用基础常量信息,该常量在容器内可使用
ENV APP_NAME=redis \
APP_EXEC=redis-server \
APP_USER=redis \
APP_GROUP=redis \
APP_VERSION=${app_ver}
# 定义应用基础目录信息,该常量在容器内可使用
ENV APP_BASE_DIR=/usr/local/${APP_NAME} \
APP_DEF_DIR=/etc/${APP_NAME} \
APP_CONF_DIR=/srv/conf/${APP_NAME} \
@@ -21,8 +28,9 @@ ENV APP_BASE_DIR=/usr/local/${APP_NAME} \
APP_CERT_DIR=/srv/cert/${APP_NAME} \
APP_WWW_DIR=/srv/www
# 设置应用需要的环境变量
ENV PATH="${APP_BASE_DIR}/bin:${PATH}"
# 设置应用需要的特定环境变量
ENV \
PATH="${APP_BASE_DIR}/bin:${PATH}"
LABEL \
"Version"="v${APP_VERSION}" \
@@ -30,25 +38,37 @@ LABEL \
"Dockerfile"="https://github.com/colovu/docker-${APP_NAME}" \
"Vendor"="Endial Fang (endial@126.com)"
# 拷贝默认 Shell 脚本至容器相关目录中
COPY prebuilds /
# 镜像内应用安装脚本
# 以下脚本可按照不同需求拆分为多个段,但需要注意各个段在结束前需要清空缓存
# set -eux: 设置 shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
RUN set -eux; \
\
# 设置程序使用静默安装,而非交互模式;类似tzdata等程序需要使用静默安装
export DEBIAN_FRONTEND=noninteractive; \
\
# 设置入口脚本的可执行权限
# 设置容器入口脚本的可执行权限
chmod +x /usr/local/bin/entrypoint.sh; \
\
# 为应用创建对应的组、用户、相关目录
APP_DIRS="${APP_DEF_DIR:-} ${APP_CONF_DIR:-} ${APP_DATA_DIR:-} ${APP_CACHE_DIR:-} ${APP_RUN_DIR:-} ${APP_LOG_DIR:-} ${APP_CERT_DIR:-} ${APP_WWW_DIR:-} ${APP_DATA_LOG_DIR:-}"; \
groupadd -r ${APP_GROUP}; \
useradd -r -g ${APP_GROUP} -s /usr/sbin/nologin ${APP_USER}; \
APP_DIRS="${APP_DEF_DIR:-} ${APP_CONF_DIR:-} ${APP_DATA_DIR:-} ${APP_CACHE_DIR:-} ${APP_RUN_DIR:-} ${APP_LOG_DIR:-} ${APP_CERT_DIR:-} ${APP_WWW_DIR:-} ${APP_DATA_LOG_DIR:-} ${APP_BASE_DIR:-${APP_DATA_DIR}}"; \
mkdir -p ${APP_DIRS}; \
groupadd -r -g 998 ${APP_GROUP}; \
useradd -r -g ${APP_GROUP} -u 999 -s /usr/sbin/nologin -d ${APP_DATA_DIR} ${APP_USER}; \
\
# 应用软件包及依赖
# 应用软件包及依赖项。相关软件包在镜像创建完成时,不会被清理
appDeps=" \
"; \
libnss-wrapper \
xz-utils \
locales \
tzdata \
"; \
\
\
\
# 安装临时使用的软件包及依赖项。相关软件包在镜像创建完后时,会被清理
fetchDeps=" \
wget \
ca-certificates \
@@ -62,27 +82,27 @@ RUN set -eux; \
\
"; \
savedAptMark="$(apt-mark showmanual) ${appDeps}"; \
apt-get -y update; \
apt-get -y install --no-install-recommends $fetchDeps; \
apt-get update; \
apt-get install -y --no-install-recommends ${fetchDeps}; \
\
# 使用下载(编译)方式安装软件
DIST_NAME="wait-for-port-1.0.0-1-linux-amd64-debian-10"; \
DIST_URL=" \
${LOCAL_SERVER} \
${local_url} \
https://downloads.bitnami.com/files/stacksmith/ \
"; \
. /usr/local/scripts/libdownload.sh && download_dist "${DIST_NAME}.tar.gz" "${DIST_URL}"; \
\
# 二进制解压
APP_BIN="/usr/local/bin"; \
tar --extract --gunzip --file "${DIST_NAME}.tar.gz" --directory "${APP_BIN}" --strip-components 4 "${DIST_NAME}/files/common/bin/"; \
tar --extract --file "${DIST_NAME}.tar.gz" --directory "${APP_BIN}" --strip-components 4 "${DIST_NAME}/files/common/bin/"; \
rm -rf "${DIST_NAME}.tar.gz" "/usr/local/bin/.buildcomplete"; \
\
# 使用下载(编译)方式安装软件
DIST_NAME="${APP_NAME}-${APP_VERSION}.tar.gz"; \
DIST_SHA256="f3c7eac42f433326a8d981b50dba0169fdfaf46abb23fcda2f933a7552ee4ed7"; \
DIST_URL=" \
${LOCAL_SERVER} \
${local_url} \
http://download.redis.io/releases/ \
"; \
. /usr/local/scripts/libdownload.sh && download_dist "${DIST_NAME}" "${DIST_URL}" --checksum "${DIST_SHA256}"; \
@@ -108,10 +128,23 @@ RUN set -eux; \
cd /; \
rm -rf ${APP_SRC} ${DIST_NAME}; \
\
# 检测是否存在overrides脚本文件,如果存在,执行
apt-get install -y --no-install-recommends ${appDeps}; \
\
\
\
# 为中国区使用重新配置 TimeZone 信息。需要安装 tzdata 软件包
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
dpkg-reconfigure -f noninteractive tzdata; \
\
# 安装 UTF-8 编码。需要安装 locales 软件包
localedef -c -i en_US -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8; \
echo 'en_GB.UTF-8 UTF-8\nen_US.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen; \
update-locale LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=POSIX && dpkg-reconfigure locales; \
\
# 检测是否存在对应版本的 overrides 脚本文件;如果存在,执行
{ [ ! -e "/usr/local/overrides/overrides-${APP_VERSION}.sh" ] || /bin/bash "/usr/local/overrides/overrides-${APP_VERSION}.sh"; }; \
\
# 设置临时目录的权限信息,设置为777是为了保证后续使用`--user`或`gosu`时,可以更改目录对应的用户属性信息;运行时会被更改为700755
# 设置应用关联目录的权限信息,设置为'777'是为了保证后续使用`--user`或`gosu`时,可以更改目录对应的用户属性信息;运行时会被更改为'700'或'755'
chown -Rf ${APP_USER}:${APP_GROUP} ${APP_DIRS}; \
chmod 777 ${APP_DIRS}; \
\
@@ -132,12 +165,14 @@ RUN set -eux; \
rm -rf /var/lib/apt/lists/*; \
\
# 验证安装的软件是否可以正常运行,常规情况下放置在命令行的最后
redis-cli --version; \
redis-server --version
gosu ${APP_USER} redis-cli --version; \
gosu ${APP_USER} redis-server --version;
VOLUME [ "/srv/data", "/srv/conf", "/var/log", "/var/run" ]
ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
LC_ALL=en_US.UTF-8
WORKDIR /srv/data/redis
VOLUME ["/srv/conf", "/srv/data", "/var/log", "/var/run"]
# 默认使用gosu切换为新建用户启动,必须保证端口在1024之上
EXPOSE 6379
@@ -145,5 +180,5 @@ EXPOSE 6379
# 容器初始化命令,默认存放在:/usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# 应用程序的服务命令,必须使用非守护进程方式运行
CMD ["redis-server", "/srv/conf/redis/redis.conf"]
# 应用程序的服务命令,必须使用非守护进程方式运行。如果使用变量,则该变量必须在运行环境中存在(ENV可以获取)
CMD ["${APP_EXEC}", "${REDIS_CONF_FILE}"]
+213 -127
View File
@@ -1,30 +1,31 @@
#!/bin/bash
#
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 应用通用业务处理函数
# 加载依赖脚本
. /usr/local/scripts/liblog.sh
#. /usr/local/scripts/liblog.sh # 日志输出函数库
. /usr/local/scripts/libcommon.sh # 通用函数库
. /usr/local/scripts/libfile.sh
. /usr/local/scripts/libfs.sh
. /usr/local/scripts/libos.sh
. /usr/local/scripts/libcommon.sh
. /usr/local/scripts/libservice.sh
. /usr/local/scripts/libvalidations.sh
# 函数列表
# 加载应用使用的环境变量初始值,该函数在相关脚本中以eval方式调用
# 加载应用使用的环境变量初始值,该函数在相关脚本中以 eval 方式调用
# 全局变量:
# ENV_* : 容器使用的全局变量
# REDIS_* : 应用配置文件使用的全局变量,变量名根据配置项定义
# APP_* : 在镜像创建时定义的全局变量
# *_* : 应用配置文件使用的全局变量,变量名根据配置项定义
# 返回值:
# 可以被 'eval' 使用的序列化输出
docker_app_env() {
# 以下变量已经在创建镜像时定义,可直接使用
# APP_NAME、APP_EXEC、APP_USER、APP_GROUP、APP_VERSION
# APP_BASE_DIR、APP_DEF_DIR、APP_CONF_DIR、APP_CERT_DIR、APP_DATA_DIR、APP_DATA_LOG_DIR、APP_CACHE_DIR、APP_RUN_DIR、APP_LOG_DIR
cat <<"EOF"
# Debug log message
# Common Settings
export ENV_DEBUG=${ENV_DEBUG:-false}
export ALLOW_EMPTY_PASSWORD="${ALLOW_EMPTY_PASSWORD:-no}"
# Paths
export REDIS_BASE_DIR="${REDIS_BASE_DIR:-${APP_BASE_DIR}}"
@@ -33,7 +34,9 @@ export REDIS_DATALOG_DIR="${REDIS_DATALOG_DIR:-${APP_DATA_LOG_DIR}}"
export REDIS_CONF_DIR="${REDIS_CONF_DIR:-${APP_CONF_DIR}}"
export REDIS_LOG_DIR="${REDIS_LOG_DIR:-${APP_LOG_DIR}}"
export REDIS_BIN_DIR="${REDIS_BIN_DIR:-${REDIS_BASE_DIR}/bin}"
export REDIS_CONF_FILE="${REDIS_CONF_DIR}/redis.conf"
export REDIS_PID_FILE="${APP_RUN_DIR}/redis_6379.pid"
# Users
export REDIS_DAEMON_USER="${REDIS_DAEMON_USER:-${APP_USER}}"
@@ -64,15 +67,16 @@ export REDIS_TLS_DH_PARAMS_FILE="${REDIS_TLS_DH_PARAMS_FILE:-}"
export REDIS_TLS_AUTH_CLIENTS="${REDIS_TLS_AUTH_CLIENTS:-yes}"
# Authentication
export REDIS_ALLOW_EMPTY_PASSWORD="${REDIS_ALLOW_EMPTY_PASSWORD:-no}"
export REDIS_PASSWORD="${REDIS_PASSWORD:-}"
EOF
# 利用 *_FILE 设置密码,不在配置命令中设置密码,增强安全性
if [[ -f "${REDIS_PASSWORD_FILE:-}" ]]; then
cat <<"EOF"
export REDIS_PASSWORD="$(< "${REDIS_PASSWORD_FILE}")"
EOF
fi
if [[ -f "${REDIS_MASTER_PASSWORD_FILE:-}" ]]; then
cat <<"EOF"
export REDIS_MASTER_PASSWORD="$(< "${REDIS_MASTER_PASSWORD_FILE}")"
@@ -107,12 +111,12 @@ redis_common_conf_set() {
value="${value//\?/\\?}"
[[ "$value" = "" ]] && value="\"$value\""
# Check if the value was set before
if grep -q "^${key} .*" "$file"; then
if grep -q "^[#\s]*${key} .*" "$file"; then
# Update the existing key
replace_in_file "$file" "^${key} .*" "${key} ${value}" false
replace_in_file "$file" "^[#\s]*${key} .*" "${key} ${value}" false
else
# Add a new key
printf '\n%s %s' "$key" "$value" >>"$file"
printf "\n%s %s" "$key" "$value" >>"$file"
fi
fi
}
@@ -125,7 +129,7 @@ redis_common_conf_set() {
redis_conf_get() {
local key="${1:?missing key}"
grep -E "^\s*$key " "${REDIS_CONF_DIR}/redis.conf" | awk '{print $2}'
grep -E "^\s*$key " "${REDIS_CONF_FILE}" | awk '{print $2}'
}
# 更新 redis.conf 配置文件中指定变量值,设置关键字及对应值
@@ -135,7 +139,7 @@ redis_conf_get() {
# $1 - 变量
# $2 - 值(列表)
redis_conf_set() {
redis_common_conf_set "${REDIS_CONF_DIR}/redis.conf" "$@"
redis_common_conf_set "${REDIS_CONF_FILE}" "$@"
}
# 更新 sentinel.conf 配置文件中指定变量值,设置关键字及对应值
@@ -155,7 +159,7 @@ redis_sentinel_conf_set() {
# $1 - 变量
redis_conf_unset() {
local key="${1:?missing key}"
remove_in_file "${REDIS_CONF_DIR}/redis.conf" "^\s*$key .*" false
remove_in_file "${REDIS_CONF_FILE}" "^\s*$key .*" false
}
# 获取 Redis 版本信息
@@ -172,69 +176,6 @@ redis_major_version() {
redis_version | grep -E -o "^[0-9]+"
}
# 检测用户参数信息是否满足条件;针对部分权限过于开放情况,打印提示信息
# 全局变量:
# REDIS_*
app_verify_minimum_env() {
local error_code=0
LOG_D "Validating settings in REDIS_* env vars..."
# Auxiliary functions
print_validation_error() {
LOG_E "$1"
error_code=1
}
# Redis authentication validations
if is_boolean_yes "$REDIS_ALLOW_EMPTY_PASSWORD"; then
LOG_W "You set the environment variable REDIS_ALLOW_EMPTY_PASSWORD=${REDIS_ALLOW_EMPTY_PASSWORD}. For safety reasons, do not use this flag in a production environment."
elif [[ -z "$REDIS_PASSWORD" ]]; then
print_validation_error "The REDIS_PASSWORD environment variable is empty or not set. Set the environment variable REDIS_ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development."
fi
if [[ -n "$REDIS_REPLICATION_MODE" ]]; then
if [[ "$REDIS_REPLICATION_MODE" =~ ^(slave|replica)$ ]]; then
if [[ -n "$REDIS_MASTER_PORT_NUMBER" ]]; then
if ! err=$(validate_port "$REDIS_MASTER_PORT_NUMBER"); then
print_validation_error "An invalid port was specified in the environment variable REDIS_MASTER_PORT_NUMBER: $err"
fi
fi
if ! is_boolean_yes "$REDIS_ALLOW_EMPTY_PASSWORD" && [[ -z "$REDIS_MASTER_PASSWORD" ]]; then
print_validation_error "The REDIS_MASTER_PASSWORD environment variable is empty or not set. Set the environment variable REDIS_ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development."
fi
elif [[ "$REDIS_REPLICATION_MODE" != "master" ]]; then
print_validation_error "Invalid replication mode. Available options are 'master/replica'"
fi
fi
if is_boolean_yes "$REDIS_TLS_ENABLED"; then
if [[ "$REDIS_PORT" == "$REDIS_TLS_PORT" ]] && [[ "$REDIS_PORT" != "6379" ]]; then
# If both ports are assigned the same numbers and they are different to the default settings
print_validation_error "Enviroment variables REDIS_PORT and REDIS_TLS_PORT point to the same port number (${REDIS_PORT}). Change one of them or disable non-TLS traffic by setting REDIS_PORT=0"
fi
if [[ -z "$REDIS_TLS_CERT_FILE" ]]; then
print_validation_error "You must provide a X.509 certificate in order to use TLS"
elif [[ ! -f "$REDIS_TLS_CERT_FILE" ]]; then
print_validation_error "The X.509 certificate file in the specified path ${REDIS_TLS_CERT_FILE} does not exist"
fi
if [[ -z "$REDIS_TLS_KEY_FILE" ]]; then
print_validation_error "You must provide a private key in order to use TLS"
elif [[ ! -f "$REDIS_TLS_KEY_FILE" ]]; then
print_validation_error "The private key file in the specified path ${REDIS_TLS_KEY_FILE} does not exist"
fi
if [[ -z "$REDIS_TLS_CA_FILE" ]]; then
print_validation_error "You must provide a CA X.509 certificate in order to use TLS"
elif [[ ! -f "$REDIS_TLS_CA_FILE" ]]; then
print_validation_error "The CA X.509 certificate file in the specified path ${REDIS_TLS_CA_FILE} does not exist"
fi
if [[ -n "$REDIS_TLS_DH_PARAMS_FILE" ]] && [[ ! -f "$REDIS_TLS_DH_PARAMS_FILE" ]]; then
print_validation_error "The DH param file in the specified path ${REDIS_TLS_DH_PARAMS_FILE} does not exist"
fi
fi
[[ "$error_code" -eq 0 ]] || exit "$error_code"
}
# 加载在后续脚本命令中使用的参数信息,包括从"*_FILE"文件中导入的配置
# 必须在其他函数使用前调用
docker_setup_env() {
@@ -276,9 +217,9 @@ redis_generate_conf() {
redis_conf_set port "$REDIS_PORT"
redis_conf_set dir "${REDIS_DATA_DIR}"
redis_conf_set logfile "${REDIS_LOG_DIR}/redis.log" # Log to stdout
redis_conf_set pidfile "${APP_RUN_DIR}/redis_6379.pid"
redis_conf_set pidfile "${REDIS_PID_FILE}"
redis_conf_set daemonize no
redis_conf_set bind 0.0.0.0 # Allow remote connections
redis_conf_set bind 127.0.0.1 # disallow remote connections when init
# Enable AOF https://redis.io/topics/persistence#append-only-file
# Leave default fsync (every second)
redis_conf_set appendonly "${REDIS_AOF_ENABLED}"
@@ -353,36 +294,93 @@ redis_configure_replication() {
fi
}
# 检测 Redis 服务是否在运行中
# 全局变量:
# APP_RUN_DIR
# 返回值:
# 布尔值
#########################
is_redis_running() {
local pid
pid="$(get_pid_from_file "$APP_RUN_DIR/redis_6379.pid")"
# 检测用户参数信息是否满足条件; 针对部分权限过于开放情况,打印提示信息
# 全局变量
# REDIS_*
app_verify_minimum_env() {
local error_code=0
LOG_D "Validating settings in REDIS_* env vars..."
if [[ -z "$pid" ]]; then
false
else
is_service_running "$pid"
# Auxiliary functions
print_validation_error() {
LOG_E "$1"
error_code=1
}
# Redis authentication validations
if is_boolean_yes "$ALLOW_EMPTY_PASSWORD"; then
LOG_W "You set the environment variable ALLOW_EMPTY_PASSWORD=${ALLOW_EMPTY_PASSWORD}. For safety reasons, do not use this flag in a production environment."
elif [[ -z "$REDIS_PASSWORD" ]]; then
print_validation_error "The REDIS_PASSWORD environment variable is empty or not set. Set the environment variable ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development."
fi
if [[ -n "$REDIS_REPLICATION_MODE" ]]; then
if [[ "$REDIS_REPLICATION_MODE" =~ ^(slave|replica)$ ]]; then
if [[ -n "$REDIS_MASTER_PORT_NUMBER" ]]; then
if ! err=$(validate_port "$REDIS_MASTER_PORT_NUMBER"); then
print_validation_error "An invalid port was specified in the environment variable REDIS_MASTER_PORT_NUMBER: $err"
fi
fi
if ! is_boolean_yes "$ALLOW_EMPTY_PASSWORD" && [[ -z "$REDIS_MASTER_PASSWORD" ]]; then
print_validation_error "The REDIS_MASTER_PASSWORD environment variable is empty or not set. Set the environment variable ALLOW_EMPTY_PASSWORD=yes to allow the container to be started with blank passwords. This is recommended only for development."
fi
elif [[ "$REDIS_REPLICATION_MODE" != "master" ]]; then
print_validation_error "Invalid replication mode. Available options are 'master/replica'"
fi
fi
if is_boolean_yes "$REDIS_TLS_ENABLED"; then
if [[ "$REDIS_PORT" == "$REDIS_TLS_PORT" ]] && [[ "$REDIS_PORT" != "6379" ]]; then
# If both ports are assigned the same numbers and they are different to the default settings
print_validation_error "Enviroment variables REDIS_PORT and REDIS_TLS_PORT point to the same port number (${REDIS_PORT}). Change one of them or disable non-TLS traffic by setting REDIS_PORT=0"
fi
if [[ -z "$REDIS_TLS_CERT_FILE" ]]; then
print_validation_error "You must provide a X.509 certificate in order to use TLS"
elif [[ ! -f "$REDIS_TLS_CERT_FILE" ]]; then
print_validation_error "The X.509 certificate file in the specified path ${REDIS_TLS_CERT_FILE} does not exist"
fi
if [[ -z "$REDIS_TLS_KEY_FILE" ]]; then
print_validation_error "You must provide a private key in order to use TLS"
elif [[ ! -f "$REDIS_TLS_KEY_FILE" ]]; then
print_validation_error "The private key file in the specified path ${REDIS_TLS_KEY_FILE} does not exist"
fi
if [[ -z "$REDIS_TLS_CA_FILE" ]]; then
print_validation_error "You must provide a CA X.509 certificate in order to use TLS"
elif [[ ! -f "$REDIS_TLS_CA_FILE" ]]; then
print_validation_error "The CA X.509 certificate file in the specified path ${REDIS_TLS_CA_FILE} does not exist"
fi
if [[ -n "$REDIS_TLS_DH_PARAMS_FILE" ]] && [[ ! -f "$REDIS_TLS_DH_PARAMS_FILE" ]]; then
print_validation_error "The DH param file in the specified path ${REDIS_TLS_DH_PARAMS_FILE} does not exist"
fi
fi
[[ "$error_code" -eq 0 ]] || exit "$error_code"
}
# 以后台方式启动 Redis 服务,并等待启动就绪
# 更改默认监听地址为 "*" 或 "0.0.0.0",以对容器外提供服务;默认配置文件应当为仅监听 localhost(127.0.0.1)
app_enable_remote_connections() {
LOG_D "Modify default config to enable all IP access"
redis_conf_set daemonize no
redis_conf_set bind 0.0.0.0 # Allow remote connections
}
# 以后台方式启动应用服务,并等待启动就绪
# 全局变量:
# REDIS_*
redis_start_server_bg() {
is_redis_running && return
debug "Starting Redis..."
if _is_run_as_root; then
gosu "$REDIS_DAEMON_USER" "redis-server" "${REDIS_CONF_FILE}" "--daemonize" "yes"
else
app_start_server_bg() {
is_app_server_running && return
LOG_I "Starting ${APP_NAME} in background..."
if is_boolean_yes "${ENV_DEBUG}"; then
"redis-server" "${REDIS_CONF_FILE}" "--daemonize" "yes"
else
"redis-server" "${REDIS_CONF_FILE}" "--daemonize" "yes" >/dev/null 2>&1
fi
local counter=3
while ! is_redis_running ; do
while ! is_app_server_running ; do
if [[ "$counter" -ne 0 ]]; then
break
fi
@@ -391,18 +389,20 @@ redis_start_server_bg() {
done
# 检测端口是否就绪
# wait-for-port --timeout 60 "$REDIS_PORT_NUMBER"
#wait-for-port --timeout 60 "$REDIS_PORT_NUMBER"
}
# 停止 Redis 后台服务
# 停止应用后台服务
# 全局变量:
# ENV_DEBUG
redis_stop_server() {
# REDIS_PID_FILE
app_stop_server() {
! is_app_server_running && return
local pass
local port
local args
LOG_I "Stopping ${APP_NAME}..."
! is_redis_running && return
pass="$(redis_conf_get "requirepass")"
is_boolean_yes "$REDIS_TLS_ENABLED" && port="$(redis_conf_get "tls-port")" || port="$(redis_conf_get "port")"
@@ -410,14 +410,14 @@ redis_stop_server() {
[[ "$port" != "0" ]] && args+=("-p" "$port")
#args+=("--daemonize" "yes")
debug "Stopping Redis..."
if _is_run_as_root; then
gosu "$REDIS_DAEMON_USER" "redis-cli" "${args[@]}" shutdown
else
if is_boolean_yes "${ENV_DEBUG}"; then
"redis-cli" "${args[@]}" shutdown
else
"redis-cli" "${args[@]}" shutdown >/dev/null 2>&1
fi
local counter=5
while is_redis_running ; do
while is_app_server_running ; do
if [[ "$counter" -ne 0 ]]; then
break
fi
@@ -426,13 +426,47 @@ redis_stop_server() {
done
}
# 检测应用服务是否在后台运行中
# 全局变量:
# REDIS_PID_FILE
# 返回值:
# 布尔值
is_app_server_running() {
local pid
pid="$(get_pid_from_file "${REDIS_PID_FILE}")"
if [[ -z "$pid" ]]; then
LOG_D "${APP_NAME} is Stopped..."
false
else
LOG_D "${APP_NAME} is Running..."
is_service_running "$pid"
fi
}
# 清理初始化应用时生成的临时文件
app_clean_tmp_file() {
LOG_D "Clean ${APP_NAME} tmp files..."
}
# 在重新启动容器时,删除标志文件及必须删除的临时文件 (容器重新启动)
# 全局变量:
# APP_*
# REDIS_*
app_clean_from_restart() {
LOG_D "Delete temp files when restart container"
}
# 应用默认初始化操作
# 执行完毕后,会在 ${REDIS_DATA_DIR} 目录中生成 app_init_flag 及 data_init_flag 文件
# 执行完毕后,生成文件 ${APP_CONF_DIR}/.app_init_flag 及 ${APP_DATA_DIR}/.data_init_flag 文件
docker_app_init() {
app_clean_from_restart
LOG_D "Check init status of ${APP_NAME}..."
# 检测配置文件是否存在
if [[ ! -f "$REDIS_CONF_FILE" || ! -f "${REDIS_DATA_DIR}/app_init_flag" ]]; then
if [[ ! -f "${APP_CONF_DIR}/.app_init_flag" ]]; then
LOG_I "No injected configuration file found, creating default config files..."
redis_generate_conf
@@ -441,38 +475,90 @@ docker_app_init() {
redis_configure_replication
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." > ${REDIS_DATA_DIR}/app_init_flag
touch ${APP_CONF_DIR}/.app_init_flag
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." >> ${APP_CONF_DIR}/.app_init_flag
else
LOG_I "User injected custom configuration detected!"
fi
if is_dir_empty "$REDIS_DATA_DIR" || [[ ! -f "${REDIS_DATA_DIR}/data_init_flag" ]]; then
if [[ ! -f "${APP_DATA_DIR}/.data_init_flag" ]]; then
LOG_I "Deploying ${APP_NAME} from scratch..."
#app_start_server_bg
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." > ${REDIS_DATA_DIR}/data_init_flag
touch ${APP_DATA_DIR}/.data_init_flag
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." >> ${APP_DATA_DIR}/.data_init_flag
else
LOG_I "Deploying ${APP_NAME} with persisted data..."
fi
}
# 用户自定义的前置初始化操作,依次执行目录 preinitdb.d 中的初始化脚本
# 执行完毕后,生成文件 ${APP_DATA_DIR}/.custom_preinit_flag
docker_custom_preinit() {
LOG_D "Check custom pre-init status of ${APP_NAME}..."
# 检测用户配置文件目录是否存在 preinitdb.d 文件夹,如果存在,尝试执行目录中的初始化脚本
if [ -d "/srv/conf/${APP_NAME}/preinitdb.d" ]; then
# 检测数据存储目录是否存在已初始化标志文件;如果不存在,检索可执行脚本文件并进行初始化操作
if [[ -n $(find "/srv/conf/${APP_NAME}/preinitdb.d/" -type f -regex ".*\.\(sh\)") ]] && \
[[ ! -f "${APP_DATA_DIR}/.custom_preinit_flag" ]]; then
LOG_I "Process custom pre-init scripts from /srv/conf/${APP_NAME}/preinitdb.d..."
# 检索所有可执行脚本,排序后执行
find "/srv/conf/${APP_NAME}/preinitdb.d/" -type f -regex ".*\.\(sh\)" | sort | docker_process_init_files
touch ${APP_DATA_DIR}/.custom_preinit_flag
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." >> ${APP_DATA_DIR}/.custom_preinit_flag
LOG_I "Custom preinit for ${APP_NAME} complete."
else
LOG_I "Custom preinit for ${APP_NAME} already done before, skipping initialization."
fi
fi
}
# 用户自定义的应用初始化操作,依次执行目录initdb.d中的初始化脚本
# 执行完毕后,会在 ${REDIS_DATA_DIR} 目录中生成 custom_init_flag 文件
# 执行完毕后,生成文件 ${APP_DATA_DIR}/.custom_init_flag
docker_custom_init() {
# 检测用户配置文件目录是否存在initdb.d文件夹,如果存在,尝试执行目录中的初始化脚本
if [ -d "${REDIS_CONF_DIR}/initdb.d" ]; then
# 检测数据存储目录是否存在已初始化标志文件;如果不存在,进行初始化操作
if [ ! -f "${REDIS_DATA_DIR}/custom_init_flag" ]; then
LOG_I "Process custom init scripts for ${APP_NAME}..."
LOG_D "Check custom init status of ${APP_NAME}..."
# 检测目录权限,防止初始化失败
ls "${REDIS_CONF_DIR}/initdb.d/" > /dev/null
# 检测用户配置文件目录是否存在 initdb.d 文件夹,如果存在,尝试执行目录中的初始化脚本
if [ -d "/srv/conf/${APP_NAME}/initdb.d" ]; then
# 检测数据存储目录是否存在已初始化标志文件;如果不存在,检索可执行脚本文件并进行初始化操作
if [[ -n $(find "/srv/conf/${APP_NAME}/initdb.d/" -type f -regex ".*\.\(sh\)") ]] && \
[[ ! -f "${APP_DATA_DIR}/.custom_init_flag" ]]; then
LOG_I "Process custom init scripts from /srv/conf/${APP_NAME}/initdb.d..."
docker_process_init_files ${REDIS_CONF_DIR}/initdb.d/*
#app_start_server_bg
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." > ${REDIS_DATA_DIR}/custom_init_flag
# 检索所有可执行脚本,排序后执行
find "/srv/conf/${APP_NAME}/initdb.d/" -type f -regex ".*\.\(sh\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
LOG_D "Executing $f"; "$f"
else
LOG_D "Sourcing $f"; . "$f"
fi
;;
*) LOG_D "Ignoring $f" ;;
esac
done
touch ${APP_DATA_DIR}/.custom_init_flag
echo "$(date '+%Y-%m-%d %H:%M:%S') : Init success." >> ${APP_DATA_DIR}/.custom_init_flag
LOG_I "Custom init for ${APP_NAME} complete."
else
LOG_I "Custom init for ${APP_NAME} already done before, skipping initialization."
fi
fi
# 停止初始化时启动的后台服务
is_app_server_running && app_stop_server
# 删除第一次运行生成的临时文件
app_clean_tmp_file
# 绑定所有 IP ,启用远程访问
app_enable_remote_connections
}
+39 -20
View File
@@ -1,77 +1,96 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 容器入口脚本
# 设置遇到执行错误则退出执行
set -o errexit
# 设置脚本遇到未初始化或声明的变量退出执行
set -o nounset
# 设置遇到管道命令失败则退出执行
set -o pipefail
# set -o xtrace # Uncomment this line for debugging purpose
# 设置调试信息输出,及命令在执行前先打印命令执行前的命令内容
# set -o xtrace
# 加载依赖脚本
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/libcommon.sh
#. /usr/local/scripts/liblog.sh # 日志输出函数库
#. /usr/local/scripts/libcommon.sh # 通用函数库
. /usr/local/bin/appcommon.sh # 应用专用函数库
. /usr/local/bin/appcommon.sh
LOG_D "Run entrypoint.sh for container init..."
APP_DIRS="${APP_DEF_DIR:-} ${APP_HOME_DIR:-} ${APP_CONF_DIR:-} ${APP_DATA_DIR:-} ${APP_CACHE_DIR:-} ${APP_RUN_DIR:-} ${APP_LOG_DIR:-} ${APP_CERT_DIR:-} ${APP_WWW_DIR:-} ${APP_DATA_LOG_DIR:-}"; \
# 加载环境变量, docker_app_env()函数在文件 app-common.sh 中定义
# 初始化环境变量。 docker_app_env()函数在文件 appcommon.sh 中定义
eval "$(docker_app_env)"
# 定义容器中使用的默认目录(未定义时设置默认值为空"")
APP_DIRS="${APP_DEF_DIR:-} ${APP_HOME_DIR:-} ${APP_CONF_DIR:-} ${APP_DATA_DIR:-} ${APP_CACHE_DIR:-} ${APP_RUN_DIR:-} ${APP_LOG_DIR:-} ${APP_CERT_DIR:-} ${APP_WWW_DIR:-} ${APP_DATA_LOG_DIR:-}"
# 打印镜像欢迎信息
docker_print_welcome
# 检测数据卷,创建默认的关联目录,并拷贝所必须的默认配置文件及初始化文件
# 全局变量:
# APP_*
docker_ensure_dir_and_configs() {
_is_restart && return
local user_id; user_id="$(id -u)"
LOG_D "Directories: ${APP_DIRS}"
for dir in ${APP_DIRS}; do
LOG_D "Check directory $dir"
ensure_dir_exists "$dir"
done
# 检测指定文件是否在配置文件存储目录存在,如果不存在则拷贝(新挂载数据卷、手动删除都会导致不存在)
LOG_D "Check config files"
if ! is_dir_empty ${APP_DEF_DIR}; then
ensure_config_file_exist ${APP_DEF_DIR}/*
LOG_D "Check config files..."
if [[ ! -z "$(ls -A "${APP_DEF_DIR}")" ]]; then
ensure_config_file_exist "${APP_DEF_DIR}" $(ls -A "${APP_DEF_DIR}")
fi
}
_main() {
# 替换命令行中的变量
set -- $(eval echo "$@")
# 如果命令行参数是以配置参数("-")开始,修改执行命令,确保使用可执行应用命令启动服务器
if [ "${1:0:1}" = '-' ]; then
set -- "${APP_EXEC}" "$@"
fi
# 命令行参数以可执行应用命令起始,且不包含直接返回的命令(如:-V、--version、--help)时,执行初始化操作
if [ "$1" = "${APP_EXEC}" ] && ! docker_command_help "$@"; then
# docker_setup_env
# 检测 ENV_* REDIS_* 环境变量是否有效
if [ "$1" = "${APP_EXEC}" ] && ! docker_command_help "$@"; then
# 检测启动容器时设置的环境变量是否有效
app_verify_minimum_env
# 检测应用需要使用的目录是否存在,并设置相应用户权限
# 检测应用需要使用的目录及配置文件是否存在
docker_ensure_dir_and_configs
# 以root用户运行时,会使用gosu重新以"APP_USER"用户运行当前脚本
LOG_D "Check if run as root"
if _is_run_as_root; then
LOG_D "Change permissions when run as root"
# 以root用户启动时,修改相应目录的所属用户信息为APP_USER,确保切换用户时,权限正常
# 以root用户启动时,修改相应目录的所属用户信息为 APP_USER ,确保切换用户时,权限正常
for dir in ${APP_DIRS}; do
LOG_D "Change ownership and permissions of $dir"
configure_permissions_ownership "$dir" -f 755 -d 755 -u "${APP_USER}"
chmod 755 ${dir}
configure_permissions_ownership "$dir" -u "${APP_USER}" -g "${APP_GROUP}"
done
# 解决使用gosu后,nginx: [emerg] open() "/dev/stdout" failed (13: Permission denied)
LOG_D "Change permissions of stdout/stderr to 0622"
chmod 0622 /dev/stdout /dev/stderr
LOG_I ""
LOG_I "Restart container with default user: ${APP_USER}"
LOG_I " command: $@"
export RESTART_FLAG=1
exec gosu "${APP_USER}" "$0" "$@"
fi
# 执行预初始化操作
docker_custom_preinit
# 执行应用初始化操作
docker_app_init
@@ -79,8 +98,8 @@ _main() {
docker_custom_init
fi
LOG_I "Start container with: $@"
# 执行命令行
LOG_I "Start container with command: $@"
# 执行命令行
exec "$@"
}
+42 -31
View File
@@ -1,4 +1,5 @@
#!/bin/bash
# Ver: 1.1 by Endial Fang (endial@126.com)
#
# shellcheck disable=SC1091
@@ -6,7 +7,7 @@
BOLD='\033[1m'
# 加载依赖项
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
@@ -14,24 +15,22 @@ BOLD='\033[1m'
# 全局变量:
# APP_NAME
print_image_welcome_page() {
local github_url="https://github.com/colovu/docker-${APP_NAME}"
if [ x"${WELCOME_MESSAGE:-}" = "x" ]; then
LOG_I ""
LOG_I " ######## ######## ### ######## ### ## ### ##"
LOG_I " ### ## ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ## ### ## ### ### ## #### ### ##"
LOG_I "######## ######## ######## ######## ## ########"
LOG_I ""
LOG_I "Welcome to the ${BOLD}${APP_NAME}${RESET} container"
LOG_I "Project on Github: ${BOLD}${github_url}${RESET}"
LOG_I "Send us your feedback at ${BOLD}endial@126.com${RESET}"
LOG_I ""
_is_restart && return
export WELCOME_MESSAGE=1
fi
local github_url="https://github.com/colovu/docker-${APP_NAME}"
LOG_I ""
LOG_I " ######## ######## ### ######## ### ## ### ##"
LOG_I " ### ## ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ### ## ### ### ## ### ## ### ##"
LOG_I " ### ## ### ## ### ### ## #### ### ##"
LOG_I "######## ######## ######## ######## ## ########"
LOG_I ""
LOG_I "Welcome to the ${BOLD}${APP_NAME}${RESET} container"
LOG_I "Project on Github: ${BOLD}${github_url}${RESET}"
LOG_I "Send us your feedback at ${BOLD}endial@126.com${RESET}"
LOG_I ""
}
# 根据需要打印欢迎信息
@@ -92,23 +91,27 @@ docker_process_init_files() {
# 默认配置文件路径:/etc/${APP_NAME}
# 目标配置文件路径:/srv/conf/${APP_NAME}
# 参数:
# $* - 文件及目录列表字符串,以" "分割
# $1 - 基础路径
# $* - 基础路径下的文件及目录列表,以" "分割
# 例子:
# ensure_config_file_exist /etc/${APP_NAME}/*
# ensure_config_file_exist /etc/${APP_NAME} conf.d server.conf
ensure_config_file_exist() {
local f
local -r base_path="${1:?paths is missing}"
local f=""
local dist=""
LOG_D "Parameter: $@"
shift 1
LOG_D "List to check: $@"
while [ "$#" -gt 0 ]; do
f="${1}"
LOG_D "Process ${f}"
if [ -d ${f} ]; then
dist="$(echo ${f} | sed -e 's/\/etc/\/srv\/conf/g')"
[ ! -d "${dist}" ] && LOG_I "Create directory: ${dist}" && mkdir -p "${dist}"
[[ ! -z $(ls -A "$f") ]] && ensure_config_file_exist ${f}/*
LOG_D "Process \"${f}\""
if [ -d "${base_path}/${f}" ]; then
dist="$(echo ${base_path}/${f} | sed -e 's/\/etc/\/srv\/conf/g')"
[[ ! -d "${dist}" ]] && LOG_I "Create directory: ${dist}" && mkdir -p "${dist}"
[[ ! -z $(ls -A "${base_path}/${f}") ]] && ensure_config_file_exist "${base_path}/${f}" $(ls -A "${base_path}/${f}")
else
dist="$(echo ${f} | sed -e 's/\/etc/\/srv\/conf/g')"
[ ! -e "${dist}" ] && LOG_I "Copy: ${f} ===> ${dist}" && cp "${f}" "${dist}"
dist="$(echo ${base_path}/${f} | sed -e 's/\/etc/\/srv\/conf/g')"
[[ ! -e "${dist}" ]] && LOG_I "Copy: ${base_path}/${f} ===> ${dist}" && cp "${base_path}/${f}" "${dist}" && rm -rf "/srv/conf/${APP_NAME}/.app_init_flag"
fi
shift
done
@@ -119,14 +122,22 @@ ensure_config_file_exist() {
# 布尔值
_is_run_as_root() {
if [[ "$(id -u)" = "0" ]]; then
LOG_D "Run as root"
LOG_D "Check if run as root: Yes"
true
else
LOG_D "User id: $(id -u)"
LOG_D "Check if run as root: No (ID $(id -u))"
false
fi
}
_is_restart() {
if [ x"${RESTART_FLAG:-}" = "x" ]; then
false
else
true
fi
}
# 检测当前脚本是被直接执行的,还是从其他脚本中使用 "source" 调用的
_is_sourced() {
[ "${#FUNCNAME[@]}" -ge 2 ] \
@@ -1,4 +1,5 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 从服务器(列表)下载相应软件包
@@ -1,7 +1,11 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 文件操作函数库
# 加载依赖项
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
# 检测"*_FILE"文件,并从文件中读取信息作为参数值;环境变量不允许 VAR 与 VAR_FILE 方式并存
+14 -13
View File
@@ -1,9 +1,10 @@
#!/bin/bash
# Ver: 1.1 by Endial Fang (endial@126.com)
#
# 文件管理函数库
# 加载依赖项
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
@@ -93,24 +94,24 @@ configure_permissions_ownership() {
read -r -a filepaths <<< "$paths"
for p in "${filepaths[@]}"; do
if [[ -e "$p" ]]; then
LOG_D "Check directory $p"
if [[ -n $dir_mode ]]; then
LOG_D "Change permissions to 755 of directories in $p"
find -L "$p" -type d -exec chmod "$dir_mode" '{}' +
LOG_D "Check $p"
if [[ -n ${dir_mode} ]]; then
LOG_D "Change permissions to ${dir_mode} of directories in $p"
find -L "$p" -type d -print | xargs -i chmod "${dir_mode}" '{}'
fi
if [[ -n $file_mode ]]; then
LOG_D "Change permissions to 755 of files in $p"
find -L "$p" -type f -exec chmod "$file_mode" '{}' +
if [[ -n ${file_mode} ]]; then
LOG_D "Change permissions to ${file_mode} of files in $p"
find -L "$p" -type f -print | xargs -i chmod "${file_mode}" '{}'
fi
if [[ -n $user ]] && [[ -n $group ]]; then
if [[ -n $user ]] && [[ -n ${group} ]]; then
LOG_D "Change ownership to ${user}:${group} of files and directories in $p"
find -L "$p" \! -user ${user} -or \! -group ${group} -exec chown -L "$user":"$group" '{}' +
find -L "$p" \( \! -user ${user} -or \! -group ${group} \) -print | xargs -i chown -L "${user}":"${group}" '{}'
elif [[ -n $user ]] && [[ -z $group ]]; then
LOG_D "Change user to ${user} of files and directories in $p"
find -L "$p" \! -user ${user} -exec chown -L "$user" '{}' +
find -L "$p" \! -user ${user} -print | xargs -i chown -L "${user}" '{}'
elif [[ -z $user ]] && [[ -n $group ]]; then
LOG_D "Change groupto ${group} of files and directories in $p"
find -L "$p" \! -group ${group} -exec chgrp -L "$group" '{}' +
LOG_D "Change group to ${group} of files and directories in $p"
find -L "$p" \! -group ${group} -print | xargs -i chgrp -L "${group}" '{}'
fi
else
LOG_E "$p does not exist"
@@ -1,4 +1,5 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 日志处理函数库
+2 -1
View File
@@ -1,11 +1,12 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 网络管理函数库
# shellcheck disable=SC1091
# 加载依赖项
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
+2 -1
View File
@@ -1,11 +1,12 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 操作系统控制函数库
# shellcheck disable=SC1091
# 加载依赖项
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
@@ -1,11 +1,12 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 服务管理函数库
# shellcheck disable=SC1091
# Load Generic Libraries
. /usr/local/scripts/libvalidations.sh
# 加载依赖项
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表
@@ -1,9 +1,10 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
#
# 数据有效性校验函数库
# 加载依赖项
. /usr/local/scripts/liblog.sh
. /usr/local/scripts/liblog.sh # 日志输出函数库
# 函数列表