#!/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/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.sviyun.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;echo git fetch --all git reset --hard origin/master else git clone $GIT_TEMPLATE_URL ${WORKDIR}/wisecloud-catalog fi ENV=`echo $* | xargs -n 1 | grep "\-env" | awk -F"=" '{print $2}' | tr ',' ' '` echo "deploy to env=$ENV" if [ "$ENV" == "dev" ]; then RANCHER_ACCESS_KEY=AADEA447C0DCD1B548BC RANCHER_SECRET_KEY=Fm3Zm851Euw2rUSLAE9DQuZokcU4BD9DF5KXMMJh elif [ "$ENV" == "test" ]; then RANCHER_ACCESS_KEY=FCCCE0B1257A2351A6A6 RANCHER_SECRET_KEY=cSSibMij1yBKJTnCBPPtvmm5eTXMBhLLEV2Wyn2A elif [ "$ENV" == "cs" ]; then RANCHER_ACCESS_KEY=B437EF7772D5F4D145A0 RANCHER_SECRET_KEY=NNKVsUHqjQKemGRqcXCar521qjfh9dcurWT9CLj4 elif [ "$ENV" == "pro" ]; 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 redis_password=123456 redis_port=7000 postgres_user=postgres_user postgres_password=postgres_password pgdata="/var/lib/postgresql/data/pgdata" export RANCHER_URL RANCHER_ACCESS_KEY RANCHER_SECRET_KEY \ redis_password REDIS_PORT postgres_user postgres_password pgdata # 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}/wisecloud-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 ; 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