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

Rocketchat plugin #53

Merged
merged 6 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ chatops: true

##### Slack Configuration

You can enable Slack notifications for the webhook. You will need a Slack webhook URL and the slack-notifier gem installed.
You can enable Slack notifications for the webhook. You will need a Slack webhook URL and the `slack-notifier` gem installed.

The `slack-notifier` gem is installed as a dependency to `puppet_webhook`

To get the Slack webhook URL you need to:

Expand All @@ -104,7 +106,8 @@ Then configure the webhook to add your Slack Webhook URL:
``` yaml
chatops: true
chatops_service: 'slack' # Required so the app knows that you're sending to Slack.
chatops_channel: '#channel' # deftaults to #general
chatops_url: 'http://hooks.slack.com/services/path/to/your/webhook' # mandatory for usage
chatops_channel: '#channel' # defaults to #general
chatops_user: 'r10k' # defaults to puppet_webhook
chatops_options:
icon_emoji: ':ocean:'
Expand All @@ -116,6 +119,37 @@ chatops_options:

**NOTE: The legacy `slack_webhook`, `slack_user`, `slack_channel`, `slack_emoji`, and `slack_proxy_url` still work, but will be removed in 3.0.0**

##### Rocket.Chat Configuration

You can enable Rocket.Chat notifications for the webhook. You will need a
Rocket.Chat incoming webhook URL and the `rocket-chat-notifier` gem installed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: Will the runtime dependency provide this during installation, or do we need to plan to provide that another way? And if the runtime dep does, should it for an optional component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a runtime dependency, the rocket-chat-notifier gem will be installed as a dependency. We could make the gem "optional", but the more you do that, the more work you put on your users to install those "optional" dependencies themselves.

Think of puppetlabs-beaker dependencies, there are so many dependencies for each supported hypervisor that you either install a ton of deps during gem install or put the entire onus on the user to install what he needs (though he may not know what he needs).

I think at this point, we should leave it as a runtime dependency and instead make it optional in the future by providing the ChatOps plugin as a separate gem for puppet_webhook unto itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnelson0 I should probably add that, right now, the Chatops class doesn't test to see if a dependency exists before loading it, so it expects all dependencies to be there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to understand what the model looks like. Something to keep in mind later if we see dependencies explode.


The `rocket-chat-notifier` gem is installed as a dependency to `puppet_webhook`

To get the Rocket.Chat incoming webhook URL you need to:

1. Go to your Rocket.Chat and then select `Administration-Integrations`.
2. Choose `New integration`.
3. Choose `Incoming WebHook`. In the webhook form configure:
* `Enabled`: `True`.
* `Name`: A name for your webhook.
* `Post to Channel`: The channel to post to by default.
4. Save changes with `Save Changes` bottom.

Then configure the webhook to add your Rocket.Chat Webhook URL:

``` yaml
chatops: true
chatops_service: 'rocketchat' # Required so the app knows that you're sending to Rocket.Chat
chatops_url: <your incoming webhook URL> # mandatory for usage
chatops_channel: '#channel' # defaults to #general
chatops_user: 'r10k' # defaults to puppet_webhook
chatops_options:
icon_emoji: ':ocean:'
http_options:
open_timeout: 10
```

### Reference

#### Server Configuration File
Expand Down
28 changes: 20 additions & 8 deletions lib/plugins/chatops.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'plugins/chatops/slack'

class PuppetWebhook
# Chatops object for sending webhook notifications to chatops tools
class Chatops
Expand All @@ -14,13 +12,27 @@ def initialize(service, url, channel, user, options = {})
def notify(message)
case @service
when 'slack'
require 'plugins/chatops/slack'
LOGGER.info("Sending Slack webhook message to #{@url}")
Chatops::Slack.new(@channel,
@url,
@user,
message,
http_options: @args[:http_options] || {},
icon_emoji: @args[:icon_emoji]).notify
Chatops::Slack.new(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might consider moving the require 'plugins/chatops/slack' in here. That will only load the integration you want to use. Not a big deal now, but as you start adding more integrations, you might not want them all loaded all the time.

@channel,
@url,
@user,
message,
http_options: @args[:http_options] || {},
icon_emoji: @args[:icon_emoji]
).notify
when 'rocketchat'
require 'plugins/chatops/rocketchat'
LOGGER.info("Sending Rocket.Chat webhook message to #{@url}")
Chatops::Rocketchat.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
Expand Down
60 changes: 60 additions & 0 deletions lib/plugins/chatops/rocketchat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rocket-chat-notifier'

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

def notify
notifier = RocketChat::Notifier.new @url, http_options: @options[:http_options]
notifier.username = @user
notifier.channel = @channel

target = if @message[:branch]
@message[:branch]
elsif @message[:module]
@message[:module]
end

msg = "r10k deployment of Puppet environment/module #{target} started..."

attachment = format_attachment(target)

notifier.ping(msg, icon_emoji: @options[:icon_emoji], attachments: [attachment])
end

private

def format_attachment(target)
attachment = {
author: 'r10k for Puppet',
title: "r10k deployment of Puppet environment #{target}"
}

case @message[:status_code]
when 200
attachment.merge!(
color: 'good',
text: "Successfully started deployment of #{target}",
fallback: "Successfully started deployment of #{target}"
)
when 500
attachment.merge!(
color: 'bad',
text: "Failed to deploy #{target}",
fallback: "Failed to deploy #{target}"
)
end

attachment
end
end
end
end
1 change: 1 addition & 0 deletions puppet_webhook.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
spec.require_paths = %w[lib config]
spec.add_runtime_dependency 'json'
spec.add_runtime_dependency 'rack-bodyparser'
spec.add_runtime_dependency 'rocket-chat-notifier'
spec.add_runtime_dependency 'sinatra'
spec.add_runtime_dependency 'sinatra-contrib'
spec.add_runtime_dependency 'slack-notifier'
Expand Down