apollo-config.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. # Copyright 1999-2019 Seata.io Group.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at、
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # apollo open api, click on the link for details:
  16. # https://github.com/ctripcorp/apollo/wiki/Apollo%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0
  17. # add config: http://{portal_address}/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items
  18. # publish config: http://{portal_address}/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases
  19. while getopts ":h:p:e:a:c:n:d:r:t:" opt
  20. do
  21. case $opt in
  22. h)
  23. host=$OPTARG
  24. ;;
  25. p)
  26. port=$OPTARG
  27. ;;
  28. e)
  29. env=$OPTARG
  30. ;;
  31. a)
  32. appId=$OPTARG
  33. ;;
  34. c)
  35. clusterName=$OPTARG
  36. ;;
  37. n)
  38. namespaceName=$OPTARG
  39. ;;
  40. d)
  41. dataChangeCreatedBy=$OPTARG
  42. ;;
  43. r)
  44. releasedBy=$OPTARG
  45. ;;
  46. t)
  47. token=$OPTARG
  48. ;;
  49. ?)
  50. echo " USAGE OPTION: $0 [-h host] [-p port] [-e env] [a appId] [-c clusterName] [-n namespaceName] [-d dataChangeCreatedBy] [-r releasedBy] [-t token] "
  51. exit 1
  52. ;;
  53. esac
  54. done
  55. if [[ -z ${host} ]]; then
  56. host=localhost
  57. fi
  58. if [[ -z ${port} ]]; then
  59. port=8070
  60. fi
  61. if [[ -z ${env} ]]; then
  62. env=DEV
  63. fi
  64. if [[ -z ${appId} ]]; then
  65. appId=seata-server
  66. fi
  67. if [[ -z ${clusterName} ]]; then
  68. clusterName=default
  69. fi
  70. if [[ -z ${namespaceName} ]]; then
  71. namespaceName=application
  72. fi
  73. if [[ -z ${dataChangeCreatedBy} ]]; then
  74. echo " dataChangeCreatedBy is empty, please usage option: [-d dataChangeCreatedBy] "
  75. exit 1
  76. fi
  77. if [[ -z ${releasedBy} ]]; then
  78. echo " releasedBy is empty, please usage option: [-r releasedBy] "
  79. exit 1
  80. fi
  81. if [[ -z ${token} ]]; then
  82. echo " token is empty, please usage option: [-t token] "
  83. exit 1
  84. fi
  85. portalAddr=$host:$port
  86. contentType="content-type:application/json;charset=UTF-8"
  87. authorization="Authorization:$token"
  88. publishBody="{\"releaseTitle\":\"$(date +%Y%m%d%H%M%S)\",\"releaseComment\":\"\",\"releasedBy\":\"${releasedBy}\"}"
  89. echo "portalAddr is ${portalAddr}"
  90. echo "env is ${env}"
  91. echo "appId is ${appId}"
  92. echo "clusterName is ${clusterName}"
  93. echo "namespaceName is ${namespaceName}"
  94. echo "dataChangeCreatedBy is ${dataChangeCreatedBy}"
  95. echo "releasedBy is ${releasedBy}"
  96. echo "token is ${token}"
  97. failCount=0
  98. tempLog=$(mktemp -u)
  99. function addConfig() {
  100. curl -X POST -H "${1}" -H "${2}" -d "${3}" "http://${4}/openapi/v1/envs/${5}/apps/${6}/clusters/${7}/namespaces/${8}/items" >"${tempLog}" 2>/dev/null
  101. log=$(cat "${tempLog}")
  102. if [[ ${log} =~ ":401" || ${log} =~ ":403"
  103. || ${log} =~ ":404" || ${log} =~ ":405"
  104. || ${log} =~ ":500" || ! ${log} =~ "{" ]]; then
  105. echo "set $9=${10} failure "
  106. (( failCount++ ))
  107. else
  108. echo "set $9=${10} successfully "
  109. fi
  110. }
  111. function publishConfig() {
  112. curl -X POST -H "${1}" -H "${2}" -d "${3}" "http://${4}/openapi/v1/envs/${5}/apps/${6}/clusters/${7}/namespaces/${8}/releases" >"${tempLog}" 2>/dev/null
  113. log=$(cat "${tempLog}")
  114. if [[ ${log} =~ ":401" || ${log} =~ ":403"
  115. || ${log} =~ ":404" || ${log} =~ ":405"
  116. || ${log} =~ ":500" || ! ${log} =~ "{" ]]; then
  117. echo " Publish fail "
  118. exit 1
  119. else
  120. echo " Publish successfully, please start seata-server. "
  121. fi
  122. }
  123. count=0
  124. COMMENT_START="#"
  125. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  126. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  127. continue
  128. fi
  129. (( count++ ))
  130. key=${line%%=*}
  131. value=${line#*=}
  132. body="{\"key\":\"${key}\",\"value\":\"${value}\",\"comment\":\"\",\"dataChangeCreatedBy\":\"${dataChangeCreatedBy}\"}"
  133. addConfig ${contentType} "${authorization}" "${body}" "${portalAddr}" "${env}" "${appId}" "${clusterName}" "${namespaceName}" "${key}" "${value}"
  134. done
  135. echo "========================================================================="
  136. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  137. echo "========================================================================="
  138. if [[ $failCount -eq 0 ]]; then
  139. read -p "Publish now, y/n: " result
  140. if [[ ${result} == "y" ]]; then
  141. publishConfig "${contentType}" "${authorization}" "${publishBody}" "${portalAddr}" "${env}" "${appId}" "${clusterName}" "${namespaceName}"
  142. else
  143. echo "Remember to publish later..."
  144. fi
  145. else
  146. echo " init apollo config fail. "
  147. fi