2013年3月28日木曜日

Installing GitHub 5.0 behind a proxy



GitLabをProxy内で立てたらえらくハマったので書きとめる
元記事 : installation gitlab 5.0
動作環境 : VMware上で動作

1. Packages / Dependencies

apt-getのproxyをとおすためにapt.confにproxyの設定を書きます
hoge@ubuntu:~$ sudo vim /etc/apt/apt.conf

Acquire::http::Proxy "http://proxy.yourcompany.com:8080/";
Acquire::https::proxy "https://proxy.yourcompany.com:8080/";
Acquire::ftp::proxy "ftp://proxy.yourcompany.com:8080/";

apt-get update で ubuntuを最新化
hoge@ubuntu:~$ sudo su
root@ubuntu:/home/hoge# apt-get update
root@ubuntu:/home/hoge# apt-get upgrade -y
root@ubuntu:/home/hoge# exit

他のpackageをインストールする。(postfix以外)
hoge@ubuntu:~$ sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev

postfixをインストールする。
hoge@ubuntu:~$ sudo apt-get install -y postfix

画面が表示されますが、ひるむことなく <OK> -> No Configuration...

Pythonのversionを確認する。ubuntu 12.04の初期状態の場合 2.7.3のため何もする必要なし。
hoge@ubuntu:~$ python --version
Python 2.7.3

2. Ruby

作業用フォルダに移り
hoge@ubuntu:~$ mkdir /tmp/ruby && cd /tmp/ruby

--proxyオプションでproxyを設定してcurlを実行
hoge@ubuntu:/tmp/ruby$ curl --proxy http://proxy.yourcompany.com:8080 --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p327.tar.gz | tar xz 

rubyのmake & install は普通に進む
hoge@ubuntu:/tmp/ruby$ cd ruby-1.9.3-p327
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ ./configure
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ make
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ sudo make install

install後rubyのversionを確認
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ ruby --version
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]

gemのproxy設定をするため「.gemrc」を作成
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ vi ~/.gemrc

http_proxyの行を追加する
http_proxy: http://proxy.yourcompany.com:8080

bundlerをインストールする
hoge@ubuntu:/tmp/ruby/ruby-1.9.3-p327$ sudo gem install bundler

3. System Users

ユーザgitを作る。
hoge@ubuntu:~$ sudo adduser --disabled-login --gecos 'GitLab' git

あとのGEMでgitコマンドがproxy経由でアクセスできるよう下ごしらえ
参考:まさおのブログ(表):proxyサーバ経由でgitを使う方法
corkscrewをinstall
hoge@ubuntu:~$ sudo apt-get install corkscrew

git-proxy.shを作成
hoge@ubuntu:~$ sudo vi /usr/local/bin/git-proxy.sh

git-proxy.shの内容は以下の通り。proxyサーバを指定する。
#/bin/bash
CORKSCREW=`which corkscrew`
$CORKSCREW proxy.yourcompany.com 8080 $1 $2

パーミッションを変更
hoge@ubuntu:~$ sudo chmod 755 /usr/local/bin/git-proxy.sh

gitユーザのbashrcを開く
hoge@ubuntu:~$ sudo vi /home/git/.bashrc

以下を追加
export HTTP_PROXY=http://proxy.yourcompany.com:8080/
export HTTPS_PROXY=http://proxy.yourcompany.com:8080/
export FTP_PROXY=http://proxy.yourcompany.com:8080/
export GIT_PROXY_COMMAND=/usr/local/bin/git-proxy.sh

4. GitLab shell

ユーザgitに切り替える
hoge@ubuntu:~$ sudo su git

gitのhomeへ移動
hoge@ubuntu:/home/hoge$ cd /home/git

gitlab-shellをclone
hoge@ubuntu:~$ git clone https://github.com/gitlabhq/gitlab-shell.git

あとは流れるままに
git@ubuntu:~$ cd gitlab-shell
git@ubuntu:~$ git checkout v1.1.0
git@ubuntu:~$ git checkout -b v1.1.0
git@ubuntu:~/gitlab-shell$ cp config.yml.example config.yml
git@ubuntu:~/gitlab-shell$ ./bin/install
mkdir -p /home/git/repositories: true
mkdir -p /home/git/.ssh: true
touch /home/git/.ssh/authorized_keys: true
chmod -R ug+rwX,o-rwx /home/git/repositories: true
find /home/git/repositories -type d -print0 | xargs -0 chmod g+s: true

5. Database - MySQL

ここをみてMySQLをセットアップします
一旦gitユーザから出て
git@ubuntu:~/gitlab-shell$ exit

Install the database packages
hoge@ubuntu:~$ sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

MySQLのrootユーザのパスワードを登録します。

repeat..

mysqlにrootでログイン
hoge@ubuntu:~$ mysql -u root -p
Enter password:

Create User(パスワードを指定して)
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '*********';
Query OK, 0 rows affected (0.00 sec)

Create Tableして
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
Query OK, 1 row affected (0.01 sec)

GRANT して
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
Query OK, 0 rows affected (0.01 sec)

MySQLを終了
mysql> \q
Bye

MySQLの接続確認
hoge@ubuntu:~$ sudo -u git -H mysql -u gitlab -p -D gitlabhq_production
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 5.5.29-0ubuntu0.12.10.1 (Ubuntu)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \q
Bye

6. GitLab

gitのhome directoryに移動して
hoge@ubuntu:~$ cd /home/git

gitにログイン
hoge@ubuntu:/home/git$ sudo su git

gitlabをclone
hoge@ubuntu:~$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab

Switched to 5-0-stable.
git@ubuntu:~$ cd gitlab
git@ubuntu:~/gitlab$ git checkout 5-0-stable
Branch 5-0-stable set up to track remote branch 5-0-stable from origin.
Switched to a new branch '5-0-stable'
git@ubuntu:~/gitlab$ exit

Configure it.
hoge@ubuntu:/home/git$ cd /home/git/gitlab
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
hoge@ubuntu:/home/git/gitlab$ sudo chown -R git log/
hoge@ubuntu:/home/git/gitlab$ sudo chown -R git tmp/
hoge@ubuntu:/home/git/gitlab$ sudo chmod -R u+rwX  log/
hoge@ubuntu:/home/git/gitlab$ sudo chmod -R u+rwX  tmp/
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H mkdir /home/git/gitlab-satellites
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H mkdir tmp/pids/
hoge@ubuntu:/home/git/gitlab$ sudo chmod -R u+rwX  tmp/pids/
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

Configure GitLab DB settings (MySQL)
hoge@ubuntu:/home/git/gitlab$ sudo -u git cp config/database.yml.mysql config/database.yml

open database.yml
hoge@ubuntu:/home/git/gitlab$ sudo -u git vim config/database.yml

username, passwordを変更する
#
# PRODUCTION
#
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: gitlabhq_production
  pool: 5
  username: root
  password: ********  ## rootのパスワードを記述
  # host: localhost
  # socket: /tmp/mysql.sock

Install Gems
hoge@ubuntu:/home/git/gitlab$ sudo gem install charlock_holmes --version '0.6.9'

bundle install (corkscrewが威力を発揮します)
hoge@ubuntu:/home/git/gitlab$ sudo su git
git@ubuntu:~/gitlab$ bundle install --deployment --without development test postgres
Fetching source index from https://rubygems.org/
Fetching git://github.com/gollum/gollum.git
remote: Counting objects: 12802, done.
remote: Compressing objects: 100% (5571/5571), done.
...

Initialise Database and Activate Advanced Features
git@ubuntu:~/gitlab$ bundle exec rake gitlab:setup RAILS_ENV=production
...
== Seed from /home/git/gitlab/db/fixtures/production/001_admin.rb

Administrator account created:

login.........admin@local.host
password......5iveL!fe
git@ubuntu:~/gitlab$ exit

起動スクリプトを設定
hoge@ubuntu:/home/git/gitlab$ sudo curl --proxy http://proxy.yourcompany:8080 --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/
5-0-stable/init.d/gitlab
hoge@ubuntu:/home/git/gitlab$ sudo chmod +x /etc/init.d/gitlab
hoge@ubuntu:/home/git/gitlab$ sudo update-rc.d gitlab defaults 21

インストール状況を確認
GitLabの環境:
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
System information
System:         Ubuntu 12.10
Current User:   git
Using RVM:      no
Ruby Version:   1.9.3p327
Gem Version:    1.8.23
Bundler Version:1.3.4
Rake Version:   10.0.3

GitLab information
Version:        5.0.0
Revision:       4606380
Directory:      /home/git/gitlab
DB Adapter:     mysql2
URL:            http://localhost
HTTP Clone URL: http://localhost/some-project.git
SSH Clone URL:  git@localhost:some-project.git
Using LDAP:     no
Using Omniauth: no

GitLab Shell
Version:        1.2.0
Repositories:   /home/git/repositories/
Hooks:          /home/git/gitlab-shell/hooks/
Git:            /usr/bin/git

チェックを実行: 何かErrorが出ていたら要確認。
hoge@ubuntu:/home/git/gitlab$ sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
Checking Environment ...

Git configured for git user? ... no
  Try fixing it:
  sudo -u git -H git config --global user.name  "GitLab"
  sudo -u git -H git config --global user.email "gitlab@localhost"
  For more information see:
  doc/install/installation.md in section "GitLab"
  Please fix the error above and rerun the checks.

...

Projects have satellites? ... can't check, you have no projects

Checking GitLab ... Finished

GitLab Serviceをstart!
hoge@ubuntu:/home/git/gitlab$ sudo service gitlab start
Gitlab service started

7. Nginx

nginxをinstall
hoge@ubuntu:~$ sudo apt-get install nginx

Download an example site config:
hoge@ubuntu:~$ sudo curl --proxy http://proxy.yourcompany.com:8080 --output /etc/nginx/sites-available/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/5-0-stable/nginx/gitlab
...
hoge@ubuntu:~$ sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

IPAddressとserver_nameを指定します
hoge@ubuntu:~$ sudo vim /etc/nginx/sites-available/gitlab

server {                                                                          
  listen 192.168.137.xxx:80 default_server;   # change it
  server_name yourservername.com;             # change it
  root /home/git/gitlab/public;

nginxを再起動
hoge@ubuntu:~$ sudo service nginx restart
Restarting nginx: nginx.

Reference

http://elinux.org/Using_git_with_a_proxy

1 件のコメント:

Tejuteju さんのコメント...

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Ruby on Rails Online Course