itamae + serverspecで テスト駆動インフラやってみた #shibuyarb

Post on 16-Jul-2015

4.201 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Copyright Drecom Co., Ltd. All Rights Reserved.

itamae + Serverspecでテスト駆動インフラやってみた

2015/03/18 shibuya.rb@sue445

Copyright Drecom Co., Ltd. All Rights Reserved.

sue445

● drecom○ 社内ツールとか社内ライブラリとか○ サーバサイドをアプリからインフラまで浅く広く

見守り業務○ PO (Precure Ojisan)

● RubyKaja 2014 @Shibuya.rb

自己紹介

Copyright Drecom Co., Ltd. All Rights Reserved.

【今期の嫁】キュアトゥインクル

Copyright Drecom Co., Ltd. All Rights Reserved.

今期の黄色は肩背中が丸出し!!!!

Copyright Drecom Co., Ltd. All Rights Reserved.

【本妻】キュアピース

Copyright Drecom Co., Ltd. All Rights Reserved.

● 経緯

● itamaeについて

● Serverspecについて

● 実際のテスト駆動インフラの流れ

● itamaeレシピを社内に公開した

● 参考書籍

● 所感

Agenda

Copyright Drecom Co., Ltd. All Rights Reserved.

● 【目的】Railsミドル全部入りのOpenStackのJenkins Slave

のスナップショットを作りたかった

● ミドル全部入りならインフラがchefで作ったスナップショットが

あるとの情報

● 差分はJenkinsユーザの作成とrbenvのインストールくらいあ

ればよさそう○ が、インフラのchefのレシピはカオスすぎて素人にはハードル高かった

● 全部入りスナップショット対して差分をitamaeでプロビジョニ

ングすることに

● せっかくなので今流行のテスト駆動インフラに挑戦してみた

経緯

Copyright Drecom Co., Ltd. All Rights Reserved.

itamae

● クックパッドが作ったプロビジョニングツール

○ http://itamae.kitchen/

○ https://speakerdeck.com/ryotarai/itamae-infra-as-

code-xian-zhuang-que-ren-hui

● Ruby製、シンプルで軽量なChefみたいなやつ

● DSLなので記述がシンプル

● DSLもRubyのコードなので適度にリファクタリングできる

● プラグインをgemから取り込める

○ 依存性をbundlerで管理できる

○ rubygems.orgに公開していればみんなが使える

Copyright Drecom Co., Ltd. All Rights Reserved.

● @mizzy氏が作ったインフラ構成をテストするためのツール

○ http://serverspec.org/

○ http://www.oreilly.co.jp/books/9784873117096/

● Ruby製

○ RSpecベースだけどテンプレをコピペするだけなので

RSpec使ったこと無くても問題ない

● 特定のプロビジョニングツール(itamaeとかChefとかPuppet

とかAnsible)に依存しないで使える

Serverspec

Copyright Drecom Co., Ltd. All Rights Reserved.

● OS毎のコマンドの違いを吸収するgem

○ https://github.com/serverspec/specinfra

○ Debianならapt-get install使うとか、yumならyum instll

使うとか

● Serverspecから切りだされてgem化されている

○ capistranoとsshkitのような関係

● itamaeもSpecinfraに依存しているので、Specinfraの中身

を知ってたらitamaeとSercerspecの両方で役に立つ

Specinfra

Copyright Drecom Co., Ltd. All Rights Reserved.

1. インフラの確認項目をテストコードで書く

2. 期待したエラーが出ることを確認 (Red)

3. サーバで適用したいインフラコードを書く

4. テストコードが通っていることを確認(Green)

5. ダメなら3に戻る

6. 必要ならリファクタリング (Refactor)

7. 1に戻る

実際のテスト駆動インフラの流れ

Copyright Drecom Co., Ltd. All Rights Reserved.

http://www.slideshare.net/t_wada/the-spirit-of-tdd/27

テスト駆動インフラでもTDDの黄金の回転

Copyright Drecom Co., Ltd. All Rights Reserved.

● 新しいサーバを作ると自分のhomeディレクトリがないので手

軽に作りたい

【例】自分のhomeディレクトリを作る

Copyright Drecom Co., Ltd. All Rights Reserved.

describe file("/home/sueyoshi_go") do

it { should be_directory }

it { should be_mode 700 }

it { should be_owned_by "sueyoshi_go" }

it { should be_grouped_into "drecom" }

end

TARGET_HOST=xxx.xxx.xxx.xxx rspec spec/myhome_spec.rb

Serverspecのテストコード(myhome_spec.rb)

Copyright Drecom Co., Ltd. All Rights Reserved.

Serverspecのテストコード(myhome_spec.rb)

/home/sueyoshi_go が ディレクトリで パーミッション700で ownerがsueyoshi_go で drecom groupに所属していること

describe file("/home/sueyoshi_go") do

it { should be_directory }

it { should be_mode 700 }

it { should be_owned_by "sueyoshi_go" }

it { should be_grouped_into "drecom" }

end

TARGET_HOST=xxx.xxx.xxx.xxx rspec spec/myhome_spec.rb

Copyright Drecom Co., Ltd. All Rights Reserved.

Serverspecのテストコード(myhome_spec.rb)

/home/sueyoshi_go が ディレクトリで パーミッション700で ownerがsueyoshi_go で drecom groupに所属していること

describe file("/home/sueyoshi_go") do

it { should be_directory }

it { should be_mode 700 }

it { should be_owned_by "sueyoshi_go" }

it { should be_grouped_into "drecom" }

end

TARGET_HOST=xxx.xxx.xxx.xxx rspec spec/myhome_spec.rb

それっぽく読める!

Copyright Drecom Co., Ltd. All Rights Reserved.

directory "/home/sueyoshi_go" do

mode "700"

owner "sueyoshi_go"

group "drecom"

not_if "ls /home/sueyoshi_go"

end

itamae ssh -h xxx.xxx.xxx.xxx -p 10022 recipes/myhome.rb

itamaeのレシピ (myhome.rb)

Copyright Drecom Co., Ltd. All Rights Reserved.

itamaeのレシピ (myhome.rb)

ディレクトリが存在していなければ/home/sueyoshi_go を作って パーミッション700で ownerがsueyoshi_goで groupをdrecomにすること

directory "/home/sueyoshi_go" do

mode "700"

owner "sueyoshi_go"

group "drecom"

not_if "ls /home/sueyoshi_go"

end

itamae ssh -h xxx.xxx.xxx.xxx -p 10022 recipes/myhome.rb

Copyright Drecom Co., Ltd. All Rights Reserved.

itamaeのレシピ (myhome.rb)

ディレクトリが存在していなければ/home/sueyoshi_go を作って パーミッション700で ownerがsueyoshi_goで groupをdrecomにすること

directory "/home/sueyoshi_go" do

mode "700"

owner "sueyoshi_go"

group "drecom"

not_if "ls /home/sueyoshi_go"

end

itamae ssh -h xxx.xxx.xxx.xxx -p 10022 recipes/myhome.rb

それっぽく読める!

Copyright Drecom Co., Ltd. All Rights Reserved.

● 目視確認の自動化

○ インフラコードを書く、適用、確認のサイクルを素早く回

せる

● インフラコードがリファクタリングできる

○ バグってもテストコードで検知できる

テスト駆動インフラのメリット

Copyright Drecom Co., Ltd. All Rights Reserved.

● itamae-recipe-jenkins_slave (Jenkins slaveを作るための

レシピ)

○ jenkinsユーザの作成

○ sshの鍵の転送

○ rbenvのインストール

○ jenkinsユーザの鍵を使ってのmasterからslaveへの疎

通確認

○ mysqlやmemcachedなどの起動

● (itamaeやServerspecを書いたことない状態から)開発期

間1週間ちょい

itamaeレシピを社内に公開した

Copyright Drecom Co., Ltd. All Rights Reserved.

source "https://rubygems.org"

gem "itamae"

gem "itamae-plugin-recipe-rbenv", ">= 0.2.2"

group :test do

gem "serverspec"

end

source "http://gem.xxxxxxx.com" do

gem "itamae-plugin-resource-sudo_remote_file", ">= 0.0.2"

gem "specinfra-plain_sudo", ">= 0.0.2"

end

Gemfile

社内gem

Copyright Drecom Co., Ltd. All Rights Reserved.

● itamae-plugin-resource-sudo_remote_file

○ レシピ実行ユーザが直接コピーできないファイルを

remote_fileするためのプラグイン

○ sudo cp /tmp -> sudo chmod -> scp -> remove

tmp_file な感じ

● specinfra-plain_sudo

○ 弊社環境だと sudo /bin/sh ~ が動かないことがあった

のでspecinfraのsudo実行周辺にモンキーパッチあてて

● どっちも弊社環境に起因していることが原因のような気がす

るので社外公開はしない予定

作った社内gem

Copyright Drecom Co., Ltd. All Rights Reserved.

● WEB+DB PRESS vol.80

○ http://gihyo.

jp/magazine/wdpress/archive/2014/vol80

○ テスト駆動インフラ特集

● Serverspec

○ http://www.oreilly.co.jp/books/9784873117096/

○ 体系的にまとまってる

○ 付録でitamaeについてもふれられてる

参考書籍

Copyright Drecom Co., Ltd. All Rights Reserved.

● itamaeはそんなにドキュメント充実してない

○ ソースは大した量じゃないのでサクッと読める

○ githubのスライドが一番よくまとまってるw

○ そんなに学習コスト高くない

● Serverspecはドキュメント充実してる

○ ベースはrspecなので普段から使ってるととっつきやす

いはず

○ オライリー本おすすめ

● どっちも日本人が開発してるので日本語でググった時の資

料が充実してる

● コマンドの作業履歴(.bash_historyとか)をコードとして残せ

るのはでかい

所感

top related