serverspec-runnerを使って複数のホストのテストレポートを作る


インフラの状態をテストするフレームワークserverspecをいつでも気軽に使えて、わかりやすい結果を表示するツールを作ってみました。

serverspec-runner

なにができるか

下記のような複数のsshログイン環境・serverspecレシピが混在した状況でyamlファイル1つで一括で実行できます。
(OSの差分吸収はserverspecに依存します)

また、テスト結果表示に下記のようなわかりやすいテーブルフォーマットやCSVを出力することができます


コンソールASCII Art風テーブルフォーマット

description result
nginx@anyhost-01(127.0.0.1)
nginx
起動を確認
Process "nginx"
should be running OK
File "/etc/logrotate.d/nginx"
should be file OK
should contain "rotate 14" NG
mysql@anyhost-02(127.0.0.1)
mysql
起動を確認
Process "mysqld"
should be running OK
Port "3306"
should be listening OK
hiracyユーザがあるかどうかの確認
Command "echo 'select * from mysql.user where user="hiracy" \G;' | mysql --user=hiracy --password=usopass" NG

markdownフォーマット(qiitaによりテーブル表示に変換されています)

使用方法

スケルトンの生成

まずは以下のコマンドでソースコード・設定雛形を生成します。

# ここでは自分のホームディレクトリのtestディレクトリを指定
serverspec-runner -r ~/test

# yを入力
want to create spec-tree to /var/tmp/test/spec? (y/n): y

# 以下の様なディレクトリが作られます

./test/
|-- scenario.yml
|-- spec
|   |-- example
|   |   `-- default.rb
|   `-- spec_helper.rb
`-- ssh_options_default.yml

scenario.ymlの作り方

実行する複数のserverspecレシピ、対象ホストを1つのyamlファイルで定義することができます
例えばwebサーバ(nginx)とdbサーバ(mysql)をテストするシナリオは以下のように自動生成されたscenario.ymlファイルを編集します

scenario.yml
service-hoge            # serverspecレシピのあるディレクトリ階層。yaml-hashで同じ階層表現にすること
  web
    - hoge-web-01       # service-hoge/web/***.rb を実行する対象のホスト名(---以下同名のシンボルで詳細を定義すること)
    - hoge-web-02       # ちなみにIPアドレス、名前解決できるホスト名直接書けば"---"以下のシンボルを省略可能
  db
    - hoge-db-01
---
hoge-web-01:            # シンボル実体 
  host: 192.168.0.11    # シンボルにアクセスするためのIPアドレス又は名前解決できるホスト名
hoge-web-02:
  host: 192.168.0.11
hoge-db-01:
  ssh_opts:             # ssh option define (not required)
    port: 2222          # ssh port option   (not required)
    user: "ssh-hiracy"  # ssh user option   (not required)
  dbuser: "db-hiracy"   # host attributes for mysql user.

尚、このようにホスト毎にNet::SSHで定義されているsshオプションが使用できますが、scenario.ymlで定義しない場合は自動生成されたssh_options_default.ymlにて同様にsshオプションを記載することでデフォルト設定として使用することができます

serverspecレシピ作成

serverspecのレシピについては(本家様)[http://serverspec.org/resource_types.html]と同じように作成して下さい。

mkdir -p ~/test/spec/service-hoge/web/
vim ~/test/spec/service-hoge/web/default.rb
~/test/spec/service-hoge/web/default.rb
require 'spec_helper'

describe "nginx" do

  describe "起動を確認" do
    describe process('nginx') do
      it { should be_running }
    end
  end

  describe file('/etc/logrotate.d/nginx') do
    it { should be_file }
    it { should contain "rotate 14" }
  end
end

結果出力のオプション

結果表示は以下の4つのパターンから選択できます

ASCII Art

serverspec-runner -t aa
+------------------------------------------------+
|description                            | result |
+------------------------------------------------+
|service-hoge-web@anyhost-01(127.0.0.1) |        |
|  nginx                                |        |
|    起動を確認                          |        |
|      Process "nginx"                  |        |
|        should be running              |   OK   |
|    File "/etc/logrotate.d/nginx"      |        |
|      should be file                   |   OK   |
|      should contain "rotate 14"       |   NG   |
+------------------------------------------------+

Markdown Table

serverspec-runner -t mkd
|description                            | result |
|:--------------------------------------|:------:|
|service-hoge-web@anyhost-01(127.0.0.1) |        |
|  nginx                                |        |
|    起動を確認                          |        |
|      Process "nginx"                  |        |
|        should be running              |   OK   |
|    File "/etc/logrotate.d/nginx"      |        |
|      should be file                   |   OK   |
|      should contain "rotate 14"       |   NG   |

CSV

Excelに読ませて、即試験項目書

serverspec-runner -t csv
description,,,,,result
service-hoge-web@anyhost-01(127.0.0.1),,,,,
,nginx,,,,
,,起動を確認,,,
,,,Process "nginx",,
,,,,should be running,OK
,,File "/etc/logrotate.d/nginx",,,
,,,should be file,,OK
,,,should contain "rotate 14",,NG

Boolean

scenario.yml全部成功すれば'ok'それ以外は'ng'を返すので監視用スクリプトとかに使えるかも

serverspec-runner -t bool
ng

尚、全てサンプルはcd ~/testにてディレクトリ移動後に実行していますが、オプションscenario.ymlやserverspecレシピのある親ディレクトリを指定できます。

まとめ

ということでserverspecを簡単に上司や顧客や出向先に見せやすいようにできるので使ったり提案してみてはどうでしょうか
改善案や不具合があったら是非ここやgithubまで