gitosisを導入

散々手こずらされたので、ここにログとして残しておきます。

gitosisのインストール

まず、http://centossrv.com/rpmforge.shtml などを参考に、RPMforgeを導入しておく。

gitをインストール。

# yum -y install git --enablerepo=rpmforge

python-setuptoolsが必要なのでインストール。

# yum -y install python-setuptools

gitosisもインストール。

# git clone git://eagain.net/gitosis.git
# cd gitosis
# python setup.py build
# python setup.py install

gitユーザーを作成

続いて、gitユーザーを作成する。名前はgitでなくてもいいと思うけど、とりあえず素直にgitで。

# useradd git

次に、リポジトリを作成。リポジトリの管理(ユーザーや鍵の管理)もgitを使って行うので、その管理を行うユーザーから、SSHで接続できるようにする必要がある。そのために必要な鍵(公開鍵)を、あらかじめコピーしておいて、登録。

[git ~]$ gitosis-init < id_rsa.pub
[git ~]$ chmod 0755 repositries/gitosis-admin.git/hooks/post_update

パスを通す

Pythonを/usr/local/python にインストールしたためか、「gitosis-serve: command not found」と怒られてしまったのでパスを通しておく。ただ、SSH接続時に実行されるコマンドなので、.bash_rc とかに書いても通らない。なので、まず sshd_config のPermitUserEnvironmentをオンにしておく。セキュリティ的なことは知らない。

# vim /etc/ssh/sshd_config
PermitUserEnvironment yes

# /etc/init.d/sshd restart

でもって、/usr/local/python/bin を通しておく。あと、「OSError: No such file or directory」みたいなエラーも吐いてくれるので、/usr/libexec/git-core も通しておく。これも環境的な問題かも。

[git ~]$ vim .ssh/environment
PATH=/usr/local/python/bin:/usr/libexec/git-core:$PATH

リポジトリの管理

最初に登録した鍵を持っているユーザーで管理する。まず、管理用のリポジトリ(gitosis-admin)をcloneする。

$ git clone git@localhost:gitosis-admin.git

こうすると、gitosis-adminって言うディレクトリが作成されるはず。ファイル構成はこんな感じ。

gitosis-admin/
  gitosis.conf   ← リポジトリの設定
  keydir/        ← 公開鍵を保存

編集したら、addしてcommitしてpushする。

$ git add . -A
$ git commit
$ git push

ユーザーを追加

ユーザーを追加するときは、gitosis.confのmembersに追加する。

$ vim gitosis.conf
[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = foo@localhost.localdomain root

さらに、公開鍵も保存しておく。もちろん、この公開鍵は、追加するユーザーが所有している鍵と対になるもの。

$ echo 'ssh-rsa ...' > keydir/root.pub

あとは、ちゃんとpushしておく。

リポジトリを追加

リポジトリを追加する場合は、gitosis.confのwritableに追加する。

$ vim gitosis.conf
[gitosis]

[group gitosis-admin]
writable = gitosis-admin hoge
members = foo@localhost.localdomain

pushすると、hogeというリポジトリが利用できるようになる。
使うためには、まずファイルを追加しておく。

$ mkdir hoge
$ cd hoge
$ git init
$ touch test
$ git add test
$ git commit

で、リモートリポジトリを指定してpushする。末尾には自動的に .git がつく。

$ git remote add origin git@localhost:hoge.git
$ git push origin master

これでちゃんとcloneしたりできるようになる。

$ git clone git@localhost:hoge.git