Open smt116 opened 7 years ago
@coderanger ping.
I wonder if some derivation of http://gsandie-blog.logdown.com/posts/168475-chef-11-libraries-and-lwrp-testing-in-chef-shell could work to resolve this.
Snippet from the above URL:
Chef::Config[:cookbook_path] = "/path/to/your/cookbooks"
run_list = "recipe[partial_search]"
cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
cl.load_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cl)
@events = Chef::EventDispatch::Dispatcher.new()
node.run_context = Chef::RunContext.new(node, cookbook_collection, @events)
node.run_list = Chef::RunList.new(run_list)
node.run_context.load(node.expand!)
This does it. I'm not too experienced with this level of Chef'ing... so I'm sure this could be more concise.
# Install event handler.
Chef::Log.debug('[poise-derived] Installing event handler')
if Chef.run_context.nil?
cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
cl.load_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cl)
@events = Chef::EventDispatch::Dispatcher.new()
Chef.node.run_context = Chef::RunContext.new(Chef.node, cookbook_collection, @events)
Chef.node.run_context.load(Chef.node.expand!)
Chef.set_run_context Chef.node.run_context
end
Chef.run_context.events.register(self.instance)
I don't use chef-shell personally so if someone wants to submit a patch for this I can take a look. Overall not much interest from me in supporting chef-shell since I don't think it's a useful tool.
The above patch is probably not what is needed, the issue is that chef-shell doesn't have a converge lifecycle in the way chef-client/solo do, so there is nothing to hook in to to set up the DSL methods. Without that, the cookbook attributes files would just fail to load.
Background
We are using
poise-derived
cookbook for lazy attributes. It works fine in our ecosystem.Problem
The problem was when we had to use the
chef-shell
command in a client mode to make some debugging.It does not work and it raises the following error:
It looks like a bug in the handler definition.
Chef.run_context
isnil
in that mode while in the normalchef-client
it is aChef::RunContext
object.Software versions
Chef: 12.17.44