November 18, 2011, Friday, 321

Setup a git and mercurial forge on NetBSD

From ezUnix

Jump to: navigation, search
                                    pdf_icon.png Download this article as a single PDF document 


Contents

Introduction

This howto was created considering you have a fresh new NetBSD 5.0.x installation.


Packages Installation

pkgin installation

The first thing we need to install is pkgin using the old traditionnal way of installing binaries on NetBSD:

# export PKG_PATH=ftp://ftp.fr.NetBSD.org/pub/pkgsrc/packages/NetBSD/amd64/5.0/All
# pkg_add -v pkgin

To be able to use pkgin we need to configure the following file : /usr/pkg/etc/pkgin/repositories.conf defining the repository we want to use, for example:

ftp://ftp.fr.NetBSD.org/pub/pkgsrc/packages/NetBSD/amd64/5.0/All

We can now update its database:

# pkgin update


Installing other components

To install the forge, we will need the following components :

  • git : scmgit
  • postgresql : postgresql84-server
  • le connecteur postgresql pour ruby : ruby18-postgres-pr
  • le binding imagemagick pour ruby : ruby18-RMagick
  • le serveur web ruby : thin
  • mercurial : mercurial

Let's install everything:

# pkgin install scmgit postgresql84-server ruby18-postgres-pr ruby18-RMagick thin mercurial

A few minutes later everything is installed and ready to be used.


Installing gems components

We will install ruby on rails using gem:

# gem install rail -v=2.2.2


Getting redmine

To finish preparation we need to fetch the latest redmine version. For example considering the 0.8.6 version:

# ftp http://rubyforge.org/frs/download.php/66633/redmine-0.8.6.tar.gz

And now it's time to configure everything.


Configuration

Adding users

Each different processes will be run in separate user privilege :

  • _redmine:_redmine for the thin webserver
  • _git:_git: for the git daemon
  • _hg:_hg: for mercurial
# groupadd _redmine
# groupadd _git
# groupadd _hg
# useradd -m -g _redmine _redmine
# useradd -m -g _git _git 
# useradd -m -g _hg _hg


Preparing the postgresql server

Fist we need to copy the init script so that postgresql could be started at boot time:

# cp /usr/pkg/share/examples/rc.d/pgsql /etc/rc.d/pgsql
# echo "pgsql=YES" >> /etc/rc.conf

We then need to initialize the database:

# $ /etc/rc.d/pgsql initdb -E unicode

And start the server

# /etc/rc.d/pgsql start

The user and the database for redmine need to be created

# su - pgsql
# createuser -I -E -P -S -R -D redmine # Enter the desired password we asked
# createdb -T template0 -O redmine -E UTF8


Now postgresql is ready


Configure redmine

Go in the repository where you want redmine to leave ~_redmine in my case:

# cd ~_redmine
# tar xvfz redmine-0.8.6.tar.gz 
# mv redmine-0.8.6 redmine # non obligatoire
# cd redmine


Configure the production database for redmine:

# cp config/database.yml.example config/database.yml

Now edit the file, the production part should be somethin like:

production:
 adapter: postgresql
 database: redmine
 host: localhost
 username: redmine
 password: my_pwd


Initialize the redmine database:

# RAILS_ENV=production rake db:migrate

Insert the default datas:

# RAILS_ENV=production rake redmine:load_default_data

Fix the user privileges so that redmine could work:

# mkdir tmp public/plugin_assets
# sudo chown -R _redmine:_redmine files log tmp public/plugin_assets
# sudo chmod -R 755 files log tmp public/plugin_assets


Configure the thin server

Consider here that redmine is installed in /home/_redmine/redmine

We need to create a rcng script to be able to start thin at boot time: /etc/rc.d/thin

#!/bin/sh
#
# PROVIDE: thin
# REQUIRE: DAEMON

. /etc/rc.subr

name="thin"
rcvar=$name
command="/usr/pkg/bin/$name"
stop_cmd="/usr/pkg/bin/thin -c /home/_redmine/redmine -e production stop"
command_args="-c /home/_redmine/redmine -e production -d -u _redmine -g _redmine start"

load_rc_config $name
run_rc_command $1


Activate it:

# chmod 755 /etc/rc.d/thin
# echo "thin=YES" >> /etc/rc.conf
# /etc/rc.d/thin start


We now have the thin webserver listening on the 3000 port.


Configure the git-daemon

Here consider that the repositories will be in /home/_git/repos.

As for thin we will need create a rcng script: /etc/rc.d/gitdaemon

#!/bin/sh
#
# PROVIDE: gitdaemon
# REQUIRE: DAEMON  

. /etc/rc.subr

name="gitdaemon"
rcvar=$name
pidfile="/var/run/$name.pid"
command="/usr/pkg/libexec/git-core/git-daemon"
command_args="--detach --base-path=/home/_git/repos --user=_git --group=_git --pid-file=$pidfile"

load_rc_config $name
run_rc_command $1


Activate it:

# chmod 755 /etc/rc.d/gitdaemon
# echo "gitdaemon=YES" >> /etc/rc.conf
# /etc/rc.d/gitdaemon start

When adding new daemon export ready repositories, in /home/_git/repos, they will be automatically available.

For example, considering we have /home/_git/repos/CBlog.git you can clone it like this:

git clone git://mybeautifulserver/Cblog.git

To be able tu push, you just have to play with ssh and the authorized_keys.
You can also restrict the shell using git-shell instead of /bin/sh for the _git user.


Configure mercurial

We will use hgwebdir.fcgi to share the mercurial repositories through http


Installing hgwebdir.fcgi

First fetch the mercurial sources as hgwebdir.fcgi isn't provided in the packages

# cd /tmp
# ftp http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
# tar xvfz mercurial-1.3.1.tar.gz
# mkdir /home/_hg/web
# cp mercurial-1.3.1/contrib/hgwebdir.fcgi /home/_hg/web
# chmod 755  /home/_hg/web/hgwebdir.fcgi


Edit /home/_hg/web/hgwebdir.fcgi, change the shebang:

#!/usr/bin/env python
should become
#!/usr/bin/env python2.5

and add following:

import os
os.chdir("/home/_hg")

after the first comment

Uncomment the following line:

os.environ["HGENCODING"] = "UTF-8"

add add this the line after:

os.environ['HGRCPATH'] = 'hgrc'

Or if you want you can juste create the following file: /home/_hg/web/hgwebdir.fcgi:

#!/usr/bin/env python2.5

import os
os.chdir("/home/_hg")

from mercurial import demandimport; demandimport.enable()

os.environ["HGENCODING"] = "UTF-8"
os.environ['HGRCPATH'] = 'hgrc'

from mercurial.hgweb.hgwebdir_mod import hgwebdir
from flup.server.fcgi import WSGIServer

WSGIServer(hgwebdir('hgweb.conf')).run()


Configure the web server

Here is an example using lighttpd, but every fastcgi ready web server can be used:

$HTTP["host"] =~ "hg.monsuperdomaine.tld" {
   accesslog.filename = "/var/log/lighttpd/hg_brokk_etoilebsd_net_access"

   url.rewrite-once = ( "^(/hgwebdir.fcgi/.*)$" => "$1", "^(/.*)$" => "/hgwebdir.fcgi$1")

   fastcgi.server = (
       "/" => (
           "hgwebdir" => (
               "bin-path" => "/home/_hg/web/hgwebdir.fcgi",
               "socket" => "/tmp/hg.socket",
               "check-local" => "disable",
           )
       )
   )
}


Configure hgwebdir.fcgi

Create a /home/_hg/hfweb.conf configuration file

[collections]
/home/_hg/repos  = /home/_hg/repos

[web]
baseurl =
encoding = UTF-8
style   = monoblue
motd = Brokk at your service sir! - A <a href="http://brokk.etoilebsd.net/" target="_blank">Brokk forge</a> service.
allow_archive = gz zip bz2


Configure the ssh access

Mercurial provide a hg-ssh script to restrict the ssh access, so let's use it

# cp /tmp/mercurial-1.3.1/hg-ssh /home/_hg</pre>

Now each user that needz to push on givent repository should have its ssh public key added in the /home/_hg/.ssh/authorized_keys using the following syntax:

command="/usr/pkg/bin/python2.5 /home/hg/hg-ssh repos/allowed_push_repo1 repos/allowed_push_repo2",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss BLAHBLAHMYBEAUTIFULLKEY

You can now clone through http :

hg clone http://hg.mybeautifullserver/myrepo

And push through ssh :

hg push ssh://[email protected]:repos/myrepo


Conclusion

You now have a complete forge with both git and mercurial repository support :


Cheers,

Bapt


Name (required):

Comment: