入門 ChefSolo 自分メモ (4)

vagrant ネタで三回引っ張ったのかorz
とりあえず、何をしなければならないか、というと ChefSolo な環境構築なのか。色々確認してみるに以下な模様。

vagrant@precise32:~$ which ruby 
/opt/vagrant_ruby/bin/ruby
vagrant@precise32:~$ which gem
/opt/vagrant_ruby/bin/gem

gem で導入しましょう。

vagrant@precise32:~$ sudo gem install chef --no-ri --no-rdoc
Fetching: mixlib-cli-1.3.0.gem (100%)
Fetching: net-ssh-2.6.6.gem (100%)
Fetching: chef-11.4.0.gem (100%)
Successfully installed mixlib-cli-1.3.0 
Successfully installed net-ssh-2.6.6  
Successfully installed chef-11.4.0
3 gems installed

この時点の snapshot を、って別にいいか。

とりあえず

ハロワをヤッてみます。まずリポジトリを作って

$ git clone git://github.com/opscode/chef-repo.git

て、git が導入されとらぬ。git 導入してリトライしてリポジトリ作成。次に knife で cookbook を作成とのこと。その前に初期設定が、とありますね。

$ knife configure

全部デフォ回答で。で、以下か。

$ knife cookbook create hello -o cookbooks/
** Creating cookbook hello
** Creating README for cookbook: hello
** Creating CHANGELOG for cookbook: hello
** Creating metadata for cookbook: hello

で、recipe を云々なのか。

$ vi cookbooks/hello/recipes/default.rb

末端行に以下を追加して上書き保存。

log "Hello, Chef!"

で、以下な localhost.json を chef-solo 配下に投入。

$ cat localhost.json
// localhost.json
{
    "run_list" : [
        "recipe[hello]"
    ]
}

もう一つ、以下な solo.rb を同様に投入。

# solo.rb
file_cache_path "/tmp/chef-solo"
cookbook_path ["/home/vagrant/helloworld/chef-repo/cookbooks"]

で、sudo 付けて実行か。

$ sudo chef-solo -c solo.rb -j ./localhost.json

あら、動かんな。Cookbook hello not found とあります。あ、-c 付けてなかった。動きました。

$ sudo chef-solo -c solo.rb -j ./localhost.json
Starting Chef Client, version 11.4.0
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello, Chef!] action write

Chef Client finished, 1 resources updated

すばら。

パケジ導入

zsh を入れてみよう、とあります。recipe が以下な模様。

package "zsh" do
    action :install
end

これを default.rb に盛り込んで再度以下。

$ sudo chef-solo -c solo.rb -j ./localhost.json
Starting Chef Client, version 11.4.0
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello, Chef!] action write

  * package[zsh] action install
    - install version 4.3.17-1ubuntu1 of package zsh

Chef Client finished, 2 resources updated

導入されてます。

$ which zsh
/usr/bin/zsh

もっかい実行してみたら zsh な出力は以下でした。

  * package[zsh] action install (up to date)
Chef Client finished, 1 resources updated 

成程。あるいは複数を云々、とありますね。

%w{ zsh gcc make readline-devel }.each do |pkg|
    package pkg do
        action :install
    end
end

む、これうぶんつだと readline-devel なんてパケジはないな。Error executing action `install` on resource 'package[readline-devel]' って叱られました。
でびあん系だと libreadline-dev なのか。このあたりの切り替えもその内出てきてくれるはず。以降、目を通しつつ色々ヤりつつ控えを追記な方向です。

knife-solo が云々

ええと、親機から ssh で接続するには以下な設定にしておけば

  config.vm.network :private_network, ip: "192.168.33.10"

ssh で接続、も可能とのこと。

$ ssh vagrant@192.168.33.10

パスワードは vagrant になってました。ちなみに gem では knife-solo のバージョンはいくつになってるのかな。あ、rubygems 導入されてませんね。入れてみるか。

# gem search knife-solo -r

*** REMOTE GEMS ***

jgrevich-knife-solo (0.1.3)
knife-solo (0.2.0)
knife-solo_data_bag (0.3.1)

0.2.0 ですね。git で最新を取得して rake install が云々とかってありますね。rubygem-rake パケジも導入しておこう。で、knife-solo は親端末には導入しといた方が良いらしい。以下で、とのこと。

$ git clone git://github.com/matschaffer/knife-solo.git
$ cd knife-solo
$ rake install

とりま面白そうなので 0.3.0 を入れてみよう。で、clone して rake したらオチた。

# rake install
(in /root/knife-solo)
rake aborted!
no such file to load -- bundler/gem_tasks

ええと、gem で bundler を入れてリトライ。

# rake install
(in /root/knife-solo)
knife-solo 0.3.0.pre3 built to pkg/knife-solo-0.3.0.pre3.gem.
rake aborted!
Couldn't install gem, run `gem install /root/knife-solo/pkg/knife-solo-0.3.0.pre3.gem' for more detailed output

あらら。knife-solo-0.3.0.pre3.gem はできてますが。手動でヤッてみます。

# gem install ./pkg/knife-solo-0.3.0.pre3.gem
Building native extensions.  This could take a while...
ERROR:  Error installing ./pkg/knife-solo-0.3.0.pre3.gem:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/yajl-ruby-1.1.0 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/yajl-ruby-1.1.0/ext/yajl/gem_make.out

これ、ruby-devel 入れれば良いのかな。で、リトライ。

# rake 
(in /root/knife-solo)
Could not find gem 'parallel (>= 0) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.
rake aborted!
Command failed with status (7): [/usr/bin/ruby -I"lib:test" "/usr/lib/ruby/...]

むむ、と言いつつ gem i してみたら入ったぞ。

# gem install ./pkg/knife-solo-0.3.0.pre3.gem 
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Thanks for installing knife-solo!

If you run into any issues please let us know at:
  https://github.com/matschaffer/knife-solo/issues

If you are upgrading knife-solo please uninstall any old versions by
running `gem clean knife-solo` to avoid any errors.

See http://bit.ly/CHEF-3255 for more information on the knife bug
that causes this.
Successfully installed yajl-ruby-1.1.0
Successfully installed ipaddress-0.8.0
Successfully installed ohai-6.16.0
Successfully installed mime-types-1.21
Successfully installed rest-client-1.6.7
Successfully installed json-1.7.7
Successfully installed net-ssh-2.6.6
Successfully installed net-ssh-gateway-1.2.0
Successfully installed net-ssh-multi-1.1
Successfully installed highline-1.6.16
Successfully installed erubis-2.7.0
Successfully installed chef-11.4.0
Successfully installed thor-0.17.0
Successfully installed archive-tar-minitar-0.5.2
Successfully installed librarian-0.0.26
Successfully installed knife-solo-0.3.0.pre3
16 gems installed
(後略

つい --no-ri --no-rdoc するのを忘れてしまうなorz
とりあえず導入はできました。

ヤッてみる

とりあえず、以下を ~/.chef/knife.rb に入れとけ、とあります。

knife[:solo_path] = '/tmp/chef-solo'

あとはよく使うコマンドが列挙されてますね。

# <host> に chef-solo を導入
$ knife solo prepare <host>
$ knife solo prepare <user>@<host>

# <host> で chef-solo を実行
knife solo cook <host>

# run_list を個別に指定 (バージョン 0.3.0 から)
$ knife solo cook <host> -o hello::default, nginx::default

# <host> に転送したレシピ群を削除して掃除する
$ knife solo clean <host >

# 新規 Chef リポジトリを作る
$ knife solo init chef-repo


おそらく一連の手順として

  • 導入
  • 実行
  • レシピ群の削除

というナニなのだろうなと。あと、knife-solo は ssh なリモート接続ユーザが sudo をパスワード略で、な環境を期待しているとのこと。
つうことで vagrant なソレを一旦 halt -> destroy して再度 up します。一旦接続してみて中身確認。ええと、まっさらな vagrant の仮想リソースは gem まで使える状態だったのでそのまま knife solo prepare しても大丈夫なはず。ええと、Vagrantfile なソレが以下だったので

  config.vm.network :private_network, ip: "192.168.33.10"

こうかな。

# knife solo prepare vagrant@192.168.33.10
WARNING: No knife configuration file found
Bootstrapping Chef...
Enter the password for vagrant@192.168.33.10:

ここで vagrant 入力。む、chef とか apt で入ってますね。

--2013-03-25 11:38:32--  https://www.opscode.com/chef/install.sh
Resolving www.opscode.com (www.opscode.com)... 184.106.28.82
Connecting to www.opscode.com (www.opscode.com)|184.106.28.82|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6471 (6.3K) [application/x-sh]
Saving to: `install.sh'

100%[======================================>] 6,471       --.-K/s   in 0s

2013-03-25 11:38:33 (3.09 MB/s) - `install.sh' saved [6471/6471]

Downloading Chef 11.4.0 for ubuntu...
Installing Chef 11.4.0
Selecting previously unselected package chef.
(Reading database ... 29361 files and directories currently installed.)
Unpacking chef (from .../chef_11.4.0_i386.deb) ...
Setting up chef (11.4.0-1.ubuntu.11.04) ...
Thank you for installing Chef!
Generating node config 'nodes/192.168.33.10.json'...
ERROR: Errno::ENOENT: No such file or directory - nodes/192.168.33.10.json

なんとなく残念なカンジなのかどうか。とは言え、抽象化という観点で見た場合、レシピが間違いなく実行されさえすれば OK な訳か。
最後に微妙なエラーが出力されてるあたり、アレなんですがとりあえずスルー。

knife solo init

これ、chef-solo なリポジトリを作ってそれを node に適用、という理解で良いのかな。とりあえず作ってみます。

# knife solo init chef-solo
WARNING: No knife configuration file found
Creating kitchen...
Creating cupboards...
# ls
chef-solo Vagrantfile

git で云々とあるな。ここではちょとスルー。基本的には作ったら

  • git 管理にする
  • cookbook 新規作成はディレクトリに入って以下
    • knife cookbook create hello -o site-cookbooks

一旦ここでエントリ反映して以降に目を通します。