基于git 的网站发布脚本,非常成熟

简介:

下面共享以下我目前正在使用的,基于Git实现的网站发布脚本

 

 

 

Deployment GIT


Installation

https://github.com/netkiller/deployment.git
$ git clone https://github.com/netkiller/deployment.git
$ chmod 755 -R deployment
$ export DEPLOY_HOME=~/deployment
		

临时使用的方法

export DEPLOY_HOME=/home/user/deployment
		
$ cd deployment/
$ ln -s bin/deploy.git run
		

使用说明

		
$ ./run
Usage: ./run [OPTION] <server-id> <directory/timepoint>

OPTION:
	development <domain> <host>
	testing <domain> <host>
	production <domain> <host>

	branch {development|testing|production} <domain> <host> <branchname>
	revert {development|testing|production} <domain> <host> <revision>
	backup <domain> <host> <directory>
	release <domain> <host> <tags> <message>

	list
	list <domain> <host>

	clean {development|testing|production} <domain> <host>
	log <project> <line>

	conf list
	cron show
	cron setup
	cron edit
		
		

模拟演示

环境说明

  1. development 开发环境

  2. testing 测试环境,代码来自开发环境的合并

  3. production 生产环境,当testing环境通过测试后,将testing 合并到 主干 即成为生产环境的代码

    另外我们可以通过release功能将主干的代码复制到tags中,命名采用版本号

创建配置文件

development

部署开发代码到开发环境

cat deployment/conf/development/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/development/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

创建密码文件

$ cat deployment/conf/development/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

testing

部署测试分支到测试环境

cat deployment/conf/testing/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/development/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

创建密码文件

$ cat deployment/conf/testing/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

production

部署主干代码到远程主机

cat deployment/conf/production/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/development/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

创建密码文件

$ cat deployment/conf/production/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

配置排出列表

有时我们不希望某些文件被上传到服务器上。我们可以通过排除列表来排除上传

cat exclude/mydomain.com/www.lst
/test/phpinfo.php
/config/database.php
/backup/*.sql
			

配置文件管理

生产环境的安全问题,例如数据库联接信息,开发环境与测试环境的数据库是可以供发人员和测试人员随意操作的,损坏之后恢复即可,但生产环境的数据库是不能随便操作的,除运维人员其他人是不应该有权限的, 我们希望部署到生产环境的时候使用另一个配置文件,并且这个配置文件只有运维人员才能编辑。

config/database.php 将覆盖原有的配置文件,然后上传到生产环境

vim share/production/mydomain.com/www/config/database.php
...
你的数据库连接信息
...
			

部署前/后脚本

部署前需要做什么

$ cat libexec/mydomain.com/www/before
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/cn/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/news/
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/images/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/bbs/images/
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/css/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/news/css
			

部署后需要做什么

cat libexec/hx9999.com/www/after
ssh www@192.168.1.1 "chown www:www -R /www/mydomain.com"
ssh www@192.168.1.1 "chown 700 -R /www/mydomain.com"
ssh www@192.168.1.1 "chown 777 -R /www/mydomain.com/www.mydomain.com/images/upload"
			

配置部署节点

在需要部署的节点上安装rsync

		
yum install xinetd rsync -y

vim /etc/xinetd.d/rsync <<VIM > /dev/null 2>&1
:%s/yes/no/
:wq
VIM

# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
		
		

/etc/rsyncd.conf 配置文件

		
# cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 8
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

hosts deny=*
hosts allow=192.168.2.0/255.255.255.0

[www]
    uid = www
    gid = www
    path = /www
    ignore errors
    read only = no
    list = no
    auth users = www
    secrets file = /etc/rsyncd.passwd
[mydomain.com]
    uid = www
    gid = www
    path = /www/mydomain.com
    ignore errors
    read only = no
    list = no
    auth users = mydomain
    secrets file = /etc/rsyncd.passwd
[example.com]
    uid = www
    gid = www
    path = /www/example.com
    ignore errors
    read only = no
    list = no
    auth users = example
    secrets file = /etc/rsyncd.passwd
		
		

创建密码

		
cat > /etc/rsyncd.passwd <<EOD
www:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
mydomain:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
example:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
EOD
		
		

部署代码

Tip

development | testing 建议使用分支管理, 而production是用master分支

开发环境部署

$ ~/deployment/run branch development mydomain.com www development 首次需要运行,切换到开发分支
$ ~/deployment/run development mydomain.com www
		

测试环境部署

$ ~/deployment/run branch development mydomain.com www testing 首次需要运行,切换到开发分支
$ ~/deployment/run testing mydomain.com www
		

如果每个bug一个分支的情况可以每次先运行

$ ~/deployment/run branch development mydomain.com www bug0005
		

生产环境部署

$ ~/deployment/run production mydomain.com  www
		

每次部署都会在服务器 /www/mydomain.com/backup/ 下备份更改的文件

回撤操作

当程序升级失败需要立即回撤到指定版本时使用

			
$ ~/deployment/run revert {development|testing|production} <domain> <host> <revision>
			
			

 

./run revert development mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
./run revert testing mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
./run revert production mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
			

发行一个版本

release 升级你的版本

$ ~/deployment/run release mydomain.com www stable-2.0
			

分支管理

查看当前分支

[www@manager deployment]$ ./run branch development mydomain.com www
* master
		

切换分支

[www@manager deployment]$ ./run branch development mydomain.com www development
HEAD is now at 461b796 提交最新代码
Branch development set up to track remote branch development from origin.
Switched to a new branch 'development'
		

现在已经切换到开发分支

[www@manager deployment]$ ./run branch development mydomain.com www
* development
  master
		

日志

部署日志 deploy.YYYY-MM-DD.log, 记录部署时间与动态

 $ cat log/deploy.2012-08-03.log
[2012-12-06 21:52:05] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:52:10] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:53:13] [checkout] commit:29dd5c3de6559e2ea6749f5a146ee36cbae750a7 /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:53:18] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:53:39] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:53:45] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:54:08] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:54:10] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:54:13] [checkout] commit:29dd5c3de6559e2ea6749f5a146ee36cbae750a7 /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:54:15] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
		

项目日志 www.example.com.log 记录项目有哪些更新, 上传的细节, 你能通过日志看到那些文件被上传

		
$ cat log/www.example.com.log
--------------------------------------------------
HEAD is now at 03b3ad5 XXXXXXXXXXXX
- share:
- libexec:
2012/12/06 21:53:45 [12488] building file list
2012/12/06 21:53:45 [12488] .d..t...... application/config/development/
2012/12/06 21:53:45 [12488] <f.st...... application/config/development/database.php
2012/12/06 21:53:45 [12488] .d..t...... application/controllers/
2012/12/06 21:53:45 [12488] <f.st...... application/controllers/info.php
2012/12/06 21:53:45 [12488] .d..t...... application/core/
2012/12/06 21:53:45 [12488] <f.st...... application/core/MY_Controller.php
2012/12/06 21:53:45 [12488] .d..t...... application/models/
2012/12/06 21:53:45 [12488] <f.st...... application/models/news.php
2012/12/06 21:53:45 [12488] .d..t...... application/views/
2012/12/06 21:53:45 [12488] <f.st...... application/views/example.html
2012/12/06 21:53:45 [12488] <f.st...... application/views/index.php
2012/12/06 21:53:45 [12488] .d..t...... resources/css/
2012/12/06 21:53:45 [12488] <f.st...... resources/css/m.css
2012/12/06 21:53:45 [12488] sent 23640 bytes  received 421 bytes  3701.69 bytes/sec
2012/12/06 21:53:45 [12488] total size is 2869760  speedup is 119.27
--------------------------------------------------
		
		

debug

启用调试模式

vim bin/deploy.git

DEBUG=yes
		

然后查看log/debug.log

 

 

 

Bash代码   收藏代码
  1. #!/bin/bash  
  2. #####################################################################  
  3. # Description: Automation Deployment Script   
  4. #              Netkiller series utilities  
  5. # Author: Neo<netkiller@msn.com>  
  6. # Homepage: http://netkiller.github.com/  
  7. #           http://netkiller.sourceforge.net/  
  8. # GIT URL:  https://github.com/netkiller/deployment.git  
  9. # $Id$  
  10. #####################################################################  
  11. # :set  tabstop=4  
  12. # :set shiftwidth=4  
  13. # :set expandtab  
  14.   
  15. if [ -z $DEPLOY_HOME ]; then  
  16.     echo 'Example: export DEPLOY_HOME=/srv/deploy'  
  17.     exit  
  18. fi  
  19. if [ -f $DEPLOY_HOME/conf/default.conf ];then  
  20.     . $DEPLOY_HOME/conf/default.conf  
  21. fi  
  22.   
  23. if [ -f $DEPLOY_HOME/conf/stage.conf ];then  
  24.     . $DEPLOY_HOME/conf/stage.conf  
  25. fi  
  26.   
  27. #================================================================================  
  28.   
  29. LOGFILE="deploy.$(date -d "today" +"%Y-%m-%d").log"  
  30. TMPDIR=$(mktemp -d --suffix=.tmp -p /tmp deploy.XXXXXX)  
  31. SVN=/usr/bin/svn  
  32. GIT=/usr/bin/git  
  33.   
  34. BACKUPDIR=/backup  
  35. RSYNC="rsync"  
  36. UPLOAD_DIR=$TMPDIR  
  37. REVISION=''  
  38. DEBUG='yes'  
  39. # development  production  testing  
  40. if [ -z $STAGE ]; then  
  41.     echo 'Example: touch conf/stage.conf'  
  42.     echo "STAGE='development' or STAGE='testing' or STAGE='production'"  
  43.     exit  
  44. fi  
  45. #================================================================================  
  46.   
  47. if [ ! -d ${TMPDIR} ]; then  
  48.     mkdir ${TMPDIR}  
  49. fi  
  50.   
  51. #chmod 700 -R ${SRCDIR}/*  
  52. umask 0077  
  53. #pkgname=${project}-${version}-${datetime}.pkg  
  54. #tar jcvf ${pkgname} /tmp/${project} --remove-files >> deploy.log  
  55. #####################################################################  
  56.   
  57. function logging(){  
  58.     local logfile="$LOGDIR/$LOGFILE"  
  59.     local timepoint=$(date -d "today" +"%Y-%m-%d %H:%M:%S")  
  60.     local status=$1  
  61.     local message=$2  
  62.     echo "[$timepoint] [${status}] ${message}" >> $logfile  
  63. }  
  64.   
  65. function debug(){  
  66.     if [ ${DEBUG} = 'yes' ]; then  
  67.         local logfile="$LOGDIR/debug.log"  
  68.         local timepoint=$(date -d "today" +"%Y-%m-%d %H:%M:%S")  
  69.         local status=$1  
  70.         local message=$2  
  71.         echo "[$timepoint] [${status}] ${message}" >> $logfile  
  72.     fi  
  73. }  
  74.   
  75. #logging 'OK' 'This is test msg!!!'  
  76. #debug 'OK' 'This is debug msg!!!'  
  77.   
  78. function conf(){  
  79.     local cmd=$2  
  80.     local prj=$3  
  81.     case $cmd in  
  82.         list)  
  83.             ls $SYSCONFDIR/*/*  
  84.             ;;  
  85.         new)  
  86.             mkdir -p $SYSCONFDIR      
  87.             #if [ ! -d ${BACKUPDIR} ]; then   
  88.             #   mkdir -p $BACKUPDIR  
  89.             #fi  
  90.   
  91.             read -p "Project directory: " prjdir  
  92.             if [ -z $prjdir ]; then  
  93.                 exit  
  94.             fi  
  95.             if [ -f $SYSCONFDIR/$prjdir.conf ]; then  
  96.                 echo "cannot create config $prjdir.conf': File exists"  
  97.                 exit 1  
  98.             fi  
  99.   
  100.             read -p "subversion url: $REPOSITORIES/: " svnurl  
  101.             if [ -z $svnurl ]; then  
  102.                 svnurl=$REPOSITORIES  
  103.             fi  
  104.             read -p "hostname: " host  
  105.             if [ -z $host ]; then  
  106.                 host="localhost"  
  107.                 echo "default hostname 'localhost'"  
  108.             fi  
  109.             read -p "upload mode ftp/scp/sftp/rsync: " mode  
  110.             if [ -z $mode ]; then  
  111.                 mode=ftp  
  112.             else  
  113.                 case $mode in  
  114.                     ftp)  
  115.                         mode="ftpdeploy"  
  116.                         ;;  
  117.                     scp)  
  118.                         mode="scpdeploy"  
  119.                         ;;  
  120.                     sftp)  
  121.                         mode="sftpdeploy"  
  122.                         ;;  
  123.                     rsync)  
  124.                         mode="rsync"  
  125.                         ;;  
  126.                 esac  
  127.             fi  
  128.             read -p "Create $prjdir config? [y/n]" -n 1 key  
  129.             echo  
  130.             if [ $key = 'y' ]; then  
  131.                 echo -ne "REPOSITORIES=$REPOSITORIES/$svnurl  
  132. COMMAND=$mode  
  133. HOSTNAME=$host  
  134.                 " >> $SYSCONFDIR/$prjdir.conf  
  135.             fi  
  136.             ;;  
  137.         remove)  
  138.             if [ -f $SYSCONFDIR/$prj ]; then  
  139.                 rm -rf $SYSCONFDIR/$prj  
  140.             fi  
  141.             ;;  
  142.         show)  
  143.             cat $SYSCONFDIR/$prj  
  144.             ;;  
  145.         edit)  
  146.             vim $SYSCONFDIR/$prj  
  147.             ;;  
  148.         *)  
  149.             ls $SYSCONFDIR/*/*  
  150.             ;;  
  151.     esac  
  152.   
  153. }  
  154.   
  155.   
  156. #####################################################################  
  157.   
  158. function config {  
  159.     local cfg=$1  
  160.     exclude_from=$PREFIX/exclude/${cfg}.lst  
  161.     include_from=$PREFIX/include/${cfg}.lst  
  162.   
  163.     if [ -f $SYSCONFDIR/${STAGE}/${cfg}.conf ];then  
  164.         . $SYSCONFDIR/${STAGE}/${cfg}.conf   
  165.     else  
  166.         echo "Please provide the config($SYSCONFDIR/${STAGE}/${cfg}.conf) to deploy!"  
  167.         exit  
  168.     fi  
  169.     if [ -z "$cfg" ]; then  
  170.         echo "Please provide the path for deploy!"  
  171.         exit  
  172.     fi  
  173.   
  174.     if [ ! -f $exclude_from ]; then  
  175.         echo "Please provide a list of excluded in the $exclude_from."  
  176.     touch $exclude_from  
  177.         exit  
  178.     fi  
  179.     if [ ! -f $include_from ]; then  
  180.         echo "Please provide a list of included in the $include_from."  
  181.     touch $include_from  
  182.         exit  
  183.     fi  
  184.   
  185. #    case ${STAGE} in  
  186. #        development)  
  187. #            SUBVERSION='development'  
  188. #            ;;  
  189. #        testing)  
  190. #            SUBVERSION=''  
  191. #            ;;  
  192. #        production)  
  193. #            ;;  
  194. #        *)  
  195. #            SUBVERSION='current'  
  196. #            ;;  
  197. #    esac  
  198.   
  199. }  
  200.   
  201. function deploy() {  
  202.   
  203.     local domain=$2  
  204.     local host=$3  
  205.     local commit=$4  
  206.     local logfile=${LOGDIR}/${host}.${domain}.log  
  207.     local backupdir=${BACKUPDIR}/${host}.${domain}/$(date '+%Y-%m-%d/%H:%M:%S')  
  208.     local message=${STAGE}/${domain}/${host}.${domain}  
  209.   
  210.     if [ $# -lt 3 ]; then  
  211.         usage  
  212.     fi  
  213.   
  214.     if [ ${STAGE} = 'production' ]; then  
  215.         read -p "Are you sure you want to continue deploying? [y/n]" -n 1 key  
  216.         echo  
  217.         if [ $key != 'y' ]; then  
  218.             exit  
  219.         fi  
  220.     fi  
  221.     if [ $host = 'all' ]; then  
  222.     for h in $(ls -1 $SYSCONFDIR/${STAGE}/$domain/ | cut -d. -f1)  
  223.     do  
  224.         /bin/sh $BINDIR/deploy deploy $domain $h  
  225.     done  
  226.     exit  
  227.     fi  
  228.   
  229.       
  230.           
  231.     #if [ ! -z $revision  ]; then  
  232.     #    REVISION="-r ${revision}"  
  233.     #fi  
  234.   
  235.     config ${domain}/${host}  
  236.   
  237.     project=$SRCDIR/${STAGE}/${domain}/${host}.${domain}  
  238.    
  239.     GIT_OPTS=${REVISION}  
  240.     echo '================================================================================'  
  241.     if [ -d ${project} ]; then   
  242.         cd $project  
  243.         #$GIT stash  
  244.         #$GIT pull --progress  
  245.         #$GIT stash clear  
  246.         #$GIT checkout .  
  247.           
  248.         $GIT reset HEAD --hard >> $logfile  
  249.         echo -n " Repository: ${REPOSITORY} "  
  250.         $GIT pull --progress          
  251.         if [ ! -z $commit ]; then  
  252.             $GIT checkout $commit .  
  253.             echo " Commit: $commit"  
  254.             logging 'checkout' "commit:$commit ${project} "  
  255.         else  
  256.             logging 'update' ${project}  
  257.         fi  
  258.           
  259.     else  
  260.         mkdir -p ${project}  
  261.         $GIT clone ${REPOSITORY} ${project} >> $logfile  
  262.         logging 'checkout' ${project}   
  263.     fi  
  264.     echo '================================================================================'  
  265.     RSYNC_OPTS=" -azv --backup --backup-dir=${backupdir} --exclude=.git --log-file=${logfile} --exclude-from=$exclude_from --include-from=$include_from"  
  266.   
  267.     if [ -d ${SHAREDIR}/${STAGE}/${domain}/${host}/ ]; then  
  268.         cp -a ${SHAREDIR}/${STAGE}/${domain}/${host}/* ${project}/  
  269.     fi  
  270.     echo '- share:' >> ${logfile}  
  271.     echo ' Share [ OK ]'  
  272.     if [ -f ${LIBEXECDIR}/${domain}/${host}/before ];then  
  273.         /bin/sh ${LIBEXECDIR}/${domain}/${host}/before >> ${logfile}  
  274.     fi  
  275.     echo '- libexec:' >> ${logfile}  
  276.     echo ' Libexec [ OK ]'  
  277.     find $SRCDIR/* -type f -name "Thumbs.db" -exec rm -rf {} \;  
  278.     echo '================================================================================'  
  279.       
  280.     for addr in ${REMOTE}   
  281.     do  
  282.           
  283.         echo " Deploy [${message}] ${addr}"  
  284.         echo '================================================================================'  
  285.           
  286.         case ${MODE} in  
  287.             FTP)  
  288.                 ftpdeploy  
  289.                 ;;  
  290.             SCP)  
  291.                 scp -ar ${project}/* ${addr}:${DESTINATION}  
  292.                 ;;  
  293.             SFTP)  
  294.                 sftpdeploy  
  295.                 ;;  
  296.             RSYNC)  
  297.                 $RSYNC $RSYNC_OPTS $OPTION ${project}/* ${addr}::${DESTINATION}  
  298.                 debug 'rsync' "$RSYNC $RSYNC_OPTS $OPTION ${project}/* ${addr}::${DESTINATION}"  
  299.                 ;;  
  300.             "RSYNC+SSH")  
  301.                 $RSYNC $RSYNC_OPTS ${project}/* ${addr}:${DESTINATION}  
  302.                 ;;  
  303.         esac  
  304.           
  305.         if [ -z "${REVISION}" ]; then  
  306.             logging 'deploy' "${message} => ${addr}:${DESTINATION}"  
  307.         else  
  308.             logging 'revert' "${message} => ${addr}:${DESTINATION}"  
  309.         fi  
  310.         echo '--------------------------------------------------' >> ${logfile}  
  311.     done  
  312.           
  313.     if [ -f ${LIBEXECDIR}/${domain}/${host}/after ];then  
  314.         #ssh ${scp} < ${LIBEXECDIR}/${domain}/${host}/after  
  315.         exit  
  316.     fi  
  317.       
  318. }  
  319.   
  320. function revert() {  
  321.   
  322.     #if [ $STAGE = 'testing' -o $STAGE = 'development' -o $STAGE = 'production' ]; then  
  323.     local domain=$3  
  324.     local host=$4  
  325.         local commit=$5  
  326. #    else  
  327. #   local domain=$1  
  328. #        local host=$2  
  329. #        local revision=$3  
  330.     #fi  
  331.     deploy $STAGE $domain $host $commit  
  332. }  
  333.   
  334. function timepoint {  
  335.     TIMEPOINT=`date '+%Y-%m-%d.%H-%M-%S'`  
  336.     echo $TIMEPOINT >> timepoint.log  
  337. }  
  338.   
  339. function unstable {  
  340.     local edition=$(basename $unstable)  
  341.     svn export ${unstable} ${src}/$edition  
  342.   
  343.     for ignore in $( cat excluded.lst ); do  
  344.         rm -rf ${src}/$edition/$ignore  
  345.     done  
  346.   
  347.     $RSYNC ${src}/$edition ${destination}  
  348.   
  349.     ssh ${remote} < script/unstable  
  350. }  
  351.   
  352. function clean() {  
  353.     local stage=$2  
  354.     local domain=$3  
  355.     local host=$4  
  356.     local project=$SRCDIR/${stage}/${domain}/$host.${domain}  
  357.       
  358.     if [ $# -lt 3 ]; then  
  359.         usage  
  360.     fi  
  361.       
  362.     rm -rf ${project}  
  363. }  
  364.   
  365. function list {  
  366.     local domain=$2  
  367.     local host=$3  
  368.     local dir=$4  
  369.     if [ -z $domain ]; then  
  370.         ls $SRCDIR/*  
  371.     exit  
  372.     fi  
  373.   
  374.     if [ -z $host ]; then  
  375.         usage  
  376.     fi  
  377.   
  378.     #config ${domain}/${host}  
  379.     ls $SRCDIR/*/${domain}/${host}.${domain}  
  380.     #git ls ${REPOSITORIES}/$dir #| awk -F '/' '{print $1}'  
  381. }  
  382.   
  383. function backup() {  
  384.     local domain=$2  
  385.     local host=$3  
  386.     local dir=$4  
  387.     local logfile=${LOGDIR}/${host}.${domain}.log  
  388.   
  389.     if [ -z $domain ]; then  
  390.         usage  
  391.     fi  
  392.   
  393.     if [ -z $host ]; then  
  394.         usage  
  395.     fi  
  396.   
  397.     config ${domain}/${host}  
  398.   
  399.     if [ -z $dir ]; then  
  400.         dir=$TMPDIR  
  401.     fi  
  402.   
  403.     for addr in ${REMOTE}  
  404.     do  
  405.         dir=$dir/${addr}  
  406.         if [ ! -d ${dir} ]; then  
  407.             mkdir -p $dir  
  408.         fi  
  409.         RSYNC_OPTS=" -azv "  
  410.         ${RSYNC} ${RSYNC_OPTS} ${OPTION} ${addr}::${DESTINATION} $dir >> ${logfile}  
  411.         logging 'backup' "rsync://${addr}::${DESTINATION} to ${dir}"  
  412.   
  413.         echo 'Backup Directory:' $dir  
  414.         exit  
  415.     done  
  416.   
  417. }  
  418. function cron(){  
  419.     local fun=$2      
  420.     case ${fun} in  
  421.         show)  
  422.             crontab -l  
  423.             ;;  
  424.         setup)  
  425.             cat $PREFIX/cron.d/crontab | crontab  
  426.             ;;  
  427.         edit)  
  428.             vim $PREFIX/cron.d/crontab  
  429.             cat $PREFIX/cron.d/crontab | crontab  
  430.             ;;  
  431.         *)  
  432.             usage  
  433.             ;;  
  434.     esac  
  435.   
  436. }  
  437.   
  438. function release() {  
  439.   
  440.     local domain=$2  
  441.     local host=$3  
  442.     local ver=$4  
  443.     local message=$5  
  444.   
  445.     if [ $# -lt 4 ]; then  
  446.         usage  
  447.     fi  
  448.   
  449.     if [ -z $message ]; then  
  450.     echo -n "Message: "  
  451.         read message  
  452.     fi  
  453.   
  454.     config ${domain}/${host}  
  455.   
  456.     local logfile=${LOGDIR}/${host}.${domain}.log  
  457.     project=$SRCDIR/${STAGE}/${domain}/${host}.${domain}  
  458.     cd $project  
  459.     $GIT tag ${ver} >> $logfile  
  460.       
  461.     logging 'release' "{GIT} tag ${ver} - ${message}"  
  462. }  
  463.   
  464. function stage(){  
  465.     case $1 in  
  466.         development)  
  467.             STAGE='development'  
  468.             ;;  
  469.         testing)  
  470.             STAGE='testing'  
  471.             ;;  
  472.         production)  
  473.             STAGE='production'  
  474.             ;;  
  475.         *)  
  476.         echo "STAGE ERROR"  
  477.         exit  
  478.             ;;  
  479.     esac  
  480.     echo $"STAGE=$STAGE" > $SYSCONFDIR/stage.conf && echo $STAGE  
  481.     logging 'stage' "${STAGE}"  
  482. }  
  483.   
  484. function branch(){  
  485.     local stage=$2  
  486.     local domain=$3  
  487.     local host=$4  
  488.     local branchname=$5  
  489.       
  490.     cd $SRCDIR/${stage}/${domain}/$host.${domain}  
  491.     if [ -z $branchname ]; then  
  492.         git branch  
  493.     else  
  494.         git reset HEAD --hard  
  495.         git checkout $branchname  
  496.     fi  
  497. }  
  498.   
  499. function usage(){  
  500. echo $"Usage: $0 [OPTION] <server-id> <directory/timepoint>"  
  501. echo -ne "  
  502. OPTION:  
  503.     development <domain> <host>  
  504.     testing <domain> <host>  
  505.     production <domain> <host>    
  506.       
  507.     branch {development|testing|production} <domain> <host> <branchname>  
  508.     revert {development|testing|production} <domain> <host> <revision>  
  509.     backup <domain> <host> <directory>  
  510.     release <domain> <host> <tags> <message>  
  511.       
  512.     list  
  513.     list <domain> <host>  
  514.       
  515.     clean {development|testing|production} <domain> <host>     
  516.     log <project> <line>  
  517.       
  518.     conf list     
  519.     cron show  
  520.     cron setup  
  521.     cron edit  
  522. "  
  523. #   stage {development|testing|production}  
  524. #   deploy <domain> <host>  
  525. #   revert <domain> <host> <revision>  
  526.   
  527. #   conf new <project>  
  528. #   conf remove <project>  
  529. #   conf show <project>  
  530. #   conf edit <project>      
  531.   
  532.     exit  
  533. }  
  534.   
  535. case "$1" in  
  536.     stage)  
  537.         stage $2  
  538.         ;;  
  539.     development)  
  540.         STAGE='development'  
  541.     deploy $@  
  542.         ;;  
  543.     testing)  
  544.         STAGE='testing'  
  545.         deploy $@  
  546.         ;;  
  547.     production)  
  548.         STAGE='production'  
  549.         deploy $@  
  550.         ;;  
  551.     branch)  
  552.         branch $@  
  553.         ;;  
  554.     revert)  
  555.     STAGE=$2  
  556.         revert $@  
  557.         ;;    
  558.     backup)  
  559.         backup $@  
  560.         ;;  
  561.     branch)  
  562.         branch $@  
  563.         ;;  
  564.     cron)  
  565.         cron $@  
  566.         ;;        
  567.     release)  
  568.         release $@  
  569.         ;;  
  570.     clean)  
  571.         clean $@  
  572.         ;;  
  573.     list)  
  574.         list $@  
  575.         ;;  
  576.     log)  
  577.         ls -1 $LOGDIR/*  
  578.         ;;  
  579.     conf)  
  580.         conf $@  
  581.         ;;        
  582.     *)  
  583.         usage  
  584.         exit 1  
  585.           
  586. esac  
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
2月前
|
存储 前端开发 开发工具
Git提交前的必备神器——自动清除调试语句脚本
Git提交前的必备神器——自动清除调试语句脚本
26 0
|
2月前
|
JavaScript 持续交付 开发工具
Git自动化利器:使用Node.js脚本定制化提交消息处理
Git自动化利器:使用Node.js脚本定制化提交消息处理
58 0
|
8月前
|
Shell 开发工具 git
教你写一个快速提交git代码的shell脚本(二)
教你写一个快速提交git代码的shell脚本(二)
|
8月前
|
Unix Shell Linux
教你写一个快速提交git代码的shell脚本(一)
教你写一个快速提交git代码的shell脚本(一)
教你写一个快速提交git代码的shell脚本(一)
|
8月前
|
前端开发 JavaScript Shell
手写 git hooks 脚本(pre-commit、commit-msg)
手写 git hooks 脚本(pre-commit、commit-msg)
206 0
|
NoSQL Java 关系型数据库
Centos 7 脚本命令环境部署安装JDK+Maven+Git+MySql+Redis,让你快人一步
Centos 7 脚本命令环境部署安装JDK+Maven+Git+MySql+Redis,让你快人一步
296 1
Centos 7 脚本命令环境部署安装JDK+Maven+Git+MySql+Redis,让你快人一步
|
网络安全 开发工具 git
多个项目如何往不同的git网站提交代码
多个项目如何往不同的git网站提交代码
多个项目如何往不同的git网站提交代码
|
数据可视化 Shell 开发工具
写个git提交脚本,再也不用命令行了
平时项目中我们绝大部分都是用bash命令行,或者用GUI可视化工具,无论是小乌龟还是gui工具,如果是工具比较推荐sourceTree,但是我更推荐git-fork[1],工具因人而已,无论习惯命令行还是工具,寻得自己喜欢的方式就行,没有好坏之分,也没有高低之分。
369 0
写个git提交脚本,再也不用命令行了
|
IDE Shell 开发工具
在 Swift 中编写脚本:Git Hooks
这周,我决定完成因为工作而推迟了一周的 TODO 事项来改进我的 Git 工作流程。
165 1
在 Swift 中编写脚本:Git Hooks
|
前端开发 网络安全 开发工具
Node与Git环境搭建登录网站
Node与Git环境搭建登录网站
103 0
Node与Git环境搭建登录网站