rackspace-cookbooks / chef-slack_handler Goto Github PK
View Code? Open in Web Editor NEWSlack handler for Chef
Home Page: https://supermarket.chef.io/cookbooks/slack_handler
License: Apache License 2.0
Slack handler for Chef
Home Page: https://supermarket.chef.io/cookbooks/slack_handler
License: Apache License 2.0
I'm executing:
chef-client -o 'recipe[existing], 'recipe[newrecipe]' --why-run
on a server to test the second recipe without having to add it to the run-list.
My existing recipe contains the handler and a bunch of other recipes.
When adding the recipe to the run-list, there is no issue.
When running the command above, an error presents itself:
/var/chef/cache/cookbooks/chef_handler/libraries/helpers.rb:51:in `get_class'
/var/chef/cache/cookbooks/chef_handler/providers/default.rb:48:in `block in class_from_file'
/var/chef/cache/cookbooks/cpla_b_handler_mattermost/recipes/default.rb:58:in `from_file'
/var/chef/cache/cookbooks/cpla_r_core/recipes/default.rb:10:in `from_file'
/var/chef/cache/cookbooks/chef_handler/libraries/helpers.rb:
44: ancestors = class_full_name.split('::')
45: class_name = ancestors.pop
46:
47: # We need to search the ancestors only for the first/uppermost namespace of the class, so we
48: # need to enable the #const_get inherit paramenter only when we are searching in Kernel scope
49: # (see COOK-4117).
50: parent = ancestors.inject(Kernel) { |scope, const_name| scope.const_get(const_name, scope === Kernel) }
51>> child = parent.const_get(class_name, parent === Kernel)
52: [parent, child]
53: end
54: end
55: end
56:
I am including a very basic check that chef environment is set on all of my nodes, which looks like this:
if node.environment() == "_default"
raise "Please re-bootstrap your node including the --environment parameter."
end
While this does successfully kill the chef-client run in these cases, it does not cause the chef-slack handler to send any event to our slack channel. I have tested other errors, and also manually triggering the slack webhook with a curl request and those all still work.
Do I need to reformat my exception somehow to get chef-slack handler to recognize it??
Version 0.7.1
as there's no minimum chef version mentioned, not sure that is the issue.
i've configured to use webhook mode and get such error at the end of the run:
Running handlers:
[2016-05-31T12:46:33+03:00] WARN: Failed to send message to Slack: undefined method `run_context' for Chef:Class
- Chef::Handler::Slack
- SyslogReporting
Running handlers complete
# chef-client --version
Chef: 11.16.4
I originally had this working setting everything for Usage 2 at the environment level, except the following, configured in the cookbook attribute file:
case node.chef_environment
when "staging"
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook1')
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook2')
when "production"
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook1')
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook2')
end
That worked well, until I attempted to factor it out to the recipe level. My recipe looked like this:
# Configures Slack integration
# https://github.com/rackspace-cookbooks/chef-slack_handler
include_recipe "slack_handler"
# Send successes to #staging and #production. Send failed runs to #errors.
case node.chef_environment
when "staging"
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook1')
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook2')
when "production"
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook1')
node.default['chef_client']['handler']['slack']['webhooks']['name'].push('webhook2')
end
# Note: Verbosity is configured at the environment level.
# Options are 'basic', 'elapsed' and 'resources'.
Running it this way broke something. I'm not sure what. We no longer get slack notifications to either of the three channels we have configured (#staging, #production, and #errors).
Not sure if this is a problem with the code, or my use case, or ?? Maybe I missed something obvious moving the attributes out of the attribute file?
I'd appreciate a little guidance.
The 0.1.0 tag on this repo is not associated with the right commit. I downloaded the 0.1.0 cookbook from Supermarket and it seems to correspond with commit f32996d.
Currently in this repo, the 0.1.0 tag is on b3f273f. That's a difference of 14 commits.
It's just a minor thing, but it would be nice to have it corrected.
channel
is listed as optional in usage 1 but no default is provided and as far as I can see the slackr gem requires it
I noticed that why-run mode is broken when this cookbook is in my runlist. I'm guessing that this is due to handlers not being loaded in why-run (see chef-cookbooks/chef_handler#40):
* chef_handler[Chef::Handler::Slack] action enable
- Would disable Chef::Handler::Slack as a start handler
- Would disable Chef::Handler::Slack as a report handler
- Would disable Chef::Handler::Slack as a exception handler
- Would load Chef::Handler::Slack from /var/chef/handlers/slack_handler_webhook.rb
================================================================================
Error executing action `enable` on resource 'chef_handler[Chef::Handler::Slack]'
================================================================================
NameError
---------
uninitialized constant Slack
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/chef_handler/libraries/helpers.rb:51:in `const_get'
/var/chef/cache/cookbooks/chef_handler/libraries/helpers.rb:51:in `get_class'
/var/chef/cache/cookbooks/chef_handler/providers/default.rb:48:in `block in class_from_file'
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:58:in `from_file'
/var/chef/cache/cookbooks/orcasnet/recipes/default.rb:20:in `from_file'
Honestly, I'm not sure whether there's anything about the Slack handler recipe that could change to fix this. I'm not sure why it's even looking up the constant in the first place. But... maybe you have some ideas?
Different issue than #14
While testing the PR above I noticed webhooks handler is not looking at default['chef_client']['handler']['slack']['fail_only']
but only at default['chef_client']['handler']['slack']['webhooks']['mywebhook']['fail_only']
Any chance this can be updated to support the new chef_handler 3.0?
I don't think icon_url
is read from config to be read by this line
This block likely needs @icon_url = @config[:icon_url]
adding:
def initialize(config = {})
# set defaults for any missing attributes
@config = config
@timeout = @config[:timeout] || 15
@icon_emoji = @config[:icon_emoji] || ':fork_and_knife:'
@username = @config[:username] || 'chef_handler'
@webhooks = @config[:webhooks]
end
In 4.0, with fail_only set to true, I'm getting :skull: Chef client run succeeded
messages upon successful Chef runs.
Proposed change to slack_handler_webhook:
if run_status.success? && !webhook['fail_only']
slack_message(" :white_check_mark: Chef client run #{run_status_human_readable} on #{run_status.node.name} #{run_status_detail(webhook['detail_level'])}", webhook['url'])
sending_to_slack = true
else
sending_to_slack = true
slack_message(" :skull: Chef client run #{run_status_human_readable} on #{run_status.node.name} #{run_status_detail(webhook['detail_level'])}", webhook['url'], run_status.exception)
end
should be
if run_status.success?
unless webhook['fail_only']
sending_to_slack = true
slack_message(" :white_check_mark: Chef client run #{run_status_human_readable} on #{run_status.node.name} #{run_status_detail(webhook['detail_level'])}", webhook['url'])
end
else
sending_to_slack = true
slack_message(" :skull: Chef client run #{run_status_human_readable} on #{run_status.node.name} #{run_status_detail(webhook['detail_level'])}", webhook['url'], run_status.exception)
end
Slack supports default configuration for icon and name of a webhook.
Inside (slack_handler_webhook.rb)[https://github.com/rackspace-cookbooks/chef-slack_handler/blob/master/files/default/slack_handler_webhook.rb] could the defaults be removed so the defaults built into Slack take effect instead? So remove the || ':fork_and_knife:'
and || 'chef_handler'
parts from this block:
def initialize(config = {})
# set defaults for any missing attributes
@config = config
@timeout = @config[:timeout] || 15
@icon_emoji = @config[:icon_emoji]
@username = @config[:username]
@webhooks = @config[:webhooks]
end
We should apply our current best practices to have CI ran through CircleCI with test-kitchen and kitchen-rackspace.
Every once in a while we see errors like this
Failed to send message to Slack: "\xE2" from ASCII-8BIT to UTF-8
which seems to stem from chef output including non utf-8 characters. It would probably be better to enforce utf-8 encoding on such messages instead of failing to send the message.
In some environments, the node names are reasonably obscure so it may be good to include an option to send the chef environment. I believe this is usually accessed via node.chef_environment
.
It's nice having a message when the deploy finishes, an optional message on deploy start would be great too for some cases.
Hi again.
I have problems after updating to latest 0.7.0 version. I've use web hooks method.
When changing message_detail_level from basic (with basic work without troubles) to other (elapsed, resources) I've receive error.
Running handlers:
[2016-05-06T13:20:29+03:00] WARN: Failed to send message to Slack: undefined local variable or method `updated_resources' for #<SlackHandlerUtil:0x0000000262dd68>
- Chef::Handler::Slack
Also I try to change:
Valid options here are basic, elapsed, resources
default['chef_client']['handler']['slack']['message_detail_level'] = 'resources'
and for web hook too:
default['chef_client']['handler']['slack']['webhooks']['webhook1']['message_detail_level'] = 'resources'.
Can you check why? Previous 0.6.0 version work fine.
ERROR -- : Cookbook file /Users/admin/.berkshelf/cookbooks/slack_handler-0.9.0/files/default/slack_handler_util.rb has a ruby syntax error:
ERROR -- : Ridley::Errors::CookbookSyntaxError: Invalid ruby files in cookbook: slack_handler (0.9.0).
There's a syntax error in #52 - totally my fault, sorry!
I'm running into this in ubuntu 14.04, chef 12.13.37. Worked fine in chef 12.6.
` * cookbook_file[/var/chef/handlers/slack_handler_util.rb] action create
================================================================================
Error executing action `create` on resource 'cookbook_file[/var/chef/handlers/slack_handler_util.rb]'
================================================================================
TypeError
---------
no implicit conversion of Chef::DelayedEvaluator into String
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:42:in `from_file'
/var/chef/cache/cookbooks/moxi-slack/recipes/default.rb:14:in `from_file'
Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/slack_handler/recipes/default.rb
37: cookbook_file "#{node['chef_handler']['handler_path']}/slack_handler_util.rb" do
38: source 'slack_handler_util.rb'
39: mode "0600"
40: action :nothing
41: # end
42: end.run_action(:create)
Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/slack_handler/recipes/default.rb:37:in `from_file'
cookbook_file("/var/chef/handlers/slack_handler_util.rb") do
provider Chef::Provider::CookbookFile
action [:nothing]
retries 0
retry_delay 2
default_guard_interpreter :default
source "slack_handler_util.rb"
declared_type "cookbook_file"
cookbook_name "slack_handler"
recipe_name "default"
mode "0600"
atomic_update #<Chef::DelayedEvaluator:0x00000001d8ce98@/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/resource/file.rb:53>
path #<Chef::DelayedEvaluator:0x00000001d8df78@/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/property.rb:180>
end
Platform:
---------
x86_64-linux`
I also had a similar error with with chef_handler, but was able to resolve the issue by changing that to use just directory instead of remote_directory to make the handler_path on disk. The newer version of chef doesn't seem to appreciate the run_action with more complex resources. Interestingly if I run just the slack integration recipe by itself, it doesn't fail. But in a full chef run it does.
got this error. can you check this.
Chef::Exceptions::ValidationFailed
Option action must be equal to one of: enable, disable! You passed :nothing.
Cookbook Trace: ---------------
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:47:in block in from_file' /var/chef/cache/cookbooks/slack_handler/recipes/default.rb:42:in
from_file'
Relevant File Content:
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:
40: end.run_action(:create)
41: 42: chef_handler "Chef::Handler::Slack" do
43: source handler_file
44: arguments [
45: node['chef_client']['handler']['slack']
46: ]
47>> action :nothing
48: end.run_action(:enable)
49:
Hi,
I met this error when using slack_handler.
Here's the log:
Recipe: chef_handler::default
- remote_directory[/var/chef/handlers] action create
Recipe:* cookbook_file[/var/chef/handlers/README] action create (up to date) (up to date)
Recipe: slack_handler::default
- cookbook_file[/var/chef/handlers/slack_handler_util.rb] action create (up to date)
- cookbook_file[/var/chef/handlers/slack_handler_webhook.rb] action create (up to date)
- chef_handler[Chef::Handler::Slack] action enable
- disable Chef::Handler::Slack as a start handler
- disable Chef::Handler::Slack as a report handler
- disable Chef::Handler::Slack as a exception handler
- load Chef::Handler::Slack from /var/chef/handlers/slack_handler_webhook.rb
- enable chef_handler[Chef::Handler::Slack] as a start handler
- enable chef_handler[Chef::Handler::Slack] as a report handler
- enable chef_handler[Chef::Handler::Slack] as a exception handler
- ruby_block[trigger_start_handlers] action create[2016-09-16T10:54:52+00:00] WARN: Failed to send message to Slack: undefined local variable or method `run_context' for #SlackHandlerUtil:0x00000001d8e1d0
Chef client version
$ chef -v
Chef Development Kit Version: 0.9.0
chef-client version: 12.5.1
berks version: 4.0.1
kitchen version: 1.4.2
Here's the config I've put on node config:
"chef_client": {
"handler": {
"slack": {
"webhooks": {
"name": ["webhook1"],
"webhook1": {
"url": "[MY HOOK URL]"
}
},
"timeout": 30,
"username": "Chef Bot",
"icon_emoji": ":fork_and_knife:",
"fail_only": false,
"send_start_message": true,
"message_detail_level": "resource",
"cookbook_detail_level": "all",
"send_environment": true
}
}
}
Please tell if you need to provide more information.
Thank you.
I'm using this cookbook to send reports to slack every time chef runs. Is there a way to send a conditional notification, say if a specific was updated?
If not, then are there any plans to add a LWRP (or custom resource) that can be notified / subscribed to?
Running handlers:
[2015-05-19T02:05:47+00:00] ERROR: Running exception handlers
[2015-05-19T02:05:47+00:00] WARN: Sending report to Slack webhook https://hooks.slack.com/services/XXXX/XXXX/XXXXXXXXXXXXXXXX
[2015-05-19T02:05:47+00:00] WARN: Sending report to Slack webhook https://hooks.slack.com/services/XXXX/XXXX/XXXXXXXXXXXXXXXX
haproxy is intentionally broken to test, please ignore that!
I'd like to integrate the slack handler directly into client.rb and not have it run as part of the cookbook. I want the handler to catch convergence failures and other errors that might occur before a runlist even gets going. I'm not clear on what I need to do this, although my initial attempt looks something like this:
In /etc/chef/client.d
in a file called slack.rb
require 'rubygems'
require '/var/chef/handlers/slack_handler_webhook'
slack_handler = Chef::Handler::Slack.new
start_handlers << slack_handler
report_handlers << slack_handler
exception_handlers << slack_handler
But this is sofar not working. Can I integrate this in this way?
I've been running into an issue with chef-client
runs after adding this handler that causes the check-in to fail due to a missing /var/chef/handlers
directory.
Code
As simple as the following:
include_recipe 'chef-client::cron'
include_recipe 'slack_handler'
Stack Trace
Chef::Exceptions::EnclosingDirectoryDoesNotExist
------------------------------------------------
cookbook_file[/var/chef/handlers/slack_handler_util.rb] (slack_handler::default line 37) had an error: Chef::Exceptions::EnclosingDirectoryDoesNotExist: Parent directory /var/chef/handlers does not exist.
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:42:in `from_file'
/var/chef/cache/cookbooks/cascade-cloud-instance/recipes/default.rb:22:in `from_file'
/var/chef/cache/cookbooks/cloud-client-tdi/recipes/default.rb:10:in `from_file'
Relevant File Content:
----------------------
/var/chef/cache/cookbooks/slack_handler/recipes/default.rb:
35: end
36:
37: cookbook_file "#{node['chef_handler']['handler_path']}/slack_handler_util.rb" do
38: source 'slack_handler_util.rb'
39: mode "0600"
40: action :nothing
41: # end
42>> end.run_action(:create)
43:
44: cookbook_file handler_file do
45: source handler_source
46: mode "0600"
47: action :nothing
48: # end
49: end.run_action(:create)
50:
51: chef_handler "Chef::Handler::Slack" do
Platform:
---------
x86_64-linux
Node is running on Amazon Linux (ami-0b33d91d) and is already bootstrapped. What's strange is didn't run into this issue when originally setting things up in test with the same exact setup.
Is it recommended to manually ensure /var/chef/handlers
exists before including the recipe, or is this an issue with the Slack handler?
It'd be nice to see the cookbooks and versions that have run in the notification to Slack. Would just need to incorporate something like #{run_status_cookbooks}
into the slack_message
calls then define this method:
def run_status_cookbooks
cookbooks = run_status.run_context.cookbook_collection
"#{cookbooks.keys.map {|x| cookbooks[x].name.to_s + ' ' + cookbooks[x].version} }"
end
I mostly grabbed that from here and yet to try a run of this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.