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!!!

Creating/Adding new user in amazon web services (AWS)

Software used
Local OS
Windows 7
Remote Host
Amazon web services
Terminal
Mingw32 included in (PortableGit-1.9.5)
Other useful tools
Putty, WinSCP






Creating User
First log in to the amazon aws using your ec2-user account

Now, create new user by using follwing commands,
[ec2-user@amazon ~]$sudo adduser username

Set password for “username” by:
[ec2-user@amazon ~]$ sudo su username
[root@amazon ec2-user]$ passwd username

Now go to home/username folder and type following command
[username@amazon ~]$ ssh-keygen -t rsa
[username@amazon ~]$ cd .ssh
[username@amazon ~]$ chmod 700 .ssh
[username@amazon ~]$ cat id_rsa.pub > .ssh/authorized_keys
[username@amazon ~]$ chmod 600 .ssh/authorized_keys
[username@amazon ~]$ sudo chown username:ec2-user .ssh

In the above step, username is the user we created and ec2-user is the default user.

Before downloading the private key, you will need to copy the key to ec2-user folder and chmod it to 777. We can only download from the file using ec2-user, since you have the key for that username.
[username@amazon ~]$ sudo cp id_rsa /home/ec2-user/
[username@amazon ~]$ sudo chmod 777 /home/ec2-user/id_rsa

Now you just need to download the private key “id_rsa”. We can use scp command to download/upload files from EC2. If you prefer GUI then you may use 'WinSCP' software. 

Now come to local machine’s terminal, where you have my_key.pem file for ec2-user and do as follows:
[username@local ~]$ scp -i my_key.pem ec2-user@amazon:/home/ec2-user/id_rsa download_folder

The above command will copy the key “id_rsa” to the download_folder directory on your local machine. Delete “/home/ec2-user/id_rsa” from server, because it’s a private key.

Once you have copied the key in your local machine, do following.
[username@local ~]$  chmod 600 id_rsa

Now we can connect to aws using new username as follows:
[username@local ~]$  ssh -i id_rsa username@amazon


Congratulation!! you have done it.
P.S. you can rename your id_rsa file to any name you want. In my case, I have renamed to my name.

So, in this manner, you can setup multiple users to use one EC2 instance!!

Possible errors
You might get following error.


Following might be your solution.

At the amazon server
[username@amazon ~]$ cat id_rsa.pub > .ssh/authorized_keys (make sure this is done)
[username@amazon ~]$ chmod 700 .ssh (make sure ssh folder has required access right)
[username@amazon ~]$ chmod 600 .ssh/authorized_keys (make sure has required access right)

At the local machine
[username@local]$ chmod 600 id_rsa (make id_rsa has access right)

Making life easier
Go to your windows machine and navigate to C:\Users\username\

Create .ssh folder and inside .ssh create a file 'config'.
Now open config in notepad and write following

------------------------
Host amazon            #can use any host name
Hostname ec2-52-0-158-182.compute-1.amazonaws.com #address of your amazon server
User username         # username of amazon server
IdentityFile /id_rsa   # private key, make sure its in .ssh folder
-------------------------

And save it and exit.

Now go into your local machine terminal and type following

[username@local ~]$  ssh amazon


Hola!! you are in.

This way you don't have to type the hostname or ip address of amazon every time you login. It makes your like much easier.

Good Luck!!!