deploy_latest.sh 7.0 KB

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