Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
Replace old slack options and calls with chatops plugin
Browse files Browse the repository at this point in the history
Replaced the old notify_slack method call in the deployments helpers
with calls to the new PuppetWebhook::Chatops plugin.

Replaced old Slack config defaults with new Chatops configuration options.
  • Loading branch information
David Hollinger authored and dhollinger committed May 1, 2018
1 parent 2c2243c commit 0dcd860
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
36 changes: 33 additions & 3 deletions lib/helpers/deployments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'plugins/mcollective'
require 'plugins/chatops'

module Deployments # rubocop:disable Style/Documentation
def deploy(branch, deleted)
Expand Down Expand Up @@ -29,12 +30,27 @@ def deploy(branch, deleted)
generate_types(branch) if types?
end
notify_slack(status_message) if slack?
if settings.chatops? do
PuppetWebhook::Chatops.new(message,
settings.chatops_service,
settings.chatops_url,
settings.chatops_channel,
settings.chatops_user,
settings.chatops_options)
end
[status_message[:status_code], status_message.to_json]
rescue StandardError => e
status_message = { status: :fail, message: e.message, trace: e.backtrace, branch: branch, status_code: 500 }
LOGGER.error("message: #{e.message} trace: #{e.backtrace}")
status 500
notify_slack(status_message) if slack?
if settings.chatops? do
PuppetWebhook::Chatops.new(message,
settings.chatops_service,
settings.chatops_url,
settings.chatops_channel,
settings.chatops_user,
settings.chatops_options)
end
status_message.to_json
end

Expand Down Expand Up @@ -62,13 +78,27 @@ def deploy_module(module_name)
end
LOGGER.info("message: #{message} module_name: #{module_name}")
status_message = { status: :success, message: message.to_s, module_name: module_name, status_code: 200 }
notify_slack(status_message) if slack?
if settings.chatops? do
PuppetWebhook::Chatops.new(message,
settings.chatops_service,
settings.chatops_url,
settings.chatops_channel,
settings.chatops_user,
settings.chatops_options)
end
status_message.to_json
rescue StandardError => e
LOGGER.error("message: #{e.message} trace: #{e.backtrace}")
status 500
status_message = { status: :fail, message: e.message, trace: e.backtrace, module_name: module_name, status_code: 500 }
notify_slack(status_message) if slack?
if settings.chatops? do
PuppetWebhook::Chatops.new(message,
settings.chatops_service,
settings.chatops_url,
settings.chatops_channel,
settings.chatops_user,
settings.chatops_options)
end
status_message.to_json
end
end
16 changes: 11 additions & 5 deletions lib/plugins/chatops.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
require 'chatops/slack'

class PuppetWebhook
# Chatops object for sending webhook notifications to chatops tools
class Chatops
def initialize(message, service, url, channel, user, **args)
@message = message
def initialize(service, url, channel, user, **args)
@service = service
@url = url
@channel = channel
@user = user
@args = args
end

def notify
def notify(message)
case @service
when 'slack'
Chatops::Slack.new(@channel, @url, @user, @args[:icon_emoji], @args[:http_options]).notify
Chatops::Slack.new(@channel,
@url,
@user,
message,
http_options: @args[:http_options],
icon_emoji: @args[:icon_emoji]
).notify
else
LOGGER.error("Service #{@service} is not currently supported")
end
end
end
end
end
11 changes: 6 additions & 5 deletions lib/plugins/chatops/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

class PuppetWebhook
class Chatops
# Sets up Slack object that will send notifications to Slack via a webhook.
class Slack
def initialize(channel, url, user, icon_emoji, message, options = {})
def initialize(channel, url, user, message, options = {})
@channel = channel
@url = url
@user = user
@icon_emoji = icon_emoji
@message = message
@options = options
end

def notify
notifier = Slack::Notifier.new @url, http_options: @options
notifier = Slack::Notifier.new @url, http_options: @options[:http_options]

target = if @message[:branch]
@message[:branch]
Expand All @@ -26,11 +26,12 @@ def notify
notifier.post text: msg[:fallback],
channel: @channel,
username: @user,
icon_emoji: @icon_emoji,
icon_emoji: @options[:icon_emoji],
attachments: [msg]
end

private

def format_message(target)
message = {
author: 'r10k for Puppet',
Expand All @@ -56,4 +57,4 @@ def format_message(target)
end
end
end
end
end
11 changes: 6 additions & 5 deletions lib/puppet_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ class PuppetWebhook < Sinatra::Base # rubocop:disable Style/Documentation
set :use_mco_ruby, false unless settings.respond_to? :use_mco_ruby=
set :use_mcollective, false unless settings.respond_to? :use_mcollective=
set :discovery_timeout, false unless settings.respond_to? :discovery_timeout=
set :slack_webhook, false unless settings.respond_to? :slack_webhook=
set :slack_channel, '#general' unless settings.respond_to? :slack_channel=
set :slack_user, 'puppet_webhook' unless settings.respond_to? :slack_user=
set :slack_emoji, ':ocean:' unless settings.respond_to? :slack_emoji=
set :slack_proxy_url, nil unless settings.respond_to? :slack_proxy_url=
set :chatops, false unless settings.respond_to? :chatops=
set :chatops_service, 'slack' unless settings.respond_to? :chatops_service=
set :chatops_url, '' unless settings.respond_to? :chatops_url=
set :chatops_channel, '#general' unless settings.respond_to? :chatops_channel=
set :chatops_user, 'puppet_webhook' unless settings.respond_to? :chatops_user=
set :chatops_options, {} unless settings.respond_to? :chatops_options=
set :default_branch, 'production' unless settings.respond_to? :default_branch=
set :ignore_environments, [] unless settings.respond_to? :ignore_environments=
set :prefix, nil unless settings.respond_to? :prefix=
Expand Down

0 comments on commit 0dcd860

Please sign in to comment.