apollo-config-interactive.sh 5.4 KB

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