IT, 筆記

[bash] 以部份檔名為環境變數名稱並讀入其值

情景

docker環境中有多個Supervisor的config檔(.ini),想在Supervisord (daemon) 服務啟動前利用環境變數控制子服務的啟動,尤如 Linux Systemd 的 enable/disable 搬。

設計考慮

  1. 因supervisor.conf中只導入.ini檔案,所以任何檔案的檔名其結尾不是.ini會被忽略,即為Disabled
  2. 以部份檔名為環境變數名稱,如{service_name}_SERVICE_ENABLE
  3. 如變數存在及其值為 Y 或 N (不論大小寫),會對其檔名處理,變數不存在即忽略。如:
    1. XYZ_SERVICE_ENABLE="y" 時,確保文件名為:xyz.ini
    2. XYZ_SERVICE_ENABLE="n" 時,確保文件名為:xyz.ini.disabled

Shell Script 例子

以下shell script 放於目錄 /docker-entrypoint.d 下,並比supervisor daemon 啟動為先。

檔名:50-1-enable-disable-supervisor-servcies.sh 

#!/bin/bash
#
set +u
# set -e

# A script to enable/disable the supervisor managed services

# NOTE:
# Supervisor config file should be placed in the config folder "${SUPERVISOR_HOME}/conf.d"
# Filename for ENABLED config file should be ended with ".ini"
# Filename for DISABLED config file should be ended with ".ini.disabled" (or whatever not ends with .ini)

# USAGE:
# To enable the service, set the variable ({SRV-NAME}_SERVICE_ENABLE) to "Y" or "y"
# To disable the service, set the variable ({SRV-NAME}_SERVICE_ENABLE) to "N" or "n"
# Unset variable will take no action (i.e. leave current enable/disable state of the service as-is)
# The variable name format is: {SRV-NAME in UPPER capital} + "_SERVICE_ENABLE"
# E.g. for service "xyz", the filename should be "xyz.ini" or "xyz.ini.disabled"
# the variable name is "XYZ_SERVICE_ENABLE"

if [ -z "${SUPERVISOR_HOME}" ]; then
    echo "[ERR] Environment variable not set: SUPERVISOR_HOME"
    exit 1
fi

SERVICE_ENB_VAR_NAME_SUFFIX="_SERVICE_ENABLE"
sp_conf_folder="${SUPERVISOR_HOME}/conf.d"

function set_supervisor_conf {
    # para 1 : Service name
    # para 2 : 1=Enable, 0=Disable
    local sp_conf_file="${sp_conf_folder}/${1}.ini"
    # Change filename to disable/enable
    if [ "$2" -gt 0 ]; then
        if [ -f "${sp_conf_file}.disabled" ]; then
            mv "${sp_conf_file}.disabled" "${sp_conf_file}"
        fi
    else
        if [ -f "${sp_conf_file}" ]; then
            mv "${sp_conf_file}" "${sp_conf_file}.disabled"
        fi
    fi
}

# Handle the service - enable, if it is disabled:
for f in `find ${sp_conf_folder} -name "*.ini.disabled"`; do
    fname=$(basename "$f")
    # TODO: Enhance the string replacement operation (e.g. sed)
    c_fname=${fname/\.ini\.disabled/}
    # To upper case and append suffix
    vn="$(echo $c_fname | tr '[:lower:]' '[:upper:]')${SERVICE_ENB_VAR_NAME_SUFFIX}"
    # Get the value
    vv=$(eval echo "\$${vn}")
    if [ ! -z $vv ]; then
        if [ $vv == 'y' ]||[ $vv == 'Y' ]; then
            set_supervisor_conf "$c_fname" 1
        fi
    fi
done  
# Handle the service - disable, if it is enabled:
for f in `find ${sp_conf_folder} -name "*.ini"`; do
    fname=$(basename "$f")
    c_fname=${fname/\.ini/}
    vn="$(echo $c_fname | tr '[:lower:]' '[:upper:]')${SERVICE_ENB_VAR_NAME_SUFFIX}"
    vv=$(eval echo "\$${vn}")
    if [ ! -z $vv ]; then
        if [ $vv == 'n' ]||[ $vv == 'N' ]; then
            set_supervisor_conf "$c_fname" 0
        fi
    fi
done  

# Show the service enable/disable state:
echo "[INFO] Supervisor controlled service(s) status:"
for f in `find ${sp_conf_folder} -name "*.ini"`; do
    fname=$(basename "$f")
    c_fname=${fname/\.ini/}
    svr_name="$(echo $c_fname | tr '[:lower:]' '[:upper:]')"
    echo "[INFO] (+) ENABLED service: ${svr_name} (Var: ${svr_name}${SERVICE_ENB_VAR_NAME_SUFFIX}, filename=$fname)"
done
for f in `find ${sp_conf_folder} -name "*.ini.disabled"`; do
    fname=$(basename "$f")
    svr_name=${fname/\.ini.disabled/}
    svr_name="$(echo $svr_name | tr '[:lower:]' '[:upper:]')"
    echo "[INFO] (-) DISABLED service: ${svr_name} (Var: ${svr_name}${SERVICE_ENB_VAR_NAME_SUFFIX}, filename=$fname)"
done

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *