gerrit replication

来源:互联网 发布:淘宝衣服的卖点范文 编辑:程序博客网 时间:2024/05/01 18:55

看了很多文档,只能说在网上完全找不到一个真正有用的文档,有很多问题还必须翻墙用谷歌找答案,国内基本上是没有相关的实例,而且官网上的文档写的十分不详细,每次看的都是一头雾水。下面是我设置主从库的过程:

1. Gerrit服务器配置

gerrit服务器A(主):

地址:192.168.3.178

Http访问:192.168.3.178:8087

Gerrit认证方式:development_become_any_account这是http访问gerrit服务器的认证方式其他的还有openid等方式。

ssh端口:29418

注意在选择是否下载replication pligin选项的时候默认是不下载的,这时候要在选项后面写为y才会下载。



Gerrit服务器B(从):

地址:192.168.4.64

Http访问:192.168.3.178:8087

Gerrit认证方式:development_become_any_account

ssh端口:29418

通过ssh的29418端口访问B:ssh -p 29418 sunzhen@192.168.4.64

为了简单起见我的两个管理员账户都是sunzhen

这时候gerrit开始后,在主机A上访问http://192.168.4.64:8087。然后加入自己的公钥。这个是为了把A的公钥加入到B中,通过port 29418访问B。

2. replicattion.config

gerrit目录下的etc目录中新建一个replication.config。这样你重新加载replication插件时就会读取该文件。

在官网上repliction.config如下:

[remote "host-one"]    url = gerrit2@host-one.example.com:/some/path/${name}.git[remote "pubmirror"]    url = mirror1.us.some.org:/pub/git/${name}.git    url = mirror2.us.some.org:/pub/git/${name}.git    url = mirror3.us.some.org:/pub/git/${name}.git    push = +refs/heads/*:refs/heads/*    push = +refs/tags/*:refs/tags/*    threads = 3    authGroup = Public Mirror GroupauthGroup = Second Public Mirror Group


开始我以为这个第一个[remote "host-one"]写自己的主机A的地址。后来发现这样写的话他其实也是镜像库的主机,与下面的mirror1mirror2mirror3没有区别。既然如此,我不知道为什么它要这么写,显得host-one与众不同。下面是我的repliction.config文件。

[remote "192.168.4.64"]  url = ssh://sunzhen@192.168.4.64:29418/${name}.git  push = +refs/heads/*:refs/heads/*  push = +refs/tags/*:refs/tags/*

这是一个最简单的配置,许多设置都采用的默认端口。

另外要说的是,url中的端口一定要是29418。之前有看到一个人的配置文件中这样写的:

url = ssh://gerrit2@slave/srv/gerrit2/git/${name}.git

这个意思是说使用默认的22端口访问相当于ssh sun@192.168.3.178。这么做我不知道他是怎么做出来的,我做了很多遍没做出来,如果有人这么做出来的话还请赐教我一下。

然后开启AB。在Agerrit页面创建项目repo1repo2。在Bgerrit页面创建项目repo2。然后输入如下指令:

sun@sun-OptiPlex-7010:~$scp -r gerrit/git/repo1.git zhulihuang@192.168.4.64:/home/zhulihuang/gerrit/git

该指令将A中的repo.git加入B中的git库下,然后重启B中的gerrit。这时候发现Bgerrit页面也有repo1

做完之后在AB都开启的情况下。在主机A下面输入

sun@sun-OptiPlex-7010:~$ ssh -p 29418 sunzhen@localhost gerrit plugin reload replicationsun@sun-OptiPlex-7010:~$ ssh -p 29418 sunzhen@localhost replication start  --all

第一句的意思是,因为我们修改了replication.config文件,所以要重新加载replication插件。第二句的就是开始复制指令。

这时候查看A下面的gerrit目录下面的error_log文件。可以看到如下信息:

[2014-09-29 15:04:34,327] INFO  com.google.gerrit.server.plugins.PluginLoader : Reloading plugin replication[2014-09-29 15:04:34,374] INFO  com.google.gerrit.server.plugins.PluginLoader : Unloading plugin replication[2014-09-29 15:05:34,653] INFO  com.google.gerrit.server.plugins.PluginLoader : Cleaned plugin plugin_replication_140929_1458_5386789366483874484.jar[2014-09-29 15:05:42,823] INFO  com.googlesource.gerrit.plugins.replication.ReplicationQueue : Push to ssh://sunzhen@192.168.4.64:29418/repo2.git references: [RemoteRefUpdate[remoteName=refs/heads/master, NOT_ATTEMPTED, (null)...601088ca216c62794be86065c45bbc71a5e6cd26, srcRef=refs/heads/master, forceUpdate, message=null]][2014-09-29 15:05:42,960] ERROR com.googlesource.gerrit.plugins.replication.ReplicationQueue : Failed replicate of refs/heads/master to ssh://sunzhen@192.168.4.64:29418/repo2.git, reason: non-fast forward[2014-09-29 15:11:58,633] INFO  com.googlesource.gerrit.plugins.replication.ReplicationQueue : Push to ssh://sunzhen@192.168.4.64:29418/repo1.git references: [RemoteRefUpdate[remoteName=refs/heads/master, NOT_ATTEMPTED, (null)...fdb7a98b21c53545977e01e2c83a0c6a9359d160, srcRef=refs/heads/master, forceUpdate, message=null]]

这说明,A中的repo1.git成功的复制到B中,而repo2.git却不能够。所以,我们不能在B中通过创建与A中同名的git库来复制,必须把A中创建的.git文件夹复制到B中才行。

这时候我们拉下A中的repo1代码,然后修改repo1之后使用git push 提交。当我们在192.168.3.178:8087确认提交并submit后,gerrit会自动触发replication插件,把它复制到B中。这之后我们拉下B中的repo1就会发现里面已经包含了A中的改变。

当然这只是最简单的情况,后续还会有很多设置的修改。

 

3. gerrit代码库实现push和pull的目标位不同主机

现在我想做的是当上传代码时代码不能上传到B主机,必须上传到A中,然后代码的pull操作是从B主机pull下来的。这时候要分为两步:

一:修改B主机的gerrit权限,使B中gerrit库除了管理员之外只能下拉代码而不能上传代码,下图是我修改的主机A和主机B的权限设置(在ALL-projects下):

主机A的设置:

主机B的设置:

设置完成后,当你在B(192.168.4.64)下使用非管理员账户执行git push语句时,会提示你错误信息。

二:编写一个config.sh脚本,该脚本有两个参数push和pull,该脚本大致的作用是:当使用./config.sh push命令时,该脚本自动修改参数,使git push的对象指向主机A(192.168.3.178),当使用./config.sh pull命令时,该脚本则修改相关参数使得该pull对象指向主机B。这就实现了从B下载代码,从A上传代码,然后A中代码库的改变又复制到B。

config.sh代码如下:

#!/bin/bash## Purpose: git push to master and pull from mirror`s repository# Date:    2014-10-9# Author:  sun.zhen@kortide.com.cn#if [ "$1" == "push" ]then fullname_old=`git config --get user.name`fullname=read -p "Your Full Name In 192.168.3.178:8087[$fullname_old?]: " fullnameif [ -z "$fullname" ]; then    fullname="$fullname_old"figit config --global user.name "$fullname"echosed -i '7d' .git/configsed '7 iurl = ssh://'$fullname'@192.168.3.178:29418/repo1' -i .git/configemail_old=`git config --get user.email`read -p "Your Email  In 192.168.3.178:8087[$email_old?]: " emailif [ -z "$email" ]; then    email="$email_old"figit config --global user.email "$email"echogit config --global color.diff autogit config --global color.status autogit config --global color.branch autogit config --global remote.origin.push refs/heads/*:refs/for/*echo Your Config Now :echo git config -lecho 'Now you can push your changes to master'echo elif [ "$1" == "pull" ]then fullname_old=`git config --get user.name`fullname=read -p "Your Full Name In 192.168.4.64:8087[$fullname_old?]: " fullnameif [ -z "$fullname" ]; then    fullname="$fullname_old"figit config --global user.name "$fullname"echosed -i '7d' .git/configsed '7 iurl = ssh://'$fullname'@192.168.4.64:29418/repo1' -i .git/configemail_old=`git config --get user.email`read -p "Your Email  In 192.168.4.64:8087[$email_old?]: " emailif [ -z "$email" ]; then    email="$email_old"figit config --global user.email "$email"echogit config --global color.diff autogit config --global color.status autogit config --global color.branch autogit config --global remote.origin.push refs/heads/*:refs/for/*echo Your Config Now :echo git config -lecho 'Now you can pull from master'echo else echo "please make sure your options is push or pull"fi 

0 0
原创粉丝点击