版本控制 SVN基礎 實戰案例 、 RPM打包

案例1:Subversion基本操作
案例2:使用Subversion協同工作
案例3:製作nginx的RPM包
1 案例1:Subversion基本操作
1.1 問題

本案例要求先快速搭建好一臺Subversion服務器,並測試該版本控制軟件:
創建版本庫
導入初始化數據
檢出數據至用戶本地副本
對本地副本進行增刪改查等操作
1.2 方案

使用YUM安裝subversion軟件,使用svn客戶端工具連接svnserver服務器並測試版本控制軟件。
1.3 步驟

實現此案例需要按照如下步驟進行。
步驟一:安裝Subversion服務器

1)YUM安裝subversion軟件
[root@web1 ~]# yum -y install subversion
[root@web1 ~]# rpm -q subversion
2)創建版本庫
[root@web1 ~]# mkdir /var/svn/
[root@web1 ~]# svnadmin create /var/svn/project
[root@web1 ~]# ls /var/svn/project/
conf/ db/ format hooks/ locks/ README.txt
3)本地導入初始化數據
[root@web1 ~]# cd /usr/lib/systemd/system/
[root@web1 ~]# svn import . file:///var/svn/project/ -m "Init Data"
4)修改配置文件,創建賬戶與密碼
[root@web1 ~]# vim /var/svn/project/conf/svnserve.conf
[general]

These options control access to the repository for unauthenticated

and authenticated users. Valid values are "write", "read",

and "none". The sample settings below are the defaults.

anon-access = none
//19行,匿名無任何權限
auth-access = write
//20行,有效賬戶可寫

The password-db option controls the location of the password

database file. Unless you specify a path starting with a /,

the file's location is relative to the directory containing

this configuration file.

If SASL is enabled (see below), this file will NOT be used.

Uncomment the line below to use the default password file.

password-db = passwd
//27行,密碼文件

The authz-db option controls the location of the authorization

rules for path-based access control. Unless you specify a path

starting with a /, the file's location is relative to the the

directory containing this file. If you don't specify an

authz-db, no path-based access control is done.

Uncomment the line below to use the default authorization file.

authz-db = authz
//34行,ACL訪問控制列表文件

This option specifies the authentication realm of the repository.

If two repositories have the same authentication realm, they should

have the same password database, and vice versa. The default realm

is repository's uuid.

realm = My First Repository

[root@web1 ~]# vim /var/svn/project/conf/passwd
… …
[users]
harry = pass
//用戶名和密碼
tom = pass
//用戶名和密碼
[root@web1 ~]# cat /var/svn/project/conf/authz
[/] //定義ACL訪問控制
harry = rw //用戶對項目根路徑可讀可寫
tom = rw

  • = r //其他人只讀
    5)啓動服務
    [root@web1 ~]# svnserve -d -r /var/svn/project1
    [root@web1 ~]# netstat -nutlp |grep svnserve
    tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN 4043/svnserve
    步驟二:客戶端測試(192.168.2.200)

1)將服務器上的代碼下載到本地
[root@web2 ~]# cd /tmp
[root@web2 ~]# svn --username harry --password pass \
co svn://192.168.2.100/ code
//建立本地副本,從服務器192.168.2.100上co下載代碼到本地code目錄
//用戶名harry,密碼pass

ATTENTION! Your password for authentication realm:
<svn://127.0.0.1:3690> b72f45f0-bbe5-4a0c-ad4a-37f52704f0b1
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.

Store password unencrypted (yes/no)? yes //提示是否保存密碼
[root@web2 ~]# cd /tmp/code
[root@web2 code]# ls
[root@web2 code]# vim user.slice //挑選任意文件修改其內容
[root@web2 code]# svn ci -m "modify user" //將本地修改的數據同步到服務器
[root@web2 code]# svn update //將服務器上新的數據同步到本地
[root@web2 code]# svn info svn://192.168.2.100 //查看版本倉庫基本信息
[root@web2 code]# svn log svn://192.168.2.100 //查看版本倉庫的日誌
[root@web2 code]# echo "test" > test.sh //本地新建一個文件
[root@web2 code]# svn ci -m "new file" //提交失敗,該文件不被svn管理
[root@web2 code]# svn add test.sh //將文件或目錄加入版本控制
[root@web2 code]# svn ci -m "new file" //再次提交,成功
[root@web2 code]# svn mkdir subdir //創建子目錄
[root@web2 code]# svn rm timers.target //使用svn刪除文件
[root@web2 code]# svn ci -m "xxx" //提交一次代碼
[root@web2 code]# vim umount.target //任意修改本地的一個文件
[root@web2 code]# svn diff //查看所有文件的差異
[root@web2 code]# svn diff umount.target //僅查看某一個文件的差異
[root@web2 code]# svn cat svn://192.168.2.100/reboot.target //查看服務器文件的內容
[root@web2 code]# sed -i 'd' tmp.mount
//刪除文件所有內容,但未提交
[root@web2 code]# svn revert tmp.mount
//還原tmp.mount文件
[root@web2 code]# rm -rf *.target
//任意刪除若干文件
[root@web2 code]# svn update
//還原
[root@web2 code]# sed -i '1a #test###' tuned.service
//修改本地副本中的代碼文件
[root@web2 code]# svn ci -m "xxx"
//提交代碼
[root@web2 code]# svn merge -r7:2 tuned.service
//將文件從版本7還原到版本2
使用svn命令測試svnserver服務時可以使用的命令列表如表-1所示。
表-1 svn命令列表

2 案例2:使用Subversion協同工作
2.1 問題

沿用練習一,通過svn工具,對subversion版本庫進行多人協同工作測試,要求如下:
該版本庫支持多個賬戶同時協作編輯文件
測試演示多人協作編輯的具體操作
手動解決版本衝突問題
備份版本庫數據
2.2 方案

使用svn客戶端工具連接subversion服務器並測試多人協同工作以及如何手動解決衝突問題,賬戶名稱分別爲harry和tom,最後使用svnadmin dump指令對版本庫進行備份工作。
2.3 步驟

實現此案例需要按照如下步驟進行。
步驟一:多人協同工作

1)遠程連接兩個終端,每個人下載代碼本地副本,注意web1(192.168.2.100)和web2(192.168.2.200)代表了兩個不同的主機,看清楚操作是在哪一臺計算機上執行!
[root@web1 ~]# cd /tmp
[root@web1 ~]# svn --username harry --password pass \

co svn://192.168.2.100/project mycode
[root@web2 ~]# cd /tmp
[root@web2 ~]# svn --username tom --password pass \
co svn://192.168.2.100/project mycode
[root@web1 ~]# cd mycode
[root@web2 ~]# cd mycode
2) harry和tom修改不同的文件
[root@web1 mycode]# sed -i "3a ###harry modify#####" tmp.mount
[root@web1 mycode]# svn ci -m "has modified"
[root@web2 mycode]# sed -i "3a ###tom modify#####" umount.target
[root@web2 mycode]# svn ci -m "has modified"
[root@web2 mycode]# svn update
[root@web1 mycode]# svn update
3)harry和tom修改相同文件的不同行
[root@srv5 ~]# cd harry
[root@web1 mycode]# sed -i "3a ###harry modify#####" user.slice
[root@web1 mycode]# svn ci -m "modified"
[root@web2 mycode]# sed -i "6a ###tom modify#####" user.slice
[root@web2 mycode]# svn ci -m "modified" //提交失敗
Sending svnserve
Transmitting file data .svn: Commit failed (details follow):
svn: File '/user.slice' is out of date(過期)
[root@web2 mycode]# svn update //提示失敗後,先更新再提交即可
[root@web2 mycode]# svn ci -m "modified" //提交成功
Sending user.slice
Transmitting file data .
4) harry和tom修改相同文件的相同行
[root@web1 mycode]# sed -i '1c [UNIT]' tuned.service
[root@web1 mycode]# svn ci -m "modified"
[root@web2 mycode]# sed -i '1c [unit]' tuned.service
[root@web2 mycode]# svn ci -m "modified"
Sending tuned.service
Transmitting file data .svn: Commit failed (details follow):
svn: File '/tuned.service' is out of date(過期)
[root@web2 mycode]# svn update //出現衝突,需要解決
Conflict discovered in 'tuned.service'.
Select: (p) postpone, (df) diff-full, (e) edit,
(mc) mine-conflict, (tc) theirs-conflict,
(s) show all options:p //選擇先標記p,隨後解決
[root@web2 mycode]# ls
tuned.service tuned.service.mine tuned.service.r10 tuned.service.r9
[root@web2 mycode]# mv tuned.service.mine tuned.service
[root@web2 mycode]# rm -rf tuned.service.r10 tuned.service.r9
[root@web2 mycode]# svn ci -m "modified" //解決衝突
步驟二:使用dump指令備份版本庫數據

[root@web1 ~]# svnadmin dump /var/svn/project > project.bak //備份

  • Dumped revision 0.
  • Dumped revision 1.
  • Dumped revision 2.
  • Dumped revision 3.
  • Dumped revision 4.
  • Dumped revision 5.
  • Dumped revision 6.
  • Dumped revision 7.
  • Dumped revision 8.
  • Dumped revision 9.
  • Dumped revision 10.
  • Dumped revision 11.
    [root@web1 ~]# svnadmin load /var/svn/project2 < project.bak //還原
    3 案例3:製作nginx的RPM包
    3.1 問題

本案例使用nginx-1.12.2版本的源碼軟件,生產對應的RPM包軟件,具體要求如下:
軟件名稱爲nginx
軟件版本爲1.12.2
RPM軟件包可以查詢描述信息
RPM軟件包可以安裝及卸載
3.2 方案

安裝rpm-build軟件包,編寫SPEC配置文件,創建新的RPM軟件包。
配置文件中的描述信息如表-2:
表-2 SPEC描述信息

3.3 步驟

實現此案例需要按照如下步驟進行。
步驟一:安裝rpm-build軟件

1)安裝rpm-build軟件包
[root@web1 ~]# yum -y install rpm-build
2)生成rpmbuild目錄結構
[root@web1 ~]# rpmbuild -ba nginx.spec //會報錯,沒有文件或目錄
[root@web1 ~]# ls /root/rpmbuild //自動生成的目錄結構
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
3)準備工作,將源碼軟件複製到SOURCES目錄
[root@web1 ~]# cp nginx-1.12.2.tar.gz /root/rpmbuild/SOURCES/
4)創建並修改SPEC配置文件
[root@web1 ~]# vim /root/rpmbuild/SPECS/nginx.spec
Name:nginx
Version:1.12.0
Release: 10
Summary: Nginx is a web server software.
License:GPL
URL: www.test.com
Source0:nginx-1.12.2.tar.gz
#BuildRequires:
#Requires:
%description
nginx [engine x] is an HTTP and reverse proxy server.
%prep
%setup –q //自動解壓源碼包,並cd進入目錄
%build
./configure
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
cp /root/rpmbuild/SPECS/nginx.sh %{buildroot}/usr/local/nginx/
##注意,cp非必須操作,注意,這裏是將一個腳本拷貝到安裝目錄,必須提前準備該文件
%files
%doc
/usr/local/nginx/* //對哪些文件與目錄打包
%changelog
步驟二:使用配置文件創建RPM包

1)安裝依賴軟件包
[root@web1 ~]# yum -y install gcc pcre-devel zlib-devel openssl-devel
2)rpmbuild創建RPM軟件包
[root@web1 ~]# rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec
[root@web1 ~]# ls /root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm
[root@web1 ~]# rpm -qpi RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm
Name : nginx Relocations: (not relocatable)
Version : 1.12.2 Vendor: (none)
Release : 10 Build Date: Mon 02 May 2016 02:30:53 AM PDT
Install Date: (not installed) Build Host: localhost
Group : Applications/Internet Source RPM: nginx-1.8.0-1.src.rpm
Size : 721243 License: GPL
Signature : (none)
URL : www.nginx.org
Summary : Nginx is a web server software.
Description :
nginx [engine x] is an HTTP and reverse proxy server.
[root@web1 ~]# rpm -qpl nginx-1.12.2-10.x86_64.rpm
/usr
/usr/local
/usr/local/nginx
/usr/local/nginx/conf
/usr/local/nginx/conf/fastcgi.conf
/usr/local/nginx/conf/fastcgi.conf.default
/usr/local/nginx/conf/fastcgi_params
/usr/local/nginx/conf/fastcgi_params.default
/usr/local/nginx/conf/koi-utf
/usr/local/nginx/conf/koi-win
/usr/local/nginx/conf/mime.types
/usr/local/nginx/conf/mime.types.default
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.default
/usr/local/nginx/conf/scgi_params
/usr/local/nginx/conf/scgi_params.default
/usr/local/nginx/conf/uwsgi_params
/usr/local/nginx/conf/uwsgi_params.default
/usr/local/nginx/conf/win-utf
/usr/local/nginx/html
/usr/local/nginx/html/50x.html
/usr/local/nginx/html/index.html
/usr/local/nginx/logs
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx
步驟三:安裝、卸載軟件

[root@web1 ~]# rpm -ivh RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm
[root@web1 ~]# rpm -qa |grep nginx
[root@web1 ~]# /usr/local/nginx/sbin/nginx
[root@web1 ~]# curl http://127.0.0.1/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章