Saturday, December 22, 2012

Gitolite Server / Git Client setup on Ubuntu 12.04 LTS

This Blog Post will go through at a high level how to setup your own git server. Why would you want to do this? Well there are a number of reasons


  1. GitHub.com goes down (Which it did today) 12/22/2012 - It's not the end of the world but it might as well be if all your developer code is on github.com!github down 730x554 GitHub confirms major service outage across all its services following scheduled maintenance
  2. Local Performance - will always be better with your own hosted server
  3. Better Control and security.
GitHub.com is great for social coding, but I was never a fan of having our own developer code or our client's code out there on the public internet. Even with security, I find that the local or self hosted servers give you more control (like anything that's not cloud based).

We will be using gitolite and Ubuntu 12.04 and I've also include gitweb so there is a browser based version of the tool to work with gitolite server.

Git Server Setup and Install
apt-get -y install git-core git-doc apache2

gitolite setup

sudo addgroup gitolite
sudo adduser --disabled-password --home /home/gitolite --ingroup gitolite gitolite
passwd gitolite

{gitolite password}

sudo apt-get -y install gitolite

usermod -a -G gitolite www-data
su - gitolite

ssh-keygen -t rsa
(choose no passphrase)


cd ~
cd .ssh
cp id_rsa.pub /tmp
gl-setup /tmp/id_rsa.pub


Change the following variable to below

$REPO_UMASK = 0027;


On your local machine

You should now be able to clone the gitolite-admin.git repository that’s created automatically by the gitolite setup script:

git clone gitolite@{git.server.ip}:gitolite-admin.git


Edit gitolite.conf to enable gitweb and git-daemon export for testing:

# FROM YOUR LOCAL MACHINE
cd gitolite-admin
vim conf/gitolite.conf
# change to:
repo    testing
    RW+     =   @all
    R       =   daemon
testing "Owner" = "Test repo"

exit the file

git add conf/gitolite.conf
git commit -m "Enabled gitweb and git-daemon export for testing repo"
git push
cd ..


Setting the repo owner and description automatically gives read access to gitweb so you don’t have to specify it explicitly.
Clone testing and add a file (so it’s not empty):

git clone gitolite@{git.server.ip}:testing.git
cd testing
echo "README" > README
git add README
git commit -m "Added README"
git push origin master

gitweb setup
Install gitweb:

sudo apt-get -y install highlight gitweb
Change the gitweb configuration to use the gitolite repo paths:

vim /etc/gitweb.conf
# change $projectroot to "/home/gitolite/repositories";
# change $projects_list to "/home/gitolite/projects.list";


Git Daemon Setup

apt-get install git-daemon-run

vim /etc/sv/git-daemon/run
Change:

#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon \
"$(git --exec-path)"/git-daemon --verbose --base-path=/var/cache /var/cache/git
to:

IMPORTANT: notice the change from -ugitdaemon to -ugitdaemon:gitolite

#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon:gitolite \
"$(git --exec-path)"/git-daemon --verbose --base-path=/home/gitolite/repositories /home/gitolite/repositories



Pretty Ur’ls

Comment out everything in this file

vim /etc/apache2/conf.d/gitweb

Add the Following to the file
vim /etc/apache2/sites-available/git

<VirtualHost *:80>
  ServerName git.server
  ServerAdmin webmaster@git.server
  DocumentRoot /usr/share/gitweb

  <Directory /usr/share/gitweb>
      Options FollowSymLinks ExecCGI
      AddHandler cgi-script cgi
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^.* /index.cgi/$0 [L,PT]
  </Directory>
</VirtualHost>


a2enmod rewrite
apache2ctl restart

Add the Following to the file

vim /etc/gitweb.conf


# Enable PATH_INFO so the server can produce URLs of the
# form: http://git.cdwilson.us/project.git/xxx/xxx
# This allows for pretty URLs *within* the Git repository, where
# my Apache rewrite rules are not active.
$feature{'pathinfo'}{'default'} = [1];



Enable Other options

$feature{'blame'}{'default'} = [1];
$feature{'blame'}{'override'} = 1;

$feature{'pickaxe'}{'default'} = [1];
$feature{'pickaxe'}{'override'} = 1;

$feature{'snapshot'}{'default'} = [1];
$feature{'snapshot'}{'override'} = 1;

$feature{'search'}{'default'} = [1];

$feature{'grep'}{'default'} = [1];
$feature{'grep'}{'override'} = 1;

$feature{'show-sizes'}{'default'} = [1];
$feature{'show-sizes'}{'override'} = 1;

$feature{'avatar'}{'default'} = ['gravatar'];
$feature{'avatar'}{'override'} = 1;

$feature{'highlight'}{'default'} = [1];
$feature{'highlight'}{'override'} = 1;


Custom Theme

sudo mv /usr/share/gitweb/static/gitweb.js /usr/share/gitweb/static/gitweb.js.orig
sudo mv /usr/share/gitweb/static/gitweb.css /usr/share/gitweb/static/gitweb.css.orig
cd /tmp
git clone git://github.com/kogakure/gitweb-theme.git
cd gitweb-theme
sudo cp gitweb.css gitweb.js /usr/share/gitweb/static/





Authorized Keys for new users - be sure to add the bolded part
scp ~/.ssh/id_rsa.pub {git.server.ip}:/tmp/id_rsa.pub


# gitolite start
command="/usr/share/gitolite/gl-auth-command username",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AXYZAAAB3NzaC1yc2EAAAADAQABAAABAQDh5pv2q85zDaL0qNTZ4DaPKRar5zPiBprXDEBxKJLkHtFZ9hrc4/BCKflixOCwljRM/UqLMNEOfTyaWynBJbBGxttWtgxFf0R4XEfWwpGNqdU2A3aAx09r6Y+TB0sVXI4aCXk7QJME46K8j3FhAfAA3PfW77DELSoW/9t2nZjv50KpmJS7SbDLn85ncua0w6zhGW7c7hSCy/EObTNY7FhLR1qd4uYFuD6wA1n2UG98UbHzFJoBv8Q2Tfb4gBk4DoYliOKOH6NBTObKDLnNTJ90Jgcr7MGdZHPIrCroUZGlK6R username@computername


Adding SSH Keys to the repository server

cat id_rsa.pub >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
rm id_rsa_gitolite.pub




Adding a repository

Now we can create our repositories:
mkdir myrepo.git
cd !$
git --bare init



git init
git remote rm origin
Now we can add our new remote:

git remote add origin gitolite@git.servername.com:myrepo.git
git push origin master



Adding Users
you will need to add local users on the gitolite server in order for them to SCP their private keys over
adduser {username}

Git Software
Resources and Reading (Highly Recommended)

http://git-scm.com/book

I Also wrote another article about Git Branching check it out!

http://www.harryyeh.com/2012/08/git-version-control-quick-reference.html


Git Client on Windows
Download and install

http://code.google.com/p/msysgit/downloads/detail?name=Git-1.7.10-preview20120409.exe&can=2&q=

Install choosing the git bash option

Git Bash is the program we are using. Git gui is good for Browsing

http://nathanj.github.com/gitguide/tour.html

example: Setup Username

git config --global user.name {First Name Last Name}

git config --global user.email {email}

ssh-keygen -t rsa
cd ~
notepad .ssh/id_rsa.pub  (this will show the key in notepad)

Git Client on Linux
On your local machine type the following commands - we assume the user name is ubuntu
gitolite uses ssh keys to manage access to the git repositories. In the following steps, we set up gitolite to initialize its admin repository with your public key.

apt-get -y install git-core git-doc
ssh-keygen -t rsa -f id_rsa_gitolite
scp ~/.ssh/id_rsa.pub {git server}:/tmp/id_rsa.pub

example: Setup Username

git config --global user.name {First Name Last Name}
git config --global user.email {email}

4 comments:

Harry Yeh said...

Of course you can't download the theme for gitweb since the theme is on github.com so don't email me asking me this. Download the them when github.com is back up - you don't need the github theme right away anyways.

Jz said...

Hey,
i followed your tutorial but I cant do git clone http://git.example.com/repo.git on the gitweb service. Why?

Jz said...

Hey,
i followed your tutorial but I cant do git clone http://git.example.com/repo.git on the gitweb service. Why?

Ruihai said...

Reply: Jz

You need the private key(id_rsa) that is in the .ssh folder. Before you do git clone http://git.example.com/repo.git, you need to:
cd ~
cd .ssh
cat id_rsa
Copy and save everything from "-----BEGIN RSA....KEY-----" to "-----END...KEY-----" to a file and name it "private.key"
In Linux:
chmod 400 private.key
ssh-add private.key

In Windows:
Install pageant(putty)
add the key to pageant