ようへいの日々精進XP

よかろうもん

serverspec を実際の運用でどのように使うかを考えてみた(1)

追記(2013/06/04)

Warning メッセージについて

*****************************************
Using a string as a subject is obsoleted.
Please use a subject type object instead.
See: http://serverspec.org/matchers.html
*****************************************

上記のメッセージについて serverspec を作られた mizzy さんから、以下のようなコメントを頂いた。

describe partisions do
を
describe file(partisions) do
にしないといけないですね。文字列を直接渡すやり方は、今後サポートされなくなります。

また、

あと、partisions は partitions が正しいのと、partition と単数の方がより自然ですね。(配列からひとつひとつ取り出した値がそこに入るので。)

typo までご指摘頂き、お恥ずかしいやら嬉しいやら。ご指摘頂いた点については記事上は修正致しました。また、後日、修正した内容をアップ致します。

追記

should_not について

  • should_not とは Rspce にて利用されているメソッド
  • serverspec をちゃんと触ろうと思った場合 Rspec の知識、経験が必要だと感じた

概要

  • 前回 redis サーバーをインストールしたホストを serverspec を使ってテストしてみた
  • ということで、今回は serverspec を運用でどのように使うかをとある仮想の現場を題材に考えてみる

参考

今回は下記を参考にさせて頂きました。

serverspec についての認識

  • こちらに書かれているようにサーバー上で稼働しているサービスや開いているポート等をテストするツール
  • 対象となるホストに対して SSH でアクセスするので物理サーバーでも仮想サーバーでもテストが可能

実際の運用(現場について)

実際に serverspec を利用することを想定した仮想現場の状況ついて整理してみる。

サーバーで使っている OS

サーバーパーティションの状態

  • / パーティション以外に /boot が存在すること

サーバーの詳細設定

  • ホスト名は vm01.xen.inokara.com となっていること
  • selinuxdisabled となっていること

サーバー上で稼働しているべきサービス

  • SSH(ポートは 22 番で稼働)
  • MySQL
  • postfix
  • Apache(ポート 80 番で稼働しており、/etc/httpd/conf.d/welcome.conf が無効になっていること)

サーバー上で稼働するべきではないサービス

テストを書く

整理した内容を元にいテストを書いていく。

spec/host1/default_spec.rb

require 'spec_helper'

# OS チェック
describe file('/etc/redhat-release') do
  it { should contain 'CentOS' }
end

# ホスト名チェック
describe file('/etc/sysconfig/network') do
  it { should contain 'vm01.xen.inokara.com' }
end

# パーティションの確認
%w{ / /boot }.each do |partition|
  describe file(partition) do
    it { should be_mounted.with( :type => 'ext4' ) }
  end
end

# SELinux 無効の確認
describe selinux do
  it { should be_disabled }
end

spec/host1/services_spec.rb

require 'spec_helper'

# SSH サービスの稼働と自動起動の確認
describe service('sshd') do
  it { should be_enabled }
  it { should be_running }
end

# SSH の Listen ポート確認
describe port(22) do
    it { should be_listening }
end

# MySQL のインストール確認
describe package('mysql-server') do
  it { should be_installed }
end 

# MySQL サービスの稼働と自動起動の確認
describe service('mysqld') do
  it { should be_enabled }
  it { should be_running }
end

# postfix のインストール確認
describe package('postfix') do
  it { should be_installed }
end 

# postfix サービスの稼働と自動起動の確認
describe service('postfix') do
  it { should be_enabled }
  it { should be_running }
end

# Apache のインストール確認
describe package('httpd') do
  it { should be_installed }
end

# httpd サービスの稼働と自動起動の確認
describe service('httpd') do
  it { should be_enabled   }
  it { should be_running   }
end

# httpd の Listen ポート確認
describe port(80) do
  it { should be_listening }
end

# welcome.conf が存在していないか確認                  
describe file('/etc/httpd/conf.d/welcome.conf') do
  it { should_not be_file }
end 

# iptables が稼働していない、自動起動しないか確認
describe service('iptables') do
  it { should_not be_enabled }
  it { should_not be_running }
end

テストを実行する

成功パターン

$ ASK_SUDO_PASSWORD=1 rake spec
/usr/local/rbenv/versions/1.9.3-p392/bin/ruby -S rspec spec/host1/default_spec.rb spec/host1/services_spec.rb
Enter sudo password: 
..
*****************************************
Using a string as a subject is obsoleted.
Please use a subject type object instead.
See: http://serverspec.org/matchers.html
*****************************************

.
*****************************************
Using a string as a subject is obsoleted.
Please use a subject type object instead.
See: http://serverspec.org/matchers.html
*****************************************

..................

Finished in 3.59 seconds
21 examples, 0 failures

途中に出力されている以下のメッセージについては...

*****************************************
Using a string as a subject is obsoleted.
Please use a subject type object instead.
See: http://serverspec.org/matchers.html
*****************************************

要調査。

失敗パターン

/etc/httpd/conf.d/welcome.conf が存在している場合...

$ pwd
/etc/httpd/conf.d
$ ls
README  welcome.conf
...............F..

Failures:

  1) File "/etc/httpd/conf.d/welcome.conf" 
     Failure/Error: it { should_not be_file }
       expected File "/etc/httpd/conf.d/welcome.conf" not to be file
     # ./spec/host1/services_spec.rb:54:in `block (2 levels) in <top (required)>'

Finished in 3.38 seconds
21 examples, 1 failure

Failed examples:

rspec ./spec/host1/services_spec.rb:54 # File "/etc/httpd/conf.d/welcome.conf" 
rake aborted!
/usr/local/rbenv/versions/1.9.3-p392/bin/ruby -S rspec spec/host1/default_spec.rb spec/host1/services_spec.rb failed

Tasks: TOP => spec
(See full trace by running task with --trace)

上記のように Failures 以下に失敗の理由等が出力される。

まとめ

  • 当初 should_not の存在を確認しないまま issue を出してしまい、すぐにコメントを頂き解決出来た。コメント頂いた Tomohiro さんに感謝!
  • OS のチェックまで必要?
  • ディストリビュージョンに依存した部分をどのように吸収するかについては引き続き勉強が必要