apollo-config-interactive.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. echo -e "Please enter the host of apollo.\n请输入apollo的host [localhost]:"
  23. read -p ">>> " host
  24. echo -e "Please enter the port of apollo.\n请输入apollo的port [8070]:"
  25. read -p ">>> " port
  26. echo -e "Please enter the env of apollo.\n请输入apollo的env [DEV]:"
  27. read -p ">>> " env
  28. echo -e "Please enter the appId of apollo.\n请输入apollo的appId [seata-server]:"
  29. read -p ">>> " appId
  30. echo -e "Please enter the clusterName of apollo.\n请输入apollo的clusterName [default]:"
  31. read -p ">>> " clusterName
  32. echo -e "Please enter the namespaceName of apollo.\n请输入apollo的namespaceName [application]:"
  33. read -p ">>> " namespaceName
  34. echo -e "Please enter the dataChangeCreatedBy of apollo.\n请输入apollo的dataChangeCreatedBy:"
  35. read -p ">>> " dataChangeCreatedBy
  36. echo -e "Please enter the releasedBy of apollo.\n请输入apollo的releasedBy:"
  37. read -p ">>> " releasedBy
  38. echo -e "Please enter the token of apollo.\n请输入apollo的token:"
  39. read -p ">>> " token
  40. read -p "Are you sure to continue? [y/n]" input
  41. case $input in
  42. [yY]*)
  43. if [[ -z ${host} ]]; then
  44. host=localhost
  45. fi
  46. if [[ -z ${port} ]]; then
  47. port=8070
  48. fi
  49. if [[ -z ${env} ]]; then
  50. env=DEV
  51. fi
  52. if [[ -z ${appId} ]]; then
  53. appId=seata-server
  54. fi
  55. if [[ -z ${clusterName} ]]; then
  56. clusterName=default
  57. fi
  58. if [[ -z ${namespaceName} ]]; then
  59. namespaceName=application
  60. fi
  61. if [[ -z ${dataChangeCreatedBy} ]]; then
  62. echo " dataChangeCreatedBy is empty, please input it "
  63. exit 1
  64. fi
  65. if [[ -z ${releasedBy} ]]; then
  66. echo " releasedBy is empty, please input it "
  67. exit 1
  68. fi
  69. if [[ -z ${token} ]]; then
  70. echo " token is empty, please input it "
  71. exit 1
  72. fi
  73. ;;
  74. [nN]*)
  75. exit
  76. ;;
  77. *)
  78. echo "Just enter y or n, please."
  79. exit
  80. ;;
  81. esac
  82. portalAddr=$host:$port
  83. contentType="content-type:application/json;charset=UTF-8"
  84. authorization="Authorization:$token"
  85. publishBody="{\"releaseTitle\":\"$(date +%Y%m%d%H%M%S)\",\"releaseComment\":\"\",\"releasedBy\":\"${releasedBy}\"}"
  86. echo "portalAddr is ${portalAddr}"
  87. echo "env is ${env}"
  88. echo "appId is ${appId}"
  89. echo "clusterName is ${clusterName}"
  90. echo "namespaceName is ${namespaceName}"
  91. echo "dataChangeCreatedBy is ${dataChangeCreatedBy}"
  92. echo "releasedBy is ${releasedBy}"
  93. echo "token is ${token}"
  94. failCount=0
  95. tempLog=$(mktemp -u)
  96. function addConfig() {
  97. 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
  98. log=$(cat "${tempLog}")
  99. if [[ ${log} =~ ":401" || ${log} =~ ":403"
  100. || ${log} =~ ":404" || ${log} =~ ":405"
  101. || ${log} =~ ":500" || ! ${log} =~ "{" ]]; then
  102. echo "set $9=${10} failure "
  103. (( failCount++ ))
  104. else
  105. echo "set $9=${10} successfully "
  106. fi
  107. }
  108. function publishConfig() {
  109. 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
  110. log=$(cat "${tempLog}")
  111. if [[ ${log} =~ ":401" || ${log} =~ ":403"
  112. || ${log} =~ ":404" || ${log} =~ ":405"
  113. || ${log} =~ ":500" || ! ${log} =~ "{" ]]; then
  114. echo " Publish fail "
  115. exit 1
  116. else
  117. echo " Publish successfully, please start seata-server. "
  118. fi
  119. }
  120. count=0
  121. COMMENT_START="#"
  122. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  123. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  124. continue
  125. fi
  126. (( count++ ))
  127. key=${line%%=*}
  128. value=${line#*=}
  129. body="{\"key\":\"${key}\",\"value\":\"${value}\",\"comment\":\"\",\"dataChangeCreatedBy\":\"${dataChangeCreatedBy}\"}"
  130. addConfig ${contentType} "${authorization}" "${body}" "${portalAddr}" "${env}" "${appId}" "${clusterName}" "${namespaceName}" "${key}" "${value}"
  131. done
  132. echo "========================================================================="
  133. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  134. echo "========================================================================="
  135. if [[ $failCount -eq 0 ]]; then
  136. read -p "Publish now, y/n: " result
  137. if [[ ${result} == "y" ]]; then
  138. publishConfig "${contentType}" "${authorization}" "${publishBody}" "${portalAddr}" "${env}" "${appId}" "${clusterName}" "${namespaceName}"
  139. else
  140. echo "Remember to publish later..."
  141. fi
  142. else
  143. echo " init apollo config fail. "
  144. fi