こんにちは! JQです。
前回は『 Amazon EC2編~ENI~ 』ということで、EC2でENIを試してみました。
今回は『Amazon OSS編~Serverspecパート①~』と題して、Serverspecを触ってみたいと思います。
**
Serverspecとは
**
Rspecを利用したサーバの状態管理のテストツールになります。
それでは実際に試してみましょう。
**
環境構築
**
インストール
1.先ずはgemからServerspecをインストールします。
1# gem install serverspec雛形の作成
2.次に適当なディレクトリで下記コマンドを実行します。
ローカルをテストしてみる為、Execを選択します。
1# serverspec-init2Select a backend type:34 1) SSH5 2) Exec (local)67Select number: 289 + spec/10 + spec/localhost/11 + spec/localhost/httpd_spec.rb12 + spec/spec_helper.rb13 + Rakefileサンプルファイルの確認
3.サンプルファイルを確認してみます。
チェック内容は以下になります。
・httpdがインストールされているか
・自動起動が設定されているか
・起動しているか
・設定ファイルのチェックが存在するか
・設定ファイルのServerNameがlocalhostか
1# cat spec/localhost/httpd_spec.rb2require 'spec_helper'34describe package('httpd') do5 it { should be_installed }6end78describe service('httpd') do9 it { should be_enabled }10 it { should be_running }11end1213describe port(80) do14 it { should be_listening }15end1617describe file('/etc/httpd/conf/httpd.conf') do18 it { should be_file }19 it { should contain "ServerName localhost" }20end実行
4.yumでインストールだけを行い、実際に試してみます。
1# rake spec2/usr/local/bin/ruby -S rspec spec/localhost/httpd_spec.rb3.FFF.F45Failures:67 1) Service "httpd"8 Failure/Error: it { should be_enabled }9 chkconfig --list httpd | grep 3:on10 expected #<Serverspec::Type::Service:0x007f1b72a95120 @name="httpd"> to be enabled1112# ./spec/localhost/httpd_spec.rb:8:in `block (2 levels) in <top (required)>'1314 2) Service "httpd"15 Failure/Error: it { should be_running }16 ps aux | grep -w -- httpd | grep -qv grep17 expected #<Serverspec::Type::Service:0x007f1b72a95120 @name="httpd"> to be running1819# ./spec/localhost/httpd_spec.rb:9:in `block (2 levels) in <top (required)>'2021 3) Port "80"22 Failure/Error: it { should be_listening }23 netstat -tunl | grep -- :80\24 expected #<Serverspec::Type::Port:0x007f1b72a98870 @name=80> to be listening2526# ./spec/localhost/httpd_spec.rb:13:in `block (2 levels) in <top (required)>'2728 4) File "/etc/httpd/conf/httpd.conf"29 Failure/Error: it { should contain "ServerName localhost" }30 grep -q -- ServerName\ localhost /etc/httpd/conf/httpd.conf3132 expected #<Serverspec::Type::File:0x007f1b72aa2cf8 @name="/etc/httpd/conf/httpd.conf"> to contain "ServerName localhost"3334# ./spec/localhost/httpd_spec.rb:18:in `block (2 levels) in <top (required)>'3536Finished in 0.58765 seconds376 examples, 4 failures3839Failed examples:4041rspec ./spec/localhost/httpd_spec.rb:8 # Service "httpd"42rspec ./spec/localhost/httpd_spec.rb:9 # Service "httpd"43rspec ./spec/localhost/httpd_spec.rb:13 # Port "80"44rspec ./spec/localhost/httpd_spec.rb:18 # File "/etc/httpd/conf/httpd.conf"45rake aborted!46/usr/local/bin/ruby -S rspec spec/localhost/httpd_spec.rb failed4748Tasks: TOP => spec49(See full trace by running task with --trace)6個中4個がエラーとして出力されています。
サーバを修正して再度実行してみます。
1# rake spec2/usr/local/bin/ruby -S rspec spec/localhost/httpd_spec.rb3......45Finished in 0.16844 seconds66 examples, 0 failuresエラーがなく、設定が成功している事がわかります。
いかがでしたでしょうか?
次回は『 Amazon OSS編~Serverspecパート②~ 』ということで、SSHでのチェックをしてみたいと思います。
お楽しみに!
——————————————————————————————————
ナレコムクラウドのFacebookに『いいね!』をクリックして頂くと
最新のお役立ちレシピが配信されます★
——————————————————————————————————