deploy_latest.sh 7.6 KB

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