10 11 / 2011

Setup GitLab (Github clone) using Vagrant and Chef

Update: Nov 28, 2011: The box that is downloaded via vagrant might not work properly. Here is the box that I’d custom built to have chef pre-installed. You can download it here http://api2.ge.tt/0/8gA4QSA/0/blob/download

This post is Part 1 of the series Rails app with distributed deployment playground

Here we’ll be installing GitlabHQ.

I won’t be showing on how to install Vagrant with VirtualBox here. If you’re not familiar with Vagrant and how to set it up, then first watch the Railscasts 292 and install the Vagrant with VirtualBox.

Then you can start with the following commands in the terminal. First of all, we have to have our own chef-repo to contain the cookbooks.

You can clone my repo or you can use your own or others. Here I’ll be using mine chef-repo

cd
git clone git://github.com/millisami/chef-repo.git

Now, lets start with the gitlabhq setup.

cd
mkdir vagrants
cd vagrants
git clone git://github.com/gitlabhq/gitlabhq.git
cd gitlabhq
vagrant init

Modify the Vagrantfile as below

Vagrant::Config.run do |config|
  config.vm.box = "lucid32"
  config.vm.network "33.33.33.20"

  config.vm.customize do |vm|
    vm.name = "GitlabHQ"
    vm.memory_size = 384
  end

  config.vm.provision :chef_solo do |chef|
    chef.log_level = :debug
    chef.cookbooks_path = ["~/chef-repo/cookbooks"]
    chef.add_recipe "base"
    chef.add_recipe "rvm::install"
    chef.add_recipe "rvm::ruby_192"

    chef.json = { 
      :base => {
        :system_packages => ["tree", "htop", "vim-nox"]
        }
      }
    }
  end
end

Then from the terminal, run the following commands.

vagrant up
vagrant ssh

Once you login, install these dependencies.

sudo apt-get install -y git-core sqlite3 libsqlite3-dev gitosis libcurl4-openssl-dev python-setuptools

sudo visudo

Add the following line at the VERY BOTTOM of the file, if not at the end, it can be nullified by later entries

vagrant ALL=(ALL) NOPASSWD: ALL

Ctrl-X to leave, Y to save your changes. Logout logout and ssh vagrant ssh again to reflect the effect.

Lets install the gem dependencies.

cd /vagrant
rvmsudo gem install bundler --pre
bundle

# Seeding the database
RAILS_ENV=production bundle exec rake db:setup
RAILS_ENV=production bundle exec rake db:seed_fu

Write the admin account and its password which we’ve to use later while accessing gitlab.

...
Administrator account created:

login.........admin@local.host
password......5iveL!fe

Then, we have to setup the following:

sudo easy_install pygments
sudo echo 'export RAILS_ENV=production' >> ~/.bash_profile
sudo adduser --system --shell /bin/sh --gecos 'git version control' --group --disabled-password --home /home/git git
ssh-keygen -t rsa
sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub
sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

echo "gem: --no-rdoc --no-ri" > ~/.gemrc
rvmsudo gem install passenger
rvmsudo passenger-install-nginx-module

Creating an Upstart file for nginx

sudo vim /etc/init/nginx.conf

description "Nginx HTTP Server"

start on filesystem
stop on runlevel [!2345]

respawn

exec /opt/nginx/sbin/nginx -g "daemon off;"

Restart the VM

sudo shutdown -r now

Verify nginx urnning

sudo initctl list | grep nginx

You should see:

nginx start/running, process 678

…Or use a browser and visit http://33.33.33.20

Open nginx.conf to configure nginx

sudo vim /opt/nginx/conf/nginx.conf

Change the user to ‘vagrant’

Then inside the http {....}, add/replace a server block looking similar to this:

...
http {
  ...

    server {
    listen 80;
    server_name localhost;
    root /vagrant/public;
    passenger_enabled on;
    }
    ...
}

Restart nginx to pick up the changes

sudo service nginx stop; sudo service nginx start

or just

sudo start|stop nginx

Setting up the first repository

  1. Access http://33.33.33.20 on the HOST machine’s browser
  2. Login in using admin@local.host and 5iveL!fe
  3. Add your HOST key at http://33.33.33.20/keys
  4. Create the first project, say (Myproject)
  5. Add a remote. git remote add vagrant-chef git@33.33.33.20:Myproject.git
  6. Push with tracking branch. git push -u vagrant-chef master

Dealing with the Git and SSH keys gotchas

  1. Added user vagrant to group git since /home/git/repositories directory is owned by git

  2. Did one clone in ~/tmp directory git clone git@localhost:gitosis-admin.git gitosis-copy to add the RSA key to the list of known hosts. So, that when later via browser, the webserver will try to create a new repo. Otherwise you might end up with the infamous 500 error page.

Thats it for this part. Hang-on for the part 2 of the series Rails app with distributed deployment playground