cookbook とか用意しなくても chef を試せる patty.rb というスクリプトを書いた

オハイ! Chef はよくも悪くも仕組みが大げさなので、思いついたときにぱっと試すのが面倒くさいと前々から思っていた。
なんとか cookbook を作らなくても簡単に試す仕組みを作れないかなと思って、自分のアプリケーションから Chef Solo を呼び出す — Gist とか 手軽に chef-solo を実行するためにスケルトンを作ってくれるやつ (ユースケース要検討) — Gist とか色々試していたんだけど、先日Opscode 社 Chef ハンズオン・トレーニングに出席したときに shef がかなり理想に近いことが分かったので、shef のコードを読み、インタラクティブ・シェルではなく 1 枚のファイルの中にレシピとテンプレートを記述できる仕組みを作ってみた。

こんな感じのレシピファイルを作成して、

  • recipe.rb
file '/tmp/hello.txt' do
  content "hello!\n"
end

template "/tmp/hello2.txt" do
  local true
  source "hello"
  variables :name => 'world'
end

template "/tmp/foobar.txt" do
  local true
  source "foobar"
  variables :name => 'tily'
end

__END__
@@ hello
hello, <%= @name %>
@@ foobar
this is another template: name is <%= @name %>

こんな風に実行すると、

  • 実行方法
ruby patty.rb recipe.rb

chef-solo っぽくスタンドアロンで Chef を実行できる。

  • 実行結果
bash-3.2$ ruby patty.rb recipe.rb 
Loading....[2012-11-10T12:48:58+09:00] DEBUG: Building node object for linwttaa.local
[2012-11-10T12:48:58+09:00] DEBUG: Extracting run list from JSON attributes provided on command line
[2012-11-10T12:48:58+09:00] DEBUG: Applying attributes from json file
[2012-11-10T12:48:58+09:00] DEBUG: Platform is mac_os_x version 10.7
[2012-11-10T12:48:58+09:00] INFO: Run List is []
[2012-11-10T12:48:58+09:00] INFO: Run List expands to []
done.

[2012-11-10T12:48:59+09:00] INFO: Processing file[/tmp/hello.txt] action create ((eval) line 2)
--- /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-tempfile20121110-23863-enwe0z 2012-11-10 12:48:59.000000000 +0900
+++ /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-diff20121110-23863-vo0wbv     2012-11-10 12:48:59.000000000 +0900
@@ -0,0 +1 @@
+hello!
[2012-11-10T12:48:59+09:00] INFO: entered create
[2012-11-10T12:48:59+09:00] INFO: file[/tmp/hello.txt] created file /tmp/hello.txt
[2012-11-10T12:48:59+09:00] INFO: Processing template[/tmp/hello2.txt] action create ((eval) line 6)
[2012-11-10T12:48:59+09:00] DEBUG: Current content's checksum:  
[2012-11-10T12:48:59+09:00] DEBUG: Rendered content's checksum: 853ff93762a06ddbf722c4ebe9ddd66d8f63ddaea97f521c3ecc20da7c976020
--- /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-tempfile20121110-23863-4d1443 2012-11-10 12:48:59.000000000 +0900
+++ /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-rendered-template20121110-23863-8svaro        2012-11-10 12:48:59.000000000 +0900
@@ -0,0 +1 @@
+hello, world
[2012-11-10T12:48:59+09:00] INFO: template[/tmp/hello2.txt] updated content
[2012-11-10T12:48:59+09:00] INFO: Processing template[/tmp/foobar.txt] action create ((eval) line 12)
[2012-11-10T12:48:59+09:00] DEBUG: Current content's checksum:  
[2012-11-10T12:48:59+09:00] DEBUG: Rendered content's checksum: 04fc2a8f0333e6412a408e347ca49aab17b1b179f42be3d4cc2fd42fc4bcdf50
--- /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-tempfile20121110-23863-1woio2b        2012-11-10 12:48:59.000000000 +0900
+++ /var/folders/9q/tq6hwz3j7qd_nctywdr_4mp40000gn/T/chef-rendered-template20121110-23863-1ai39uj       2012-11-10 12:48:59.000000000 +0900
@@ -0,0 +1 @@
+this is another template: name is tily
[2012-11-10T12:48:59+09:00] INFO: template[/tmp/foobar.txt] updated content

実験的なスクリプトなのであまり品質は高くないかも。でもなんかこういう「Chef をカンタンに使える仕組み」がもっとあっていいと思っている。