deploy.sh 12.7 KB
#!/bin/bash
################################################################
#@Authro Lijian
#@Date 2007-06-08
#@param1 -h target host
#@param2 -p ssh port for target host
#@param3 -v git pull version or commit
#@param4 -c if true, only change autoconfig, don't update source code
#@param5 -g git group, not null
#@param6 -b app branch for git
################################################################
BASE_BIN_DIR=`dirname $0`
. $BASE_BIN_DIR/scripts/app-iptable.sh
. $BASE_BIN_DIR/scripts/env.sh

################### Input parameter for user ###################################
SSH_HOST=""
SSH_PORT=22
CODE_VERSION=""
NOT_UPDATE="false"
GIT_GROUP=$DEFAULT_GIT_GROUP
APP_BRANCH=$USER ##使用当前登陆的系统账户作为git分支
if [ $APP_BRANCH = "root" ]; then
APP_BRANCH=master
fi
param(){
	if [ $1 ]; then
		prefix=`expr substr $1 1 2`
		if [ $prefix = "-h" ]; then
			SSH_HOST=`echo $1|awk '{print substr($0, 3)}'`
		fi
		if [ $prefix = "-p" ]; then
			SSH_PORT=`echo $1|awk '{print substr($0, 3)}'`
		fi
		if [ $prefix = "-v" ]; then
			CODE_VERSION=`echo $1|awk '{print substr($0,3)}'`
		fi
		if [ $prefix = "-c" ]; then
			NOT_UPDATE="true"
		fi
		if [ $prefix = "-g" ]; then
			GIT_GROUP=`echo $1|awk '{print substr($0,3)}'`
		fi
		if [ $prefix = "-b" ]; then
			APP_BRANCH=`echo $1|awk '{print substr($0,3)}'`
		fi
	fi
}

#parse parameter
param $1
param $2
param $3
param $4
param $5
param $6

if [ -z $GIT_GROUP ]; then
    echo "**************************************************************************"
    echo "*Error: git group is null [$GIT_BASE_URL GIT_GROUP/$APP_NAME.git]*********"
	echo "*Please add -g args. For example: sh deploy -gyoho30, yoho30 is git group*"
    echo "**************************************************************************"    
    exit 1
fi

echo -e "\033[33m[INFO] GIT_BASE_URL=$GIT_BASE_URL$GIT_GROUP"
echo -e "\033[33m[INFO] APP_BRANCH=$APP_BRANCH"
echo -e "\033[33m[INFO] SSH_HOST=$SSH_HOST"
echo -e "\033[33m[INFO] SSH_PORT=$SSH_PORT"
echo -e "\033[33m[INFO] CODE_VERSION=$CODE_VERSION"
echo -e "\033[33m[INFO] NOT_UPDATE=$NOT_UPDATE"

IDENTITY_FILE="$7 $8" 

echo -ne "\033[0m"
################################################################################

################################ Init path for app #############################
BASE_HOME=$HOME
OPPS_HOME=$BASE_HOME/opps
OPPS_DEPLOY_HOME=$OPPS_HOME/deploy
APPS_BASE_HOME=$OPPS_HOME/apps
BACKUP_BASE_HOME=$OPPS_HOME/backup
init(){
	REMOTE_BASE_HOME=/home/$USER_LOGIN
	PROJECT_TARGET=$APP_NAME.zip
	PROJECT_SOURCE_BASE_DIR=$APPS_BASE_HOME/$APP_NAME
	PROJECT_SOURCE_DIR=$PROJECT_SOURCE_BASE_DIR/$APP_BRANCH
	WEB_APP_BACKUP_DIR=$BACKUP_BASE_HOME/$APP_NAME
	WEB_APP_ROOT=$REMOTE_BASE_HOME/$APP_NAME
	#WEB_APP_HOME=$WEB_APP_ROOT/deploy
	AUTOCONFIG_HOME=$OPPS_DEPLOY_HOME/autoconfig/$APP_BRANCH
}


################################# main ######################################
main(){
	echo -e "\033[33mPlease choose app to deploy:"
	echo -ne "\033[0m"
	select item in `ls -l $APPS_BASE_HOME | awk 'NR>1 {print $9}'` Exit
	
	do
		if [ $item = "Exit" ]; then
			exit
		else
			#temp=${app_name}_ips
			#deploy \${$temp[@]}
	
			APP_NAME=$item
	
			echo -e "\033[33mPlease choose operation:"
			echo -ne "\033[0m"
			select item in Deploy Rollback Back
			do
				if [ $item = "Deploy" ]; then
					deploy
				elif [ $item = "Rollback" ]; then
					rollback
				elif [ $item = "Back" ]; then
					break
				else
					echo "Input error!"
				fi
			done
		fi
	done
}

################################Update source code & package####################
package(){
	####update autoconfig
	if [ -d "$AUTOCONFIG_HOME" ]; then
		echo "[INFO] start to update config..."
		cd $AUTOCONFIG_HOME
		echo `pwd`
		git pull origin master
	fi
	
	###update source code
	if [ ! -d "$PROJECT_SOURCE_DIR" ]; then
		echo "[INFO] git clone -b $APP_BRANCH $GIT_BASE_URL$GIT_GROUP/$APP_NAME.git $APP_BRANCH"
		cd $PROJECT_SOURCE_BASE_DIR
		git clone -b $APP_BRANCH $GIT_BASE_URL$GIT_GROUP/$APP_NAME.git $APP_BRANCH
	else
		cd $PROJECT_SOURCE_DIR
		echo `pwd`
		if [ $NOT_UPDATE = "false" ]; then
			echo "[INFO] start to update source code from [$GIT_BASE_URL$GIT_GROUP/$APP_NAME.git]"
			if [ -z $CODE_VERSION ]; then
				echo "[INFO] git pull origin $APP_BRANCH"
				git pull origin $APP_BRANCH
			else
				echo "[INFO] start to update source code to $CODE_VERSION..."
				git pull origin $APP_BRANCH $CODE_VERSION
			fi
		fi
	fi
	cd $PROJECT_SOURCE_DIR/bin
	###package app
	echo "[INFO] start to package application..."
	if [ ! -d $AUTOCONFIG_HOME ]; then
		mkdir -p $AUTOCONFIG_HOME
	fi
	sh $PROJECT_SOURCE_DIR/bin/mvn-package.sh $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties $AUTOCONFIG_HOME/global-autoconfig.properties
	echo "[INFO] package application finish."
	
	###commit autoconfig
	ciautoconfig
}

####################################commit autoconfig#################################
ciautoconfig(){
	echo "[INFO] start to commit autoconfig..."
	cd $AUTOCONFIG_HOME
	echo "[INFO] current path: "`pwd`
	echo "[INFO] git pull origin master"
	git pull origin master > /dev/null
	
	echo "[INFO] git add $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties"
	git add $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties > /dev/null
	
	echo "[INFO] git commit -m "commit autoconfig." $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties"
	git commit -m "commit autoconfig." $AUTOCONFIG_HOME/${APP_NAME}-autoconfig.properties > /dev/null
	
	echo "[INFO] git -u push origin master"
	git push -u origin master > /dev/null
	echo "[INFO] commit autoconfig finish."
}


####################################Backup app########################################
backup(){
	if [ -d $WEB_APP_BACKUP_DIR ]; then
		echo "[INFO] backup dir exist."
	else
		mkdir $WEB_APP_BACKUP_DIR
	fi
	
	echo "[INFO] backup dir is [$WEB_APP_BACKUP_DIR]"

	if [ -f $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET ]; then
		echo "[INFO] cp -f $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP"
		cp -f $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $WEB_APP_BACKUP_DIR/$PROJECT_TARGET.$TIMESTAMP
	fi
}

######################################synch app########################################
synch(){
	if [ $1 = "localhost" ]; then
		echo "[INFO] mkdir $OPPS_DEPLOY_HOME/$APP_NAME"
		mkdir $OPPS_DEPLOY_HOME/$APP_NAME
		
		echo "[INFO] cp -r $OPPS_DEPLOY_HOME/$APP_NAME $BASE_HOME"
		cp -r $OPPS_DEPLOY_HOME/$APP_NAME $BASE_HOME
		
		echo "[INFO] rm -rf $OPPS_DEPLOY_HOME/$APP_NAME"
		rm -rf $OPPS_DEPLOY_HOME/$APP_NAME
	
		echo "[INFO] cp $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $BASE_HOME/$APP_NAME"
		cp $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $BASE_HOME/$APP_NAME
		
		echo "[INFO] cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME"
		cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME
	else
		if [ -z $USER_HOME ]; then
			USER_LOGIN=`awk -v item=$1 'BEGIN{ split (item, host, "@"); print host[1]}'`
			init
		fi
		
		echo "[INFO] mkdir $OPPS_DEPLOY_HOME/$APP_NAME"
		mkdir $OPPS_DEPLOY_HOME/$APP_NAME
		
		echo "[INFO] scp -p $IDENTITY_FILE -r -P $SSH_PORT $OPPS_DEPLOY_HOME/$APP_NAME $1:$REMOTE_BASE_HOME"
		scp -p $IDENTITY_FILE -r -P $SSH_PORT $OPPS_DEPLOY_HOME/$APP_NAME $1:$REMOTE_BASE_HOME
		
		echo "[INFO] rm -rf $OPPS_DEPLOY_HOME/$APP_NAME"
		rm -rf $OPPS_DEPLOY_HOME/$APP_NAME
	
		echo "[INFO] scp -p $IDENTITY_FILE -P $SSH_PORT $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $1:$WEB_APP_ROOT"
		scp -p $IDENTITY_FILE -P $SSH_PORT $PROJECT_SOURCE_DIR/deploy/target/$PROJECT_TARGET $1:$WEB_APP_ROOT
		
		echo "[INFO] scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $1:$WEB_APP_ROOT"
		scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $1:$WEB_APP_ROOT
	fi
}

#################################################################################

##################################deploy app########################################
deploy(){
	init
	package
	
	app_ips=${APP_NAME//-/_}_${APP_BRANCH}_ips
	
	if [ $APP_BRANCH = "dev" ]; then
		if [ -z $SSH_HOST ]; then
			if [ -z `eval echo \\${$app_ips[@]}` ]; then
				app_ips=DEV_DEFAULT_SERVER
			fi
			echo "[INFO] app_ips = ${app_ips[*]}"
			for var in `eval echo \\${$app_ips[@]}`; do
				synch $var
				if [ $var = "localhost" ]; then
					sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
				else
					ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
				fi
			done
		else
			echo "[INFO] SSH_HOST = $SSH_HOST"
			synch $SSH_HOST
			ssh $IDENTITY_FILE -p $SSH_PORT $SSH_HOST "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
		fi
	else
		backup

		echo -e "\033[33mPlease choose host to deploy:"
		echo -ne "\033[0m"
		select item in All `eval echo \\${$app_ips[@]}` Back Home
		do
			if [ $item = "Back" ]; then
				break
			elif [ $item = "Home" ]; then
				main
			elif [ $item = "All" ]; then
				for var in `eval echo \\${$app_ips[@]}`; do
					synch $var
					if [ $var = "localhost" ]; then
						sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
					else
						ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
					fi
				done
			else
				synch $item
				if [ $item = "localhost" ]; then
					sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
				else
					ssh $IDENTITY_FILE -p $SSH_PORT $item "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
				fi
			fi
		done
	fi
}

#################################rollback app########################################
rollback(){
	init
	
	echo "[INFO] App start to rollback[$WEB_APP_BACKUP_DIR]......."
	echo -e "\033[33mPlease choose a version to rollback..."
	echo -ne "\033[0m"
	select rollback_file_name in `ls -tl $WEB_APP_BACKUP_DIR | awk 'NR>1 {print $9}' | head -10` Back
	do
		if [ $rollback_file_name = "Back" ]; then
			break
		else
			echo "[INFO] rollback_file_name=$rollback_file_name"
			app_ips=${APP_NAME//-/_}_${USER}_ips
			
			echo -e "\033[33mPlease choose host to rollback:"
			echo -ne "\033[0m"
			select item in All `eval echo \\${$app_ips[@]}` Back Home
			do
				if [ $item = "Back" ]; then
					break
				elif [ $item = "Home" ]; then
					main
				elif [ $item = "All" ]; then
					for var in `eval echo \\${$app_ips[@]}`; do
						if [ $var = "localhost" ]; then
							echo "cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET"
							cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET
							echo "cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME"
							cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME
							echo "sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME"
							sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
						else
							if [ -z $USER_HOME ]; then
								USER_LOGIN=`awk -v item=$var 'BEGIN{ split (item, host, "@"); print host[1]}'`
								init
							fi
							echo "scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $var:$WEB_APP_ROOT/$PROJECT_TARGET"
							scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $var:$WEB_APP_ROOT/$PROJECT_TARGET
							echo "scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT"
							scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $var:$WEB_APP_ROOT
							echo "ssh $IDENTITY_FILE -p $SSH_PORT $var sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
							ssh $IDENTITY_FILE -p $SSH_PORT $var "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
						fi
					done
				else
					if [ $item = "localhost" ]; then
						echo "cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET"
						cp $WEB_APP_BACKUP_DIR/$rollback_file_name $BASE_HOME/$APP_NAME/$PROJECT_TARGET
						echo "cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME"
						cp $OPPS_DEPLOY_HOME/scripts/function.sh $BASE_HOME/$APP_NAME
						echo "sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME"
						sh $BASE_HOME/$APP_NAME/function.sh $BASE_HOME $APP_NAME
					else
						if [ -z $USER_HOME ]; then
							USER_LOGIN=`awk -v item=$item 'BEGIN{ split (item, host, "@"); print host[1]}'`
							init
						fi
						echo "scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $item:$WEB_APP_ROOT/$PROJECT_TARGET"
						scp -p $IDENTITY_FILE -P $SSH_PORT $WEB_APP_BACKUP_DIR/$rollback_file_name $item:$WEB_APP_ROOT/$PROJECT_TARGET
						echo "scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $item:$WEB_APP_ROOT"
						scp -p $IDENTITY_FILE -P $SSH_PORT $OPPS_DEPLOY_HOME/scripts/function.sh $item:$WEB_APP_ROOT
						echo "ssh $IDENTITY_FILE -p $SSH_PORT $item sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
						ssh $IDENTITY_FILE -p $SSH_PORT $item "sh $WEB_APP_ROOT/function.sh $REMOTE_BASE_HOME $APP_NAME"
					fi
				fi
			done	
		fi
	done
}

main