deploy_latest.sh 8.5 KB

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