Ubuntu ServerにGerritをインストールする | Qtとその他諸々の学習ブログ

Qtとその他諸々の学習ブログ

学習メモ。間違えやアドバイス、質問があればコメント下さい。Qtが好きなのでQtの学習が多いです。

自宅のUbuntu ServerにGerritをインストールしました。
手順は以下のとおりです。

■インストール手順
・OracleのJava JDKをインストール
http://www.kkaneko.com/rinkou/javaintro/ubuntu_jdk.html
に従ってインストールしました。

・Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Filesのインストール
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9.1/install.html#cryptography
に従ってインストールしました。
自分の環境だと、JCE jurisdiction policy JAR filesは下記のパスに有りました。
/usr/lib/jvm/java-8-oracle/jre/lib/security
ちなみに$JAVA_HOMEは
/usr/lib/jvm/java-8-oracle


・PostgreSQLのインストール
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9.1/install.html
には「PostgreSQL or H2 is the recommended database for Gerrit Code Review. 」と書いてあったので、
PostgreSQLを使うことにしました。

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04
に従い、下記コマンドを実行するだけでインストールされます。(すでにインストールされてました)
$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

・DBのセットアップ
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04
に従い、
$ sudo -i -u postgres
にてpostgresユーザーにログイン後、
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9.1/install.html#createdb_postgres
に従って下記コマンドを実行しました。postgresユーザーにログインしないと、「createuser: could not connect to database postgres: FATAL:  Peer authentication failed for user "postgres"」というエラーが出てしまいます。
$ createuser --username=postgres -RDIElPS gerrit2

$ createdb --username=postgres -E UTF-8 -l ja_JP.UTF8 -T template0 -O gerrit2 reviewdb
※後述の「■トラブルシューティング1」参照

・Gerritのインストール
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9.1/install.html#init
にしたがって進めます。
java -jar gerrit.war init -d /path/to/your/gerrit_application_directory
をやると質問形式でインストールが進みます。
下記のとおりに設定しました。
===============
*** Gerrit Code Review 2.9.1
***

Create '/home/noboru/data_hdd/gerrit/site' [Y/n]? y

*** Git Repositories
***

Location of Git repositories   [git]: /home/noboru/data_hdd/git/repositories

*** SQL Database
***

Database server type           [h2]: ?
       Supported options are:
         h2
         jdbc
         mysql
         oracle
         postgresql
Database server type           [h2]: postgresql
Server hostname                [localhost]:
Server port                    [(postgresql default)]:
Database name                  [reviewdb]:
Database username              [gerrit2]:
gerrit2's password             :
              confirm password :

*** Index
***

Type                           [LUCENE/?]:

*** User Authentication
***

Authentication method          [OPENID/?]:

*** Review Labels
***

Install Verified label         [y/N]?

*** Email Delivery
***

SMTP server hostname           [localhost]:
SMTP server port               [(default)]:
SMTP encryption                [NONE/?]:
SMTP username                  :

*** Container Process
***

Run as                         [gerrit2]:
Java runtime                   [/usr/lib/jvm/java-8-oracle/jre]:
Copy gerrit.war to /home/noboru/data_hdd/gerrit/site/bin/gerrit.war [Y/n]?
Copying gerrit.war to /home/noboru/data_hdd/gerrit/site/bin/gerrit.war

*** SSH Daemon
***

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

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v149
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]?
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar ... OK
Checksum bcpkix-jdk15on-149.jar OK

Gerrit Code Review is not shipped with Bouncy Castle Crypto Provider v149
** This library is required by Bouncy Castle Crypto SSL v149. **
Download and install it now [Y/n]?
Downloading http://www.bouncycastle.org/download/bcprov-jdk15on-149.jar ... OK
Checksum bcprov-jdk15on-149.jar OK
Generating SSH host key ... rsa... dsa... done

*** HTTP Daemon
***

Behind reverse proxy           [y/N]?
Use SSL (https://)             [y/N]?
Listen on address              [*]:
Listen on port                 [8080]:
Canonical URL                  [http://ubuntu:8080/]:

*** Plugins
***

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

Initialized /home/noboru/data_hdd/gerrit/site
Executing /home/noboru/data_hdd/gerrit/site/bin/gerrit.sh start
Starting Gerrit Code Review: FAILED
error: cannot start Gerrit: exit status 1
Waiting for server on ubuntu:8080 ... OK
Opening http://ubuntu:8080/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
  http://ubuntu:8080/#/admin/projects/
===============
起動に失敗していますがその対処は
後述の「■トラブルシューティング2」を参照してください。
Listen on port                 [8080]:
Canonical URL                  [http://ubuntu:8080/]:
の値を
Listen on port                 [8081]:
Canonical URL                  [http://192.168.1.32:8081/]:
変更しました。





■トラブルシューティング1
$ createdb --username=postgres -E UTF-8 -O gerrit2 reviewdb
を実行したら下記のエラーが出ました。
=======
createdb: database creation failed: ERROR:  encoding "UTF8" does not match locale "ja_JP.EUC-JP"
DETAIL:  The chosen LC_CTYPE setting requires encoding "EUC_JP".
=======
PostgreSQLのロケール設定がEUC_JPになっているっぽいです。
ロケールとテンプレートを指定することで解決出来ました。
$ createdb --username=postgres -E UTF-8 -l ja_JP.UTF8 -T template0 -O gerrit2 reviewdb

http://oshiete.goo.ne.jp/qa/6825563.html
http://wsjp.blogspot.jp/2012/04/postgresqlinitdbdb.html
とman createdbを参照しました。


■トラブルシューティング2
gerritの起動に失敗します。
gerrit2$ /path/to/gerrit/site/bin/gerrit.sh start
で起動するのですが、
Starting Gerrit Code Review: FAILED
とメッセージが表示されてしまい、起動に失敗しているようです。

/path/to/gerrit/site/logs/error_logを見てみると下記のメッセージが有りました。
==============
FAILED ServerConnector@1e5b33e5{HTTP/1.1}{0.0.0.0:8080}: java.net.BindException: Address already in use
java.net.BindException: Address already in use
==============
ということで、8080ポートを使っているのがダメらしいので、変更しました。
/path/to/gerrit/site/etc/gerrit.config
を修正します。
canonicalWebUrlとlistenUrlのポート番号を8080→8081に変更しました。
また、canonicalWebUrlはサーバーを名前指定からIPアドレス指定に変更しています。名前解決できていないので。
==============
canonicalWebUrl = http://192.168.1.32:8081/
listenUrl = http://*:8081/
==============
これで再度
gerrit2$ /path/to/gerrit/site/bin/gerrit.sh start
を実行したら
Starting Gerrit Code Review: OK
と表示されて、
ブラウザで
http://192.168.1.32:8081/#/admin/projects/
を表示したらgerrit.configのbasePathに指定したフォルダのgitリポジトリ一覧が見えました。
basePathにはインストール時に
Location of Git repositories   [git]:
の質問で入力した値が設定されています。

===================
2014/10/28追記
■トラブルシューティング3
自動起動の設定について、Gerritのドキュメントに書いてある方法だけだとうまく行きませんでした。
下記が参考になりました。
自動起動の設定についてのメール
http://code.google.com/p/gerrit/issues/detail?id=1372
「** ERROR: GERRIT_SITE not set」というエラーについて
https://groups.google.com/forum/#!topic/repo-discuss/1dn-kJh4HD4

ポイントは、ランレベル2で起動させる点とGERRIT_SITEの設定でした。

起動スクリプトの設定。startに2を入れておくのがポイント。
sudo ln -snf /path/to/gerrit/site/bin/gerrit.sh /etc/init.d/gerrit
update-rc.d gerrit start 90 2 3 4 5 . stop 01 0 1 6 .

GERRIT_SITEの設定。
Create /etc/default/gerritcodereview containing the line:
GERRIT_SITE=/path/to/gerrit/site
===================


■参照URL
Gerritインストールガイド
https://gerrit-documentation.storage.googleapis.com/Documentation/2.9.1/install.html

Oracle Java JDKのインストール
http://www.kkaneko.com/rinkou/javaintro/ubuntu_jdk.html
https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get

PostgreSQLのインストール
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Filesについて
http://blog.scheakur.com/post/50807486592/java-cryptography-extension-jce-unlimited-strength


以上。