系統環境
RHEL5.4最小化安裝(關iptables,關selinux) + ssh + yum
一,安裝必須的軟件包.
yum install subversion (SVN服務器)
mysql-server (用於codestriker)
httpd mod_dav_svn mod_perl (用於支持WEB方式管理SVN服務器)
sendmail (用於配置用戶提交代碼後發郵件提醒)
wget gcc-c++ make unzip perl* (必備軟件包)
ntsysv vim-enhanced (可選)
二,基本的SVN服務器配置
1,新建一個目錄用於存儲SVN全部文件
# mkdir /home/svn
2,新建一個版本倉庫
# svnadmin create /home/svn/project
3,初始化版本倉庫中的目錄
# mkdir project project/server project/client project/test (創建臨時目錄)
# svn import project/ file:///home/svn/project -m "初始化SVN目錄"
# rm -rf project (刪除臨時創建的目錄)
4,添加用戶
要添加SVN用戶很是簡單,只需在/home/svn/project/conf/passwd文件添加一個形如「username=password"的條目就能夠了.爲了測試,我添加了以下內容:
[users]
# harry = harryssecret
# sally = sallyssecret
pm = pm_pw
server_group = server_pw
client_group = client_pw
test_group = test_pw
5,修改用戶訪問策略
/home/svn/project/conf/authz記錄用戶的訪問策略,如下是參考:
[groups]
project_p = pm
project_s = server_group
project_c = client_group
project_t = test_group
[project:/]
@project_p = rw
* =
[project:/server]
@project_p = rw
@project_s = rw
* =
[project:/client]
@project_p = rw
@project_c = rw
* =
[project:/doc]
@project_p = rw
@project_s = rw
@project_c = rw
@project_t = rw
* =
以上信息表示,只有pm有根目錄的讀寫權,server_group能訪問server目錄,client_group能訪問client目錄,全部人均可以訪問doc目錄.
6,修改svnserve.conf文件,讓用戶和策略配置升效.
svnserve.conf內容以下:
[general]
anon-access = none
auth-access = write
password-db = /home/svn/project/conf/passwd
authz-db = /home/svn/project/conf/authz
7,啓動服務器
# svnserve -d -r /home/svn
8,測試服務器
# svn co svn://192.168.60.10/project
Authentication realm: <svn://192.168.60.10:3690> 92731041-2dae-4c23-97fd-9e1ed7f0d18d
Password for 'root':
Authentication realm: <svn://192.168.60.10:3690> 92731041-2dae-4c23-97fd-9e1ed7f0d18d
Username: server_group
Password for 'server_group':
svn: Authorization failed ( server_group沒用根目錄的訪問權 )
# svn co svn://192.168.60.10/project
Authentication realm: <svn://192.168.60.10:3690> 92731041-2dae-4c23-97fd-9e1ed7f0d18d
Password for 'root':
Authentication realm: <svn://192.168.60.10:3690> 92731041-2dae-4c23-97fd-9e1ed7f0d18d
Username: pm
Password for 'pm':
A project/test
A project/server
A project/client
Checked out revision 1. ( 測試提取成功 )
# cd project/server
# vim main.c
# svn add main.c
# svn commit main.c -m "測試一下個人C程序,看什麼看,不行啊??"
Adding main.c
Transmitting file data .
Committed revision 2. ( 測試提交成功 )
三,配置SVN服務器的HTTP支持
1,轉換SVN服務器的密碼
因爲SVN服務器的密碼是明文的,HTTP服務器不與支持,因此須要轉換成HTTP支持的格式。我寫了一個Perl腳本完成這個工做.
腳本內容以下:
# cd /home/svn/project/conf/
# cat PtoWP.pl
#!/usr/bin/perl
# write by huabo, 2009-11-20
use warnings;
use strict;
#open the svn passwd file
open (FILE, "passwd") or die ("Cannot open the passwd file!!!\n");
#clear the apache passwd file
open (OUT_FILE, ">webpasswd") or die ("Cannot open the webpasswd file!!!\n");
close (OUT_FILE);
#begin
foreach (<FILE>) {
if($_ =~ m/^[^#].*=/) {
$_ =~ s/=//;
`htpasswd -b webpasswd $_`;
}
}
# ./PtoWP.pl ( 先給該腳本加可執行權限,而後執行以轉換密碼 )
Adding password for user pm
Adding password for user server_group
Adding password for user client_group
Adding password for user test_group
如今目錄下會多一個webpasswd文件。
2,修改httpd.conf,添加關於SVN服務器的內容
編輯/etc/httpd/conf/httpd.conf,在最後添加以下信息:
<Location /project>
DAV svn
SVNPath /home/svn/project/
AuthType Basic
AuthName "svn for project"
AuthUserFile /home/svn/project/conf/webpasswd
AuthzSVNAccessFile /home/svn/project/conf/authz
Satisfy all
Require valid-user
</Location>
3,啓動HTTPD服務器
# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
4,用瀏覽器訪問http://192.168.60.10/project/server/測試
測試結果以下圖所示:html
( 測試成功 )
四,配置郵件提醒支持
1,安裝Perl模塊Module::Build
# wget http://search.cpan.org/CPAN/authors/id/D/DA/DAGOLDEN/Module-Build-0.36_11.tar.gz
# tar xvf Module-Build-0.36_11.tar.gz
# cd Module-Build-0.36_11
# perl Build.PL
# ./Build
# ./Build test
# ./Build install
# cd ..
2,安裝Perl模塊Authen::SASL
# wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/Authen-SASL-2.15.tar.gz
# tar xvf Authen-SASL-2.15.tar.gz
# cd Authen-SASL-2.15
# perl Makefile.PL
# make test
# make install
# cd ..
3,安裝Perl模塊Net::SMTP_auth
# wget http://search.cpan.org/CPAN/authors/id/A/AP/APLEINER/Net-SMTP_auth-0.08.tar.gz
# tar xvf Net-SMTP_auth-0.08.tar.gz
# cd Net-SMTP_auth-0.08
# perl Makefile.PL
# make test
# make install
# cd ..
4,安裝Perl模塊SVN::Notify
# wget http://search.cpan.org/CPAN/authors/id/D/DW/DWHEELER/SVN-Notify-2.80.tar.gz
# tar xvf SVN-Notify-2.80.tar.gz
# cd SVN-Notify-2.80
# perl Build.PL
# ./Build
# ./Build test
# ./Build install
# cd ..
5,啓動郵件服務器
# service sendmail restart
Shutting down sendmail: [FAILED]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
6,配置自動發郵件腳本
修改post-commit腳本,以支持郵件通知功能.
# cd /home/svn/project/hooks/
# vim post-commit
內容以下:
#!/bin/sh
REPOS="$1"
REV="$2"
/usr/bin/svnnotify --repos-path "$1" --revision "$2" --to caodaijun@pica.com --from caodaijun@feinno.com --handler "HTML::ColorDiff" --with-diff --smtp localhost --smtp-user root --smtp-pass 5201314318 -c "UTF-8" -g zh_CN -o raw --svnlook /usr/bin/svnlook --subject-prefix '[SVN Update]'
(to參數表明接收郵件的地址,能夠有多個,當你有多個老大的時候,這就很重要了,:)。from參數是虛擬的,表明你的發送地址,通常狀況下,這個參數 不重要,但若是接收者的郵件服務器有反垃圾郵件的功能,須要斷定源地址的話,這個參數是否合法就顯得很重要了)
再給該腳本添加可執行權限
# chmod +x post-commit
7,再次提交時,就會給指定郵件地址發信了。
以下圖所示:java
五,其它經常使用配置
1,強制寫log腳本
配置pre-commit文件,要求用戶每次更新文件都必須寫log.
# cd /home/svn/project/hooks/
# vim pre-commit
文件內容以下:
#!/bin/sh
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`
if [ "$LOGMSG" -lt 5(要求的log長度,依實際須要修改) ];
then
echo -e "\nEmpty log message not allowed. Commit aborted!" 1>&2
exit 1
fi
配置完成後,給本件加上可執行權限。再提交代碼時,就必須按要求寫註釋了,:)
2,可修改log腳本
配置pre-revprop-change文件,此文件在show log中修改log時會運行,獲得修改的權限,不然會報錯:DAV request failed; it's possible that the repository's pre-revprop-change hook either failed or is non-existent. At least one property change failed; repository is unchanged
# cd /home/svn/project/hooks/
# vim pre-revprop-change
文件內容以下:
REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
if ["$PROPNAME" = "svn:log"];then exit 0;fi
exit 1
配置完後加可執行權限升效。
六,備份管理
svn服務器的按期備份是很重要的,最簡單的方式是定時備份倉庫目錄。
1,新建備份目錄
# mkdir /opt/project_backup
2,編寫備份腳本
# cd /home/svn/
# vim project_backup.sh
內容以下:
#!/bin/bash
#write by huabo, 2009-11-20
cd /home/svn
now=`/bin/date +%Y%m%d`
/bin/tar czvf "project_backup_$now.tar.gz" project/ && rm -rf /opt/project_backup/* && /bin/mv project_backup_*.tar.gz /opt/project_backup/
if [ $? == 0 ]
then
result="OK!!"
else
result="False!!"
fi
#send mail to administrator
/bin/mail caodaijun@pica.com -s "project_backup_$now" <<MESSAGE
Result: `/bin/echo $result`
MESSAGE
給該腳本添加可執行權限。
3,設定天天定時執行該腳本.
# crontab -e
輸入以下內容:
0 23 * * * /home/svn/project_backup.sh
表示天天晚上23點運行此腳本。
通過以上三步操做,就能夠自動備份SVN資料了,且不論備份是否成功,都會給用戶發送郵件信息。
七,用svnstat分析SVN數據.
1,安裝JAVA
svnstat是JAVA應用程序,須要先安裝JAVA環境。
下載jre,URL: http://javadl.sun.com/webapps/download/AutoDL?BundleId=39484
安裝:
# chmod +x jre-6u20-linux-i586-rpm.bin
# ./jre-6u20-linux-i586-rpm.bin
2,下載svnstat
# wget http://downloads.sourceforge.net/project/svnstat/svnstat/Release-1.0/SvnStat-1.0.zip?use_mirror=jaist
# unzip SvnStat-1.0.zip
3,更新代碼
# pwd
/root
# svn co svn://192.168.60.10/project
A project/test
A project/server
A project/server/main.c
A project/client
Checked out revision 5.
4,生成svnstat數據
# svn log project -v --xml --non-interactive > project.log
# cd SvnStat-1.0
# java -classpath SvnStat-all.jar de.agentlab.svnstat.SvnStat -jar SvnStat-all.jar -r /root/project.log -d /var/www/html/
5,用瀏覽器登陸便可看到許多統計出來的圖表。大體以下圖所示:mysql
八,用statsvn分析SVN數據
1,下載statsvn
# wget http://downloads.sourceforge.net/project/statsvn/statsvn/0.7.0/statsvn-0.7.0.zip?use_mirror=jaist
# unzip statsvn-0.7.0.zip
# cd statsvn-0.7.0
2,生成statsvn數據
# mkdir /var/www/html/statsvn
# java -jar statsvn.jar -verbose -output-dir /var/www/html/statsvn/ /root/project.log /root/project
3,用瀏覽器測測試效果以下圖:linux
九,配置codestriker.
1.安裝codestriker依賴的perl包.
# perl -MCPAN -e 'install "Template"'
2.下載codestriker
# wget http://downloads.sourceforge.net/project/codestriker/codestriker/1.9.10/codestriker-1.9.10.tar.gz?use_mirror=jaist&ts=1279246587
# mkdir /var/www/codestriker
# cd /var/www/codestriker
# tar xvf /path/codestriker-1.9.10.tar.gz
# chown -R apache.apache codestriker-1.9.10
3.配置數據庫
# service mysqld restart
# mysql -uroot mysql
執行:
CREATE DATABASE codestrikerdb CHARACTER SET utf8;
GRANT SELECT,INSERT,UPDATE,DELETE,INDEX,ALTER,CREATE,DROP,REFERENCES ON codestrikerdb.* TO codestriker@localhost IDENTIFIED BY 'cspasswd';
FLUSH PRIVILEGES;
quit
4.配置codestriker
# cd codestriker-1.9.10/
# vim codestriker.conf
注意如下幾點(詳細可查看codestriker的安裝文檔)
a.數據庫的用戶名密碼要配對
b.svn的數據倉庫要配對,個人以下:
@valid_repositories =
(
'svn:file:///home/svn/project',
)
5.執行codestriker的安裝腳本
# cd bin/
# ./install.pl
6.配置http支持
# vim /etc/httpd/conf/httpd.conf
在最後面加上以下內容:
Alias /codestriker/ /var/www/codestriker/codestriker-1.9.10/cgi-bin/
Alias /codestrikerhtml/ /var/www/codestriker/codestriker-1.9.10/html/
<Directory "/var/www/codestriker/codestriker-1.9.10/cgi-bin/">
SetHandler perl-script
PerlHandler ModPerl::Registry
Options +ExecCGI
</Directory>
<Directory "/var/www/codestriker/codestriker-1.9.10/html/">
AllowOverride None
Allow from all
</Directory>
7.重啓HTTP服務器
# service httpd restart
8.在瀏覽器中輸入http://192.168.60.10/codestriker/codestriker.pl便可訪問,以下圖:c++