deploy_wisecloud_latest.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/bash
  2. # Usage:
  3. # deploy all(skip installed stack): ./$1
  4. # deploy & upgrade: ./$1 --upgrade
  5. # deploy force(rm stack, then deploy) ./$1 --force
  6. # deploy specify app: ./$1 --module="modle1,module2,module3"
  7. # deploy to specify env: ./$1 --env="test"
  8. # deploy base dir
  9. WORKDIR="/tmp/wisecloud_deploy"
  10. # default modules
  11. #APP="ues aaa cas authcenter dcmp"
  12. # get user selected modules
  13. SELECTED_APP=`echo $* | xargs -n 1 | grep "\-module" | awk -F"=" '{print $2}' | tr ',' ' '`
  14. # stack contain one or more submodule's name
  15. for i in $SELECTED_APP; do
  16. DEPLOY_APP="$DEPLOY_APP ${i%%_*}"
  17. done
  18. # sort and uniq
  19. DEPLOY_APP=$(echo $DEPLOY_APP | xargs -n1 | sort | uniq | xargs)
  20. cat <<'EOF'
  21. _ _ _ _
  22. __| | ___ _ __ | | ___ _ _ ___| |_ __ _ _ __| |_
  23. / _` |/ _ \ '_ \| |/ _ \| | | | / __| __/ _` | '__| __|
  24. | (_| | __/ |_) | | (_) | |_| | \__ \ || (_| | | | |_
  25. \__,_|\___| .__/|_|\___/ \__, | |___/\__\__,_|_| \__|
  26. |_| |___/
  27. EOF
  28. # if no select, use defult
  29. [ -n "$DEPLOY_APP" ] && APP=$DEPLOY_APP && \
  30. echo "start to deploy app: $APP" && echo
  31. # git address
  32. GIT_TEMPLATE_URL="git@git.sviyun.com:svicloud/catalog-wisecloud.git"
  33. #GIT_MODULE_VERSION_URL=http://git.sviyun.com/svicloud/catalog-wisecloud/raw/master/version.json
  34. # service connect string(use dev env key & secret)
  35. RANCHER_URL=http://console.sviyun.com:8080/v1
  36. # update the git repo
  37. if [ -d ${WORKDIR}/wisecloud-catalog/.git ]; then
  38. cd ${WORKDIR}/wisecloud-catalog
  39. git --no-pager log --graph \
  40. --pretty=format:'%h - %d% %s (%cr [%an])' \
  41. --abbrev-commit --date=relative -20
  42. echo;echo
  43. git fetch --all
  44. git reset --hard origin/master
  45. else
  46. git clone $GIT_TEMPLATE_URL ${WORKDIR}/wisecloud-catalog
  47. fi
  48. ENV=`echo $* | xargs -n 1 | grep "\-env" | awk -F"=" '{print $2}' | tr ',' ' '`
  49. echo "Start to deploy to env=$ENV"
  50. if [ "$ENV" == "cs" ]; then
  51. RANCHER_ACCESS_KEY=344E9FA150DD81B6F254
  52. RANCHER_SECRET_KEY=9tBspbMqp3ny8ZzXbBBTpYNke5fhgLPWfXyvqqTi
  53. elif [ "$ENV" == "test" ]; then
  54. RANCHER_ACCESS_KEY=A06AEE798A8AFC04ADC4
  55. RANCHER_SECRET_KEY=rQ8bXELUn7evjEwoNgYQs7mbH9DQ3YN9WukPUoF4
  56. elif [ "$ENV" == "powercloud" ]; then
  57. RANCHER_ACCESS_KEY=DB9833C639BCA696277F
  58. RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE
  59. elif [ "$ENV" == "powercloudtest" ]; then
  60. RANCHER_ACCESS_KEY=0CA5094E8E4562442D99
  61. RANCHER_SECRET_KEY=7TH5j64DSUPFbf6b9cFr4mp7Brq5iUrPBSx6sJxM
  62. else
  63. echo "#####################################"
  64. echo "Please specify the right env that has access key and secret key, exit!"
  65. echo "#####################################"
  66. exit 1
  67. fi
  68. export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY
  69. # get the laste redis docker-compose & rancher-compose
  70. for module in $APP; do
  71. echo -e "\nCurrent module: $module"
  72. # if has another process
  73. retry=100
  74. while true; do
  75. ps_count=`ps -ef | grep "deploy_latest" | grep -q "$module" | wc -l`
  76. retry_count=`expr $retry_count + 1`
  77. # 100 times
  78. if [ "$retry_count" -ge $retry ]; then
  79. echo "timeout, coutinue."
  80. break
  81. fi
  82. # has other process
  83. if [ $ps_count -ge 2 ] ; then
  84. echo "another process \"$module\" is running, waiting."
  85. sleep 1
  86. else
  87. break
  88. fi
  89. done
  90. # prepare the sub dir
  91. mkdir -p $WORKDIR/$module
  92. cd $WORKDIR/$module
  93. # get the module version
  94. # VARS=`curl $GIT_MODULE_VERSION_URL 2>/dev/null | jq .${module} \
  95. # | sed "s/[ \t]*:[ \t]*/=/g" | tr -d ',"{} ' | xargs -n1 | sed 's/^/export /'`
  96. # # export to shell
  97. # if [[ "$module" != "redis" ]]; then
  98. # # export the key
  99. # if ! eval "$VARS" >/dev/null 2>&1; then
  100. # echo "eval failed, please use: curl $GIT_MODULE_VERSION_URL to confirm!"
  101. # exit 1
  102. # else
  103. # echo "VARS: $VARS"
  104. # fi
  105. # fi
  106. cd ${WORKDIR}/wisecloud-catalog/templates/$module/0
  107. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/rancher-compose.yml . 2>/dev/null
  108. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/docker-compose.yml . 2>/dev/null
  109. dos2unix * &>/dev/null
  110. # replace the VERSION string
  111. #sed -i 's/@{.*VERSION}/latest/' rancher-compose.yml
  112. #sed -i 's/@{.*VERSION}/latest/' docker-compose.yml
  113. # has question
  114. if grep -q "question" rancher-compose.yml; then
  115. # has answer.txt
  116. if [ -f "answer.txt" ]; then
  117. ENV_ARGS="--env-file answer.txt"
  118. # has not answer.txt
  119. else
  120. answer_exists=false
  121. echo "#####################################"
  122. echo "you define questions in rancher-compose.yml"
  123. echo "but there is no answer.txt in the compose directory, exit!"
  124. echo "#####################################"
  125. exit 2
  126. fi
  127. else
  128. ENV_ARGS=""
  129. fi
  130. # compose up
  131. if echo "$*" | grep -q "\-force"; then
  132. echo "removing $module..."
  133. sleep 1
  134. rancher-compose -p ${module} $ENV_ARGS rm -f
  135. sleep 1
  136. # try again
  137. rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
  138. sleep 3
  139. echo "remove complete"
  140. fi
  141. # compose upgrade or create
  142. echo
  143. if echo "$*" | grep -q "\-upgrade"; then
  144. echo "Creating & upgrade $module..."
  145. sleep 1
  146. # TODO
  147. if ! rancher-compose -p ${module} $ENV_ARGS upgrade <old_service> <new_service>; then
  148. echo "#####################################"
  149. echo "rancher-compose -p ${module} upgrade failed, exit!"
  150. echo "#####################################"
  151. exit 3
  152. fi
  153. else
  154. echo "Creating & up $module..."
  155. sleep 1
  156. if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
  157. echo "#####################################"
  158. echo "rancher-compose -p ${module} up -d failed, exit!"
  159. echo "#####################################"
  160. exit 5
  161. else
  162. echo "start successful"
  163. echo
  164. fi
  165. fi
  166. done