0

Can anyone help me with this please?

NoMethodError
-------------
undefined method `execute' for Chat::Mattermost:Class

Relevant File Content: (file name libraries/chat.rb)

4:
5:  module Chat
6:    class Mattermost
7:
8:      def self.log_to_chat(message)
9>>       execute "echo" do
10:          command "echo #{message}"
11:        end
12:      end
13:
14:    end
15:  end
16:

I read that DSL syntax isn't available in a definition, so I am guessing I need to do something resembling r = Chef::Resource::Execute.new("echo #{message}") and r.run_command :run but I'm not quite so sure how to do it.

Other relevant code, my method is "called" like this:

log "this is a message" do
  level :info
  notifies :run, Chat::Mattermost.log_to_chat("#{name}"), :immediately
end

edit: second attempt

NoMethodError
-------------
undefined method `events' for nil:NilClass

for code:

5:  require 'chef/resource/execute'
6:
7:  module Chat
8:    class Mattermost
9:
10:      def self.log_to_chat(message)
11:        cmd = Chef::Resource::Execute.new("echo #{message}")
12>>       cmd.run_action(:run)
13:
Rob
  • 14,746
  • 28
  • 47
  • 65
vikingsteve
  • 38,481
  • 23
  • 112
  • 156

1 Answers1

1
notifies :run, Chat::Mattermost.log_to_chat("#{name}"), :immediately
  1. You can't call notify on non-resource.
  2. You can't create resource dynamically by just calling notifies, it won't work.
  3. As second parameter (when called from resource block) to notifies you should pass string which will be used to search resource in chef context.

If you want to enhance log resource provider, you should implement your own, similar to this, put it in libraries and call log resource with your newly implemented class name as provider.

log 'this is a message' do
  provider Chef::Provider::Log::MyCustomLog
end
Szymon
  • 1,525
  • 1
  • 11
  • 12
  • Ok, i had the wrong approach.... Can I avoid the `notifies` and log provider completely... just call `Chat::Mattermost.log_to_chat('...')` before the `log "this is a message"` block - is that possible, and how do I define `log_to_chat` method ? – vikingsteve Aug 25 '16 at 09:59
  • Chef works in two 'phases', [compile and runtime](http://stackoverflow.com/a/26000270/4894399). Using library function between resources will result in all `log_to_chat` function calls and after that all chef resources will be executed. So in you 'chat' you will probably see all log messages regardless resources status. Your function definition is valid (just don't use resources inside), call it from chef code like you mentioned `Chat::Mattermost.log_to_chat("message")`. – Szymon Aug 25 '16 at 14:05
  • Thanks again. If you see my "edit: second attempt", I tried to execute a shell command without resources, but it's failing with `undefined method `events' for nil:NilClass` - any idea why? I found very few examples of using `Chef::Resource::Execute` directly. – vikingsteve Aug 25 '16 at 16:22
  • For executing shell in plain ruby code you should use open2e/capture2e from standard ruby [Open3](https://ruby-doc.org/stdlib-2.2.3/libdoc/open3/rdoc/Open3.html#method-c-capture2e) library. Using resource would probably require building chef context. – Szymon Aug 25 '16 at 16:37