deploy_latest.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/bash
  2. # Usage:
  3. # deploy with upgrade: --upgrade, DO NOT USE
  4. # deploy force(rm stack, then deploy) --force (±ØÌî)
  5. # deploy specify app: --module="ems_server" (±ØÌî)
  6. # deploy to specify env: --env="powercloud" (±ØÌî)
  7. # deploy with specify git branch, default master --git-branch="dev"
  8. # deploy with specify version, default 0 --compose_version=1
  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} &>/dev/null
  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" == "cs" ]; then
  72. RANCHER_ACCESS_KEY=344E9FA150DD81B6F254
  73. RANCHER_SECRET_KEY=9tBspbMqp3ny8ZzXbBBTpYNke5fhgLPWfXyvqqTi
  74. elif [ "$ENV" == "powercloud" ]; then
  75. RANCHER_ACCESS_KEY=DB9833C639BCA696277F
  76. RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE
  77. elif [ "$ENV" == "powercloud-patch" ]; then
  78. RANCHER_ACCESS_KEY=C128E6FFCF3C88C1E9F3
  79. RANCHER_SECRET_KEY=YJ2VS7P3CRkkD6p92HN5gb5uKbavwSpkWRj1PxDK
  80. elif [ "$ENV" == "powercloudtest" ]; then
  81. RANCHER_ACCESS_KEY=DDB533DC33E600A4862F
  82. RANCHER_SECRET_KEY=iLSsbdpf3FR5ThFDW2S4XsX6j2WZtu1zmw4nopU2
  83. elif [ "$ENV" == "test" ]; then
  84. RANCHER_ACCESS_KEY=C98222E5206624BFB807
  85. RANCHER_SECRET_KEY=LBg2tF96miU8rBPgN2kR9uXV2qbrKKuQTrTEy5Wq
  86. elif [ "$ENV" == "wmsdev" ]; then
  87. RANCHER_ACCESS_KEY=3DD561A59A4648A7CA6E
  88. RANCHER_SECRET_KEY=z7Nxtad8UmdsTacRiBn1A2BqNEyQNW2xNzXpar2o
  89. else
  90. echo "#####################################"
  91. echo "Please specify the right env that has access key and secret key, exit!"
  92. echo "#####################################"
  93. exit 1
  94. fi
  95. export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY
  96. # get the laste redis docker-compose & rancher-compose
  97. for module in $APP; do
  98. echo -e "\nCurrent module: $module"
  99. # if has another process
  100. retry=100
  101. while true; do
  102. ps_count=`ps -ef | grep "deploy_latest" | grep -q "$module" | wc -l`
  103. retry_count=`expr $retry_count + 1`
  104. # 100 times
  105. if [ "$retry_count" -ge $retry ]; then
  106. echo "timeout, coutinue."
  107. break
  108. fi
  109. # has other process
  110. if [ $ps_count -ge 2 ] ; then
  111. echo "another process \"$module\" is running, waiting."
  112. sleep 1
  113. else
  114. break
  115. fi
  116. done
  117. # prepare the sub dir
  118. mkdir -p $WORKDIR/$module
  119. cd $WORKDIR/$module
  120. # get the module version
  121. # VARS=`curl $GIT_MODULE_VERSION_URL 2>/dev/null | jq .${module} \
  122. # | sed "s/[ \t]*:[ \t]*/=/g" | tr -d ',"{} ' | xargs -n1 | sed 's/^/export /'`
  123. # # export to shell
  124. # if [[ "$module" != "redis" ]]; then
  125. # # export the key
  126. # if ! eval "$VARS" >/dev/null 2>&1; then
  127. # echo "eval failed, please use: curl $GIT_MODULE_VERSION_URL to confirm!"
  128. # exit 1
  129. # else
  130. # echo "VARS: $VARS"
  131. # fi
  132. # fi
  133. COMPOSE_VERSION=`echo $* | xargs -n 1 | grep "\-compose_version" | awk -F"=" '{print $2}' | tr ',' ' '`
  134. if [ -z "$COMPOSE_VERSION" ]; then
  135. compose_version_number=0
  136. else
  137. compose_version_number=$COMPOSE_VERSION
  138. fi
  139. if ! cd ${WORKDIR}/wisecloud-catalog/templates/$module/${compose_version_number}; then
  140. echo " Maybe you have input the wrong compose_version"
  141. exit 1
  142. fi
  143. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/rancher-compose.yml . 2>/dev/null
  144. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/docker-compose.yml . 2>/dev/null
  145. dos2unix * &>/dev/null
  146. # replace the VERSION string
  147. #sed -i 's/@{.*VERSION}/latest/' rancher-compose.yml
  148. #sed -i 's/@{.*VERSION}/latest/' docker-compose.yml
  149. # has question
  150. if grep -q "question" rancher-compose.yml; then
  151. # has answer.txt
  152. if [ -f "answer.txt" ]; then
  153. ENV_ARGS="--env-file answer.txt"
  154. # has not answer.txt
  155. else
  156. answer_exists=false
  157. echo "#####################################"
  158. echo "you define questions in rancher-compose.yml"
  159. echo "but there is no answer.txt in the compose directory, exit!"
  160. echo "#####################################"
  161. exit 2
  162. fi
  163. else
  164. ENV_ARGS=""
  165. fi
  166. # rancher-compose pull image
  167. rancher-compose -p ${module} $ENV_ARGS pull
  168. # compose up
  169. if echo "$*" | grep -q "\-force"; then
  170. echo "removing $module..."
  171. rancher-compose -p ${module} $ENV_ARGS rm -f
  172. sleep 1
  173. # try again
  174. rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
  175. sleep 3
  176. echo "remove complete"
  177. fi
  178. # compose upgrade or create
  179. echo
  180. if echo "$*" | grep -q "\-upgrade"; then
  181. echo "creating & upgrade $module..."
  182. sleep 1
  183. # TODO
  184. if ! rancher-compose -p ${module} $ENV_ARGS upgrade old_service new_service; then
  185. echo "#####################################"
  186. echo "rancher-compose -p ${module} upgrade failed, exit!"
  187. echo "#####################################"
  188. exit 3
  189. fi
  190. else
  191. echo "creating & up $module..."
  192. sleep 1
  193. if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
  194. echo "#####################################"
  195. echo "rancher-compose -p ${module} up -d failed, exit!"
  196. echo "#####################################"
  197. exit 5
  198. else
  199. echo "start successful"
  200. echo
  201. fi
  202. fi
  203. done