配置文件渲染

使用方式:

添加 <file_name>.template 文件,用于生成 <file_name>

例子:如配置文件内容

<$ REGION $>|bj

配置文件中 region 均配置为 <$ REGION $> 即可

#!/bin/bash

# print log message to stdlog
info() {
    # color 31: red
    # color 32: green
    # color 33: yellow
    echo -e "\E[32;40m[NOTICE] $(date '+%H:%M:%S') ${1}\E[0m"
}

# print log message to stdlog
warning() {
    # color 31: red
    # color 32: green
    # color 33: yellow
    echo -e "\E[33;40m[WARNING] $(date '+%H:%M:%S') ${1}\E[0m"
}

# print log message to stdlog
error() {
    # color 31: red
    # color 32: green
    # color 33: yellow
    echo -e "\E[31;40m[ERROR] $(date '+%H:%M:%S') ${1}\E[0m"
}

# the main method
main() {
    if [ X"$1" == "X" ];then
        exit 0
    fi

    config_dir="$1"
    module="$2"
    local pwd_path=`pwd`
    local deploy_conf="${pwd_path}/deploy.conf"
    if [ ! -d "${config_dir}" ]; then
        exit 0
    fi

    for t in `ls "${config_dir}"|grep template`;do
        file=${t%*.template}
        cp "${config_dir}/${t}" "${config_dir}/${file}.tb"
    done

    if [[ -e "${deploy_conf}" ]]; then
        cat "${deploy_conf}"|
        while read line;do
            key="${line%%|*}"
            value="${line##*|}"
            for t in `ls "${config_dir}"|grep template`;do
                file=${t%*.template}
                sed -i "s#${key}#${value}#g" "${config_dir}/${file}.tb"
            done
        done
        if [[ ${?} -ne 0 ]]; then
            error "Generate ${module} config Failed, plz do it self"
            exit 1
        fi

        for t in `ls "${config_dir}"|grep template`;do
            file=${t%*.template}
            mv "${config_dir}/${file}.tb" "${config_dir}/${file}"
        done
    else
        error "Generate ${module} config Failed, because deploy.conf is not exist, plz do it self"
        exit 1
    fi
    info "Generate ${module} config Success!"
    exit 0
}
main "$@"

Last updated