CentOS7でGit鯖立てる

マシンが3台。今後増える予定。
CentOSなのはクラウド側の仕様。
6.5だけとか言ってたので、無理と断って7を入れてもらった。
ほんとはUbuntuServerとかのがいいのだけど。
それぞれでnodeで書かれたマイクロサービス群が動作する。
1台で毎分3万クライアントを捌く仕様。
マージンは10倍くらいあるので能力は1/10設計。
鯖増やすだけでスケールするように、各自が相互認識して、自動協調するように作ってある。
バージョン管理とデプロイ用に、1台でgit鯖を運用し、後の2台はcloneすることにした。
ほんとは、別にリポジトリ鯖がほしいところだけど、閉じてることもあって贅沢は言えない感じ。
手順をメモっておく。
ちなみに、sshのポートは内側のLAN環境だけ相互に開いている。めんどくさ。

gitをパッケージで入れる

git鯖になるマシンに、gitコマンドを入れる。

# yum install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
Package git-1.8.3.1-6.el7_2.1.x86_64 already installed and latest version
Nothing to do

あ、nodeパッケージ入れるのに、すでに入れてあったわ...

# rpm -qa | grep git-
git-1.8.3.1-6.el7_2.1.x86_64

ローカルマシンにも入っているのを確認した。

gitユーザーを作り、制限シェルgit-shellを適用する

gitユーザーからはリポジトリへのプッシュやプル以外のシェル操作ができなくなる。
制限シェルの適用は後のが楽そう。最初から制限シェルにしないで、普通にadduser gitのがいいかな。
めんどくさくなりそうな方でやってみる。
# useradd -s /usr/bin/git-shell git
gitユーザーにパスワード設定っているのかな? 直にはログインしないんだけど。付けずに進める。


gitユーザーになってみる。

# su git
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.

あらら、制限シェルが入ってない?
別パッケージかな?

# which git-shell
/bin/git-shell

いるじゃん。
あ、このメッセーは、制限シェルで動いたってことなのか...

接続用の公開鍵を入れるディレクトリとファイルを作る

制限シェルのgitユーザーはなんにもできないので、rootで作業する。

# mkdir /home/git/.ssh
# touch /home/git/.ssh/authorized_keys
# chown -R git:git /home/git/.ssh
# chmod 700 /home/git/.ssh 
# chmod 600 /home/git/.ssh/authorized_keys 

こんな感じ。

# find /home/git/.ssh -ls
100830768    0 drwx------   2 git      git            28 Sep  1 16:25 /home/git/.ssh
100830772    0 -rw-------   1 git      git             0 Sep  1 16:25 /home/git/.ssh/authorized_keys

ローカルマシンで鍵を作って、Git鯖に登録する

鍵作成。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kinneko/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kinneko/.ssh/id_rsa.
Your public key has been saved in /home/kinneko/.ssh/id_rsa.pub.
The key fingerprint is:
27:89:39:3c:a6:88:42:58:78:83:a7:f8:7e:41:0a:33 kinneko@saba02
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
| o               |
|o =              |
|E= ... o .       |
|+= o  B S .      |
|.o...o o o       |
|o o ..           |
|..  .            |
|  ..             |
+-----------------+

パスフレーズを入れると、~/.sshに鍵が保存される。

$ ls -l ~/.ssh/id*
-rw-------. 1 kinneko kinneko 1766 Sep  1 17:00 /home/kinneko/.ssh/id_rsa
-rw-r--r--. 1 kinneko kinneko  398 Sep  1 17:00 /home/kinneko/.ssh/id_rsa.pub

ローカルの鍵を鯖に書き込む。
gitアカウントのパスワード作らなかったので、めんどくさいのでコピペで。

$ cat id_rsa.pub
〜

鯖側でペースト。

# vi /home/git/.ssh/authorized_keys
〜

もう一台もやっておく。

git鯖でプロジェクトのディレクトリを作成

名前はprojectにしたけど任意で。

# mkdir -p /opt/git/project.git
# cd /opt/git/project.git

リポジトリの初期化

# git --bare init
Initialized empty Git repository in /opt/git/project.git/
# chown -R git:git /opt/git/project.git/ 

ローカルマシンでリポジトリを作成

ローカルにディレクトリを切る。

$ mkdir ~/myproject
$ cd ~/myproject/

リポジトリの初期化。

$ git init
Initialized empty Git repository in /home/kinneko/myproject/.git/

gitコマンドのユーザー設定。

$ git config --global user.email "kinneko@gmail.com"
$ git config --global user.name "kinneko"

リポジトリにファイルの追加。

$ touch 20160901
$ ls
20160901
$ git add .
$ git commit -m 'initial commit'
[master (root-commit) f7ebae9] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 20160901
$ git log
commit f7ebae94335a44821716c60250828941a1d31369
Author: kinneko <kinneko@gmail.com>
Date:   Thu Sep 1 17:27:51 2016 +0900

    initial commit

ローカルマシンからgit鯖にpushする

$ git remote add origin git@192.168.0.3:/opt/git/project.git
$ git push origin master
Enter passphrase for key '/home/kinneko/.ssh/id_rsa':
Counting objects: 3, done.
Writing objects: 100% (3/3), 205 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.0.3:/opt/git/project.git
 * [new branch]      master -> master

新規にクローンしてみる

$ mkdir ~/myproject2
$ cd ~/myproject2
$ git clone git@192.168.0.3:/opt/git/project.git
Cloning into 'project'...
Enter passphrase for key '/home/kinneko/.ssh/id_rsa':
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
$ cd project/
$ ls
20160901
$ git log
commit f7ebae94335a44821716c60250828941a1d31369
Author: kinneko <kinneko@gmail.com>
Date:   Thu Sep 1 17:27:51 2016 +0900

    initial commit

反映されているのがわかった。


あとは、ファイル追加してよしなに管理する。
おしまい。