apollo-config.sh 4.9 KB

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