[chef] Re: Re: Re: Re: Library function in recipe


Chronological Thread 
  • From: Vinod Guddad < >
  • To: ,
  • Subject: [chef] Re: Re: Re: Re: Library function in recipe
  • Date: Sun, 12 Aug 2012 11:29:18 -0700

The send message from the library does work i.e. logEvent method works in a recipe (but outside a ruby_block). But logEvent method does not seem to be recognized in the recipe within a ruby_block. I have tried logEvent, EventLogLib.logEvent, EventLogLib::logEvent and nothing works inside the ruby_block.
I want logEvent to be fired during the execute phase of convergence only.

The default.rb recipe in eventLogger cookbook is listed below. This cookbook also contains the EventLogLib library.
===================
</code>
ruby_block "mystart" do
block do
EventLogLib.logEvent("Begin test", "START")
end
end

log "Hi there"

ruby_block "myend" do
block do
EventLogLib.logEvent("End Test", "END")
end
end
</code>
===================

Here is the debug trace:
===================
[Sun, 12 Aug 2012 18:18:14 +0000] INFO: *** Chef 10.12.0 ***
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Building node object for opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Extracting run list from JSON attributes provided on command line
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Setting the run_list to ["recipe[chef_handler::default]", "recipe[chef_handler::async]", "recipe[eventLogger::default]"] from JSON
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Applying attributes from json file
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Platform is ubuntu version 12.04
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Run List is [recipe[chef_handler::default], recipe[chef_handler::async], recipe[eventLogger::default]]
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Run List expands to [chef_handler::default, chef_handler::async, eventLogger::default]
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Starting Chef Run for opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Running start handlers
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Start handlers complete.
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: No chefignore file found at /var/chef-solo/cookbooks/chefignore no files will be ignored
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading cookbook eventLogger's library file: /var/chef-solo/cookbooks/eventLogger/libraries/loglib.rb
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading cookbook chef_handler's providers from /var/chef-solo/cookbooks/chef_handler/providers/default.rb
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loaded contents of /var/chef-solo/cookbooks/chef_handler/providers/default.rb into a provider named chef_handler defined in Chef::Provider::ChefHandler
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading cookbook chef_handler's resources from /var/chef-solo/cookbooks/chef_handler/resources/default.rb
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loaded contents of /var/chef-solo/cookbooks/chef_handler/resources/default.rb into a resource named chef_handler defined in Chef::Resource::ChefHandler
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Node opstrain-ubuntu-precise loading cookbook chef_handler's attribute file /var/chef-solo/cookbooks/chef_handler/attributes/default.rb
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading Recipe chef_handler::default via include_recipe
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Found recipe default in cookbook chef_handler
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Chef Handlers will be at: /var/handlers
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing remote_directory[/var/handlers] action create (chef_handler::default line 10)
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing cookbook_file[/var/handlers/async_handler.rb] action create (dynamically defined)
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing chef_gem[stomp] action install (chef_handler::default line 27)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] detected omnibus installation in /opt/chef/embedded/bin
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] using gem from running ruby environment
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] found installed gem stomp version 1.2.5 matching stomp (>= 0)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] is already installed - nothing to do
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading Recipe chef_handler::async via include_recipe
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Found recipe async in cookbook chef_handler
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing chef_handler[Chef::Handler::Deployment::Summarize] action enable (chef_handler::async line 8)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Chef::Handler::Deployment::Summarize has not been loaded.
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Enabling chef_handler[Chef::Handler::Deployment::Summarize] as a report handler
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Enabling chef_handler[Chef::Handler::Deployment::Summarize] as a exception handler
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading Recipe eventLogger::default via include_recipe
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Found recipe default in cookbook eventLogger
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Loading from cookbook_path: /var/chef-solo/cookbooks
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Converging node opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Processing remote_directory[/var/handlers] on opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing remote_directory[/var/handlers] action nothing (chef_handler::default line 10)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Doing nothing for remote_directory[/var/handlers]
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Processing cookbook_file[/tmp/stomp-1.2.5.gem] on opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing cookbook_file[/tmp/stomp-1.2.5.gem] action create (chef_handler::default line 20)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Processing chef_gem[stomp] on opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing chef_gem[stomp] action install (chef_handler::default line 27)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] detected omnibus installation in /opt/chef/embedded/bin
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] using gem from running ruby environment
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] found installed gem stomp version 1.2.5 matching stomp (>= 0)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: chef_gem[stomp] is already installed - nothing to do
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Processing chef_handler[Chef::Handler::Deployment::Summarize] on opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing chef_handler[Chef::Handler::Deployment::Summarize] action nothing (chef_handler::async line 8)
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Doing nothing for chef_handler[Chef::Handler::Deployment::Summarize]
[Sun, 12 Aug 2012 18:18:15 +0000] DEBUG: Processing ruby_block[mystart] on opstrain-ubuntu-precise
[Sun, 12 Aug 2012 18:18:15 +0000] INFO: Processing ruby_block[mystart] action create (eventLogger::default line 5)
[Sun, 12 Aug 2012 18:18:15 +0000] ERROR: ruby_block[mystart] (eventLogger::default line 5) has had an error
[Sun, 12 Aug 2012 18:18:15 +0000] ERROR: ruby_block[mystart] (/var/chef-solo/cookbooks/eventLogger/recipes/default.rb:5:in `from_file') had an error:
ruby_block[mystart] (eventLogger::default line 5) had an error: NoMethodError: undefined method `logEvent' for EventLogLib:Module
/var/chef-solo/cookbooks/eventLogger/recipes/default.rb:7:in `block (2 levels) in from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `action_create'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource.rb:454:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:49:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block (2 levels) in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:94:in `block in execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:92:in `execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:80:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:330:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:163:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:207:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application.rb:70:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:19:in `load'
/usr/bin/chef-solo:19:in `<main>'
[Sun, 12 Aug 2012 18:18:15 +0000] ERROR: Running exception handlers
[Sun, 12 Aug 2012 18:18:16 +0000] ERROR: Exception handlers complete
[Sun, 12 Aug 2012 18:18:16 +0000] DEBUG: Re-raising exception: NoMethodError - ruby_block[mystart] (eventLogger::default line 5) had an error: NoMethodError: undefined method `logEvent' for EventLogLib:Module
/var/chef-solo/cookbooks/eventLogger/recipes/default.rb:7:in `block (2 levels) in from_file'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `call'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `action_create'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource.rb:454:in `run_action'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:49:in `run_action'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block (2 levels) in converge'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `each'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block in converge'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:94:in `block in execute_each_resource'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:92:in `execute_each_resource'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:80:in `converge'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:330:in `converge'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:163:in `run'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:207:in `block in run_application'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `loop'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `run_application'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application.rb:70:in `run'
  /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/bin/chef-solo:25:in `<top (required)>'
  /usr/bin/chef-solo:19:in `load'
  /usr/bin/chef-solo:19:in `<main>'
[Sun, 12 Aug 2012 18:18:16 +0000] FATAL: Stacktrace dumped to /var/chef-solo/chef-stacktrace.out
[Sun, 12 Aug 2012 18:18:16 +0000] DEBUG: NoMethodError: ruby_block[mystart] (eventLogger::default line 5) had an error: NoMethodError: undefined method `logEvent' for EventLogLib:Module
/var/chef-solo/cookbooks/eventLogger/recipes/default.rb:7:in `block (2 levels) in from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/provider/ruby_block.rb:28:in `action_create'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource.rb:454:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:49:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block (2 levels) in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:85:in `block in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:94:in `block in execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/resource_collection.rb:92:in `execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/runner.rb:80:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:330:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/client.rb:163:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:207:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application/solo.rb:195:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/lib/chef/application.rb:70:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.12.0/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:19:in `load'
/usr/bin/chef-solo:19:in `<main>'
[Sun, 12 Aug 2012 18:18:16 +0000] FATAL: NoMethodError: ruby_block[mystart] (eventLogger::default line 5) had an error: NoMethodError: undefined method `logEvent' for EventLogLib:Module


===================

Thanks
Vinod


On Wed, Aug 8, 2012 at 3:34 PM, AJ Christensen < " target="_blank"> > wrote:
Post the stack trace with log level set to :debug

You probably want something like below in your library file:

Chef::Recipe.send(:include, EventLogLib)

https://github.com/fujin/chef-discovery/blob/master/libraries/recipe.rb#L17

You could model this as an LWRP, or even one of the new conditional
output formatters in 10.14, similar to specdoc -- queuedoc or
something. :) (perhaps?)

--AJ

On 9 August 2012 08:11,  < "> > wrote:
> I am getting a method not defined exception with the following code in the
> recipe. I tried both the commented and un-commented lines in the ruby_block
> below and both did not work.
> The library is listed below as well.
>
> <code>
> class Chef::Recipe
>   include EventLogLib
> end
>
> ruby_block "mystart" do
> block do
> #EventLogLib.logEvent("def","START")
> logEvent("def","START")
> end
> end
> </code>
>
> Here is the library defined in the cookbook.
> <code>
> module EventLogLib
>   def logEvent(msg, msgstatus)
>         require "rubygems"
>         require "stomp"
>         stomp_server = node[:chef_handler][:stomp_server]
>         stomp_port = node[:chef_handler][:stomp_port]
>         stomp_queue = node[:chef_handler][:stomp_queue]
>         stomp_user = node[:chef_handler][:stomp_user]
>         stomp_password =  node[:chef_handler][:stomp_password]
>         eventQueue = "#{stomp_queue}"
>
>         summary = { :deploymentId => node.deployment_id,
>                                 :vmId => node.vm_id,
>                                 :vmHostname => node.hostname,
>                                 :vmIpaddress => node.ipaddress,
>                                 :stepMessage => msg,
>                                 :stepStatus => msgstatus
>                           }
>
>         client = Stomp::Client.new(stomp_user, stomp_password, stomp_server,
> stomp_port, true)
>         client.publish(eventQueue, summary.to_json, {:persistent => false})
>         client.close
>         Chef::Log.info ("message #{summary.to_json} sent to queue
> #{eventQueue}")
>   end
> end
> </code>




Archive powered by MHonArc 2.6.16.

§