参考文献

导入代码的时候,遇到了很多困难,查阅了多方资料,最终还是完成。
值得参考的文献:
[1]: 将AOSP完整导入Gerrit Server
[2]: Repo Gerrit进阶
[3]: Repo 命令参考资料
[4]: Repo 推送一个已有的代码到新的 gerrit 服务器
[5]: 本地git项目放到gerrit仓库的三种方法

搭建Gerrit Server

gerrit安装

下载gerrit安装包至/home/gerrit/gerrit_war/目录,然后执行一下命令:

java -jar /home/gerrit/gerrit_war/gerrit-3.5.0.1.war init -d review_site

过程可以默认回车安装,之后去修改gerrit.config配置文件即可。

gerrit服务器基本信息

gerrit安装目录:/home/gerrit/review_site
gerrit代码镜像:/home/gerrit/review_site/git
gerrit配置文件:/home/gerrit/review_site/etc/gerrit.config
gerrit.config文件内容如下:(账号登录方式我采用的是ldap模式,登录方式和邮箱请自行配置)

[gerrit]
        basePath = git
        canonicalWebUrl = http://gerritbak.com/
        serverId = 74c71aa9-3f3d-4319-beb5-87d103fe31a4
[container]
        javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
        javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
        user = gerrit
        javaHome = /usr/lib/jvm/java-11-openjdk-amd64
[index]
        type = lucene
[auth]
        type = LDAP
        gitBasicAuthPolicy = HTTP
        userNameCaseInsensitive = true
[ldap]
        server = ldap://xxx.xxx.xxx.xxx
        username = cn=admin,dc=xxx,dc=xx
        accountBase = ou=peoples,dc=xxx,dc=xx
        groupBase = ou=peoples,dc=xxx,dc=xx
[receive]
        enableSignedPush = false
        maxBatchCommits = 10000000
        timeout = 120min
[sendemail]
        smtpServer = localhost
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = proxy-http://*:8080/
[cache]
        directory = cache
[sendemail]
        smtpServer = smtp.xxxxxxx.com
        smtpServerPort = 465
        smtpEncryption = ssl
        smtpUser = xxxxxx@xxx.com
        smtpPass = xxxxxx
        sslVerify = false
        from = xxxxxx<xxxxxx@xxx.com>
[download]
        command = checkout
        command = cherry_pick
        command = pull
        command = format_patch
        scheme = ssh
        scheme = http
        scheme = anon_http
        scheme = anon_git
        scheme = repo_download
[gitweb]
        type = gitweb
        cgi = /usr/lib/cgi-bin/gitweb.cgi

gerrit相关命令

gerrit服务重启命令:sudo sh /home/gerrit/review_site/bin/gerrit.sh restart
gerrit服务停止命令:sudo sh /home/gerrit/review_site/bin/gerrit.sh stop
gerrit服务开启命令:sudo sh /home/gerrit/review_site/bin/gerrit.sh start
gerrit创建仓库命令:

ssh -p 29418 gerrit@172.30.50.60 gerrit create-project --empty-commit [projectname]

apache2相关配置

apache2安装目录:/etc/apache2
apache2配置文件:/etc/apache2/sites-enabled/000-default.conf
000-default.conf文件内容如下:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName gerritbak.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined


        ProxyRequests off
        ProxyVia Off
        ProxyPreserveHost On

        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        AllowEncodedSlashes On
        ProxyPass / http://xxx.xxx.xxx.xxx:8080/  nocanon

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

apache2服务重启:sudo systemctl restart apache2

repo下载代码

repo init, 我们一般下载code的方式为:

repo init -u xxx -b yyy -m zzz.xml
repo sync -c -j8	# -c:仅获取服务器中的当前清单分支; -j:将同步操作拆分成多个线程.

注意:
-m 当有多个清单文件,可以指定清单库的某个清单文件为有效的清单文件,如果省略该参数,则用该库里默认的清单文件;

repo init命令执行后主要干了两件事:

  1. 下载repo到.repo/repo里;
  2. 下载并检出-u所指定的manifest库,并建立.repo/manifest.xml链接;
  3. 之后用repo sync 才是下载库信息到 .repo/里,并根据manifest.xml里path和name等信息切出本地代码,建立对应关系。

安卓代码完整导入Gerrit服务器

repo循环在新服务器创建代码仓库

  • 如果是repo库,执行以下命令会遍历所有目录,打印出所有的git库并输出日志pro.log
repo forall -c 'echo $REPO_PROJECT' | tee pro.log
  • 通过上一步输出的日志pro.log,写一个脚本,循环去新的gerrit服务器批量创建代码库;
#!/bin/bash

USER_NAME="gerrit"
SERVER_IP="172.30.50.60"
SERVER_PORT="29418"

function creatEmptyGerritProject()
{
     for i in cat pro.log;
     do
         echo $i
         echo "ssh -p $SERVER_PORT $USER_NAME@$SERVER_IP gerrit create-project --empty-commit $i"
         ssh -p $SERVER_PORT $USER_NAME@$SERVER_IP gerrit create-project --empty-commit $i
     done
}
creatEmptyGerritProject

或者

repo forall -c 'pwd && ssh -p 29418 gerrit@172.30.50.60 gerrit create-project --empty-commit $REPO_PROJECT'

repo循环推送代码至新的服务器

  • 代码库创建好之后,可以继续循环push代码:
(repo forall -c 'pwd && git push -f --all ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT')2>&1 | tee push.log
  • 其中,git push命令有以下这么几种,哪个好用,用哪个:
git push ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT +refs/heads/*
git push ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT +refs/heads/* +refs/tags/
git push -f --all ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT
git push --mirror ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT

Gerrit代码迁移备份方法

创建好Gerrit备份服务器之后,需要迁移备份代码至新的服务器,以下是我尝试的多种方法,请根据自己的实际情况操作。

方法1

只讲方法,不贴具体命令了,这种方式只能单个分支推送代码。

  1. git clone的方式下载源码;
  2. 切换远程仓库地址;
  3. 然后git push,将代码推送到新的gerrit服务器。

方法2

克隆源码镜像,然后git push

git clone --mirror ssh://gerrit@gerrit.com:29418/$REPO_PROJECT(源码地址)
git push --mirror ssh://gerrit@gerritbak.com:29418/$REPO_PROJECT(新地址)
git clone --mirror $code_url#下载代码到任意位置
cd #进入下载完的代码目录下
git remote set-url origin $new_url#设置远程代码库地址为新建的库的地址
git push -f origin #推送代码至新建代码库

方法3

停止备份服务器的gerrit服务

	sudo sh /home/gerrit/review_site/bin/gerrit.sh stop

克隆源码镜像至新的gerrit代码目录/home/gerrit/review_site/git;

	git clone --mirror ssh://gerrit@gerrit.com:29418/$REPO_PROJECT(源码地址)

然后执行命令重建gerrit数据库索引,命令如下:

	#review_site是gerrit的安装目录,gerrit-3.5.0.1.war是gerrit的war包
	java -jar /home/gerrit/gerrit_war/gerrit-3.5.0.1.war reindex -d review_site

开启备份服务器的gerrit服务

	sudo sh /home/gerrit/review_site/bin/gerrit.sh start

方法4

与方法3大致一样,只是直接复制源码目录至新的gerrit代码目录,然后重建gerrit索引,重启服务。

优缺点分析和总结

  1. 方法1和方法2是比较常见的上传代码的方式,适合单个代码库以及代码容量不是很大的代码库;
  2. 但是安卓的repo库,是由上百个git库组成,而且有的库很大,上传时容易由于网络或服务器配置等原因导致上传时间过长甚至上传中断和上传失败的情况,让人抓狂;
  3. 方法3和方法4更加简单易操作。

代码实时同步操作

我们需要实现Gerrit服务器与它的备份服务器之间的代码同步,即当Gerrit有代码更新时,要实现能够实时同步到Gerrit备份服务器,我采用了以下冷备份的方式:
Ubuntu使用lsyncd实现远端目录实时同步

E
N
D
END

更多推荐