脚印

一脚一印 一点一滴 【欢迎光临·转载请注明出处】
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【自动构建系列】自动部署环境的shell脚本

Posted on 2012-03-12 16:45  August  阅读(6557)  评论(1编辑  收藏  举报

最近项目组在推行自动构建,在每次build完了之后,需要自动把文件部署到linux服务器上,因此根据项目的具体的情况,写了一个shell脚本,供自动构建工具来调用执行部署。

粗略的思路就是:停止系统-->备份现有的环境-->部署新build的文件-->修改需要修改的配置文件(如果需要)-->启动系统

大致脚本如下:

#!/bin/bash

DAY=`date +%Y-%m-%d-%H:%M:%S`

#the path for deleting the log and tmp
APP_MAIN_PATH=/data/yousystem/jboss-5.1.0.GA/server/default

#the key word for searching the process id
PID_MSG=yousystem/

#msg of the file which is running now,backup it.
APP_FILE_NAME1=yousystem.war
APP_FILE_PATH1=/data/yousystem/jboss-5.1.0.GA/server/default/deploy

#msg of the start file which is used to start the system
APP_START_FILE=app-run.sh
APP_START_PATH=/data/yousystem

#msg of the update file
BUILD_FILE_NAME1=yousystem.war
BUILD_FILE_PATH=/data/backup/autobuild/build_files_backup

#msg of the old files for backup
OLD_FILE_PATH=/data/backup/autobuild/old_files_backup

#path of the shell script log
SHELL_LOG_PATH=/data/backup/autobuild

echo '=============================adp==================================='>>$SHELL_LOG_PATH/log_build.log
mkdir -p /data/backup
mkdir -p /data/backup/autobuild
mkdir -p /data/backup/autobuild/auto-build-scripts
mkdir -p /data/backup/autobuild/build_files_backup
mkdir -p /data/backup/autobuild/old_files_backup

#关闭正在运行的系统
time_now_1=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_1 'Begin to kill the progress.....'>>$SHELL_LOG_PATH/log_build.log
pid=`ps -ef|grep $PID_MSG|grep -v grep|awk '{print $2}'`
if [ -n "$pid" ]
then
echo 'The pid: ' $pid ' will be killed....'>>$SHELL_LOG_PATH/log_build.log
kill -9 $pid
fi
rm -rf $APP_MAIN_PATH/log/ $APP_MAIN_PATH/tmp/ $APP_MAIN_PATH/work/
time_now_2=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_2 'End to kill the progress.....'>>$SHELL_LOG_PATH/log_build.log

#备份现有的环境,具体的备份路径和范围由个人决定
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_3=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_3 'Begin to backup the files: '$APP_FILE_NAME1>>$SHELL_LOG_PATH/log_build.log
cp -r $APP_FILE_PATH1/$APP_FILE_NAME1 $OLD_FILE_PATH/$APP_FILE_NAME1-$DAY
time_now_4=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_4 'End to backup the files.....'>>$SHELL_LOG_PATH/log_build.log

#备份完之后先删除掉现有的环境,备份哪些就删除哪些,主要是怕覆盖的时候有文件错误
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_5=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_5 'Begin to delete the files: ' $APP_FILE_NAME1>>$SHELL_LOG_PATH/log_build.log
rm -rf $APP_FILE_PATH1/$APP_FILE_NAME1
time_now_6=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_6 'End to delete the files....'>>$SHELL_LOG_PATH/log_build.log

#把最新build的文件拷贝到需要解压的目录
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_7=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_7 'Begin to copy the build files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.log
cp -r $BUILD_FILE_PATH/$BUILD_FILE_NAME1 $APP_FILE_PATH1
time_now_8=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_8 'End to copy the build files.....'>>$SHELL_LOG_PATH/log_build.log

#删除环境上的缓存文件
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_9=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_9 'Begin to backup the build files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.log
mv -f $BUILD_FILE_PATH/$BUILD_FILE_NAME1 $BUILD_FILE_PATH/$BUILD_FILE_NAME1-$DAY
time_now_10=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_10 'End to backup the build files.....'>>$SHELL_LOG_PATH/log_build.log


#解压新build的文件(视个人项目而定,一般war包不需要解压,但是这里我们需要去修改war包中的配置文件)
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_11=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_11 'Begin to uncompress the files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.log
cd $APP_FILE_PATH1
mkdir unwar
echo 'mkdir success'>>$SHELL_LOG_PATH/log_build.log
cp -r $APP_FILE_NAME1 unwar/
echo 'copy file success'>>$SHELL_LOG_PATH/log_build.log
cd unwar/
jar -xf $APP_FILE_NAME1
echo 'uncompress file success'>>$SHELL_LOG_PATH/log_build.log
rm -rf $APP_FILE_NAME1
echo 'delete the copy file success'>>$SHELL_LOG_PATH/log_build.log
cd $APP_FILE_PATH1
rm -rf $APP_FILE_NAME1
echo 'delete the update file success'>>$SHELL_LOG_PATH/log_build.log
mv unwar $APP_FILE_NAME1
echo 'change the folder name success'>>$SHELL_LOG_PATH/log_build.log
time_now_12=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_12 'End to uncompress the files:.....'>>$SHELL_LOG_PATH/log_build.log

#重启系统
echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.log
time_now_13=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_13 'Begin to restart the system....'>>$SHELL_LOG_PATH/log_build.log
echo 'Start File is: ' $APP_START_PATH/$APP_START_FILE>>$SHELL_LOG_PATH/log_build.log
sh $APP_START_PATH/$APP_START_FILE
time_now_14=`date +%Y-%m-%d" "%H:%M:%S`
echo $time_now_14 'End to restart the system....'>>$SHELL_LOG_PATH/log_build.log