Skip to content

Instantly share code, notes, and snippets.

@vinyar
Last active May 5, 2017 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinyar/a1f3d6afaac8e1f3627de79ff62dc14e to your computer and use it in GitHub Desktop.
Save vinyar/a1f3d6afaac8e1f3627de79ff62dc14e to your computer and use it in GitHub Desktop.
How to find out where Chef constructs are coming from via Chef-Shell and Test Kitchen through Pry
$ chef-shell [ruby-2.0.0p648]
loading configuration: none (standalone session)
Session type: standalone
Loading...........done.
...
chef (12.19.36)> require 'pry'
=> true
chef (12.19.36)> node
=> <Chef::Node:0x3fd54e75cdf4 @name="seavinyar02.fios-router.home">
chef (12.19.36)> binding.pry
2.3.1 (main):0 > node.methods
=> [:inspect,
:trace,
:reset,
:version,
...
2.3.1 (main):0 > node.class
=> Chef::Node
2.3.1 (main):0 > show-doc Chef::Node
Error: No docs found for: Chef::Node
2.3.1 (main):0 > show-doc Chef::Node.node
Error: Couldn't locate a definition for Chef::Node.node!
2.3.1 (main):0 > show-doc Chef::Node#node
From: /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/node.rb @ line 95:
Owner: Chef::Node
Visibility: public
Signature: node()
Number of lines: 1
Used by DSL
2.3.1 (main):0 > exit
=> nil
chef (12.19.36)> exit
alexvinyar@seavinyar02 ~ $
Step one:
Put require 'pry';binding.pry somewhere in your recipe. For example in one of the resources:
directory 'bla' do
require 'pry';binding.pry
owner 'root'
group 'root'
mode '0755'
action :create
recursive true
end
Step two:
Converge the instance to get cookbooks into TK
Fetch out the command line to execute inside instance.
$ kitchen converge -l debug
...
...
--> sudo -E /opt/chef/bin/chef-solo --config /tmp/kitchen/solo.rb --log_level auto --no-color --json-attributes /tmp/kitchen/dna.json --force-formatter
')
...
..
CTRL+C
$ kitchen login
<paste the command from above> - it will trigger the break point interactively. With TK you don't have control of the session. Unless you use remote pry but I was never able to get it to work consistently, so this is much quicker.
vagrant@test-node:~$ sudo -E /opt/chef/bin/chef-solo --config /tmp/kitchen/solo.rb --log_level auto --no-color --json-attributes /tmp/kitchen/dna.json --force-formatter
')
...
[1] pry(#<Chef::CookbookVersion>)>
[1] pry(#<Chef::CookbookVersion>)*
[2017-05-04T19:09:38+00:00] INFO: Loading cookbooks [lk_base@0.2.3, build-essential@8.0.0, line@0.6.3, hostsfile@2.4.5, ntp@3.3.1, sudo@3.3.1, seven_zip@2.0.2, mingw@2.0.0, windows@3.0.5, ohai@5.0.3]
Synchronizing Cookbooks:
- lk_base (0.2.3)
- build-essential (8.0.0)
- hostsfile (2.4.5)
- ntp (3.3.1)
- sudo (3.3.1)
- seven_zip (2.0.2)
- line (0.6.3)
- ohai (5.0.3)
- mingw (2.0.0)
- windows (3.0.5)
Installing Cookbook Gems:
Compiling Cookbooks...
Frame number: 0/30
From: /tmp/kitchen/cache/cookbooks/lk_base/recipes/default.rb @ line 17 self.from_file:
12: # other platforms and cloud providers
13:
14:
15: directory 'bla' do
16: require 'pry';binding.pry
=> 17: owner 'root'
18: group 'root'
19: mode '0755'
20: action :create
21: recursive true
22: end
[1] pry(#<Chef::Resource::Directory>)>
Look up some construct of interest:
I happen to know that there is a lesser known construct to look up the cookbook that's currently being executed: cookbook_name
[4] pry(#<Chef::Resource::Directory>)> cookbook_name
=> "lk_base"
[5] pry(#<Chef::Resource::Directory>)> cookbook_name.class
=> String
[7] pry(#<Chef::Resource::Directory>)> show-doc
show-doc
[7] pry(#<Chef::Resource::Directory>)> show-doc cookbook_name
From: /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/resource.rb @ line 1277:
Owner: Chef::Resource
Visibility: public
Signature: cookbook_name()
Number of lines: 1
return [String] The cookbook this resource was declared in.
[8] pry(#<Chef::Resource::Directory>)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment