123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #!/bin/bash
- # Usage:
- # deploy all(skip installed stack): ./$1
- # deploy & upgrade: ./$1 --upgrade
- # deploy force(rm stack, then deploy) ./$1 --force
- # deploy specify app: ./$1 --module="modle1,module2,module3"
- # deploy to specify env: ./$1 --env="test"
- # deploy base dir
- WORKDIR="/tmp/powercloud_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-powercloud.git"
- #GIT_MODULE_VERSION_URL=http://git.sviyun.com/svicloud/catalog-powercloud/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}/powercloud-catalog/.git ]; then
- cd ${WORKDIR}/powercloud-catalog
- git --no-pager log --graph \
- --pretty=format:'%h - %d% %s (%cr [%an])' \
- --abbrev-commit --date=relative -20
- echo;echo
- git fetch --all
- git reset --hard origin/master
- else
- git clone $GIT_TEMPLATE_URL ${WORKDIR}/powercloud-catalog
- 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=B437EF7772D5F4D145A0
- RANCHER_SECRET_KEY=NNKVsUHqjQKemGRqcXCar521qjfh9dcurWT9CLj4
- elif [ "$ENV" == "test" ]; then
- RANCHER_ACCESS_KEY=xxxxxxxxxxxx
- RANCHER_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- elif [ "$ENV" == "powercloud" ]; then
- RANCHER_ACCESS_KEY=DB9833C639BCA696277F
- RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE
- elif [ "$ENV" == "powercloudtest" ]; then
- RANCHER_ACCESS_KEY=xxxxxxxxxxxx
- RANCHER_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- else
- echo "Please specify the right env that has access key and secret key, exit!"
- 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"
- # 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
- cd ${WORKDIR}/powercloud-catalog/templates/$module/0
- #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 "you define questions in docker-compose.yml"
- echo "but there is no answer.txt in the compose directory, exit!"
- exit 5
- fi
- else
- ENV_ARGS=""
- fi
- # compose up
- if echo "$*" | grep -q "\-force"; then
- echo "removing $module..."
- sleep 1
- rancher-compose -p ${module} $ENV_ARGS rm -f
- sleep 1
- rancher-compose -p ${module} $ENV_ARGS rm -f 2>/dev/null
- sleep 5
- echo "remove complete"
- fi
- # compose upgrade
- 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 "rancher-compose -p ${module} upgrade failed, exit!"
- exit 1
- fi
- else
- echo "creating & up $module..."
- sleep 1
- if ! rancher-compose -p ${module} $ENV_ARGS up -d; then
- echo "rancher-compose -p ${module} up -d failed, exit!"
- exit 1
- fi
- fi
- done
|