LoginSignup
8
9

More than 5 years have passed since last update.

Chefのレシピをデバッグしてみよう。

Last updated at Posted at 2016-02-26

関連記事

自作したvim-pluginレシピの不具合

chef-shell(旧shef)

  • chef-shellはchef-soloやchef-client環境で使うものである。
  • ホスト(レシピを扱うローカル端末)ではなくゲスト(ノード)で実行する。

起動

  • わからないと困るオプションだけ暗記しておく。
    • -s:chef-soloモードで起動
    • -z:chef-clientモードで起動
    • -a:スタンドアロンモードで起動(デフォルト)
    • -c:使用する設定ファイル
    • -j:chef-soloモードのみ有効でrun_listを含むNodeのAttributeとして使うJSONファイルを指定
  • helpを入力するとコマンドとその説明が表示される。
  • 終了したい場合はexitを入力する。
$ cd chef-solo
$ chef-shell -s -c solo.rb -j dna.json

loading configuration: solo.rb
Session type: solo
Loading...done.

This is the chef-shell.
 Chef Version: 12.7.2
 http://www.chef.io/
 http://docs.chef.io/

run `help' for help, `exit' or ^D to quit.

Ohai2u vagrant@dev!
chef (12.7.2)>

レシピモード

  • レシピを確認するとき使う。
  • include_recipeを使って問題があるレシピをインクルードする。
  • run_chefでインクルードしたレシピを実行する。
  • すこし長いがdirectory部分に問題があることはすぐわかった。
  • node['user']が読み込まれてない。
chef (12.7.2)> recipe_mode
chef:recipe (12.7.2)> include_recipe "vim-plugin::default"
chef:recipe (12.7.2)> run_chef
[2016-02-25T11:51:33+00:00] INFO: Processing directory[/home//.vim] action create (vim-plugin::default line 10)
[2016-02-25T11:51:33+00:00] DEBUG: Providers for generic directory resource enabled on node include: [Chef::Provider::Directory]
[2016-02-25T11:51:33+00:00] DEBUG: Provider for action create on resource directory[/home//.vim] is Chef::Provider::Directory

================================================================================
Error executing action `create` on resource 'directory[/home//.vim]'
================================================================================

Chef::Exceptions::InsufficientPermissions
-----------------------------------------
Cannot create directory[/home//.vim] at /home//.vim due to insufficient permissions

Resource Declaration:
---------------------
# In /home/vagrant/chef-solo/cookbooks-3/vim-plugin/recipes/default.rb

 10: directory "/home/#{node['user']}/.vim" do
 11:   owner node['user']
 12:   group node['group']
 13:   mode '0755'
 14:   action :create
 15: end
 16:

Compiled Resource:
------------------
# Declared in /home/vagrant/chef-solo/cookbooks-3/vim-plugin/recipes/default.rb:10:in `from_file'

directory("/home//.vim") do
  action [:create]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  path "/home//.vim"
  declared_type :directory
  cookbook_name :"vim-plugin"
  recipe_name "default"
  mode "0755"
end

[2016-02-25T11:51:33+00:00] INFO: Running queued delayed notifications before re-raising exception
Chef::Exceptions::InsufficientPermissions: directory[/home//.vim] (vim-plugin::default line 10) had an error: Chef::Exceptions::InsufficientPermissions: Cannot create directory[/home//.vim] at /home//.vim due to insufficient permissions

... snip ...

solo.rb

  • chef-soloの設定ファイル。chef-clientの設定ファイルはclient.rbである。
  • solo.rbを確認したら、environmentがデフォルト(_default)になっていた。
  • userやgroupなどのattributeはenvironments(development.json)に保持している。
  • node(dev.json)のchef_environmentもdevelopmentに設定したがそれが適用されてない。
[vagrant@dev chef-solo]$ cat solo.rb
node_name "dev"

base = File.expand_path('..', __FILE__)

nodes_path                File.join(base, 'nodes')
role_path                 File.join(base, 'roles')
data_bag_path             File.join(base, 'data_bags')
encrypted_data_bag_secret File.join(base, 'data_bag_key')
environment_path          File.join(base, 'environments')
environment               "_default"
ssl_verify_mode           :verify_peer

cookbook_path []
cookbook_path << File.join(base, 'cookbooks-1') # /Users/devnote/Documents/study/chef/vendor/bundle/ruby/2.2.0/gems/knife-solo-0.5.1/lib/knife-solo/resources/patch_cookbooks
cookbook_path << File.join(base, 'cookbooks-2') # /Users/devnote/Documents/study/chef/cookbooks
cookbook_path << File.join(base, 'cookbooks-3') # /Users/devnote/Documents/study/chef/site-cookbooks
  • solo.rbの_defaultをdevelopmentで修正した。
$ sed -i -e "s/_default/development/g" solo.rb
$ cat solo.rb
node_name "dev"

base = File.expand_path('..', __FILE__)

nodes_path                File.join(base, 'nodes')
role_path                 File.join(base, 'roles')
data_bag_path             File.join(base, 'data_bags')
encrypted_data_bag_secret File.join(base, 'data_bag_key')
environment_path          File.join(base, 'environments')
environment               "development"
ssl_verify_mode           :verify_peer

cookbook_path []
cookbook_path << File.join(base, 'cookbooks-1') # /Users/devnote/Documents/study/chef/vendor/bundle/ruby/2.2.0/gems/knife-solo-0.5.1/lib/knife-solo/resources/patch_cookbooks
cookbook_path << File.join(base, 'cookbooks-2') # /Users/devnote/Documents/study/chef/cookbooks
cookbook_path << File.join(base, 'cookbooks-3') # /Users/devnote/Documents/study/chef/site-cookbooks
  • レシピモードでもう一度確認したら、今度は正常に実行された。
  • スクリプトは問題がない。
chef:recipe (12.7.2)> include_recipe "vim-plugin::default"
chef:recipe (12.7.2)> run_chef
[2016-02-25T11:57:55+00:00] INFO: Processing directory[/home/vagrant/.vim] action create (vim-plugin::default line 10)
[2016-02-25T11:57:55+00:00] DEBUG: Providers for generic directory resource enabled on node include: [Chef::Provider::Directory]
[2016-02-25T11:57:55+00:00] DEBUG: Provider for action create on resource directory[/home/vagrant/.vim] is Chef::Provider::Directory
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing directory[/home/vagrant/.vim/backup] action create (vim-plugin::default line 18)
[2016-02-25T11:57:55+00:00] DEBUG: Providers for generic directory resource enabled on node include: [Chef::Provider::Directory]
[2016-02-25T11:57:55+00:00] DEBUG: Provider for action create on resource directory[/home/vagrant/.vim/backup] is Chef::Provider::Directory
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing directory[/home/vagrant/.vim/bundle] action create (vim-plugin::default line 18)
[2016-02-25T11:57:55+00:00] DEBUG: Providers for generic directory resource enabled on node include: [Chef::Provider::Directory]
[2016-02-25T11:57:55+00:00] DEBUG: Provider for action create on resource directory[/home/vagrant/.vim/bundle] is Chef::Provider::Directory
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing directory[/home/vagrant/.vim/swap] action create (vim-plugin::default line 18)
[2016-02-25T11:57:55+00:00] DEBUG: Providers for generic directory resource enabled on node include: [Chef::Provider::Directory]
[2016-02-25T11:57:55+00:00] DEBUG: Provider for action create on resource directory[/home/vagrant/.vim/swap] is Chef::Provider::Directory
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing execute[install neobundle] action run (vim-plugin::default line 26)
[2016-02-25T11:57:55+00:00] DEBUG: Skipping execute[install neobundle] due to not_if ruby block
[2016-02-25T11:57:55+00:00] INFO: Processing cookbook_file[/home/vagrant/.vimrc_scss_indent] action create (vim-plugin::default line 36)
[2016-02-25T11:57:55+00:00] DEBUG: cookbook_file[/home/vagrant/.vimrc_scss_indent] checksumming file at /home/vagrant/.vimrc_scss_indent.
[2016-02-25T11:57:55+00:00] DEBUG: cookbook_file[/home/vagrant/.vimrc_scss_indent] staging /home/vagrant/chef-solo/cookbooks-3/vim-plugin/files/default/.vimrc_scss_indent to /home/vagrant/..vimrc_scss_indent20160225-6651-1b7m1of
[2016-02-25T11:57:55+00:00] DEBUG: calculating checksum of /home/vagrant/..vimrc_scss_indent20160225-6651-1b7m1of to compare with 05be8b3325aa3c67568c37216114a0a530a55dc9d199d7476a82d306b969222e
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing cookbook_file[/home/vagrant/.vimrc] action create (vim-plugin::default line 36)
[2016-02-25T11:57:55+00:00] DEBUG: cookbook_file[/home/vagrant/.vimrc] checksumming file at /home/vagrant/.vimrc.
[2016-02-25T11:57:55+00:00] DEBUG: cookbook_file[/home/vagrant/.vimrc] staging /home/vagrant/chef-solo/cookbooks-3/vim-plugin/files/default/.vimrc to /home/vagrant/..vimrc20160225-6651-1i2xivg
[2016-02-25T11:57:55+00:00] DEBUG: calculating checksum of /home/vagrant/..vimrc20160225-6651-1i2xivg to compare with bd258180199e8ac6b76be5c6de6067e4396d5563490666a932ebe1bfc02e0133
[2016-02-25T11:57:55+00:00] DEBUG: Found target_mode == current_mode, not updating mode
[2016-02-25T11:57:55+00:00] DEBUG: Found target_uid == current_uid, not updating owner
[2016-02-25T11:57:55+00:00] DEBUG: Found target_gid == current_gid, not updating group
[2016-02-25T11:57:55+00:00] INFO: Processing execute[install vim plugin via neobundle] action run (vim-plugin::default line 43)
[2016-02-25T11:57:55+00:00] DEBUG: Providers for generic execute resource enabled on node include: [Chef::Provider::Execute]
[2016-02-25T11:57:55+00:00] DEBUG: Provider for action run on resource execute[install vim plugin via neobundle] is Chef::Provider::Execute
not found in 'runtimepath': "ftdetect/*.vim"
not found in 'runtimepath': "colors/mopkai.vim"
Error detected while processing /home/vagrant/.vimrc:
line  707:
E185: Cannot find color scheme 'mopkai'
[neobundle] command AlpacaTagsSet is not found.
[neobundle] Update started: (2016/02/25 11:57:55)
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"
[neobundle] ( 1/65) [                    ] vimproc.vim
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"

... snip ...

neobundle] (65/65) [====================] vim-haml
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"
not found in 'runtimepath': "autoload/vimproc.vim"
[neobundle] (65/65): |vim-haml| Updated
[neobundle]
[neobundle] Installed bundles:
[neobundle]   vimproc.vim

... snip ...

[neobundle]   vim-haml
[neobundle] Update done: (2016/02/25 12:01:06)
[2016-02-25T12:01:06+00:00] INFO: execute[install vim plugin via neobundle] ran successfully
 => true
chef:recipe (12.7.2)>
  • knife-soloを実行するとき、-Eオプションを付きでEnvironmentsを指定する必要がある。
  • knife-soloを使うたびに-Eオプションを付けないと_defaultで指定されてしまう。
  • 最初からやり直し
    • vagrant destroy
    • vagrant up
    • bin/knife solo bootstrap dev -E development
  • しかし、今度もneoinstall処理が早かった。ノードを調べたがやはり設置されてない。

script処理

問題点

  • リソース内で定義したスクリプトはroot権限で実行される。
  • user、group、environmentを指定するとそのユーザで実行されると思っていたが、そうではないみたいだ。
  • そして、vimをtarballで設置してもyumで設置しないと実行するときエラーが発生する。
vim: command not found

解決策

  • 最終的には次のように変更した。
    • yumでもvimを設置する。しかし、vimを実行する場合は以前tarballで設置されたvimが優先される。(/usr/local/binが優先されるため)
    • ユーザを指定してスクリプトを実行する。
    • 実行が終わったら.vim-neoinstalledというファイルを生成する。
    • .vim-neoinstalledというファイルが存在しない場合に限ってスクリプトを実行するようになる。
package 'vim' do
  action :installl
end

... snip ...

execute 'install vim plugin via neobundle' do
  user node['user']
  group node['group']
  cwd "/home/#{node['user']}"
  environment 'HOME' => "/home/#{node['user']}"
  command "sudo -u #{node['user']} /home/#{node['user']}/.vim/bundle/neobundle.vim/bin/neoinstall 1>.vim-neoinstalled 2>.vim-neoinstalled" 
  not_if { File.exists?("/home/#{node['user']}/.vim-neoinstalled") }
end

breakpoint(追記)

  • 今度はscriptには問題がなかったので、使っていないが使い方だけ簡単に書いておく。
  • recipe内にbreakpointを設定する。
  • chef-shellにてrun_chefを実行するとbreakpointが設定されているところで止まる。
  • 進みたいときはchef_run.resumeを入力する。
breakpoint "before bash 'install vim plugin via neobundle'" do
  action :break
end

感想

  • Chefは難しい。
  • 日本語の本が少ない。入門書にはかなりいい良書が2冊あるが、もっと知りたい場合は公式ドキュメントに依存するしかない。まあ当たり前だな。

参考

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9