deploy_wisecloud_latest.sh 5.8 KB

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