[How to] Gitlab on C1 server


#1

Gitlab is a Github-like service that you can install on your own server.

There are two main issues when installing it on C1 servers :

  • therubyracer segfaults when precompiling assets.
  • the online labs cloud uses dynamic IPs, so unless you managed to reserve a static IP, a reboot can render your server non-functionnal.

For the first problem, there are instructions here to install it along with libv8, but knowing nothing about ruby, I didn’t manage to make them work with the install process. However, it appears nodejs can be used as a drop-in replacement in this case, and I haven’t found broken functionalities yet.

The second problem is easily solved by a small script.

We are mostly going to follow the official gitlab installation process available here, with a few minor differences, which involve the removal of therubyracer dependency and management of configuration files.
We will also install the 7.7-stable version, versus the 7.6 in the doc.

We start after the base configuration of the server (ssh keys, iptables, etc)

# run as root!
apt-get update -y
apt-get upgrade -y
apt-get install sudo -y

# here we add nodejs and npm to the default install

apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake libkrb5-dev nodejs npm git-core

# we skip installing postfix, we can't send mail from the online cloud while it's in beta anyway

apt-get install -y git-core

# install ruby
mkdir /tmp/ruby && cd /tmp/ruby 
curl -L --progress -O http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz
tar xf ruby-2.1.5.tar.gz && cd ruby-2.1.5
./configure --disable-install-rdoc && make && make install

sudo gem install bundler --no-ri --no-rdoc
# configure bundle to use 3 of our 4 cores
# see http://robots.thoughtbot.com/parallel-gem-installing-using-bundler
bundle config --global jobs 3

This will take a while. While it is compiling, feel free to continue installing the rest in another terminal.

# Create user git
sudo adduser --disabled-password --gecos 'GitLab' git

Follow steps 4 and 5 from the official docs.

Now, we can actually install gitlab. We will do a little bit different when dealing with the configuration files in that we will put them in a separate directory for easier management, and then link them where gitlab expects them.

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-7-stable gitlab
sudo -u git -H mkdir gitlab_conf && cd gitlab_conf
sudo -u git -H mkdir config
sudo -u git -H cp /home/git/gitlab/config/gitlab.yml.example /home/git/gitlab_conf/config/gitlab_base.yml

Edit that last file, and set the e-mail. The host field will be taken care of by the script.

sudo -u git -H editor /home/git/gitlab_conf/config/gitlab_base.yml 

Here is the script, save it as /home/git/gitlab_conf/update_conf.py :

#!/usr/bin/env python2

from __future__ import print_function
import yaml
import urllib2
import json

def update_conf():
        """
        reads gitlab conf from template 'gitlab_base.yml' and "gitlab-shell_base.yml",
        updates ip adress, then dumps new conf into 'gitlab_autogen.yml'
        """
        
	conf_dir = "/home/git/gitlab_conf/config/"
        gl_conf = conf_dir + "gitlab_base.yml"
	gl_shell_conf = conf_dir + "gitlab-shell_base.yml"
        outfile = conf_dir + "gitlab_autogen.yml"
	outfile_shell = conf_dir + "gitlab-shell_autogen.yml"
        
        metadata = json.loads(urllib2.urlopen("http://169.254.42.42/conf?format=json").read())

	try:
        	with open(gl_conf) as f:
                	data_gl = yaml.safe_load(f)
	        data_gl["production"]["gitlab"]['host'] = str(metadata['public_ip']['address'])
	        with open(outfile, "w") as f:
        	        yaml.safe_dump(data_gl, f)
	except:
		print("WARNING: couldn't update gitlab configuration")
        
	try:
		with open(gl_shell_conf) as f:
			data_gl_shell = yaml.safe_load(f)
		data_gl_shell['gitlab_url'] = 'http://' + str(metadata['public_ip']['address']) + '/'
		with open(outfile_shell, "w") as f:
			yaml.safe_dump(data_gl_shell, f)
	except:
		print("WARNING: couldn't update gitlab-shell configuration")


if __name__ == '__main__':
        update_conf() 

Then run it.

chmod +x /home/git/gitlab_conf/update_conf.py
sudo -u git -H /home/git/gitlab_conf/update_conf.py

It should give you a warning telling you it couldn’t update gitlab-shell configuration, that’s ok for now. We’ll link it and continue with the install.

sudo -u git -H ln -s /home/git/gitlab_conf/config/gitlab_autogen.yml /home/git/gitlab/config/gitlab.yml
cd /home/git/gitlab
# Make sure GitLab can write to the log/ and tmp/ directories
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/
# Create directory for satellites
sudo -u git -H mkdir /home/git/gitlab-satellites
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/
# Make sure GitLab can write to the public/uploads/ directory
sudo chmod -R u+rwX  public/uploads
sudo -u git -H cp /home/git/gitlab/config/unicorn.rb.example /home/git/gitlab_conf/config/unicorn.rb

Edit that last file, change number of workers to 3

sudo -u git -H editor /home/git/gitlab_conf/config/unicorn.rb

sudo -u git -H ln -s /home/git/gitlab_conf/config/unicorn.rb /home/git/gitlab/config/unicorn.rb
sudo -u git -H mkdir /home/git/gitlab_conf/config/initializers
sudo -u git -H cp /home/git/gitlab/config/initializers/rack_attack.rb.example /home/git/gitlab_conf/config/initializers/rack_attack.rb
sudo -u git -H ln -s /home/git/gitlab_conf/config/initializers/rack_attack.rb /home/git/gitlab/config/initializers/rack_attack.rb
sudo -u git -H cp /home/git/gitlab/config/resque.yml.example /home/git/gitlab_conf/config/resque.yml
sudo -u git -H ln -s /home/git/gitlab_conf/config/resque.yml /home/git/gitlab/config/resque.yml
sudo -u git cp /home/git/gitlab/config/database.yml.postgresql /home/git/gitlab_conf/config/database.yml
sudo -u git -H ln -s /home/git/gitlab_conf/config/database.yml /home/git/gitlab/config/database.yml
sudo -u git -H chmod o-rwx /home/git/gitlab_conf/config/database.yml

Configure defauls for git, email should be the same as the one you put in your gitlab_base.yml.

sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "user@example.com"
sudo -u git -H git config --global core.autocrlf input

After the compilation/install of Ruby is done (from the first part earlier), we will launch bundle install, which will download all dependencies of gitlab. Before that, we need to remove all references to therubyracer, first from Gemfile then from Gemfile.lock :

# comment out the line which references therubyracer
sudo -u git -H editor /home/git/gitlab/Gemfile
# remove the line referencing therubyracer, and the two others below it (libv8, ref)
sudo -u git -H editor /home/git/gitlab/Gemfile.lock

Launch bundle install (this will take a long time).

sudo -u git -H bundle install --no-deployment --path vendor/bundle --without development test mysql aws
sudo -u git -H bundle install --deployment --without development test mysql aws

Install gitlab-shell

sudo -u git -H bundle exec rake gitlab:shell:install[v2.4.2] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
sudo -u git -H cp /home/git/gitlab-shell/config.yml.example /home/git/gitlab_conf/config/gitlab-shell_base.yml
sudo -u git -H /home/git/gitlab_conf/update_conf.py
sudo -u git -H ln -s /home/git/gitlab_conf/config/gitlab-shell_autogen.yml /home/git/gitlab-shell/config.yml

Continue with rest of installation

cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
sudo cp /home/git/gitlab/lib/support/init.d/gitlab /etc/init.d/gitlab
sudo update-rc.d gitlab defaults 21
sudo cp /home/git/gitlab/lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
sudo cp /home/git/gitlab/lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo unlink /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
# edit and set "server_name _"
sudo editor /etc/nginx/sites-available/gitlab

Set the python configuration script to run after configuration is brought up : create file /etc/network/if-up.d/gitlab-update-conf

#!/bin/sh

sudo -u git -H /home/git/gitlab_conf/update_conf.py

exit 0

then,

chmod +x /etc/network/if-up.d/gitlab-update-conf

Everything should be ok now, restart the services,

service nginx restart
service gitlab restart
# check that everything is ok : 
cd /home/git/gitlab/
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

If everything is green, your gitlab is set up, you can navigate to the IP in your browser and log in with the default login/pass : root // 5iveL!fe


#2

Hi @oceyral,

Thanks for this great tutorial :smile: