set setting reset

脂肪と糖にはたらくやつ

Serverspec のセットアップ (rbenv / bundler)

Serverspec を使うために ruby の環境をセットアップします。 環境は CentOS 7 です。 O'REILLY を参考に進めていきます。

O'Reilly Japan - Serverspec

rbenv のインストール

rbenv は ruby のバージョン切替ができるツールです。 以下の手順でセットアップします。

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ rbenv --version
rbenv 0.4.0-192-g825de5d

インストールできる ruby のバージョン確認

$ rbenv install --list

ruby のインストール

今回は 2.2.3 をインストールします。

$ rbenv install 2.2.3
Downloading ruby-2.2.3.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/df795f2f99860745a416092a4004b016ccf77e8b82dec956b120f18bdc71edce
Installing ruby-2.2.3...
BUILD FAILED (CentOS Linux 7 using ruby-build 20151028-11-gbd22205)

Inspect or clean up the working tree at /tmp/ruby-build.20151119040352.4827
Results logged to /tmp/ruby-build.20151119040352.4827.log

Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.

Configure options used:
  --prefix=/home/vagrant/.rbenv/versions/2.2.3
  LDFLAGS=-L/home/vagrant/.rbenv/versions/2.2.3/lib
  CPPFLAGS=-I/home/vagrant/.rbenv/versions/2.2.3/include

エラーになってしまいました。パッケージが足りないようです。

$ sudo yum install -y openssl-devel readline-devel zlib-devel

もう一度インストールします。

$ rbenv install 2.2.3
Downloading ruby-2.2.3.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/df795f2f99860745a416092a4004b016ccf77e8b82dec956b120f18bdc71edce
Installing ruby-2.2.3...
Installed ruby-2.2.3 to /home/vagrant/.rbenv/versions/2.2.3

できました!
インストールした ruby を local 環境に設定します。

$ rbenv local 2.2.3
$ rbenv local
2.2.3
$ ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

bundler のインストール

Gemfile に記載したパッケージをインストールしてくれる bundler をインストールします。

$ gem install bundler

bundler を使って Serverspec をインストール

serverspec ディレクトリを作成して作業ディレクトリとします。 また、Gemfile を用意します。

$ pwd
/home/vagrant/serverspec
$ cat Gemfile
source 'https://rubygems.org'
gem 'serverspec'

インストールします。

$ bundle install
Fetching gem metadata from https://rubygems.org/.......
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Installing diff-lcs 1.2.5
Installing multi_json 1.11.2
Installing net-ssh 2.9.2
Installing net-scp 1.2.1
Installing net-telnet 0.1.1
Installing rspec-support 3.4.0
Installing rspec-core 3.4.1
Installing rspec-expectations 3.4.0
Installing rspec-mocks 3.4.0
Installing rspec 3.4.0
Installing rspec-its 1.2.0
Installing sfl 2.2
Installing specinfra 2.44.2
Installing serverspec 2.24.3
Using bundler 1.10.6
Bundle complete! 1 Gemfile dependency, 15 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

インストールできました。

サンプルの生成

postgresql の構築テストを行いたいので、target host name を test-db としてみます。

$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: n
Input target host name: test-db
 + spec/
 + spec/test-db/
 + spec/test-db/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

spec の配下にホストごとのディレクトリが生成され、それぞれにテストコードがあるようです。
ホストごとでは大規模になったときに大変とどこかに書いてあった気がします。。
とりあえずはこれで進めてみようと思います。

一旦ここまで。