Class: Chef::Handler::Slack

Inherits:
Chef::Handler show all
Defined in:
lib/cookbook_sdk/handlers/slack.rb

Overview

Slack Handler goal is send messages to a Slack channel with Chef run status. It can be used as a start, failure or success handler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Slack

Returns a new instance of Slack.



14
15
16
17
18
19
20
21
# File 'lib/cookbook_sdk/handlers/slack.rb', line 14

def initialize(options = {})
  @token = options.fetch(:token) { raise Exceptions::ConfigurationError, "Slack 'token' should be provided!" }
  @channel = options.fetch(:channel, '#chef')
  @username = options.fetch(:username, 'Chef')
  @on_start = options.fetch(:on_start, true)
  @on_success = options.fetch(:on_success, true)
  @on_failure = options.fetch(:on_failure, true)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def channel
  @channel
end

#on_failureObject (readonly)

Returns the value of attribute on_failure.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def on_failure
  @on_failure
end

#on_startObject (readonly)

Returns the value of attribute on_start.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def on_start
  @on_start
end

#on_successObject (readonly)

Returns the value of attribute on_success.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def on_success
  @on_success
end

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/cookbook_sdk/handlers/slack.rb', line 12

def username
  @username
end

Instance Method Details

#reportObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cookbook_sdk/handlers/slack.rb', line 23

def report
  options = { :pretext => "Run at #{run_status.node.name}" }

  if !run_status.is_a?(Chef::RunStatus) || elapsed_time.nil?
    report_start(options)
  elsif run_status.success?
    report_success(options)
  else
    report_failure(options)
  end
end