diff options
| -rw-r--r-- | Dockerfile | 3 | ||||
| -rw-r--r-- | README.md | 23 | ||||
| -rw-r--r-- | lib/oxidized/hook/slackdiff.rb | 30 | 
3 files changed, 55 insertions, 1 deletions
| @@ -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 @@ -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 | 
