123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #!/bin/bash
- # Usage:
- # deploy with upgrade: --upgrade, DO NOT USE
- # deploy force(rm stack, then deploy) --force (±ØÌî)
- # deploy specify app: --module="ems_server" (±ØÌî)
- # deploy to specify env: --env="powercloud" (±ØÌî)
- # deploy with specify git branch, default master --git-branch="dev"
- # deploy with specify version, default 0 --compose_version=1
- # deploy base dir
- WORKDIR="/tmp/wisecloud_deploy"
- # default modules
- #APP="ues aaa cas authcenter dcmp"
- # get user selected modules
- SELECTED_APP=`echo $* | xargs -n 1 | grep "\-module" | awk -F"=" '{print $2}' | tr ',' ' '`
- # stack contain one or more submodule's name
- for i in $SELECTED_APP; do
- DEPLOY_APP="$DEPLOY_APP ${i%%_*}"
- done
- # sort and uniq
- DEPLOY_APP=$(echo $DEPLOY_APP | xargs -n1 | sort | uniq | xargs)
- cat <<'EOF'
- _ _ _ _
- __| | ___ _ __ | | ___ _ _ ___| |_ __ _ _ __| |_
- / _` |/ _ \ '_ \| |/ _ \| | | | / __| __/ _` | '__| __|
- | (_| | __/ |_) | | (_) | |_| | \__ \ || (_| | | | |_
- \__,_|\___| .__/|_|\___/ \__, | |___/\__\__,_|_| \__|
- |_| |___/
- EOF
- # if no select, use defult
- [ -n "$DEPLOY_APP" ] && APP=$DEPLOY_APP && \
- echo "start to deploy app: $APP" && echo
- # git address
- GIT_TEMPLATE_URL="git@git.svicloud.com:svicloud/catalog-wisecloud.git"
- #GIT_MODULE_VERSION_URL=http://git.sviyun.com/svicloud/catalog-wisecloud/raw/master/version.json
- # service connect string(use dev env key & secret)
- RANCHER_URL=http://console.sviyun.com:8080/v1
- # update the git repo
- if [ -d ${WORKDIR}/wisecloud-catalog/.git ]; then
- cd ${WORKDIR}/wisecloud-catalog
- git --no-pager log --graph \
- --pretty=format:'%h - %d% %s (%cr [%an])' \
- --abbrev-commit --date=relative -20
- echo
- else
- git clone $GIT_TEMPLATE_URL ${WORKDIR}/wisecloud-catalog
- cd ${WORKDIR}/wisecloud-catalog
- fi
- # fetch remote tags
- git fetch --all
- GIT_BRANCH=`echo $* | xargs -n 1 | grep "\-git\-branch" | awk -F"=" '{print $2}' | tr ',' ' '`
- GIT_BRANCH=${GIT_BRANCH:-master}
- # has branch
- if git branch -a | grep -wq "${GIT_BRANCH}"; then
- # if current branch not the wanted, checkout it
- current_branch=`git branch | grep "\*" | awk '{print $2}'`
- if [ "${GIT_BRANCH}" != "${current_branch}" ]; then
- # checkout
- git checkout -b ${GIT_BRANCH} &>/dev/null
- fi
-
- # reset hard the branch
- git reset --hard origin/${GIT_BRANCH}
-
- # no branch
- else
- echo "#####################################"
- echo "Please specify the right \"--git-branch\", exit!"
- echo "#####################################"
- exit 1
- fi
- ENV=`echo $* | xargs -n 1 | grep "\-env" | awk -F"=" '{print $2}' | tr ',' ' '`
- echo "deploy to env=$ENV"
- if [ "$ENV" == "cs" ]; then
- RANCHER_ACCESS_KEY=344E9FA150DD81B6F254
- RANCHER_SECRET_KEY=9tBspbMqp3ny8ZzXbBBTpYNke5fhgLPWfXyvqqTi
- elif [ "$ENV" == "powercloud" ]; then
- RANCHER_ACCESS_KEY=DB9833C639BCA696277F
- RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE
- elif [ "$ENV" == "powercloud-patch" ]; then
- RANCHER_ACCESS_KEY=C128E6FFCF3C88C1E9F3
- RANCHER_SECRET_KEY=YJ2VS7P3CRkkD6p92HN5gb5uKbavwSpkWRj1PxDK
-
- elif [ "$ENV" == "powercloudtest" ]; then
- RANCHER_ACCESS_KEY=DDB533DC33E600A4862F
- RANCHER_SECRET_KEY=iLSsbdpf3FR5ThFDW2S4XsX6j2WZtu1zmw4nopU2
-
- elif [ "$ENV" == "test" ]; then
- RANCHER_ACCESS_KEY=C98222E5206624BFB807
- RANCHER_SECRET_KEY=LBg2tF96miU8rBPgN2kR9uXV2qbrKKuQTrTEy5Wq
- elif [ "$ENV" == "wmsdev" ]; then
- RANCHER_ACCESS_KEY=3DD561A59A4648A7CA6E
- RANCHER_SECRET_KEY=z7Nxtad8UmdsTacRiBn1A2BqNEyQNW2xNzXpar2o
- else
- echo "#####################################"
- echo "Please specify the right env that has access key and secret key, exit!"
- echo "#####################################"
- exit 1
- fi
- export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY
- # get the laste redis docker-compose & rancher-compose
- for module in $APP; do
- echo -e "\nCurrent module: $module"
- # if has another process
- retry=100
- while true; do
- ps_count=`ps -ef | grep "deploy_latest" | grep -q "$module" | wc -l`
- retry_count=`expr $retry_count + 1`
- # 100 times
- if [ "$retry_count" -ge $retry ]; then
- echo "timeout, coutinue."
- break
- fi
- # has other process
- if [ $ps_count -ge 2 ] ; then
- echo "another process \"$module\" is running, waiting."
- sleep 1
- else
- break
- fi
- done
- # prepare the sub dir
- mkdir -p $WORKDIR/$module
- cd $WORKDIR/$module
- # get the module version
- # VARS=`curl $GIT_MODULE_VERSION_URL 2>/dev/null | jq .${module} \
- # | sed "s/[ \t]*:[ \t]*/=/g" | tr -d ',"{} ' | xargs -n1 | sed 's/^/export /'`
- # # export to shell
- # if [[ "$module" != "redis" ]]; then
- # # export the key
- # if ! eval "$VARS" >/dev/null 2>&1; then
- # echo "eval failed, please use: curl $GIT_MODULE_VERSION_URL to confirm!"
- # exit 1
- # else
- # echo "VARS: $VARS"
- # fi
- # fi
- COMPOSE_VERSION=`echo $* | xargs -n 1 | grep "\-compose_version" | awk -F"=" '{print $2}' | tr ',' ' '`
- if [ -z "$COMPOSE_VERSION" ]; then
- compose_version_number=0
- else
- compose_version_number=$COMPOSE_VERSION
- fi
- if ! cd ${WORKDIR}/wisecloud-catalog/templates/$module/${compose_version_number}; then
- echo " Maybe you have input the wrong compose_version"
- exit 1
- fi
-
- #curl -OL ${GIT_TEMPLATE_URL}/$module/0/rancher-compose.yml . 2>/dev/null
- #curl -OL ${GIT_TEMPLATE_URL}/$module/0/docker-compose.yml . 2>/dev/null
- dos2unix * &>/dev/null
- # replace the VERSION string
- #sed -i 's/@{.*VERSION}/latest/' rancher-compose.yml
- #sed -i 's/@{.*VERSION}/latest/' docker-compose.yml
- # has question
- if grep -q "question" rancher-compose.yml; then
- # has answer.txt
- if [ -f "answer.txt" ]; then
- ENV_ARGS="--env-file answer.txt"
- # has not answer.txt
- else
- answer_exists=false
- echo "#####################################"
- echo "you define questions in rancher-compose.yml"
- echo "but there is no answer.txt in the compose directory, exit!"
- echo "#####################################"
- exit 2
- fi
- else
- ENV_ARGS=""
- fi
- # rancher-compose pull image
- rancher-compose -p ${module} $ENV_ARGS pull
- # compose up
- if echo "$*" | grep -q "\-force"; then
- echo "removing $module..."
- rancher-compose -p ${module} $ENV_ARGS rm -f
- sleep 1
- # try again
- rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
- sleep 3
- echo "remove complete"
- fi
- # compose upgrade or create
- echo
- if echo "$*" | grep -q "\-upgrade"; then
- echo "creating & upgrade $module..."
- sleep 1
- # TODO
- if ! rancher-compose -p ${module} $ENV_ARGS upgrade old_service new_service; then
- echo "#####################################"
- echo "rancher-compose -p ${module} upgrade failed, exit!"
- echo "#####################################"
- exit 3
- fi
- else
- echo "creating & up $module..."
- sleep 1
- if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
- echo "#####################################"
- echo "rancher-compose -p ${module} up -d failed, exit!"
- echo "#####################################"
- exit 5
- else
- echo "start successful"
- echo
- fi
- fi
- done
|