summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorytti <saku@ytti.fi>2017-02-16 23:23:46 +0200
committerGitHub <noreply@github.com>2017-02-16 23:23:46 +0200
commitabf143c0934190c3d3b1544ceda7dcaee8b87723 (patch)
treeab1b14193e50f3ae5075ebf1b481200c66bbae6a
parentdeae31cb04c184266e1b9c843325604a90113665 (diff)
parented2ec68d0a6a821d1f2226cfa437150d94a508d7 (diff)
Merge pull request #729 from Netflix-Skunkworks/hook_slackdiff
New Slack hook - posts colourized diffs to a channel
-rw-r--r--Dockerfile3
-rw-r--r--README.md23
-rw-r--r--lib/oxidized/hook/slackdiff.rb30
3 files changed, 55 insertions, 1 deletions
diff --git a/Dockerfile b/Dockerfile
index 5bb2a4c..fb2a635 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,7 +3,7 @@ MAINTAINER Samer Abdel-Hafez <sam@arahant.net>
RUN add-apt-repository ppa:brightbox/ruby-ng && \
apt-get update && \
- apt-get install -y ruby2.3 ruby2.3-dev libsqlite3-dev libssl-dev pkg-config make cmake libssh2-1-dev git
+ apt-get install -y ruby2.3 ruby2.3-dev libsqlite3-dev libssl-dev pkg-config make cmake libssh2-1-dev git g++
RUN mkdir -p /tmp/oxidized
COPY . /tmp/oxidized/
@@ -17,6 +17,7 @@ RUN gem install oxidized-web --no-ri --no-rdoc
# dependencies for hooks
RUN gem install aws-sdk
+RUN gem install slack-api
RUN rm -rf /tmp/oxidized
diff --git a/README.md b/README.md
index e1d53fc..2f3defc 100644
--- a/README.md
+++ b/README.md
@@ -874,6 +874,29 @@ AWS SNS hook requires the following configuration keys:
Your AWS credentials should be stored in `~/.aws/credentials`.
+## Hook type: slackdiff
+
+The `slackdiff` hook posts colorized config diffs to a [Slack](http://www.slack.com) channel of your choice. It only triggers for `post_store` events.
+
+You will need to manually install the `slack-api` gem on your system:
+
+```
+gem install slack-api
+```
+
+Configuration example:
+
+``` yaml
+hooks:
+ slack:
+ type: slackdiff
+ events: [post_store]
+ token: SLACK_BOT_TOKEN
+ channel: "#network-changes"
+```
+
+Note the channel name must be in quotes.
+
# Ruby API
The following objects exist in Oxidized.
diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb
new file mode 100644
index 0000000..b37c9c4
--- /dev/null
+++ b/lib/oxidized/hook/slackdiff.rb
@@ -0,0 +1,30 @@
+require 'slack'
+
+class SlackDiff < Oxidized::Hook
+ def validate_cfg!
+ raise KeyError, 'hook.token is required' unless cfg.has_key?('token')
+ raise KeyError, 'hook.channel is required' unless cfg.has_key?('channel')
+ end
+
+ def run_hook(ctx)
+ if ctx.node
+ if ctx.event.to_s == "post_store"
+ log "Connecting to slack"
+ client = Slack::Client.new token: cfg.token
+ client.auth_test
+ log "Connected"
+ gitoutput = ctx.node.output.new
+ diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil
+ title = "#{ctx.node.name.to_s} #{ctx.node.group.to_s} #{ctx.node.model.class.name.to_s.downcase}"
+ log "Posting diff as snippet to #{cfg.channel}"
+ client.files_upload(channels: cfg.channel, as_user: true,
+ content: diff[:patch].lines.to_a[4..-1].join,
+ filetype: "diff",
+ title: title,
+ filename: "change"
+ )
+ log "Finished"
+ end
+ end
+ end
+end