Skip to content

Commit

Permalink
Merge pull request #413 from amateo/feature/rocketchat
Browse files Browse the repository at this point in the history
Add supports for rocketchat
  • Loading branch information
rnelson0 authored Apr 5, 2018
2 parents ce8613b + 6c3adcf commit df718ce
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,33 @@ class { 'r10k::webhook::config':
}
```

### Webhook Rocket.Chat notifications

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.

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.

```puppet
class { 'r10k::webhook::config':
. . .
rocketchat_webhook => <your incoming webhook URL>, # mandatory for usage
rocketchat_username => 'username', # defaults to r10k
rocketchat_channel => '#channel', # defaults to #r10k
}
```


### Webhook Default Branch

The default branch of the controlrepo is commonly called `production`. This value can be overridden if you use another default branch name, such as `master`.
Expand Down
3 changes: 3 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
$webhook_slack_channel = undef
$webhook_slack_username = undef
$webhook_slack_proxy_url = undef
$webhook_rocketchat_webhook = undef
$webhook_rocketchat_channel = undef
$webhook_rocketchat_username = undef
$webhook_configfile_owner = 'root'
$webhook_configfile_group = $root_group
$webhook_configfile_mode = '0644'
Expand Down
6 changes: 6 additions & 0 deletions manifests/webhook/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
$slack_channel = $r10k::params::webhook_slack_channel,
$slack_username = $r10k::params::webhook_slack_username,
$slack_proxy_url = $r10k::params::webhook_slack_proxy_url,
$rocketchat_webhook = $r10k::params::webhook_rocketchat_webhook,
$rocketchat_channel = $r10k::params::webhook_rocketchat_channel,
$rocketchat_username = $r10k::params::webhook_rocketchat_username,
$configfile_owner = $r10k::params::webhook_configfile_owner,
$configfile_group = $r10k::params::webhook_configfile_group,
$configfile_mode = $r10k::params::webhook_configfile_mode,
Expand Down Expand Up @@ -82,6 +85,9 @@
'slack_channel' => $slack_channel,
'slack_username' => $slack_username,
'slack_proxy_url' => $slack_proxy_url,
'rocketchat_webhook' => $rocketchat_webhook,
'rocketchat_channel' => $rocketchat_channel,
'rocketchat_username' => $rocketchat_username,
'ignore_environments' => $ignore_environments,
'mco_arguments' => $mco_arguments,
'generate_types' => $generate_types,
Expand Down
45 changes: 45 additions & 0 deletions spec/classes/webhook/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,51 @@
it { is_expected.to contain_file('webhook.yaml').with_content(content) }
end

context 'FOSS with Rocket.Chat webhook enabled' do
let :params do
{
rocketchat_webhook: 'rocketchat_webhook'
}
end

it do
is_expected.to contain_file('webhook.yaml').with(
path: '/etc/webhook.yaml',
ensure: 'file',
owner: 'root',
group: 'root',
mode: '0644',
notify: 'Service[webhook]'
)
end
content = '---
access_logfile: "/var/log/webhook/access.log"
allow_uppercase: true
bind_address: "0.0.0.0"
client_cfg: "/var/lib/peadmin/.mcollective"
client_timeout: "120"
command_prefix: "umask 0022;"
default_branch: "production"
discovery_timeout: "10"
enable_mutex_lock: false
enable_ssl: true
generate_types: false
ignore_environments: []
pass: "puppet"
port: "8088"
prefix: false
prefix_command: "/bin/echo example"
protected: true
r10k_deploy_arguments: "-pv"
rocketchat_webhook: "rocketchat_webhook"
server_software: "WebHook"
use_mco_ruby: false
use_mcollective: true
user: "puppet"
'
it { is_expected.to contain_file('webhook.yaml').with_content(content) }
end

context 'Puppet Enterprise 2015.3.1 removing Webhook Config' do
let :params do
{
Expand Down
59 changes: 59 additions & 0 deletions templates/webhook.bin.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ if $config['slack_webhook'] then
require 'slack-notifier'
end

if $config['rocketchat_webhook'] then
require 'rocket-chat-notifier'
end

$command_prefix = $config['command_prefix'] || ''

class Server < Sinatra::Base
Expand Down Expand Up @@ -337,6 +341,54 @@ class Server < Sinatra::Base
notifier.post text: message[:fallback], attachments: [message]
end

def notify_rocketchat(status_message)
if $config['rocketchat_channel']
rocketchat_channel = $config['rocketchat_channel']
else
rocketchat_channel = '#r10k'
end

if $config['rocketchat_username']
rocketchat_user = $config['rocketchat_username']
else
rocketchat_user = 'r10k'
end

notifier = RocketChat::Notifier.new $config['rocketchat_webhook']
notifier.username = rocketchat_user
notifier.channel = rocketchat_channel

if status_message[:branch]
target = status_message[:branch]
elsif status_message[:module]
target = status_message[:module]
end

message = "r10k deployment of Puppet environment/module #{target}"
attachments = {
author: 'r10k for Puppet',
title: "r10k deployment of Puppet environment #{target}",
}

case status_message[:status_code]
when 200
attachments.merge!(
color: "good",
text: "Successfully deployed #{target}",
fallback: "Successfully deployed #{target}",
)
when 500
attachments.merge!(
color: "bad",
text: "Failed to deploy #{target}",
fallback: "Failed to deploy #{target}",
)
end

notifier.ping message, attachments: [attachments]

end

def deploy_module(module_name)
begin
if $config['use_mcollective']
Expand All @@ -349,12 +401,14 @@ class Server < Sinatra::Base
$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?
notify_rocketchat(status_message) if rocketchat?
status_message.to_json
rescue => 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?
notify_rocketchat(status_message) if rocketchat?
status_message.to_json
end
end
Expand Down Expand Up @@ -383,6 +437,7 @@ class Server < Sinatra::Base
generate_types(branch) if types?
end
notify_slack(status_message) if slack?
notify_rocketchat(status_message) if rocketchat?
status_message.to_json
rescue => e
status_message = {:status => :fail, :message => e.message, :trace => e.backtrace, :branch => branch, :status_code => 500}
Expand Down Expand Up @@ -422,6 +477,10 @@ class Server < Sinatra::Base
!!$config['slack_webhook']
end

def rocketchat?
!!$config['rocketchat_webhook']
end

def types?
!!$config['generate_types']
end
Expand Down

0 comments on commit df718ce

Please sign in to comment.