作用

生成SVN仓库,Web服务器的配置文件和钩子程序


参数说明

 

SVN_ROOT SVN的根目录

SVNADMIN_BIN svnadmin的路径

SVN_BIN svn的路径

SVN_USER 用来更新SVN的用户名

SVN_PWD 用来更新SVN的密码

 

WEB_ETC Apachehost配置目录

WEB_ROOT ApacheWeb根目录

WEB_USER Apache的运行User

WEB_GROUP Apache的运行Group

 

HOST_PREFIX 生成的Web域名加的前缀

HOST_SUFFIX 生成的Web域名加的后缀

 
使用方法:
在脚本的同目录下建立conf文件夹,放入模板文件
SVN:  svnserve.conf  authz  passwd
SVN钩子:  post-commit
Web服务器的host配置文件: host.conf
 
模板例子:
1. host.conf
请保留替换参数  PROJECT  PROJECT_HOST
<VirtualHost *:80>
        ServerAdmin shenhd@ableengine.com
        DocumentRoot "/var/www/html/PROJECT"
        ServerName www.PROJECT_HOST.com
        <Directory "/var/www/html/PROJECT">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>
2. post-commit
请保留替换参数 SVN_USER  SVN_PWD PROJECT_ROOT
#!/bin/bash
export LC_CTYPE=zh_CN.UTF-8
su www -c "svn update --username SVN_USER --password SVN_PWD PROJECT_ROOT"

 

代码如下
#!/bin/bash
###################################################################
#                         deploy script                           #
#   一个简单的部署脚本,在创建Apache虚拟主机配置同时,添加SVN仓 #
# 库并做一个钩子自动更新到Apache Web根目录                        #
#                                                     2013-03-09  #
#                                                         shenhd  #
###################################################################
 
##### define variables #####
PROJECT=$1
 
SVN_ROOT=PATH/svn
SVNADMIN_BIN=/usr/bin/svnadmin
SVN_BIN=/usr/bin/svn
SVN_USER=USER
SVN_PWD=PASSWORD
 
WEB_ETC=APACHE_CONF_DIR
WEB_ROOT=APACHE_HTDOC_DIR
WEB_USER=www
WEB_GROUP=www
 
HOST_PREFIX=
HOST_SUFFIX=
##### define variables #####
 
if [ "${UID}" != "0" ]
then
echo Please grant it root privileges
exit
elif [ "${PROJECT}" == "" ]
then
echo Please specify a project name
exit
fi
 
function create_svn() {
SVN_REPO=${SVN_ROOT}/${PROJECT}
# create project svn repository
$SVNADMIN_BIN create ${SVN_REPO}
# backup config files
mv ${SVN_REPO}/conf/svnserve.conf ${SVN_REPO}/conf/svnserve.conf.orgin
mv ${SVN_REPO}/conf/authz ${SVN_REPO}/conf/authz.orgin
mv ${SVN_REPO}/conf/passwd ${SVN_REPO}/conf/passwd.orgin
 
cp ./conf/svnserve.conf ${SVN_REPO}/conf/
cp ./conf/authz ${SVN_REPO}/conf/
sed -i "s/PROJECT/${PROJECT}/g" ${SVN_REPO}/conf/authz
cp ./conf/passwd ${SVN_REPO}/conf/
 
if [ ! -f "${SVN_REPO}/conf/authz" ] || [ ! -f "${SVN_REPO}/conf/passwd" ] \
|| [ ! -f "${SVN_REPO}/conf/svnserve.conf" ]
then
echo Cannot change configure files for svn
exit
fi
}
 
function create_host() {
PROJECT_ETC=${WEB_ETC}/${PROJECT}.conf
PROJECT_ROOT=${WEB_ROOT}/${PROJECT}
mkdir -p ${PROJECT_ROOT}
 
if [ ! -d ${PROJECT_ROOT} ]
then
echo Cannot create project htdoc root
exit
fi
# change directory owner
chown -R ${WEB_USER}:${WEB_GROUP} ${PROJECT_ROOT}
 
cp ./conf/host.conf ${PROJECT_ETC}
sed -i "s/PROJECT_HOST/${HOST_PREFIX}${PROJECT}${HOST_SUFFIX}/g" ${PROJECT_ETC}
sed -i "s/PROJECT/${PROJECT}/g" ${PROJECT_ETC}
}
 
function hook_svn() {
${SVN_BIN} checkout svn://127.0.0.1/${PROJECT} --username ${SVN_USER} --password ${SVN_PWD} ${PROJECT_ROOT}
 
cp ./conf/post-commit ${SVN_REPO}/hooks/
chmod +x ${SVN_REPO}/hooks/post-commit
HOOK=${SVN_REPO}/hooks/post-commit
sed -i "s/SVN_USER/${SVN_USER}/g" ${HOOK}
sed -i "s/SVN_PWD/${SVN_PWD}/g" ${HOOK}
WEBROOT=${PROJECT_ROOT//\//\\\/}
sed -i "s/PROJECT_ROOT/${WEBROOT}/g" ${HOOK}
}
 
create_svn
create_host
hook_svn
 
echo Reload your web server.