summaryrefslogtreecommitdiff
path: root/lib/oxidized
diff options
context:
space:
mode:
authorrgnv <rondutt@gmail.com>2018-04-12 15:22:50 -0700
committerrgnv <rondutt@gmail.com>2018-04-12 15:22:50 -0700
commit09bcf46e276662ef518aac5fb11bc2eb4e127d36 (patch)
treef464316ecbf52e47ced87c064049972d7f578bf4 /lib/oxidized
parent56ec279b2e9e2735ed88405fd177e77f9e376801 (diff)
Added Cisco Spark hook
Diffstat (limited to 'lib/oxidized')
-rw-r--r--lib/oxidized/hook/ciscosparkdiff.rb54
1 files changed, 54 insertions, 0 deletions
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