`

生产自动部署的脚本

    博客分类:
  • OS
 
阅读更多

写了一个生产自动部署的脚本,做个笔记!

一键部署脚本 写道
###################
# #
# author : ............... #
# date : 20161218 #
# explain: X71 shell #
###################

#Defining a parser,we use ksh
#!/bin/ksh

#The startup script, first to sleep for a second.
sleep 1

#get input first param.
#If the first parameter is empty,Access to local time format yyyymmdd for T_DATE.
#If the first parameter is not empty,T_DATE get parameter.
if [ -n $1 ]
then
T_DATE=$1
else
#format date ,AIX is date +%Y%m%d
T_DATE=$(date +%Y%m%d)
fi

#Define a log file, which is used to output the program execution log information.
mkdir -p $HOME/backup/${T_DATE}/
LOG=$HOME/backup/${T_DATE}/production_deploy.log
touch $LOG
#Create the log file
#echo 'create '$HOME/backup/${T_DATE}/production_deploy.log >> $LOG

#Handle new programs, new program from TMP is transferred to the new backup directory.
#Processing flow:
#1.First of all, create the directory '$HOME/backup/${T_DATE}/new'.
#2.Traverse under the TMP *.zip, *. The war file name.
#3.If the result is empty,Print log information 'please upload files'.
#4.If the result is not empty,Transfer the file to the '$HOME/backup/${T_DATE}/new' directory.
#5.Unpack the '$HOME/backup/${T_DATE}/new' directory *.zip file.
#
dealNewFiles(){
echo '--------------------dealNewFiles begin---------------------------' >> $LOG
mkdir -p $HOME/backup/${T_DATE}/new
echo 'mkdir -p '$HOME'/backup/'${T_DATE}'/new result is ' $? >> $LOG
##mv *.zip and *.war
cd $HOME/tmp/
zipAndWarFiles=`ls |egrep 'zip|war' |grep -v grep`
echo 'print $HOME/tmp/ *.zip & *.war :' $zipAndWarFiles >> $LOG
if [[ -n ${zipAndWarFiles[*]} ]];
then
mv ${zipAndWarFiles[@]} $HOME/backup/${T_DATE}/new
echo 'mv '${zipAndWarFiles[@]} $HOME/backup/${T_DATE}/new >> $LOG
else
echo please upload files
exit 0
fi

##unzip *.zip
cd $HOME/backup/${T_DATE}/new
echo 'cd '$HOME/backup/${T_DATE}/new >> $LOG
for loop in `ls | grep zip |grep -v grep`
do
`jar -xvf $loop`
echo 'jar -xvf '$loop' result is '$? >> $LOG
done
echo '--------------------dealNewFiles end----------------------------' >> $LOG
}

#
#Backup the old program
#According to the files in the '$HOME/backup/${T_DATE}/new/' directory names, to search for program files .
#If in the user directory that is service programs,if in the user/batch directory that is batch programs.
#Processing flow:
#1.Create '$HOME/backup/${T_DATE}/old/' directory.
#2.Traversing the '$HOME/backup/${T_DATE}/new/' files in the directory name, coexist in the array.
#3.Create a folder with the program same in the '$HOME/backup/${T_DATE}/old' directory.
#4.According to the path of the program, determine the type of program, and transferred to the name of the corresponding folder.
#
dealOldFiles(){

echo '--------------------dealOldFiles begin--------------------------' >> $LOG
mkdir -p $HOME/backup/${T_DATE}/old/
echo 'mkdir -p '$HOME/backup/${T_DATE}/old/ >> $LOG
##read new files
cd $HOME/backup/${T_DATE}/new/
echo 'ls ' $HOME/backup/${T_DATE}/new/ >> $LOG
for loop in `ls`
do
if [[ -d $loop ]] && [[ ! -d $HOME/backup/${T_DATE}/old/$loop ]]
then
mkdir -p $HOME/backup/${T_DATE}/old/$loop/
if [ -d $HOME/$loop ]
then
cp -r $HOME/$loop $HOME/backup/${T_DATE}/old/
echo 'cp -r '$HOME/$loop $HOME/backup/${T_DATE}/old/$loop >> $LOG
else
cp -r $HOME/batch/$loop $HOME/backup/${T_DATE}/old/
echo 'cp -r '$HOME/batch/$loop $HOME/backup/${T_DATE}/old/ >> $LOG
fi
fi
done

##deal ebank store folder
#if [[ $LOGIN = wcb ]]
#then
# cd /WebSphere/AppServer/profiles/Custom01/installedApps/pwcbap1Cell01/
# if [[ -d EBank_war.ear ]]
# then
# cd ./EBank_war.ear/EBank.war
# cp -r store $HOME/backup/${T_DATE}/old/
# else
# cd ./ebank_war.ear/ebank.war
# cp -r store $HOME/backup/${T_DATE}/old/
# fi
#fi
echo '--------------------dealOldFiles end----------------------------' >> $LOG
}

#
#To stop consumers module.
#Processing flow:
#1.To determine whether a folder '$HOME/backup/${T_DATE}/new/AroundServiceApp' and $LOGIN is dhtz.
#2.If consumers module, edit to the AS at the end of the folder, and kill processes.
#3.If the consumer module does not exist, the print log.
#
stopAs(){
echo '=====> stopAs begin......' >> $LOG

if [[ $LOGIN = dhtz ]] && [[ -d $HOME/backup/${T_DATE}/new/AroundServiceApp ]]
then
cd $HOME/backup/${T_DATE}/new/AroundServiceApp/
echo 'ls '$HOME/backup/${T_DATE}/new/AroundServiceApp/ >> $LOG
for loop in `ls |grep AS |grep -v sh`
do
echo 'kill -9 '$loop >> $LOG
ps -ef |grep java | grep $loop | grep -v grep | awk '{print $2}' | xargs kill -9
done
else
echo 'No consumer module.....'
fi
echo '====>stopAs end.......' >> $LOG
}

#
#Deploy new program, and start.
#Transfer the new application from the directory to corresponding program directory.
#If it is a service program, to stop the process first, then replace, finally start the new program.
#If it is batch process substitution can be directly.
#Of course don't forget to assign permissions.
#Processing flow:
#1.Traverse all the folder name in the directory.
#2.If the program under the user's path, we kill process to replace the new program, assign permissions, the last startup scripts.
# Otherwise the replacement program and assign permissions.
#
deployNewExe(){
echo '--------------------deployNewExe begin--------------------------' >> $LOG
stopAs

cd $HOME/backup/${T_DATE}/new/
echo 'ls '$HOME/backup/${T_DATE}/new/ >> $LOG
for loop in `ls`
do
if [ -d $HOME/$loop ]
then
echo 'kill -9 '$loop >> $LOG
ps -ef |grep java | grep $loop | grep -v grep | awk '{print $2}' | xargs kill -9
cd $HOME/backup/${T_DATE}/new/$loop/
cp -r * $HOME/$loop/
echo 'cp -r '$HOME/backup/${T_DATE}/new/$loop/ $HOME/$loop/ >> $LOG
cd $HOME/
chmod -R 775 $loop/
echo 'chmod -R 775 '$loop/ >> $LOG
cd $HOME/$loop/
./start.sh 1 >> /dev/null 2 >> /dev/null
echo $HOME/$loop/'./start.sh &' >>$LOG
else
cd $HOME/backup/${T_DATE}/new/$loop/
echo 'cd ' $HOME/backup/${T_DATE}/new/$loop/ >>$LOG
cp -r * $HOME/batch/$loop/
echo 'cp -r '$HOME/backup/${T_DATE}/new/$loop/ $HOME/batch/$loop/ >> $LOG
cd $HOME/batch/
chmod -R 775 $loop
echo 'chmod -R 775 '$HOME/batch/$loop/ >> $LOG
fi

done
echo '--------------------deployNewExe end----------------------------' >> $LOG
}

#The script always put in storage.
main(){
#print welcome
echo "--------------------------main----------------------------------" >> $LOG
echo "******* Welcome to production_deploy.sh ********" >> $LOG
echo >> $LOG
echo "==>" Date is $T_DATE >> $LOG
echo "==> deal new files" >> $LOG
dealNewFiles
echo "==> deal old files" >> $LOG
dealOldFiles
echo "==> deal new exe" >> $LOG
deployNewExe
}

## begin exe main
main
echo "--------------------------end-----------------------------------" >> $LOG
echo >> $LOG
echo >> $LOG
cat $LOG
exit 0




























 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics