deploy_powercloud_latest.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/powercloud_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.svicloud.com:svicloud/catalog-powercloud.git"
  33. #GIT_MODULE_VERSION_URL=http://git.sviyun.com/svicloud/catalog-powercloud/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}/powercloud-catalog/.git ]; then
  38. cd ${WORKDIR}/powercloud-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}/powercloud-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=B437EF7772D5F4D145A0
  52. RANCHER_SECRET_KEY=NNKVsUHqjQKemGRqcXCar521qjfh9dcurWT9CLj4
  53. elif [ "$ENV" == "test" ]; then
  54. RANCHER_ACCESS_KEY=xxxxxxxxxxxx
  55. RANCHER_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  56. elif [ "$ENV" == "powercloud" ]; then
  57. RANCHER_ACCESS_KEY=DB9833C639BCA696277F
  58. RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE
  59. elif [ "$ENV" == "powercloudtest" ]; then
  60. RANCHER_ACCESS_KEY=xxxxxxxxxxxx
  61. RANCHER_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  62. else
  63. echo "Please specify the right env that has access key and secret key, exit!"
  64. exit 1
  65. fi
  66. export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY \
  67. # get the laste redis docker-compose & rancher-compose
  68. for module in $APP; do
  69. echo -e "\nCurrent: $module"
  70. # if has another process
  71. retry=100
  72. while true; do
  73. ps_count=`ps -ef | grep "deploy_latest" | grep -q "$module" | wc -l`
  74. retry_count=`expr $retry_count + 1`
  75. # 100 times
  76. if [ "$retry_count" -ge $retry ]; then
  77. echo "timeout, coutinue."
  78. break
  79. fi
  80. # has other process
  81. if [ $ps_count -ge 2 ] ; then
  82. echo "another process \"$module\" is running, waiting."
  83. sleep 1
  84. else
  85. break
  86. fi
  87. done
  88. # prepare the sub dir
  89. mkdir -p $WORKDIR/$module
  90. cd $WORKDIR/$module
  91. # get the module version
  92. # VARS=`curl $GIT_MODULE_VERSION_URL 2>/dev/null | jq .${module} \
  93. # | sed "s/[ \t]*:[ \t]*/=/g" | tr -d ',"{} ' | xargs -n1 | sed 's/^/export /'`
  94. # # export to shell
  95. # if [[ "$module" != "redis" ]]; then
  96. # # export the key
  97. # if ! eval "$VARS" >/dev/null 2>&1; then
  98. # echo "eval failed, please use: curl $GIT_MODULE_VERSION_URL to confirm!"
  99. # exit 1
  100. # else
  101. # echo "VARS: $VARS"
  102. # fi
  103. # fi
  104. cd ${WORKDIR}/powercloud-catalog/templates/$module/0
  105. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/rancher-compose.yml . 2>/dev/null
  106. #curl -OL ${GIT_TEMPLATE_URL}/$module/0/docker-compose.yml . 2>/dev/null
  107. dos2unix * &>/dev/null
  108. # replace the VERSION string
  109. #sed -i 's/@{.*VERSION}/latest/' rancher-compose.yml
  110. #sed -i 's/@{.*VERSION}/latest/' docker-compose.yml
  111. # has question
  112. if grep -q "question" rancher-compose.yml; then
  113. # has answer.txt
  114. if [ -f "answer.txt" ]; then
  115. ENV_ARGS="--env-file answer.txt"
  116. # has not answer.txt
  117. else
  118. answer_exists=false
  119. echo "you define questions in docker-compose.yml"
  120. echo "but there is no answer.txt in the compose directory, exit!"
  121. exit 5
  122. fi
  123. else
  124. ENV_ARGS=""
  125. fi
  126. # compose up
  127. if echo "$*" | grep -q "\-force"; then
  128. echo "removing $module..."
  129. sleep 1
  130. rancher-compose -p ${module} $ENV_ARGS rm -f
  131. sleep 1
  132. rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
  133. sleep 5
  134. echo "remove complete"
  135. fi
  136. # compose upgrade
  137. echo
  138. if echo "$*" | grep -q "\-upgrade"; then
  139. echo "creating & upgrade $module..."
  140. sleep 1
  141. # TODO
  142. if ! rancher-compose -p ${module} $ENV_ARGS upgrade $old_service $new_service; then
  143. echo "rancher-compose -p ${module} upgrade failed, exit!"
  144. exit 1
  145. fi
  146. else
  147. echo "creating & up $module..."
  148. sleep 1
  149. if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
  150. echo "rancher-compose -p ${module} up -d failed, exit!"
  151. exit 1
  152. fi
  153. fi
  154. done