0

Presently I have a chef recipe whereby I post messages to chat, inside a loop:

artifacts.each do |artifactItem|
    # Deploy the artifact
    #...

    # Post to chat
    chat_post "deployed artifact #{artifact_name}"
end

The result on my chat is like this:

chef [BOT]
deployed artifact A

chef [BOT]
deployed artifact B

chef [BOT]
deployed artifact C

I am wondering - is there an easy "queue" mechanism in chef, where I can queue up my deployment messages, and post them all at once (when my recipe completes) ? If so how would the code look.

vikingsteve
  • 38,481
  • 23
  • 112
  • 156

2 Answers2

3

The easiest way to do this would be to use the delayed notifications system.

artifacts.each do |artifactItem|
    # Deploy the artifact
    #...

    # Post to chat
    r = chat_post "deployed artifact #{artifact_name}" do
      action :nothing
    end
    ruby_block "notification for #{artifact_name}" do
      block { }
      notifies :someaction, r
    end
end

Or something like that, make sure you check what action to use for the notification (whatever the default action on the chat_post resource is. Also this assumes chat_post is a resource and not some kind of helper method. If it's not a resource, you might need two ruby_blocks.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Thanks. The `chat_post` is actually under `chat/definitions/post.rb`... I guess it is a "helper method" (since we didnt completely understand how to implement it as a resource / provider / LWRP thing...). Though event based notifications seem like a better way to do it... – vikingsteve Sep 13 '16 at 09:16
  • You'll have to port it over to a custom resource to do this, but that should hopefully be pretty easy :) – coderanger Sep 13 '16 at 18:39
  • Ok, thanks @coderanger. I asked another question about specifically that, maybe you can help? http://stackoverflow.com/questions/39488843/chef-how-to-write-a-custom-resource-containing-dsl-for-execute – vikingsteve Sep 14 '16 at 11:04
0

You can use node.run_state to save transient data that is accessible for the current chef run.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • 1
    True, but probably not a good idea here. Global state should be used only as a last resort and `run_state` in particular needs to be used very carefully as it's a shared global namespace. – coderanger Sep 13 '16 at 07:56