From 09bcf46e276662ef518aac5fb11bc2eb4e127d36 Mon Sep 17 00:00:00 2001 From: rgnv Date: Thu, 12 Apr 2018 15:22:50 -0700 Subject: Added Cisco Spark hook --- README.md | 1 + docs/Hooks.md | 37 +++++++++++++++++++++++++ lib/oxidized/hook/ciscosparkdiff.rb | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/oxidized/hook/ciscosparkdiff.rb diff --git a/README.md b/README.md index ebe3d43..4a389f3 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Check out the [Oxidized TREX 2014 presentation](http://youtu.be/kBQ_CTUuqeU#t=3h * [Hook: awssns](docs/Hooks.md#hook-type-awssns) * [Hook: slackdiff](docs/Hooks.md#hook-type-slackdiff) * [Hook: xmppdiff](docs/Hooks.md#hook-type-xmppdiff) + * [Hook: ciscosparkdiff](docs/Hooks.md#hook-type-ciscosparkdiff) 5. [Creating and Extending Models](docs/Creating-Models.md) 6. [Help](#help) 7. [Ruby API](docs/Ruby-API.md#ruby-api) diff --git a/docs/Hooks.md b/docs/Hooks.md index fb80a19..d1b6925 100644 --- a/docs/Hooks.md +++ b/docs/Hooks.md @@ -184,6 +184,43 @@ hooks: Note the channel name must be in quotes. +## Hook type: ciscosparkdiff + +The `ciscosparkdiff` hook posts config diffs to a [Cisco Spark](https://www.ciscospark.com/) space of your choice. It only triggers for `post_store` events. + +You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby): + +```shell +gem install cisco_spark +``` + +### ciscosparkdiff hook configuration example + +```yaml +hooks: + ciscospark: + type: ciscosparkdiff + events: [post_store] + accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html) + space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html) + diff: true +``` + +Optionally you can disable snippets and post a formatted message, for instance linking to a commit in a git repo. Named parameters `%{node}`, `%{group}`, `%{model}` and `%{commitref}` are available. + +```yaml +hooks: + ciscospark: + type: ciscosparkdiff + events: [post_store] + accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html) + space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html) + diff: false + message: "%{node} %{group} %{model} updated https://git.intranet/network-changes/commit/%{commitref}" +``` + +Note the space and access tokens must be in quotes. + ## Hook type: xmppdiff The `xmppdiff` hook posts config diffs to a [XMPP](https://en.wikipedia.org/wiki/XMPP) chatroom of your choice. It only triggers for `post_store` events. diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb new file mode 100644 index 0000000..a1c9130 --- /dev/null +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -0,0 +1,54 @@ +require 'cisco_spark' + +# defaults to posting a diff, if messageformat is supplied them a message will be posted too +# diffenable defaults to true +# Modified from slackdiff + +class CiscoSparkDiff < Oxidized::Hook + def validate_cfg! + raise KeyError, 'hook.accesskey is required' unless cfg.has_key?('accesskey') + raise KeyError, 'hook.space is required' unless cfg.has_key?('space') + end + + def run_hook(ctx) + if ctx.node + if ctx.event.to_s == "post_store" + log "Connecting to Cisco Spark" + CiscoSpark.configure do |config| + config.api_key = cfg.accesskey + config.proxy = cfg.proxy if cfg.has_key?('proxy') + end + space = cfg.space + client = CiscoSpark::Room.new(id: space) + client.fetch + log "Connected" + # diff snippet - default + diffenable = true + if cfg.has_key?('diff') == true + if cfg.diff == false + diffenable = false + end + end + if diffenable == true + gitoutput = ctx.node.output.new + diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil + title = "#{ctx.node.name.to_s}" + log "Posting diff as snippet to #{cfg.space}" + message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join + ) + room = CiscoSpark::Room.new(id: space) + room.send_message(message) + end + # message custom formatted - optional + if cfg.has_key?('message') == true + log cfg.message + msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase} + log msg + log "Posting message to #{cfg.space}" + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + end + log "Finished" + end + end + end +end -- cgit v1.2.1 From 93508ddee0226a271ab2b7db56443518e90ec50c Mon Sep 17 00:00:00 2001 From: rgnv Date: Thu, 12 Apr 2018 15:32:44 -0700 Subject: Fixed markdown for Spark hook --- docs/Hooks.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/Hooks.md b/docs/Hooks.md index d1b6925..92ba9ed 100644 --- a/docs/Hooks.md +++ b/docs/Hooks.md @@ -188,7 +188,7 @@ Note the channel name must be in quotes. The `ciscosparkdiff` hook posts config diffs to a [Cisco Spark](https://www.ciscospark.com/) space of your choice. It only triggers for `post_store` events. -You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby): +You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby) and generate either a [Bot or OAUTH access key](https://developer.ciscospark.com/apps.html), and retrieve the [Spark Space ID](https://developer.ciscospark.com/endpoint-rooms-get.html) ```shell gem install cisco_spark @@ -201,8 +201,8 @@ hooks: ciscospark: type: ciscosparkdiff events: [post_store] - accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html) - space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html) + accesskey: SPARK_BOT_API_OR_OAUTH_KEY + space: SPARK_SPACE_ID diff: true ``` -- cgit v1.2.1 From 48e50aca2229214fd5465d7faf1bd2908358c9ab Mon Sep 17 00:00:00 2001 From: rgnv Date: Thu, 12 Apr 2018 15:34:11 -0700 Subject: Fixed markdown for Spark hook --- docs/Hooks.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/Hooks.md b/docs/Hooks.md index 92ba9ed..b11570a 100644 --- a/docs/Hooks.md +++ b/docs/Hooks.md @@ -188,7 +188,7 @@ Note the channel name must be in quotes. The `ciscosparkdiff` hook posts config diffs to a [Cisco Spark](https://www.ciscospark.com/) space of your choice. It only triggers for `post_store` events. -You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby) and generate either a [Bot or OAUTH access key](https://developer.ciscospark.com/apps.html), and retrieve the [Spark Space ID](https://developer.ciscospark.com/endpoint-rooms-get.html) +You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby ) and generate either a [Bot or OAUTH access key](https://developer.ciscospark.com/apps.html), and retrieve the [Spark Space ID](https://developer.ciscospark.com/endpoint-rooms-get.html) ```shell gem install cisco_spark @@ -213,8 +213,8 @@ hooks: ciscospark: type: ciscosparkdiff events: [post_store] - accesskey: [SPARK_BOT_API_OR_OAUTH_KEY](https://developer.ciscospark.com/apps.html) - space: [SPARK_SPACE_ID](https://developer.ciscospark.com/endpoint-rooms-get.html) + accesskey: SPARK_BOT_API_OR_OAUTH_KEY + space: SPARK_SPACE_ID diff: false message: "%{node} %{group} %{model} updated https://git.intranet/network-changes/commit/%{commitref}" ``` -- cgit v1.2.1 From ec90b081f0fed05497ce6ae21e4b4ba4d44ecc01 Mon Sep 17 00:00:00 2001 From: rgnv Date: Thu, 12 Apr 2018 15:41:31 -0700 Subject: fix formatting to comply with Rubocop --- docs/Hooks.md | 2 +- lib/oxidized/hook/ciscosparkdiff.rb | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/docs/Hooks.md b/docs/Hooks.md index b11570a..ee7b3ee 100644 --- a/docs/Hooks.md +++ b/docs/Hooks.md @@ -188,7 +188,7 @@ Note the channel name must be in quotes. The `ciscosparkdiff` hook posts config diffs to a [Cisco Spark](https://www.ciscospark.com/) space of your choice. It only triggers for `post_store` events. -You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby ) and generate either a [Bot or OAUTH access key](https://developer.ciscospark.com/apps.html), and retrieve the [Spark Space ID](https://developer.ciscospark.com/endpoint-rooms-get.html) +You will need to manually install the `cisco_spark` gem on your system (see [cisco_spark-ruby](https://github.com/NGMarmaduke/cisco_spark-ruby)) and generate either a [Bot or OAUTH access key](https://developer.ciscospark.com/apps.html), and retrieve the [Spark Space ID](https://developer.ciscospark.com/endpoint-rooms-get.html) ```shell gem install cisco_spark diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb index a1c9130..12094e6 100644 --- a/lib/oxidized/hook/ciscosparkdiff.rb +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -22,7 +22,6 @@ class CiscoSparkDiff < Oxidized::Hook client = CiscoSpark::Room.new(id: space) client.fetch log "Connected" - # diff snippet - default diffenable = true if cfg.has_key?('diff') == true if cfg.diff == false @@ -34,12 +33,10 @@ class CiscoSparkDiff < Oxidized::Hook diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil title = "#{ctx.node.name.to_s}" log "Posting diff as snippet to #{cfg.space}" - message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join - ) + message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) room = CiscoSpark::Room.new(id: space) room.send_message(message) end - # message custom formatted - optional if cfg.has_key?('message') == true log cfg.message msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase} -- cgit v1.2.1 From a26a1f78a71a03f5de7bbeee11a87d57be6a5343 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 15 Apr 2018 08:58:25 +0200 Subject: massage into rubocop compliance --- lib/oxidized/hook/ciscosparkdiff.rb | 66 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb index 12094e6..d43ff81 100644 --- a/lib/oxidized/hook/ciscosparkdiff.rb +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -11,41 +11,39 @@ class CiscoSparkDiff < Oxidized::Hook end def run_hook(ctx) - if ctx.node - if ctx.event.to_s == "post_store" - log "Connecting to Cisco Spark" - CiscoSpark.configure do |config| - config.api_key = cfg.accesskey - config.proxy = cfg.proxy if cfg.has_key?('proxy') - end - space = cfg.space - client = CiscoSpark::Room.new(id: space) - client.fetch - log "Connected" - diffenable = true - if cfg.has_key?('diff') == true - if cfg.diff == false - diffenable = false - end - end - if diffenable == true - gitoutput = ctx.node.output.new - diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil - title = "#{ctx.node.name.to_s}" - log "Posting diff as snippet to #{cfg.space}" - message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) - room = CiscoSpark::Room.new(id: space) - room.send_message(message) - end - if cfg.has_key?('message') == true - log cfg.message - msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase} - log msg - log "Posting message to #{cfg.space}" - client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) - end - log "Finished" + return unless ctx.node + return unless ctx.event.to_s == "post_store" + log "Connecting to Cisco Spark" + CiscoSpark.configure do |config| + config.api_key = cfg.accesskey + config.proxy = cfg.proxy if cfg.has_key?('proxy') + end + space = cfg.space + client = CiscoSpark::Room.new(id: space) + client.fetch + log "Connected" + diffenable = true + if cfg.has_key?('diff') == true + if cfg.diff == false + diffenable = false end end + if diffenable == true + gitoutput = ctx.node.output.new + diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil + title = ctx.node.name.to_s + log "Posting diff as snippet to #{cfg.space}" + message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) + room = CiscoSpark::Room.new(id: space) + room.send_message(message) + end + if cfg.has_key?('message') == true + log cfg.message + msg = format(cfg.message, :node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase) + log msg + log "Posting message to #{cfg.space}" + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + end + log "Finished" end end -- cgit v1.2.1 From cd92bac4a582eb8e19d9d036455591c9684eae72 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 15 Apr 2018 09:29:32 +0200 Subject: add cisco_spark gem to docker container for ciscosparkdiff hook --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 07adc0f..89772db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,7 @@ COPY --from=libssh2-backport \ RUN dpkg -i /tmp/*.deb # dependencies for hooks -RUN gem install aws-sdk slack-api xmpp4r +RUN gem install aws-sdk slack-api xmpp4r cisco_spark # build and install oxidized COPY . /tmp/oxidized/ -- cgit v1.2.1 From 83a00943d7333fafdfe352173713a22a5ac89f6f Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Fri, 27 Apr 2018 12:39:07 +0200 Subject: bring ciscosparkdiff.rb back into compliance with rubocop --- lib/oxidized/hook/ciscosparkdiff.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb index d43ff81..e45d7c6 100644 --- a/lib/oxidized/hook/ciscosparkdiff.rb +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -15,8 +15,8 @@ class CiscoSparkDiff < Oxidized::Hook return unless ctx.event.to_s == "post_store" log "Connecting to Cisco Spark" CiscoSpark.configure do |config| - config.api_key = cfg.accesskey - config.proxy = cfg.proxy if cfg.has_key?('proxy') + config.api_key = cfg.accesskey + config.proxy = cfg.proxy if cfg.has_key?('proxy') end space = cfg.space client = CiscoSpark::Room.new(id: space) @@ -39,10 +39,10 @@ class CiscoSparkDiff < Oxidized::Hook end if cfg.has_key?('message') == true log cfg.message - msg = format(cfg.message, :node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase) + msg = cfg.message % { :node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase } log msg log "Posting message to #{cfg.space}" - client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) end log "Finished" end -- cgit v1.2.1