deploy_wisecloud_latest.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "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. redis_password=123456
  69. redis_port=7000
  70. postgres_user=postgres_user
  71. postgres_password=postgres_password
  72. pgdata="/var/lib/postgresql/data/pgdata"
  73. export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY \
  74. redis_password REDIS_PORT postgres_user postgres_password pgdata
  75. # get the laste redis docker-compose & rancher-compose
  76. for module in $APP; do
  77. echo -e "\nCurrent: $module"
  78. # if has another process
  79. retry=100
  80. while true; do
  81. ps_count=`ps -ef | grep "deploy_latest" | grep -q "$module" | wc -l`
  82. retry_count=`expr $retry_count + 1`
  83. # 100 times
  84. if [ "$retry_count" -ge $retry ]; then
  85. echo "timeout, coutinue."
  86. break
  87. fi
  88. # has other process
  89. if [ $ps_count -ge 2 ] ; then
  90. echo "another process \"$module\" is running, waiting."
  91. sleep 1
  92. else
  93. break
  94. fi
  95. done
  96. # prepare the sub dir
  97. mkdir -p $WORKDIR/$module
  98. cd $WORKDIR/$module
  99. # get the module version
  100. # VARS=`curl $GIT_MODULE_VERSION_URL 2>/dev/null | jq .${module} \
  101. # | sed "s/[ \t]*:[ \t]*/=/g" | tr -d ',"{} ' | xargs -n1 | sed 's/^/export /'`
  102. # # export to shell
  103. # if [[ "$module" != "redis" ]]; then
  104. # # export the key
  105. # if ! eval "$VARS" >/dev/null 2>&1; then
  106. # echo "eval failed, please use: curl $GIT_MODULE_VERSION_URL to confirm!"
  107. # exit 1
  108. # else
  109. # echo "VARS: $VARS"
  110. # fi
  111. # fi
  112. cd ${WORKDIR}/wisecloud-catalog/templates/$module/0
  113. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/rancher-compose.yml . 2>/dev/null
  114. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/docker-compose.yml . 2>/dev/null
  115. dos2unix * &>/dev/null
  116. # replace the VERSION string
  117. #sed -i 's/@{.*VERSION}/latest/' rancher-compose.yml
  118. #sed -i 's/@{.*VERSION}/latest/' docker-compose.yml
  119. # has question
  120. if grep -q "question" rancher-compose.yml; then
  121. # has answer.txt
  122. if [ -f "answer.txt" ]; then
  123. ENV_ARGS="--env-file answer.txt"
  124. # has not answer.txt
  125. else
  126. answer_exists=false
  127. echo "#####################################"
  128. echo "you define questions in docker-compose.yml"
  129. echo "but there is no answer.txt in the compose directory, exit!"
  130. echo "#####################################"
  131. exit 2
  132. fi
  133. else
  134. ENV_ARGS=""
  135. fi
  136. # compose up
  137. if echo "$*" | grep -q "\-force"; then
  138. echo "removing $module..."
  139. sleep 1
  140. rancher-compose -p ${module} $ENV_ARGS rm -f
  141. sleep 1
  142. rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
  143. sleep 5
  144. echo "remove complete"
  145. fi
  146. # compose upgrade
  147. echo
  148. if echo "$*" | grep -q "\-upgrade"; then
  149. echo "creating & upgrade $module..."
  150. sleep 1
  151. # TODO
  152. if ! rancher-compose -p ${module} $ENV_ARGS upgrade <old_service> <new_service>; then
  153. echo "#####################################"
  154. echo "rancher-compose -p ${module} upgrade failed, exit!"
  155. echo "#####################################"
  156. exit 3
  157. fi
  158. else
  159. echo "creating & up $module..."
  160. sleep 1
  161. if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
  162. echo "#####################################"
  163. echo "rancher-compose -p ${module} up -d failed, exit!"
  164. echo "#####################################"
  165. exit 5
  166. fi
  167. fi
  168. done