#!/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=344E9FA150DD81B6F254 RANCHER_SECRET_KEY=9tBspbMqp3ny8ZzXbBBTpYNke5fhgLPWfXyvqqTi elif [ "$ENV" == "test" ]; then RANCHER_ACCESS_KEY=A06AEE798A8AFC04ADC4 RANCHER_SECRET_KEY=rQ8bXELUn7evjEwoNgYQs7mbH9DQ3YN9WukPUoF4 elif [ "$ENV" == "powercloud" ]; then RANCHER_ACCESS_KEY=DB9833C639BCA696277F RANCHER_SECRET_KEY=dJBSgKxYUSXTm2n7dvijsjdndt8P37srrhZxwgqE elif [ "$ENV" == "powercloudtest" ]; then RANCHER_ACCESS_KEY=0CA5094E8E4562442D99 RANCHER_SECRET_KEY=7TH5j64DSUPFbf6b9cFr4mp7Brq5iUrPBSx6sJxM 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