Showing posts with label gerrit with mysql. Show all posts
Showing posts with label gerrit with mysql. Show all posts

Gerrit with MySQL installation guide

Hello,

Gerrit is a great tool for pre-commit code review. Installation on Linux based OS is kind of default and it's nice and simple. However when you try to do it on Windows you may end up having some sort of issues. So this has been tested on Windows OS but you can also try it on Linux based OS.

1. Download and install MySQL: http://dev.mysql.com/downloads/

2. Download and install MySQL Workbench - useful tool for accessing the databases: http://dev.mysql.com/downloads/workbench/

3. Create Gerrit review database and user for it:

CREATE DATABASE gerritdb;
ALTER DATABASE gerritdb charset=latin1;

CREATE USER 'gerrit'@'localhost' IDENTIFIED BY 'gerrit';
GRANT ALL ON gerritdb.* TO 'gerrit'@'localhost';
FLUSH PRIVILEGES;

CREATE USER 'gerrit'@'<hostname>' IDENTIFIED BY 'gerrit';
GRANT ALL ON gerritdb.* TO 'gerrit'@'<hostname>';
FLUSH PRIVILEGES;


5. Create a folder to host gerrit and place the WAR in it.

6. Installation:

gerrit/$ java -jar gerrit.war init

It will ask for various different settings. For some of them default option seems OK. See below some of the setting you may want to specify other than defaults:

canonicalWebUrl = http://gerrit:8080/

[database]
    type = mysql
    hostname = localhost // OR network hostname
    database = gerritdb
    username = gerrit (password prompt = gerrit)

Run as user: gerrit

NOTE: if you use LDAP and/or you have a mail server it's better to set it up directly during installation rather than later. Note all passwords provided will not be visible in the /gerrit/etc/gerrit.config file which is good.

Example of LDAP settings:
[ldap]
    server = ldap://ldapSrvName.aaa.bbb
    username = CN=John Smith, OU=Country,OU=Users,DC=aaa,DC=bbb
    accountBase = DC=aaa,DC=bbb
    groupBase = DC=aaa,DC=bbb

Example of e-mail server settings:
[sendemail]
    enable = true
    from = SERVER
    threadPoolSize = 1
    smtpServer = emailSrvName.aaa.bbb
    smtpServerPort = 25
    smtpEncryption = tls
    sslVerify = false

7. Go to /gerrit/etc and generate ssh keys with the specified names:
gerrit/etc/$ ssh-keygen
name: ssh_host_key

chmod 600 ssh_host_key && chmod 600 ssh_host_key.pub

8. On Windows there may be an error regarding virtual memory command, so if you are installing this on Win OS edit /gerrit/bin/gerrit.sh:
- comment out virtual memory command (on or near line #303)
- add line at the beginning GERRIT_SITE=<path_to_gerrit_folder>
e.g. GERRIT_SITE=/c/gerrit

9. It's likely that gerrit has failed to create some of the tables (if not then you can skip this step). Create the missing tables manually in the MySQL database:
create table account_group_by_id_aud (
  added_by INT DEFAULT 0 NOT NULL,
  removed_by INT,
  removed_on TIMESTAMP NULL DEFAULT NULL,
  group_id INT DEFAULT 0 NOT NULL
  include_uuid VARCHAR(255) BINARY DEFAULT '' NOT NULL
  added_on TIMESTAMP NOT_NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(group_id,include_uuid,added_on)
);

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 DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(account_id,group_id,added_on)
);

create tables changes (
  change_key VARCHAR(60) BINARY DEFAULT '' NOT NULL,
  created_on TIMESTAMP NOT NULL,
  last_updated_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  owner_account_id INT DEFAULT 0 NOT NULL,
  dest_project_name VARCHAR(255) BINARY DEFAULT '' NOT NULL,
  dest_branch_name VARCHAR(255) BINARY DEFAULT '' NOT NULL,
  status CHAR(1) DEFAULT ' ' NOT NULL,
  current_patch_set_id INT DEFAULT 0 NOT NULL,
  subject VARCHAR(255) BINARY DEFAULT '' NOT NULL,
  topic VARCHAR(255) BINARY,
  original_subject VARCHAR(255) BINARY,
  row_version INT DEFAULT 0 NOT NULL,
  change_id INT DEFAULT 0 NOT NULL,
  PRIMARY KEY(change_id)
);

10. Run again gerrit WAR to check if there are no table errors this time:
gerrit/$ java -jar gerrit.war init --batch -d <installation_folder>

11. Run reindex task:
gerrit/$ java -jar gerrit.war reindex

12. Make sure the changes done for /gerrit/bin/gerrit.sh are still there. If not apply step 8 again.

13. There may be an error when querying users accounts, so to prevent run the following updates on the database:
SET collation_connection = 'utf8_general_ci'
ALTER DATABASE gerritdb CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE accounts CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE account_external_ids CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci


14. Start gerrit.sh script

If failed to start no worries, check it if it's actually running because the message may just be misleading. If really failed then possible errors / fixes:
- port blocked - kill all java processes and try again
- reindex gerrit (see the step 11)

If nothing else helps you can try to drop the database and recreate it again.


Hope that helps. Cheers!

If you have other issues or questions check out official documentation and tutorials: https://www.gerritcodereview.com/