Go框架demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.7 KiB

3 years ago
  1. #!/bin/bash
  2. # init_config.sh
  3. # init enviroment config scripts.
  4. # @version 170322:1
  5. # @author zhangxuhong <zhangxuhong@xitu.io>
  6. #
  7. # ----------------------------[manual config here]------------------------------
  8. # base config
  9. REPO_SRC=/data/repo/baidu_link_submit_ms
  10. REPO_NAME=baidu_link_submit_ms
  11. BUILD_TARGET="${REPO_NAME}"
  12. RELEASE_ENV=$1 # test, beta, online
  13. # nginx config file type
  14. DOMAIN_NAME="baidu-link-submit-ms.juejin.im"
  15. PUBLIC_UPSTREAM_PORT=13100
  16. PRIVATE_UPSTREAM_PORT=13100
  17. # ------------------------------------------------------------------------------
  18. # default config
  19. NGINX_CONF_TMPL_PUBLIC="REPO_NAME.public.conf"
  20. NGINX_CONF_TMPL_PRIVATE="REPO_NAME.private.conf"
  21. SYSTEMCTL_CONF_TMPL_BETA="REPO_NAME.beta.service"
  22. SYSTEMCTL_CONF_TMPL_RELEASE="REPO_NAME.online.service"
  23. # auto generated config
  24. # nginx
  25. NGINX_CONFIG_TMPL_FOLDER="${REPO_SRC}/scripts/config_template/nginx"
  26. NGINX_CONFIG_DEPLOY_FOLDER="${REPO_SRC}/config/nginx"
  27. NGINX_CONF_PUBLIC="${REPO_NAME}.public.conf"
  28. NGINX_CONF_PRIVATE="${REPO_NAME}.private.conf"
  29. NGINX_CONF_PUBLIC_ADDR="${NGINX_CONFIG_DEPLOY_FOLDER}/${NGINX_CONF_PUBLIC}"
  30. NGINX_CONF_PRIVATE_ADDR="${NGINX_CONFIG_DEPLOY_FOLDER}/${NGINX_CONF_PRIVATE}"
  31. if [ "${PUBLIC_UPSTREAM_PORT}" != "" ]; then
  32. UPSTREAM_ADDR_PUBLIC="127.0.0.1:${PUBLIC_UPSTREAM_PORT}"
  33. fi
  34. if [ "${PRIVATE_UPSTREAM_PORT}" != "" ]; then
  35. UPSTREAM_ADDR_PRIVATE="127.0.0.1:${PRIVATE_UPSTREAM_PORT}"
  36. fi
  37. # systemctl
  38. SYSTEMCTL_CONFIG_TMPL_FOLDER="${REPO_SRC}/scripts/config_template/systemctl"
  39. SYSTEMCTL_CONFIG_DEPLOY_FOLDER="${REPO_SRC}/config/systemctl"
  40. SYSTEMCTL_CONF="${REPO_NAME}.service"
  41. SYSTEMCTL_CONF_ADDR="${SYSTEMCTL_CONFIG_DEPLOY_FOLDER}/${SYSTEMCTL_CONF}"
  42. # release env
  43. if [ "$RELEASE_ENV" == "dev" ]; then
  44. RELEASE_ENV_LOCATION="$RELEASE_ENV"
  45. elif [ "$RELEASE_ENV" == "beta" ]; then
  46. RELEASE_ENV_LOCATION="$RELEASE_ENV"
  47. elif [ "$RELEASE_ENV" == "online" ]; then
  48. RELEASE_ENV_LOCATION="$RELEASE_ENV"
  49. else
  50. RELEASE_ENV_LOCATION=""
  51. fi
  52. if [ "$RELEASE_ENV_LOCATION" == "" ]; then
  53. echo "please input an avaliable deploy environment [dev/beta/online]";
  54. echo "example: ./init_config.sh.sh beta";
  55. exit 1;
  56. fi
  57. # ------------------------------------------------------------------------------
  58. # scripts start
  59. # check env
  60. NOW_PWD=`pwd`
  61. if [ "$NOW_PWD" != "${REPO_SRC}/scripts" ] && [ "$NOW_PWD" != "${REPO_SRC}" ]; then
  62. echo -e "\033[43m[WARRING!] PWD is ${NOW_PWD}, but REPO_SRC is ${REPO_SRC} \033[0m";
  63. echo -e "please check your REPO_SRC config to make sure that you config this file correctly.";
  64. exit 1;
  65. fi
  66. # [init nginx config]
  67. # deploy tmpl config to deploy folder
  68. # for public config
  69. if [ "${PUBLIC_UPSTREAM_PORT}" != "" ]; then
  70. # deploy tmpl config to deploy folder
  71. if [ -f "${NGINX_CONF_PUBLIC_ADDR}" ]; then
  72. rm -f "${NGINX_CONF_PUBLIC_ADDR}";
  73. fi
  74. cp "${NGINX_CONFIG_TMPL_FOLDER}/${NGINX_CONF_TMPL_PUBLIC}" "${NGINX_CONF_PUBLIC_ADDR}";
  75. # replace config param
  76. sed -i "s/REPO_NAME/${REPO_NAME}/g" "${NGINX_CONF_PUBLIC_ADDR}";
  77. # replace domain name
  78. sed -i "s/DOMAIN_NAME/${DOMAIN_NAME}/g" "${NGINX_CONF_PUBLIC_ADDR}";
  79. # replace upstream addr
  80. sed -i "s/UPSTREAM_ADDR/${UPSTREAM_ADDR_PUBLIC}/g" "${NGINX_CONF_PUBLIC_ADDR}";
  81. fi
  82. # for private config
  83. if [ "${PRIVATE_UPSTREAM_PORT}" != "" ]; then
  84. # deploy tmpl config to deploy folder
  85. if [ -f "${NGINX_CONF_PRIVATE_ADDR}" ]; then
  86. rm -f "${NGINX_CONF_PRIVATE_ADDR}";
  87. fi
  88. cp "${NGINX_CONFIG_TMPL_FOLDER}/${NGINX_CONF_TMPL_PRIVATE}" "${NGINX_CONF_PRIVATE_ADDR}"
  89. # replace config param
  90. sed -i "s/REPO_NAME/${REPO_NAME}/g" "${NGINX_CONF_PRIVATE_ADDR}";
  91. # replace domain name
  92. sed -i "s/DOMAIN_NAME/${DOMAIN_NAME}/g" "${NGINX_CONF_PRIVATE_ADDR}";
  93. # replace upstream addr
  94. sed -i "s/UPSTREAM_ADDR/${UPSTREAM_ADDR_PRIVATE}/g" "${NGINX_CONF_PRIVATE_ADDR}";
  95. fi
  96. # [init systemctl config]
  97. # deploy tmpl config to deploy folder
  98. # for beta service
  99. if [ "${RELEASE_ENV_LOCATION}" == "beta" ]; then
  100. if [ -f "${SYSTEMCTL_CONF_ADDR}" ]; then
  101. rm -f "${SYSTEMCTL_CONF_ADDR}";
  102. fi
  103. cp "${SYSTEMCTL_CONFIG_TMPL_FOLDER}/${SYSTEMCTL_CONF_TMPL_BETA}" "${SYSTEMCTL_CONF_ADDR}"
  104. # replace repo name
  105. sed -i "s/REPO_NAME/${REPO_NAME}/g" "${SYSTEMCTL_CONF_ADDR}";
  106. # replace build target
  107. sed -i "s/BUILD_TARGET/${BUILD_TARGET}/g" "${SYSTEMCTL_CONF_ADDR}";
  108. fi
  109. # for online service
  110. if [ "${RELEASE_ENV_LOCATION}" == "online" ]; then
  111. if [ -f "${SYSTEMCTL_CONF_ADDR}" ]; then
  112. rm -f "${SYSTEMCTL_CONF_ADDR}";
  113. fi
  114. cp "${SYSTEMCTL_CONFIG_TMPL_FOLDER}/${SYSTEMCTL_CONF_TMPL_RELEASE}" "${SYSTEMCTL_CONF_ADDR}"
  115. # replace repo name
  116. sed -i "s/REPO_NAME/${REPO_NAME}/g" "${SYSTEMCTL_CONF_ADDR}";
  117. # replace build target
  118. sed -i "s/BUILD_TARGET/${BUILD_TARGET}/g" "${SYSTEMCTL_CONF_ADDR}";
  119. fi
  120. exit 0;