Loging (){
if [ -z $LOGFILE ];then
[ ! -d /var/log/scripts ] && mkdir -p /var/log/scripts
LOGFILE=/var/log/scripts/`basename $0`.log
fi
if [ "$1"="-s" ];then
echo "`date +"%G-%m-%d %H:%M:%S"` $2" | tee -a $LOGFILE && return 0
fi
echo "`date +"%G-%m-%d %H:%M:%S"` $1" >> $LOGFILE
}
##Upload file to ftp servers
#You must Define variables needed,eg:
# FTPSERVER=192.168.80.220
# FTPLOGIN=backup
# FTPASSWORD=hc360bak
# FTP_SUBDIR=$1
# CMD_LIST=( ls "lcd $1" )
ftp_f (){
#SUBDIR=$FTP_SUBDIR
if [ -z $CMD_LIST ];then
Loging -s "WARNING:The CMD_LIST is null,and nothing can be excuted."
fi
[ -z $FTPSERVER ] && Loging -s "WARNING:The FTPSERVER is null,and the ftp server mybe not login."
[ -z $FTPLOGIN ] && Loging -s "WARNING:The FTPLOGIN is null,and the ftp server mybe not login."
[ -z $FTPPASSWORD ] && Loging -s "WARNING:The FTPPASSWORD is null,and the ftp server mybe not login."
echo "machine $FTPSERVER login $FTPLOGIN password \"$FTPPASSWORD\"" >~/.netrc
chmod 600 ~/.netrc
#Create command list file
i=0
while [ $i -lt ${#CMD_LIST[*]} ]
do
echo "${CMD_LIST[${i}]}" >>~/.cmd_list
let i=$i+1
done
[ $? -eq 0 ] && Loging "INFO:Create comand file \"cmd_list\" is successful."
#ensure ftp can be closed
echo "bye" >>~/.cmd_list
ftp $FTPSERVER <~/.cmd_list
if [ $? -eq 0 ];then
Loging "INFO:Exec command as \"cmd_list\" is complete."
else
Loging "ERROR:Upload files to FTP is failure!" && return 1
fi
#clean temp file
#rm -rf ~/.netrc
rm -rf ~/.cmd_list
return 0
if [ ! -f /usr/local/scripts/pub/functions ];then
echo "ERROR:/usr/local/scripts/pub/functions is not exist,and exit!" && exit 1
fi
. /usr/local/scripts/pub/functions
## initcial scipts variables
init_v(){
local PROJECT=
if [ -z $PROFILE ];then
PROFILE=/etc/profile.d/deploy.sh
fi
if [ -z $1 ];then
Loging "ERROR:Init error for project is not speciafied." && return 1
fi
#The $PROFILE is not exist and create it!
if [ ! -f $PROFILE ];then
echo "export D_${1}=`date +"%G%m%d"`:0" >>$PROFILE
fi
#Import the project var
. $PROFILE
#if project is not exist and create var
eval PROJECT=\$D_$1
if [ -z $PROJECT ];then
echo "export D_${1}=`date +"%G%m%d"`:0" >>$PROFILE
PROJECT="`date +"%G%m%d"`:0"
fi
#export the var
DATE=`echo $PROJECT|awk -F: '{print $1}'`
COUNT=`echo $PROJECT|awk -F: '{print $2}'`
if [ "$DATE" != `date +"%G%m%d"` ];then
Loging -s "INFO:The date is expire,and initate date and count."
DATE=`date +"%G%m%d"`
COUNT=0
fi
export DATE COUNT
}
## Write variable to profile config file
write_v(){
if [ -z $PROFILE ];then
PROFILE=/etc/profile.d/deploy.sh
fi
sed -i /D_${1}/s/=.*/=${DATE}:$COUNT/g $PROFILE
}
##Define help function
help () {
echo -e "Usage:deploy.sh [command] [project name]\n"
echo -e "Usage:deploy.sh roll number [project name]"
echo -e "Command:"
echo -e "\tdeploy Deploy the project from ftp downloading *.war,and deploying. "
echo -e "\tdeployb Backup the old project files or dirs,first,And than deploy the war from remote."
echo -e "\tbackup Backup the old project files or dirs."
echo -e "\troll:number Roll back the backup files from backup ftp,the number default is last count of backup file"
echo -e "eg.:"
echo -e "\tdeploy.sh deploy info"
}
##Download file from remote server.
download_f (){
if [ -z $1 ];then
Loging "ERROR:Please speciafied argument for download_f func." && return 1
fi
local R_PATH=`dirname $1`
local R_FILENAME=`basename $1`
local L_PATH=/tmp
Loging -s "INFO:Begin download project file from remote server."
CMD_LIST=( "bin" "cd $R_PATH" "lcd $L_PATH" "get $R_FILENAME" "bye" )
ftp_f
if [ $? -ne 0 ];then
Loging -s "ERROR:Download $R_FILENAME is failure!" && return 1
fi
return 0
}
##Shutdown server
srv_shutd (){
#kill mutipl process of server
for i in `ps -ef|grep $1|grep -v grep|awk '{print \$2}'`
do
kill -9 $i
done
[ $? -eq 0 ] && Loging "INFO:Shutdown server is successfull!"
return 0
}
srv_start(){
if [ -z $TOMCAT_HOME ];then
TOMCAT_HOME=/usr/local/tomcat6
fi
#export java envirment variables
. /etc/profile.d/java.sh
##clear server cache files
rm -rf $TOMCAT_HOME/work/*
$TOMCAT_HOME/bin/startup.sh
}
##Backup the old directory
backup_f (){
#define the variables of function
local BACKUP_BASE_PATH=`dirname $1`
local BACKUP_DIR=`basename $1`
local BACKUP_FILE=${BACKUP_DIR}.bak${COUNT}.tar.gz
local EXCLUDE=
local BACKUP_COMMAND="tar -C $BACKUP_BASE_PATH -zcvf $BACKUP_FILE $BACKUP_DIR $EXCLUDE"
if [ ! -d $1 -a ! -f $1 ];then
Loging "ERROR:The $1 directory or file is not exsit! " && continue
fi
if [ -z "$BACKUP_COMMAND" ];then
Loging "ERROR:Backup command is not defined!" && return 1
fi
#execute command
echo $BACKUP_COMMAND
$BACKUP_COMMAND 2>&1 >/dev/null
if [ $? -eq 0 ];then
Loging -s "INFO: $BACKUP_FILE is made successful!"
else
Loging -s "ERROR: $BACKUP_FILE is made failure!" && return 1
fi
#upload file to remote ftp server
CMD_LIST=( "mkdir $DATE" "cd $DATE" "mkdir backup" "cd backup" "put $BACKUP_FILE" "bye" )
ftp_f
if [ $? -eq 0 ];then
Loging -s "INFO:Backup $BACKUP_FILE to remote is successful!"
rm -rf $BACKUP_FILE
else
Loging -s "ERROR:Backup $BACKUP_FILE is failure!" && return 1
fi
return 0
}
#Roll backup file from ftp server,usage: roll_f <project> <backup count>
roll_f (){
local PORJECT=$1
local ROLL_COUNT=$2
if [ -z $ROLL_COUNT ];then
let ROLL_COUNT=$COUNT-1
fi
#Download backup files from remote ftp.
download_f $DATE/backup/${PORJECT}.bak$ROLL_COUNT.tar.gz
if [ ! -f /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz ];then
Loging "ERROR:Roll back failuer,mybe the /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz is not exist!"
return 1
fi
rm -rf /tmp/$PORJECT
tar -C /tmp -zxvf /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz >>/dev/null
if [ ! -d /tmp/$PORJECT ];then
Loging "ERROR:The /tmp/$PORJECT is not exsit.Exit!"
return 1
fi
#Shutdown server of tomcat
srv_shutd tomcat
if [ $? -ne 0 ];then
Loging "ERROR:Shutdown server failure."
return 1
fi
#Clear apps files
rm -rf $APPS_BASE/$PORJECT
rm -rf $APPS_BASE/${PORJECT}.war
mv /tmp/$PORJECT $APPS_BASE/
srv_start
if [ $? -eq 0 ];then
Loging "INFO:Move roll back files is successfull!"
else
Loging -s "ERROR:roll back failure!" && return 1
fi
}
##Deploy new files to server
deploy_f (){
local PROJECT=$1
#Download new files package from remote ftp.
#remove old app file and then download new file
#rm -rf /tmp/$PROJECT.war
download_f $DATE/$PROJECT.war
if [ ! -f /tmp/${PROJECT}.war ];then
Loging "ERROR:deploy failuer,mybe the /tmp/$PROJECT.war is not exist!"
return 1
fi
srv_shutd tomcat
if [ $? -ne 0 ];then
Loging -s "ERROR:Shutdown server failure."
return 1
fi
#Clear the project in server
rm -rf $APPS_BASE/$PROJECT
rm -rf $APPS_BASE/$PROJECT.war
mv /tmp/$PROJECT.war $APPS_BASE/
#Start server
srv_start || return 1
Loging -s "INFO:Start server is ok!"
}
set_var () {
##Start scripts
#FTPSERVER=101.251.113.247
#FTPLOGIN=hdong
#FTPPASSWORD="ECHwey81"
##MMT ftp authentication
FTPSERVER=101.251.113.247
FTPLOGIN=zhounan
FTPPASSWORD="123ZXC,./"
CMD_LIST=( ls "lcd $1" )
PROFILE=/etc/profile.d/deploy.sh
if [ -Z $TOMCAT_HOME ];then
read -t20 -p "The variable $TOMCAT_HOME is null,Please specify the TOMCAT_HOME path:" TOMCAT_HOME
fi
TOMCAT_HOME=${TOMCAT_HOME:-"/usr/local/tomcat6"}
read -t10 -p "The apps default base path is \$TOMCAT_HOME/webapps,are you sure change it (Y/N):" FG
case $FG in
y|Y|yes|YES|Yes) read -t 30 -p "Please input:" APPS_BASE ;;
*) APPS_BASE=$TOMCAT_HOME/webapps
esac
}
###############################################Debug###############
#######################end#######################################
cd ~
#Initate variables
init_v $2
Loging -s "INFO:Initated variables complete.DATE=$DATE,COUNT=$COUNT."
case $1 in
"deploy")shift
if [ -z $1 ];then
help
fi
#Define variables
set_var
#Start deploy war-file
deploy_f $1
if [ $? -eq 0 ];then
Loging -s "INFO:Complete depoy successfull!"
fi
;;
"backup") shift
if [ -z $1 ];then
help
fi
#Define variables
set_var
#Start backup old project
backup_f $TOMCAT_HOME/webapps/$1
if [ $? -eq 0 ];then
Loging -s "INFO:Complete backup old project successfull!"
#Write variables
let COUNT=$COUNT+1
write_v
fi
;;
"deployb")shift
if [ -z $1 ];then
help
fi
#Define variables
set_var
#Start backup old project
backup_f $TOMCAT_HOME/webapps/$1
if [ $? -eq 0 ];then
Loging -s "INFO:Complete backup old project successfull!"
#Write variables
let COUNT=$COUNT+1
write_v
fi
#Start deploy war-file
deploy_f $1
if [ $? -eq 0 ];then
Loging -s "INFO:Complete depoy successfull!"
fi
;;
"roll") shift
if [ -z $1 ];then
help
fi
#Define variables
set_var
#Start Roll back from ftp server.
roll_f $1 $2
if [ $? -eq 0 ];then
Loging -s "INFO:Complete roll back successfull!"
fi
;;
*) help&& exit 1
esac