CI 系统搭建:三. Gerrit 的安装配置

重新安装 gerrit 需要特别注意:

  • 如果有项目在使用记得备份 /etc/gerrit/git 目录
  • rm -fr /etc/gerrit
  • mysql -uroot -p
  • >>>drop database gerritdb;
  • >>>create database gerritdb;
  • 重新安装吧。
  • 如果 /etc/gerrit/logs/error_log 里出现 java.lang.IllegalStateException: Missing project All-Projects 这个错误,那就重新安装下,记得清除一下 db。

之前写过两篇文章,CI 系统搭建:一. 基础环境设置、规划CI 系统搭建:二. GitLab 的安装配置,本文主要记录 Gerrit 的安装的过程。

 

下载 Gerrit 包

目前最新版为 2.8

wget http://gerrit-releases.storage.googleapis.com/gerrit-2.8.war 

安装 Gerrit 依赖

Gerrit 的包是 java 格式,需要安装 jre

apt-get install default-jre daemon

创建 Gerrit 数据库

上一篇再安装 GitLab 时已经安装了 Mysql,直接创建库

sudo mysql -uroot -p
> create database gerritdb;
> grant all on gerritdb.* to 'gerrituser'@'localhost' identified by 'gerritpass';

开始安装

把 gerrit 安装再 /etc/gerrit/ 下

# java -jar gerrit-2.8.war init -d /etc/gerrit/

*** Gerrit Code Review 2.8
***

Create '/etc/gerrit'           [Y/n]? y

*** Git Repositories
***

Location of Git repositories   [git]:

*** SQL Database
***

Database server type           [h2]: mysql

Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
**  This library is required for your configuration. **
Download and install it now [Y/n]? y
Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar ... OK
Checksum mysql-connector-java-5.1.21.jar OK
Server hostname                [localhost]:
Server port                    [(mysql default)]:
Database name                  [reviewdb]: gerritdb
Database username              [root]: gerrituser
gerrituser's password          :
              confirm password :

*** User Authentication
***

Authentication method          [OPENID/?]: http
Get username from custom HTTP header [y/N]? n
SSO logout URL                 :

*** Email Delivery
***

SMTP server hostname           [localhost]: smtp.googlemail.com
SMTP server port               [(default)]: 587
SMTP encryption                [NONE/?]: tls
SMTP username                  [root]: gerrit@thstack.com
review@thstack.com's password  :
              confirm password :

*** Container Process
***

Run as                         [root]:
Java runtime                   [/usr/lib/jvm/java-6-openjdk-amd64/jre]:
Copy gerrit-2.8.war to /etc/gerrit/bin/gerrit.war [Y/n]? y
Copying gerrit-2.8.war to /etc/gerrit/bin/gerrit.war

*** SSH Daemon
***

Listen on address              [*]:
Listen on port                 [29418]:

Gerrit Code Review is not shipped with Bouncy Castle Crypto v144
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]? y
Downloading http://www.bouncycastle.org/download/bcprov-jdk16-144.jar ... OK
Checksum bcprov-jdk16-144.jar OK
Generating SSH host key ... rsa... dsa... done

*** HTTP Daemon
***

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? n
Subdirectory on proxy server   [/]:
Listen on address              [*]:
Listen on port                 [8081]: 8082
Canonical URL                  [http://www.thstack.com/]: http://review.thstack.com/

*** Plugins
***

Install plugin reviewnotes version v2.8 [y/N]? y
Install plugin download-commands version v2.8 [y/N]? y
Install plugin replication version v2.8 [y/N]? y
Install plugin commit-message-length-validator version v2.8 [y/N]? y

Initialized /etc/gerrit
Executing /etc/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on review.thstack.com:80 ... OK
Opening http://review.thstack.com/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:

http://review.thstack.com/#/admin/projects/

在安装完后 Gerrit 默认会打开浏览器,由于我的系统是 Server 版没有桌面和浏览器,所以才会出现上面倒数第三行的 …FAILED 同时从上面的信息看出我使用了 http 方式验证、启用代理服务器,指定了 Gerrit 端口为 8082,URL 为 review.thstack.com。需要在 nginx 里设置下端口转发。

Gerrit 启动脚本

cp /etc/gerrit/bin/gerrit.sh /etc/init.d/gerrit
vim /etc/init.d/gerrit
GERRIT_SITE=/etc/gerrit/       # 在代码 47 行增加
sudo update-rc.d gerrit defaults 21
service gerrit restart

Nginx

修改 Nginx 配置文件,给 Gerrit 做端口转发和访问控制,把下面内容写入到文件最后

# vim /etc/nginx/sites-available/gitlab
server {
  listen *:80;
  server_name review.thstack.com;
  allow   all;
  deny    all;
  auth_basic "THSTACK INC. Review System Login";
  auth_basic_user_file /etc/gerrit/etc/htpasswd.conf;

  location / { 
    proxy_pass  http://review.thstack.com:8082;
  }   
}

创建 htpasswd.conf 文件,并添加 admin 用户、密码到文件中

# touch /etc/gerrit/etc/htpasswd.conf
# htpasswd /etc/gerrit/etc/htpasswd.conf admin
  New password: 
  Re-type new password: 
  Adding password for user admin

默认第一个登录 Gerrit 的用户是 Admin。

访问

在浏览器 url 输入:http://review.thstack.com/,记得添加 hosts 解析。由于做了访问控制,会出现如下验证框,输入刚才创建的 admin 和密码 gerrit-admin-access

注册 admin 的邮箱,并添加 admin@thstack.com 密钥 gerrit-admin-sshkey

使用 htpasswd 创建 longgeek 用户和密码

htpasswd /etc/gerrit/etc/htpasswd.conf longgeek

换个浏览器或清除下浏览器保存的 admin 的密码 访问 http://review.thstack.com/ 输入 longgeek 和密码: gerrit-longgeek-login

注册邮箱和添加 longgeek@thstack.com 的密钥 gerrit-longgeek-create-user

最后

如果想 Gitlab 上创建的项目使用 Gerrit 的 Code Review 功能,两个系统的用户必须统一,也就是说不管哪个用户使用 Gerrit,前提是这个用户在 Gitlab 和 Gerrit 上都已注册,邮箱一致、sshkey 一致。当然 Nginx 访问控制用户的密码那就随意了。至于 Gitlab 上创建的项目如何同步到 Gerrit 上、Gitlab 如何使用 Gerrit 的 Code Review 功能等等都在 Jenkins 安装完之后会一起整合在一起。请关注后面的文章。

posted @ 2015-04-22 14:21  Chaoa  阅读(5295)  评论(0编辑  收藏  举报