|
|
- #!/bin/bash
- # init_config.sh
- # init enviroment config scripts.
- # @version 170322:1
- # @author zhangxuhong <zhangxuhong@xitu.io>
- #
-
- # ----------------------------[manual config here]------------------------------
-
-
- # base config
- REPO_SRC=/data/repo/baidu_link_submit_ms
- REPO_NAME=baidu_link_submit_ms
- BUILD_TARGET="${REPO_NAME}"
- RELEASE_ENV=$1 # test, beta, online
-
- # nginx config file type
- DOMAIN_NAME="baidu-link-submit-ms.juejin.im"
- PUBLIC_UPSTREAM_PORT=13100
- PRIVATE_UPSTREAM_PORT=13100
-
-
- # ------------------------------------------------------------------------------
-
- # default config
- NGINX_CONF_TMPL_PUBLIC="REPO_NAME.public.conf"
- NGINX_CONF_TMPL_PRIVATE="REPO_NAME.private.conf"
- SYSTEMCTL_CONF_TMPL_BETA="REPO_NAME.beta.service"
- SYSTEMCTL_CONF_TMPL_RELEASE="REPO_NAME.online.service"
-
- # auto generated config
- # nginx
- NGINX_CONFIG_TMPL_FOLDER="${REPO_SRC}/scripts/config_template/nginx"
- NGINX_CONFIG_DEPLOY_FOLDER="${REPO_SRC}/config/nginx"
- NGINX_CONF_PUBLIC="${REPO_NAME}.public.conf"
- NGINX_CONF_PRIVATE="${REPO_NAME}.private.conf"
- NGINX_CONF_PUBLIC_ADDR="${NGINX_CONFIG_DEPLOY_FOLDER}/${NGINX_CONF_PUBLIC}"
- NGINX_CONF_PRIVATE_ADDR="${NGINX_CONFIG_DEPLOY_FOLDER}/${NGINX_CONF_PRIVATE}"
-
- if [ "${PUBLIC_UPSTREAM_PORT}" != "" ]; then
- UPSTREAM_ADDR_PUBLIC="127.0.0.1:${PUBLIC_UPSTREAM_PORT}"
- fi
- if [ "${PRIVATE_UPSTREAM_PORT}" != "" ]; then
- UPSTREAM_ADDR_PRIVATE="127.0.0.1:${PRIVATE_UPSTREAM_PORT}"
- fi
-
- # systemctl
- SYSTEMCTL_CONFIG_TMPL_FOLDER="${REPO_SRC}/scripts/config_template/systemctl"
- SYSTEMCTL_CONFIG_DEPLOY_FOLDER="${REPO_SRC}/config/systemctl"
- SYSTEMCTL_CONF="${REPO_NAME}.service"
- SYSTEMCTL_CONF_ADDR="${SYSTEMCTL_CONFIG_DEPLOY_FOLDER}/${SYSTEMCTL_CONF}"
-
- # release env
- if [ "$RELEASE_ENV" == "dev" ]; then
- RELEASE_ENV_LOCATION="$RELEASE_ENV"
- elif [ "$RELEASE_ENV" == "beta" ]; then
- RELEASE_ENV_LOCATION="$RELEASE_ENV"
- elif [ "$RELEASE_ENV" == "online" ]; then
- RELEASE_ENV_LOCATION="$RELEASE_ENV"
- else
- RELEASE_ENV_LOCATION=""
- fi
- if [ "$RELEASE_ENV_LOCATION" == "" ]; then
- echo "please input an avaliable deploy environment [dev/beta/online]";
- echo "example: ./init_config.sh.sh beta";
- exit 1;
- fi
-
- # ------------------------------------------------------------------------------
-
- # scripts start
-
- # check env
- NOW_PWD=`pwd`
- if [ "$NOW_PWD" != "${REPO_SRC}/scripts" ] && [ "$NOW_PWD" != "${REPO_SRC}" ]; then
- echo -e "\033[43m[WARRING!] PWD is ${NOW_PWD}, but REPO_SRC is ${REPO_SRC} \033[0m";
- echo -e "please check your REPO_SRC config to make sure that you config this file correctly.";
- exit 1;
- fi
-
-
- # [init nginx config]
-
- # deploy tmpl config to deploy folder
- # for public config
- if [ "${PUBLIC_UPSTREAM_PORT}" != "" ]; then
- # deploy tmpl config to deploy folder
- if [ -f "${NGINX_CONF_PUBLIC_ADDR}" ]; then
- rm -f "${NGINX_CONF_PUBLIC_ADDR}";
- fi
- cp "${NGINX_CONFIG_TMPL_FOLDER}/${NGINX_CONF_TMPL_PUBLIC}" "${NGINX_CONF_PUBLIC_ADDR}";
- # replace config param
- sed -i "s/REPO_NAME/${REPO_NAME}/g" "${NGINX_CONF_PUBLIC_ADDR}";
- # replace domain name
- sed -i "s/DOMAIN_NAME/${DOMAIN_NAME}/g" "${NGINX_CONF_PUBLIC_ADDR}";
- # replace upstream addr
- sed -i "s/UPSTREAM_ADDR/${UPSTREAM_ADDR_PUBLIC}/g" "${NGINX_CONF_PUBLIC_ADDR}";
- fi
-
- # for private config
- if [ "${PRIVATE_UPSTREAM_PORT}" != "" ]; then
- # deploy tmpl config to deploy folder
- if [ -f "${NGINX_CONF_PRIVATE_ADDR}" ]; then
- rm -f "${NGINX_CONF_PRIVATE_ADDR}";
- fi
- cp "${NGINX_CONFIG_TMPL_FOLDER}/${NGINX_CONF_TMPL_PRIVATE}" "${NGINX_CONF_PRIVATE_ADDR}"
- # replace config param
- sed -i "s/REPO_NAME/${REPO_NAME}/g" "${NGINX_CONF_PRIVATE_ADDR}";
- # replace domain name
- sed -i "s/DOMAIN_NAME/${DOMAIN_NAME}/g" "${NGINX_CONF_PRIVATE_ADDR}";
- # replace upstream addr
- sed -i "s/UPSTREAM_ADDR/${UPSTREAM_ADDR_PRIVATE}/g" "${NGINX_CONF_PRIVATE_ADDR}";
- fi
-
-
- # [init systemctl config]
-
- # deploy tmpl config to deploy folder
- # for beta service
- if [ "${RELEASE_ENV_LOCATION}" == "beta" ]; then
- if [ -f "${SYSTEMCTL_CONF_ADDR}" ]; then
- rm -f "${SYSTEMCTL_CONF_ADDR}";
- fi
- cp "${SYSTEMCTL_CONFIG_TMPL_FOLDER}/${SYSTEMCTL_CONF_TMPL_BETA}" "${SYSTEMCTL_CONF_ADDR}"
- # replace repo name
- sed -i "s/REPO_NAME/${REPO_NAME}/g" "${SYSTEMCTL_CONF_ADDR}";
- # replace build target
- sed -i "s/BUILD_TARGET/${BUILD_TARGET}/g" "${SYSTEMCTL_CONF_ADDR}";
- fi
-
- # for online service
- if [ "${RELEASE_ENV_LOCATION}" == "online" ]; then
- if [ -f "${SYSTEMCTL_CONF_ADDR}" ]; then
- rm -f "${SYSTEMCTL_CONF_ADDR}";
- fi
- cp "${SYSTEMCTL_CONFIG_TMPL_FOLDER}/${SYSTEMCTL_CONF_TMPL_RELEASE}" "${SYSTEMCTL_CONF_ADDR}"
- # replace repo name
- sed -i "s/REPO_NAME/${REPO_NAME}/g" "${SYSTEMCTL_CONF_ADDR}";
- # replace build target
- sed -i "s/BUILD_TARGET/${BUILD_TARGET}/g" "${SYSTEMCTL_CONF_ADDR}";
- fi
-
-
-
- exit 0;
|