Gitlab+Gerrit+Ldap+nginx+mysql 之Gerrit搭建与配置(一)

公司之前一直用gitlab来存放项目代码 ,目前因为业务需要(代码bug太多)需要引入gerrit来进行code review。

最初用的是centos 7.4,但是安装完成后,GerritResource中没有ALL_Project.git和All_Users.git生成,日志中一直在报错,反复装了几次后发现,在centos7上的兼容性不是太完美,所以采购了centos6.8 服务器进行安装,还是比较顺利的完成了安装。

另外本文采用LDAP认证,LDAP的配置本文不做讲解,参考其他文章。

已经在用:
gitlab-ce-9.3.3-ce.0.el7.x86_64
最新部署:
centos 6.8 x64
gerrit 2.15.6
nginx 1.10.2
mysql 5.6

1、建立专有帐户
$ useradd gerrit -m -s /bin/bash
$ passwd gerrit ##本文密码与用户同名
$ su - gerrit

2、配置 Java 环境

[[email protected] ~]# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
[[email protected] ~]# 

3、从官网下载gerrit
https://www.gerritcodereview.com/
当前最新版本为 2.15.6。

4、安装 MySQL

5、创建数据库、用户、授权
这里选择 MySQL 作为 Gerrit 的数据库,其它选项全部采用默认配置。对于这种选择,需要连上 MySQL,为 Gerrit 创建相应的数据库,用户,并为用户授权:

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> SELECT USER();
+----------------+
| USER()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)

mysql> create database reviewdb;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE USER ‘gerrit‘@‘localhost‘ IDENTIFIED BY ‘gerrit‘;
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on reviewdb.* to [email protected] identified by ‘gerrit‘;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| reviewdb           |
| sys                |
+--------------------+
8 rows in set (0.01 sec)

6、安装gerrit
把包传到gerrit的家目录,修改相关的权限 。通过如下命令安装 Gerrit:

[[email protected] ~]$ java -jar  gerrit-2.15.6.war  init -d review_site
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore

*** Gerrit Code Review 2.15.6
*** 

*** Git Repositories
*** 

Location of Git repositories   [GerritResource]:
**git 代码仓库的位置, 默认会在之前敲的init -d 目录下新建一个GerritResource文件夹,这个选项用于配置 Gerrit 的 Git 仓库的保存地址。上面的配置将创建 /home/gerrit/review_site/GerritResource 目录用于保存 Gerrit 的 Git 仓库。
*** SQL Database
*** 

Database server type           [mysql]:  ##这里使用Mysql数据,默认是H2不太会用。
Server hostname                [localhost]: 10.81.232.115   ##数据库地址
Server port                    [(mysql default)]: 3306    ##数据库端口
Database name                  [reviewdb]:      ##数据库名称
Database username              [gerrit]:      ##数据库用户
Change gerrit‘s password       [y/N]?]   ##数据库用户的密码

*** Index
*** 

Type                           [lucene/?]:    ##默认

*** User Authentication
*** 

Authentication method          [http/?]:     ##使用哪种认证方式,这里使用ldap
Get username from custom HTTP header [y/N]?  ##默认
SSO logout URL                 :   ##默认
Enable signed push support     [y/N]?   ##默认

*** Review Labels
*** 

Install Verified label         [y/N]?   ##默认

*** Email Delivery
*** 

SMTP server hostname           [smtp.exmail.qq.com]:
SMTP server port               [465]:
SMTP encryption                [tls/?]:
SMTP username                  [[email protected]]:
Change [email protected]‘s password [y/N]? 

*** Container Process
*** 

Run as                         [gerrit]:
Java runtime                   [/usr/share/jdk1.8.0_151/jre]:
Upgrade review_site/bin/gerrit.war [Y/n]? n

*** SSH Daemon
*** 

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

*** HTTP Daemon
*** 

Behind reverse proxy           [Y/n]? Y  ##是否使用反向代理 ,这是配置为是
Proxy uses SSL (https://)      [y/N]?
Subdirectory on proxy server   [/]:
Listen on address              [*]:
Listen on port                 [8080]:
Canonical URL                  [http://gerrit.baidu.com/]: 

*** Cache
*** 

*** Plugins
*** 

##以下这些插件要全部安装上,不然后续很麻烦。
##download-commands 尤其是这个插件,不安装的是没有clone地址的

Installing plugins.
Install plugin commit-message-length-validator version v2.15.6 [y/N]?
Install plugin download-commands version v2.15.6 [y/N]?
Install plugin hooks version v2.15.6 [y/N]?
Install plugin replication version v2.15.6 [y/N]?
Install plugin reviewnotes version v2.15.6 [y/N]?
Install plugin singleusergroup version v2.15.6 [y/N]?
Initializing plugins.
No plugins found with init steps.

*** Experimental features
*** 

Enable any experimental features [y/N]? 

Initialized /data/gerrit/review_site
[[email protected] ~]$ 

##默认安装完后服务是会启动的。

##这里因为有调整,所以手工启动服务了。
[[email protected] bin]# ./gerrit.sh  start
Starting Gerrit Code Review: OK
[[email protected] bin]#
[[email protected] bin]# netstat -antlop | grep 8082
tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN      16538/GerritCodeRev  off (0.00/0/0)
[[email protected] bin]# 

监控日志输出:

[[email protected] logs]$ pwd
/home/gerrit/review_site/logs
[[email protected] logs]$ tail -f error_log

排错过程:
1、Gerrit 安装过程中,可能会报出如下的 Exception:

Exception in thread "main" com.google.gwtorm.server.OrmException: Cannot apply SQL
CREATE TABLE account_group_members_audit (
added_by INT DEFAULT 0 NOT NULL,
removed_by INT,
removed_on TIMESTAMP NULL DEFAULT NULL,
account_id INT DEFAULT 0 NOT NULL,
group_id INT DEFAULT 0 NOT NULL,
added_on TIMESTAMP NOT NULL
,PRIMARY KEY(account_id,group_id,added_on)
)
    at com.google.gwtorm.jdbc.JdbcExecutor.execute(JdbcExecutor.java:44)
    at com.google.gwtorm.jdbc.JdbcSchema.createRelations(JdbcSchema.java:134)
    at com.google.gwtorm.jdbc.JdbcSchema.updateSchema(JdbcSchema.java:104)
    at com.google.gerrit.server.schema.SchemaCreator.create(SchemaCreator.java:81)
    at com.google.gerrit.server.schema.SchemaUpdater.update(SchemaUpdater.java:108)
    at com.google.gerrit.pgm.init.BaseInit$SiteRun.upgradeSchema(BaseInit.java:386)
    at com.google.gerrit.pgm.init.BaseInit.run(BaseInit.java:143)
    at com.google.gerrit.pgm.util.AbstractProgram.main(AbstractProgram.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.gerrit.launcher.GerritLauncher.invokeProgram(GerritLauncher.java:204)
    at com.google.gerrit.launcher.GerritLauncher.mainImpl(GerritLauncher.java:108)
    at com.google.gerrit.launcher.GerritLauncher.main(GerritLauncher.java:63)
    at Main.main(Main.java:24)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Invalid default value for ‘added_on‘
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2497)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2455)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:839)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:739)
    at com.google.gwtorm.jdbc.JdbcExecutor.execute(JdbcExecutor.java:42)
    ... 15 more

这个异常可通过如下方式解决:使用 MySQL root 用户登录,设置

set global explicit_defaults_for_timestamp=1;

像下面这样:

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 76
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> set global explicit_defaults_for_timestamp=1;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

如果数据库是主从,一定要在主库上创建库、用户、授权。否则会报错:

Enable any experimental features [y/N]? 

Exception in thread "main" com.google.gwtorm.server.OrmException: Cannot apply SQL
CREATE TABLE account_group_id(s SERIAL)
        at com.google.gwtorm.jdbc.JdbcExecutor.execute(JdbcExecutor.java:44)
        at com.google.gwtorm.jdbc.JdbcSchema.createSequences(JdbcSchema.java:122)
        at com.google.gwtorm.jdbc.JdbcSchema.updateSchema(JdbcSchema.java:103)
        at com.google.gerrit.server.schema.SchemaCreator.create(SchemaCreator.java:84)
        at com.google.gerrit.server.schema.SchemaUpdater.update(SchemaUpdater.java:111)
        at com.google.gerrit.pgm.init.BaseInit$SiteRun.upgradeSchema(BaseInit.java:389)
        at com.google.gerrit.pgm.init.BaseInit.run(BaseInit.java:146)
        at com.google.gerrit.pgm.util.AbstractProgram.main(AbstractProgram.java:61)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.google.gerrit.launcher.GerritLauncher.invokeProgram(GerritLauncher.java:223)
        at com.google.gerrit.launcher.GerritLauncher.mainImpl(GerritLauncher.java:119)
        at com.google.gerrit.launcher.GerritLauncher.main(GerritLauncher.java:63)
        at Main.main(Main.java:24)
Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cannot execute this statement
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2448)
        at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
        at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
        at com.google.gwtorm.jdbc.JdbcExecutor.execute(JdbcExecutor.java:42)
        ... 15 more

解决办法: 在主库上建库、用户、授权。
最终配置文件:

[[email protected] etc]$ more gerrit.config
[gerrit]
        basePath = GerritResource
        serverId = 5ae3e303-92dc-4823-8284-e21a14034f75
        canonicalWebUrl = http://gerrit.baidu.com/
[database]
        type = mysql
        hostname = localhost
        database = reviewdb
        username = gerrit
[noteDb "changes"]
        disableReviewDb = true
        primaryStorage = note db
        read = true
        sequence = true
        write = true
[index]
        type = LUCENE
[auth]
        type = LDAP
        gitBasicAuthPolicy = LDAP
[ldap]
        server = ldap://101.11.23.191:389
        username = cn=Manager,dc=baidu,dc=com
        accountBase = dc=baidu,dc=com
        groupBase = dc=baidu,dc=com
[receive]
        enableSignedPush = false
[sendemail]
        enable = true
        smtpServer = smtp.exmail.qq.com
        smtpServerPort = 587
        smtpEncryption = TLS
        smtpUser = [email protected]
        smtpPass = 123456
        from = [email protected]
[container]
        user = gerrit
        javaHome = /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el6_10.x86_64/jre
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = http://*:8080/
[cache]
        directory = cache
[gitweb]
    cgi = /var/www/git/gitweb.cgi
    type = gitweb
[plugins]
        # allow install plugin
        allowRemoteAdmin = true
[[email protected] etc]$ 

后续配置:
1、在/review_gerrit/bin/gerrit.sh中实际代码第一行加入
GERRIT_SITE=/home/gerrit/review_site

例如:

[[email protected] logs]# vim /review_gerrit/bin/gerrit.sh
GERRIT_SITE=/home/gerrit/review_site
#!/bin/sh
  GERRIT_SITE            =  /home/gerrit/review_site

2、全局使用gerrit:
[[email protected] logs]# ln -s /home/gerrit/review_site/bin/gerrit.sh /usr/bin/gerrit
安装插件:

[[email protected] ~]$ ssh -i .ssh/id_rsa -p 29418 [email protected] gerrit plugin install -n replication.jar -  < gerrit-2.15.6/WEB-INF/plugins/replication.jar
fatal: remote installation is disabled
[[email protected] ~]$

3、# 安装复制插件

unzip gerrit-2.14.1.war
ssh -p 29418 [email protected] gerrit plugin install -n replication.jar - < ./WEB-INF/plugins/replication.jar

#如果安装时提示权限问题,要在gerrit.config 上添加 allowRemoteAdmin 参数,文件见下面

[plugins]
        # allow install plugin
        allowRemoteAdmin = true

重启一下服务

[[email protected] ~]$ gerrit restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit‘s process for the kernel‘s out-of-memory killer.
         This may be caused by /usr/bin/gerrit not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit‘s PID=6302 with e.g.:
         echo ‘-1000‘ | sudo tee /proc/6302/oom_score_adj
OK
[[email protected] ~]$

安装replication

[[email protected] ~]$ ssh -i .ssh/id_rsa -p 29418 [email protected] gerrit plugin install -n replication.jar -  < gerrit-2.15.6/WEB-INF/plugins/replication.jar
[[email protected] ~]$
查看
[[email protected] etc]$ ssh -i .ssh/id_rsa -p 29418 [email protected] gerrit plugin ls
Warning: Identity file .ssh/id_rsa not accessible: No such file or directory.
Name                           Version    Status   File
-------------------------------------------------------------------------------
replication                    v2.15.6    ENABLED  replication.jar
[[email protected] etc]$ 

4、Gerrit部署成功后创建项目看不到clone地址
在gerrit部署成功后使用admin账号登录,完成管理员注册流程,然后尝试创建第一个项目。创建项目test,权限继承ALL project,然后进入test,但是看不到clone地址。如下图:

解决办法:
安装插件download-commands

5、添加key的时候报504

解决办法:
确保gerrit.config中,邮箱配置是正确的。另外你的gerrit所在的机器是可以发送邮件的。我这里用的是postfix。配置完后,通过以下命令能收到邮件,即为正常
echo testmail| mail -s "testmail" [email protected]

6、代码同步到gerrit后,开发者diff不了,一点就报错,很忧伤 。如下图:

解决办法 :
nginx配置文件的配置问题:

proxy_pass http://gerrit_server/;
修改为
proxy_pass http://gerrit_server;

 重启nginx服务

7、

配置/home/gerrit/review_site/etc/replication.config,没有的话新建:

URL这里目前只能写多个了,尝试使用了namespace无效。
就是仓库没办法使用变量,当有新增的话 维护起来比较麻烦,
目前还没有更好的解决办法,哪位大佬给予指导。

原文地址:http://blog.51cto.com/zhaowl/2315989

时间: 2024-08-08 14:53:13

Gitlab+Gerrit+Ldap+nginx+mysql 之Gerrit搭建与配置(一)的相关文章

Linux Debian 下LNMP服务器——nginx+mysql+php环境搭建及配置

昨天刚给公司服务器装了LNMP服务器环境,在这里简单记录一下过程备忘. 这里我在安装的时候是用的Dotdeb源,仅供参考. 1.导入Dotdeb源,据说Dotdeb源里的软件版本比较新. 在向源中导入Dotdeb前,我们需要先获取GnuPG key并导入: wget http://www.dotdeb.org/dotdeb.gpg cat dotdeb.gpg | apt-key add - 提示OK,表明导入成功.然后我们开始导入Dotdeb源,源列表的位置在“/etc/apt/sources

Cacti监控服务器配置教程(基于CentOS+Nginx+MySQL+PHP环境搭建)

具体案例:局域网内有两台主机,一台Linux.一台Windows,现在需要配置一台Cacti监控服务器对这两台主机进行监控环境说明:1.Linux主机操作系统:CentOS 6.2IP地址:192.168.21.164子网掩码:255.255.255.0网关:192.168.21.2DNS:8.8.8.8 8.8.4.42.Windows主机操作系统:Windows Server 2003IP地址:192.168.21.130子网掩码:255.255.255.0网关:192.168.21.2DN

如何搭建基于ldap和mysql的gerrit服务

一.搭建数据库以及配置数据库 1.使用yum install mysql-server安装mysql 2.mysqladmin -u root password archermind(第一次设置密码).设置密码 3.修改 vi /etc/my.cnf 增加[client]项以及修改[mysqld]项.具体如下所示: [client] #password       = your_password port            = 3306 socket          = /var/lib/

tomcat+nginx+mysql+jdk 环境搭建

1.安装JDK zrsz 上传到服务器安装 jdk 7u55 2.安装tomcat 下载tomcat 7 3.安装nginx nginx 安装依赖一下三个包 3.1  openssl-1.0.1c.tar.gz 3.2  pcre-8.36.tar.gz 3.3  zlib-1.2.8.tar.gz 3.4  yum install mhash 3.5  ./configure   /configure --prefix=/usr/local/nginx/  --with-http_stub_s

Linux + Nginx + Mysql + PHP 环境搭建

首先安装Linux系统,我以虚拟机安装来做示例,先去下载 VitualBox,这是一款开源的虚拟机软件,https://www.virtualbox.org 官网地址.或者是VMware,www.vmware.com,不过这个软件是收费的.当然同时还要去下载一个Linux镜像,我下载是Centos 7系统,https://www.centos.org/download 下载好了之后打开虚拟机,我用的是VMware,选择创建自定义虚拟机: 继续下一步: 点击完成. 看到这个界面后,点击CD/DVD

Docker + node(koa) + nginx + mysql 线上环境部署

在上一篇 Docker + node(koa) + nginx + mysql 开发环境搭建,我们进行了本地开发环境搭建 现在我们就来开始线上环境部署 如果本地环境搭建没有什么问题,那么线上部署的配置也就很简单了 我所使用的环境,Linux Mint,命令有不同可以适当更改 目录结构 - compose 新建,线上环境配置 - data - conf - node_modules - static - docker-compose.yml - docker-compose-prod.yml 新建

centos6 LNMP的搭建(linux+nginx+mysql+php)

LNMP的搭建(linux+nginx+mysql+php) 简介 LNMP代表的就是:Linux系统下Nginx+MySQL+PHP网站服务器架构. Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统.代表版本有:debian.centos.ubuntu.fedora.gentoo等. Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器. Mysql是一个小型关系型数据库管理系统. PHP是一种在服务器端执行的嵌入HTML文档

在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境

阿里云的云服务器(ECS)可以选择多种操作系统,打算用它运行 Drupal或者 WordPress ,你最好选择 Linux 系统,这篇文章的演示是基于阿里云的 CentOS 操作系统的服务器.我们在上面搭建一个 nginx + mysql + php-fpm 的环境,这就是常说的 LNMP .我们不过多解释什么是什么,而是着重讲流程与方法,想了解具体的细节,去搜索下吧:)这个手册是在阿里云上测试的,不过应该也适用于其它使用 CentOS 系统的服务器. 背景 宁皓网的< CentOS:在阿里云

Gerrit与Jenkins/Hudson CI服务器搭建

配置Git 很多系统(例如Linux)已经默认提供了Git,在Git主页也可以找到安装程序.对于Windows用户,最好的选择是MsysGit.请注意,如果你安装了Apple Developer Tools (for Xcode 4),那么其中已经自带Git二进制包了.如果遇到了问题,help.github.com中可以找到很多非常出色的指南. 因为所有的Git提交都带有作者和电子邮件地址,如果你还没有设置过这些内容,请执行以下命令进行配置: $ git config --global user