Friday, August 7, 2015

Installing Gerrit in Amazon web services (AWS)

Installing Gerrit

This tutorial will teach you step by step how to install and run Gerrit in AWS. We will use default database and Apache as our web server.

Requirements

Check if you already have Java installed?

$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode)

If Java isn’t installed, install it:

Check if apache is installed

$ httpd -V
Server version: Apache/2.2.16
Server built:   May 12 2011 11:58:18
Server's Module Magic Number: x
Server loaded:  APR 1.4.2, APR-Util 1.3.9
Compiled using: APR 1.2.12, APR-Util 1.3.9
Architecture:   64-bit
Server MPM:     Worker
 threaded:     yes (fixed thread count)
 forked:     yes (variable process count)
Server compiled with....

If apache isn’t installed, install it:

Download Gerrit

Download the latest Gerrit web from following link

You can download gerrit using the command below:
## wget https://gerrit-releases.storage.googleapis.com/gerrit-2.11.2.war -O /home/gerrit2/gerrit.war

Initalize the site

It’s time to run the initialization, and with the batch switch enabled, we don’t have to answer any questions at all:

user@host:~$ java -jar gerrit.war init --batch -d ~/gerrit_site
  Generating SSH host key ... rsa(simple)... done
  Initialized /home/gerrit2/gerrit_testsite
  Executing /home/gerrit2/gerrit_testsite/bin/gerrit.sh start
  Starting Gerrit Code Review: OK
user@host:~$

When the init is complete, you can review your settings in the file '$site_path/etc/gerrit.config'.

Open gerrit.config file and change as follows

[gerrit]
basePath = git
canonicalWebUrl = http://amazonhostname/gerrit/
[database]
type = h2
database = db/ReviewDB
[index]
type = LUCENE
[auth]
type = HTTP
[sendemail]
smtpServer = localhost
[container]
user = ec2-user
javaHome = /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = http://localhost:8080/gerrit/
[cache]
directory = cache

Note that initialization also starts the server. If any settings changes are made, the server must be restarted before they will take effect.

user@host:~$ ~/gerrit_testsite/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: OK
user@host:~$

Now open /etc/httpd/vhosts.d/gerrit.conf and change it as follows

<VirtualHost *>
    ServerName Amazonhostname
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On

<Proxy *>
Order deny,allow
Allow from all
</Proxy>
 <Location /gerrit/login/>
AuthType Basic
AuthName "Gerrit Code Review"
Require valid-user
AuthUserFile '/etc/httpd/gerrit.htpasswd'
 </Location>
  
    AllowEncodedSlashes On
    ProxyPass /gerrit/  http://localhost:8080/gerrit/ 
</VirtualHost>

Gerrit needs user to register into its website. It is recommended to use same username which is used to installed the Gerrit in amazon server.  However, you can create new user for Gerrit by using following tutorial CreateNewuser Link.

Once you have user ready to be registered for Gerrit then add same user in Apache for authentication by using following command

user@host:~$ sudo htpasswd -c /etc/httpd/gerrit.htpasswd new_user 
//if this command is run for first time

To add more user
//note remove -c flag
user@host:~$ sudo htpasswd /etc/httpd/gerrit.htpasswd new_user 

Now restart apache 
user@host:~$ sudo service httpd restart
Stopping httpd:  OK
Starting httpd:    OK
user@host:~$

Registering key in Gerrit

Open a browser and enter the http://amazonhomstname/gerrit/


You will be asked for the username and password. Enter the password and username which you created above. Make sure you enter the username which you want to be the gerrit administrator. The first user to sign-in and register an account will be automatically placed into the fully privileged Administrators group, permitting server management over the web and over SSH. Subsequent users will be automatically registered as unprivileged users.

Once signed in as your user, you find a little wizard to get you started. The wizard helps you fill out:

  • Real name (visible name in Gerrit)
  • Register your email (it must be confirmed later)
  • Select a username with which to communicate with Gerrit over ssh+git


The gerrit ssh server will only accept requests from users it knows and will require a ssh public key to let them in. The ssh public key for username is registered in the settings page (check right side of the page):

user@host:~$ cat .ssh/id_rsa.pub
  ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5E785mWtMckorP5v40PyFeui9T50dKpaGYw67Mlv2J3aGBG3tS0qBQxKEpiV0J4+W0RgQHbWfNqdUYen9bC5VVH/GatYWkpL9TjjUcHzF1rX3Eyv7PHuHLAyd/8Zdv6R3saF+hNpp1JW0BSa7HXzK7iNCVA3kBuBthxeGh3OoFbaXHn1zwwVQw8I5+Lp9OOIY7sJEsM/kW699XDV6z2zlkByNVEp45j+g26x5rCnGS8GJM7A0uHsaWJddO6TiyR6/2SOBF1VtKw49XLTQcmDInFAZzUsAZSDKlfYloPkpA6YdqeG0eJqau+jtzuigydoVj4j9xidcJ9HtxZcJNuraw== user@host
user@host:~$ 

Copy the string starting with ssh-rsa to your clipboard and then paste it into the box for RSA keys. 
Verify that the ssh connection works for you.

Testing Connection

Type following command in the local terminal. 

If everything goes well then you should get above welcome message. 

Project Creation

Your base Gerrit server is now running and you have a user that’s ready to interact with it. You now have two options, either you create a new test project to work with or you already have a git with history that you would like to import into Gerrit and try out code review on.

New project from scratch

If you choose to create a new repository from scratch, it’s easier for you to create a project with an initial commit in it. That way first time setup between client and server is easier.
This is done via the SSH port:

user@home:~$ ssh -p 29418 username@lhost gerrit create-project --empty-commit --name demo-project
user@home:~$
This will create a repository that you can clone to work with.

Already existing project

The other alternative is if you already have a git project that you want to try out Gerrit on. First you have to create the project. This is done via the SSH port:

user@home:~$ ssh -p 29418 username@host gerrit create-project --name demo-project
user@home:~$
You need to make sure that at least initially your account is granted "Create Reference" privileges for the refs/heads/* reference. This is done via the web interface in the Admin/Projects/Access page that correspond to your project.
After that it’s time to upload the previous history to the server:

user@home:~/my-project$ git push ssh://user@localhost:29418/demo-project *:*
Counting objects: 2011, done.
Writing objects: 100% (2011/2011), 456293 bytes, done.
Total 2011 (delta 0), reused 0 (delta 0)
To ssh://user@localhost:29418/demo-project
* [new branch]      master -> master
 user@host:~/my-project$


Cloning project from the host

Directly cloning from git server may not reflect changes in Gerrit website. Thus, we need to clone from the Gerrit server. We need to specify the port address to clone from the Gerrit server. Make sure you do this in you config file located at C:\user\username\.ssh. If there is no config file create one.

Host gerrit
Homename ec2- 52-5-208-121.compute-1.amazonaws.com 
User ec2-user
IdentityFile /id_rsa
Port 29418

Now run following command

user@home:~/my-project$ git push ssh://user@localhost:29418/demo-pr
The output is of above command is, 

Once we get the project in the local machine then go into project directory and make necessary changes as follows


Tell git about the necessary changes by add command and commit changes






Commit hook and change id

A problem that arises for submitting changes is that commit hook is not automatically attached. A hack for this approach is to make one fail attempt to push. On doing so, the error message will automatically highlight the Change-Id, see below example: 



Now type following command in terminal as follows

user@home:~/gitdir=$(git rev-parse --git-dir); scp -i id_rsa -p -P 29418 username@host:hooks/commit-msg ${gitdir}/hooks/



Now commit it as follows
user@home:~/git commit --amend








Finally push the changes to the Gerrit server as follows

user@home:~/demo-project$ git push origin HEAD:refs/for/master



After doing this, all registered user should get an email with the link to the changes. Also, you can see the link just below "New Changes" in the above output. Open that link to see the changes.

That's all folks!!!

4 comments:

  1. When will i find integration with AWS CodeCommit? how can Gerrit handle that?

    ReplyDelete
  2. Thanks for providing this informative information you may also refer.
    http://www.s4techno.com/blog/2015/12/24/aws-rds-in-sql-server-5-minute-deploy/

    ReplyDelete
  3. According to Stanford Medical, It is really the SINGLE reason this country's women get to live 10 years more and weigh 42 pounds lighter than we do.

    (Just so you know, it has totally NOTHING to do with genetics or some secret-exercise and really, EVERYTHING about "how" they eat.)

    BTW, I said "HOW", not "WHAT"...

    Click this link to discover if this brief quiz can help you discover your true weight loss possibilities

    ReplyDelete
  4. Really appreciate this. Helped me a lot in setting up and using Gerrit on my server.

    ReplyDelete