Jenkins + Github + Ruby on Rails (RSpec, Cucumber) によるCI環境構築 (Ubuntu 12.04 server)

■Jenkins インストール

$ sudo apt-get install jenkins

Ruby関連インストール

$ sudo apt-get install gcc
$ sudo apt-get install build-essential
$ sudo apt-get install bison byacc gperf
$ sudo apt-get install zlib1g-dev
$ sudo apt-get install libreadline5 libreadline-dev
$ sudo apt-get install libncurses5 libncurses5-dev
$ sudo apt-get install libssl-dev
$ sudo apt-get install sqlite3 libsqlite3-dev
$ sudo apt-get install curl
$ sudo apt-get install ruby
$ sudo apt-get install rubygems
$ sudo gem install rubygems-update

参考:http://d.hatena.ne.jp/qkrhn081/20120201/1328068379

■Jenkinsユーザー設定

$ sudo -i
# passwd jenkins
# exit

■RVMインストール(Jenkinsユーザーで)

$ su - jenkins
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
$ vi .bashrc
(末尾に以下を追記)

if [[-s $HOME/.rvm/scripts/rvm]] ; then
  source $HOME/.rvm/scripts/rvm
fi

$ source .bashrc
$ type rvm | head -1
(「rvm は関数です」とか表示されていればOK)

■任意バージョンのRubyインストール

$ rvm install 1.9.2
$ rvm 1.9.2 --default
$ ruby -v
(「ruby 1.9.2p〜」と出ていればOK)

■Gitインストール

$ sudo apt-get install git-core

Github用の鍵作成

$ su - jenkins
$ ssh-keygen

.ssh/id_rsa.pub をコピーして、GithubのKeyに追加

Githubリポジトリをclone

$ git config --global user.name "XXXXX"
$ git config --global user.email XXXXX@gmail.com
$ git clone https://xxxxx@github.com/xxxxx/xxxxxx.git

■Jenkins設定

ブラウザでJenkins画面を開き、「プラグインの管理」へ移動。
以下をインストールする。
Github API plugin
Github plugin
・Git plugin
Ruby metrics
・Rake plugin

RVMパス、Gitパス、メールサーバーなど必要な項目を設定

新規ジョブを作成
ジョブの管理画面でGithubリポジトリ、ビルド処理、ビルド後処理などを設定
ビルドシェル

bundle install
rake db:migrate
reke db:test:prepare
rspec spec
rake cucumber

マシン再起動後、ジョブ実行

Git 共有リポジトリ運用の始め方 (bareリポジトリ)

■このエントリの前提条件
・gitインストール済み
・複数サーバー(svrA,svrB)上にそれぞれリポジトリあり
・svrAにbareリポジトリをつくる
・srvAにはSSHでアクセスできる
・srvAのローカルリポジトリには既にファイルがコミットされている

共有リポジトリ設定

【srvA】
■bareリポジトリを作成

$ mkdir ~/testapp.git
$ cd ~/testapp.git
$ git --bare init

■既存のローカルリポジトリ(~/testapp) を共有リポジトリへpush

$ cd ~/testapp
$ git push ~/testapp.git master

■今後変更していくローカルリポジトリをcloneで作成

$ mkdir ~/testapp_use
$ cd ~/testapp_use
$ git clone ~/testapp.git testapp

【srvB】
■srvAの共有リポジトリをclone (sshを利用)

$ cd ~
$ git clone srvA_user_name@srvA_ip_address:testapp.git testapp
srvA_user_name@srvA_ip_address's password: xxxxx

srvAの変更内容をpush後、srvBでpull
【srvA】
■ファイルの内容を変更、コミット

$ cd ~/testapp_use/testapp
$ vi readme.txt
$ git add .
$ git commit -m "fix on srvA"
$ git push

【srvB】
■変更内容をpull

$ cd ~/testapp
$ git pull

srvBの変更内容をpush後、srvAでpull
【srvB】
■ファイル内容を変更

$ cd ~/testapp
$ vi readme.txt
$ git add .
$ git commit -m "fix on srvB"
$ git push
srvA_user_name@srvA_ip_address's password: xxxxx

【srvA】
■変更内容をpull

$ cd ~/testapp_use/testapp
$ git pull

Ubuntu から Windows の共有プリンタを使う

$ sudo apt-get install cups

$ sudo apt-get install samba

$ sudo apt-get install samba-client

$ sudo vi /etc/cups/cups.conf

# Only listen for connections from the local machine.
Listen localhost:631
Listen 192.168.6.1:631
Listen /var/run/cups/cups.sock

# Restrict access to the server...

Order deny,allow
Allow From 192.168.6.6

# Restrict access to the admin pages...

Order deny,allow
Allow From 192.168.6.6

$ sudo /etc/init.d/cups restart

http://192.168.6.1:631 にアクセス

管理 プリンタ追加をクリック
Windows Printer via SAMBA を選択して「続ける」
「smb://smb://WORKGROUP;USER:PASSWORD@host/printer_name」を入力して「続ける」
※WORKGROUPは不要の場合もあり。
新しいプリンタ名に任意入力
メーカーで「Generic」を選択
モデルで「Generic PostScript Printer(en)」を選択
「デフォルトオプションの設定」をクリック

$ sudo vi /etc/samba/smb.conf
上段:変更前 下段:変更後

# load printers = yes
load printers = yes

        • -

; printcap name = /etc/printcap
printcap name = /etc/printcap

        • -

; printing = cups
printing = cups

$ sudo /etc/init.d/smbd restart

OpenVPN on Amazon EC2 (Ubuntu10.04) Windowsクライアント編

■前提条件
サーバー編で作成した鍵(client01.crt, client01.key, ca.crt)がクライアントマシンにあること

OpenVPNクライアントをダウンロード、インストール
http://openvpn.net/index.php/download.html

■クライアント鍵(.crt, .key)、サーバーCA公開鍵(ca.crt)を配置
「C:\Program Files\OpenVPN\config」にコピー。

■設定ファイルをコピー
「C:\Program Files\OpenVPN\sample-config\client.ovpn」を
「C:\Program Files\OpenVPN\config」にコピー。

■設定ファイルを編集(「xxx.xxx.xxx.xxx」はEC2(OpenVPNサーバー)に割り当てたElasticIP)
サーバー情報を変更

変更前

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote my-server-1 1194

変更後

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote xxx.xxx.xxx.xxx 1194

鍵情報を変更

変更前

# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
ca ca.crt
cert client.crt
key client.key

変更後

# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
ca "C:\\Program Files\\OpenVPN\\config\\ca.crt"
cert "C:\\Program Files\\OpenVPN\\config\\client01.crt"
key "C:\\Program Files\\OpenVPN\\config\\client01.key"

■クライアント起動
「C:\Program Files\OpenVPN\config\\client.ovpn」を右クリックして、
「Start OpenVPN on this config file」をクリック。
※もしくはGUIで起動(デスクトップにショートカットがあるはず)

パスフレーズを聞かれるので、クライアント鍵作成時に入力したものを入力後、クライアント起動されればOK
最後に

Fri Mar 30 14:14:15 2012 Initialization Sequence Completed

と出ていれば正常にVPN接続されている。
pingで疎通確認などもする。

OpenVPN on Amazon EC2 (Ubuntu10.04) サーバー編

■前提条件
・EC2インスタンスが作成済み
・ElasticIPが割り当て済み
・セキュリティグループは以下を開放
 ・22/tcp
 ・1194/udp

■参考ブログ(かなりお世話になった)
http://blog.livedoor.jp/dynakou/search?q=openvpn

■rootパスワードをセット

$ sudo -i
# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

OpenVPNをインストール

# apt-get install openvpn

ディレクトリコピー

# cp -rp /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn

■パラメータファイル編集

# vi /etc/openvpn/easy-rsa/2.0/vars
export KEY_COUNTRY="JP"
export KEY_PROVINCE="KYOTO"
export KEY_CITY="KYOTO"
export KEY_ORG="MyORG"
export KEY_EMAIL="foo@bar.com"

■source実行

# source /etc/openvpn/easy-rsa/2.0/vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/2.0/keys

■ゴミ掃除

# /etc/openvpn/easy-rsa/2.0/clean-all

認証局の証明書と鍵を作成(Enter連打)

# /etc/openvpn/easy-rsa/2.0/build-ca
Country Name (2 letter code) [JP]:
State or Province Name (full name) [KYOTO]:
Locality Name (eg, city) [KYOTO]:
Organization Name (eg, company) [MyORG]:
Organizational Unit Name (eg, section) :
Common Name (eg, your name or your server's hostname) [MyORG CA]:
Name
:
Email Address [foo@bar.com]:

■証明書と鍵ができていることを確認(ca.crt, ca.keyができている)

# ls -la /etc/openvpn/easy-rsa/2.0/keys/

■サーバー証明書を作成(Enter連打(最後に2回「y」))

# /etc/openvpn/easy-rsa/2.0/build-key-server server
Country Name (2 letter code) [JP]:
State or Province Name (full name) [KYOTO]:
Locality Name (eg, city) [KYOTO]:
Organization Name (eg, company) [MyORG]:
Organizational Unit Name (eg, section) :
Common Name (eg, your name or your server's hostname) [server]:
Name
:
Email Address [foo@bar.com]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password :
An optional company name
:
Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'JP'
stateOrProvinceName :PRINTABLE:'KYOTO'
localityName :PRINTABLE:'KYOTO'
organizationName :PRINTABLE:'MyORG'
commonName :PRINTABLE:'server'
emailAddress :IA5STRING:'foo@bar.com'
Certificate is to be certified until Mar 28 03:39:51 2022 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

■証明書ができていることを確認(server.crt, server.csr, server.key ができている)

# ls -la /etc/openvpn/easy-rsa/2.0/keys/

Diffie-Hellman keyの作成

# /etc/openvpn/easy-rsa/2.0/build-dh

■dh鍵ができているか確認(dh1024.pem ができている)

# ls -la /etc/openvpn/easy-rsa/2.0/keys/

■クライアント鍵の作成(Enter連打(最後に2回「y」))

# /etc/openvpn/easy-rsa/2.0/build-key-pass client01
Generating a 1024 bit RSA private key
.........................++++++
..........++++++
writing new private key to 'client01.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

        • -

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.

        • -

Country Name (2 letter code) [JP]:
State or Province Name (full name) [KYOTO]:
Locality Name (eg, city) [KYOTO]:
Organization Name (eg, company) [MyORG]:
Organizational Unit Name (eg, section) :
Common Name (eg, your name or your server's hostname) [client01]:
Name
:
Email Address [foo@bar.com]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password :
An optional company name
:
Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'JP'
stateOrProvinceName :PRINTABLE:'KYOTO'
localityName :PRINTABLE:'KYOTO'
organizationName :PRINTABLE:'MyORG'
commonName :PRINTABLE:'client01'
emailAddress :IA5STRING:'foo@bar.com'
Certificate is to be certified until Mar 28 04:35:48 2022 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

■クライアント鍵ができているか確認(client01.csr, client01.key ができている)

# ls -la /etc/openvpn/easy-rsa/2.0/keys/

■サーバー設定ファイルの編集

# cp -p /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
# gunzip /etc/openvpn/server.conf.gz
# vi /etc/openvpn/server.conf

■キー情報を変更
変更前

ca ca.crt
cert server.crt
key server.key # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
# openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh dh1024.pem

変更後

# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
key /etc/openvpn/easy-rsa/2.0/keys/server.key # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
# openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem

■バーチャルIP情報を変更
変更前

server 10.8.0.0 255.255.255.0

変更後(任意)

server 192.168.6.0 255.255.255.0

■クライアント間通信設定をコメント解除
変更前

;client-to-client

変更後

client-to-client

■OpenVPNサーバーの起動

# openvpn /etc/openvpn/server.conf
Fri Mar 30 13:04:29 2012 OpenVPN 2.1.0 i486-pc-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11] [MH] [PF_INET6] [eurephia] built on Jul 20 2010
Fri Mar 30 13:04:29 2012 NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables
Fri Mar 30 13:04:29 2012 Diffie-Hellman initialized with 1024 bit key
Fri Mar 30 13:04:29 2012 /usr/bin/openssl-vulnkey -q -b 1024 -m
Fri Mar 30 13:04:30 2012 TLS-Auth MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ]
Fri Mar 30 13:04:30 2012 ROUTE default_gateway=10.150.169.1
Fri Mar 30 13:04:30 2012 TUN/TAP device tun0 opened
Fri Mar 30 13:04:30 2012 TUN/TAP TX queue length set to 100
Fri Mar 30 13:04:30 2012 /sbin/ifconfig tun0 192.168.6.1 pointopoint 192.168.6.2 mtu 1500
Fri Mar 30 13:04:30 2012 /sbin/route add -net 192.168.6.0 netmask 255.255.255.0 gw 192.168.6.2
Fri Mar 30 13:04:30 2012 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:0 EL:0 AF:3/1 ]
Fri Mar 30 13:04:30 2012 Socket Buffers: R=[111616->131072] S=[111616->131072]
Fri Mar 30 13:04:30 2012 UDPv4 link local (bound): [undef]
Fri Mar 30 13:04:30 2012 UDPv4 link remote: [undef]
Fri Mar 30 13:04:30 2012 MULTI: multi_init called, r=256 v=256
Fri Mar 30 13:04:30 2012 IFCONFIG POOL: base=192.168.6.4 size=62
Fri Mar 30 13:04:30 2012 IFCONFIG POOL LIST
Fri Mar 30 13:04:30 2012 Initialization Sequence Completed

はじめてのHeroku

■前提
Ruby1.9.2とRails3.0.10がインストール済
・Herokuアカウント登録済
・gitインストール済(sudo apt-get install git-core)
・libssl-dev, openssl, libopenssl-ruby がインストール済
ssh-keygen で鍵情報作成済

Railsアプリ新規作成

$ mkdir -p ~/git/heroku
$ cd git/heroku
$ rails new hogeheroku01
$ cd hogeheroku01

$ vi Gemfile

group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
end

$ bundle install
$ rails server
(WEBrickが起動されるので、localhost:3000にアクセスして確認)

■scaffold

$ rails g scaffold Person name:string gender:string birthday:date
$ rake db:migrate
$ rails server
(WEBrickが起動されるので、localhost:3000/people にアクセスして確認)

■herokuコマンドインストール
(heroku Toolbelt 以前)

$ gem install heroku
$ sudo apt-get install libreadline-dev
$ cd ~/.rvm/src/ruby-1.9.2-p290/ext/readline/
$ ruby extconf.rb && make && make install
$ heroku keys:add
Email: xxxxxx@gmail.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn] Y
Generating new SSH public key.
Uploading ssh public key /home/xxxx/.ssh/id_rsa.pub

(heroku Toolbelt)

$ sudo apt-get install heroku-toolbelt

■gitコミット&Herokuデプロイ

$ cd ~/git/heroku/hogeheroku01
$ git init
$ git add .
$ git commit -m "first commit"
$ heroku create
(以下のように出ていればOK)
Createing xxxxxxxxx... done, 〜
http:xxxxxxxxx.heroku.com/ | git@heroku.com:xxxxxxxxx.git
Git remote heroku added
(HerokuのMyAppに作られているか確認。アプリ名はこの時点では適当につけられている)
(MyAppsでアプリ名を好きなものに変更)

$ vi .git/config
(以下の変更を行う)
url = git@heroku.com:変更したアプリ名.git

$ git push heroku master
(色々とpushされているログがでる)

$ heroku rake db:migrate
$ heroku open
(http://hogeheroku01.heroku.com/people にアクセス)
(RailsアプリがデプロイされていればOK)