分类: 系统运维
2014-03-12 16:15:59
Gitolite hooks mirror实现
----Kenny Song
Gitolite系统基于perl编写,工作原理如下:
#读取
git clone --> ssh -- // à sshd à git-upload-pack
git fetch à ssh -- // à sshd à git-upload-pack
#写入
git push à ssh -- // à sshd à git-receive-pack
Gitolite 中包含很多可以选择的sub-command,其中最有用的莫过于mirror指令。
目前我们从Site 的同步的逻辑是:建立在分支上,通过fetch数据到本地,然后再push到服务器上。这样做有如下问题:
1,增加服务器的负载,(fetch和push),2,不能实时同步,
通过gitolite的hooks中的post-recive脚本可以实现基于库的自动同步,设置脚本后当用户在他的桌面执行git push后,本地的gitolite接收数据并更新ref后,触发post-receive脚本,把他的这次提交push到已经设置了mirror的slave机器上。具体过程如下:
这样做的好处:
1, 实时同步,在用户提交后即刻同步到remote机器上,时效性更强
2, 降低服务器负载,减少了fetch的过程,
3, 降低了带宽的使用量,每次提交较少,带宽占用就少,批量同步会持续的占用带宽,并且每次同步都要拉取增个branch。
具体实现步骤如下:
上图中User 为用户, squidaccout为local repository, gitmirror 为remote repository ,默认local 和remote 都已经在git账户下安装gitolite 3.x,并且各自运行良好,测试的repo为:mirrortest
设置local repository:
[git@squidaccount ~]$ ssh-keygen
[git@squidaccount ~]$ cp .ssh/id_rsa.pub server-squidaccount.pub
修改.gitolite.rc 修改成如下:
$ENV{PATH}="/usr/local/git/bin:$ENV{PATH}";
%RC = (
UMASK => 0077,
GIT_CONFIG_KEYS => '/data/git/.ssh/id_rsa.pub',
LOG_EXTRA => 1,
ROLES => {
READERS => 1,
WRITERS => 1,
},
HOSTNAME => "squidaccount",
ENABLE => [
'help',
'desc',
'info',
'perms',
'writable',
'mirror',
'ssh-authkeys',
'git-config',
'daemon',
'gitweb',
'Mirroring',
'ssh-authkeys-split',
],
);
1;
设置gitolite.conf文件中的repo mirrortest 如下:
repo mirrortest
RW+ = @all 权限可以分别设置
option mirror.master = squidaccount
option mirror.slaves = gitmirror
option mirror.redirectOK = all
设置remote repository:
[git@gitmirror ~]$ ssh-keygen
[git@gitmirror ~]$ cp .ssh/id_rsa.pub gitmirror.pub
修改.gitolite.rc 修改成如下:
%RC = (
UMASK => 0077,
GIT_CONFIG_KEYS => '',
LOG_EXTRA => 1,
ROLES => {
READERS => 1,
WRITERS => 1,
},
HOSTNAME => "gitmirror",
ENABLE => [
'help',
'desc',
'info',
'perms',
'writable',
'mirror',
'ssh-authkeys',
'git-config',
'daemon',
'gitweb',
'Mirroring',
'ssh-authkeys-split',
],
);
1;
设置gitolite.conf文件中的repo mirrortest 如下:
repo mirrortest
RW+ = @all 权限可以分别设置
option mirror.master = squidaccount
option mirror.slaves = gitmirror
option mirror.redirectOK = all
把两台机子的key都添加到gitolite-admin中。确认两台机子都可互相访问:
[git@squidaccount ~]$ ssh git@gitmirror info
hello server-squidaccount, this is git@gitmirror running gitolite3 v3.5.3.1-11-g8f1fd84 on git 1.8.0
R W mirrortest
R W testing
[git@gitmirror ~]$ ssh git@squidaccount info
hello gitmirror, this is git@squidaccount running gitolite3 v3.5.3.1-11-g8f1fd84 on git 1.8.0
R W mirrortest
R W testing
设置同步hooks
编辑.ssh/config文件:
[git@squidaccount ~]$ cat .ssh/config
Host gitmirror
HostName gitmirror
User git
IdentityFile ~/.ssh/id_dsa.pub
[git@squidaccount ~]$ chmod 644 .ssh/config
设置post-receive
[git@squidaccount hooks]$ pwd
/data/git/repositories/mirrortest.git/hooks
[git@squidaccount hooks]$ ls
applypatch-msg.sample post-receive pre-applypatch.sample prepare-commit-msg.sample update
commit-msg.sample post-update.sample pre-commit.sample pre-rebase.sample update.sample
[git@squidaccount hooks]$
修改post-receive:
#!/bin/bash
source /etc/profile
#ssh -v gitmirror info
gitolite mirror push gitmirror mirrortest
[git@squidaccount hooks]$ chmod 755 post-receive
设置完成。
测试运行如下:
[kennys@squid mirrortest]$ ls
get_current_state.pl send_current_system_state.pl testing
mirrortest test.file
[kennys@squid mirrortest]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@squidaccount:mirrortest
[branch "master"]
remote = origin
merge = refs/heads/master
[kennys@squid mirrortest]$ touch gittest
[kennys@squid mirrortest]$ echo "Kenny Test" >> gittest
[kennys@squid mirrortest]$ cat gittest
Kenny Test
[kennys@squid mirrortest]$ git add .
[kennys@squid mirrortest]$ git commit -m "Kenny Test"
[master c3ddd37] Kenny Test
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 gittest
[kennys@squid mirrortest]$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: To gitmirror:mirrortest
remote: 33580ab..c3ddd37 master -> master
To git@squidaccount:mirrortest
33580ab..c3ddd37 master -> master
[kennys@squid mirrortest]$
Remote服务器中的mirrortest
[git@gitmirror ~]$ cd mirrortest/
[git@gitmirror mirrortest]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@gitmirror:mirrortest
[branch "master"]
remote = origin
merge = refs/heads/master
[git@gitmirror mirrortest]$ git pull
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (12/12), done.
From 10.128.161.165:mirrortest
0c12803..c3ddd37 master -> origin/master
Updating 0c12803..c3ddd37
Fast-forward
gittest | 1 +
test20140312 | 3 +++
2 files changed, 4 insertions(+)
create mode 100644 gittest
create mode 100644 test20140312
[git@gitmirror mirrortest]$ cat gittest
Kenny Test
[git@gitmirror mirrortest]$
参考链接: