From 65c3e39c8323356a195f6cffd84e6e7a8406ce28 Mon Sep 17 00:00:00 2001 From: ernestgwilsonii Date: Tue, 23 Aug 2016 15:42:27 -0400 Subject: [PATCH] initial commit --- .npmignore | 33 ++++++ LICENSE | 2 +- index.coffee | 8 ++ package.json | 19 ++++ scripts/hubot-chrysus-dispatcher-mtr.js | 142 ++++++++++++++++++++++++ test/test.js | 9 ++ 6 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 .npmignore create mode 100644 index.coffee create mode 100644 package.json create mode 100644 scripts/hubot-chrysus-dispatcher-mtr.js create mode 100644 test/test.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..e2174d5 --- /dev/null +++ b/.npmignore @@ -0,0 +1,33 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Commenting this out is preferred by some people, see +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Users Environment Variables +.lock-wscript + +# ignore test directory +test/ + +.idea diff --git a/LICENSE b/LICENSE index f8b4661..840a718 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016 chrysus-io +Copyright (c) 2016 ernestgwilsonii Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/index.coffee b/index.coffee new file mode 100644 index 0000000..e6ab3d6 --- /dev/null +++ b/index.coffee @@ -0,0 +1,8 @@ +Fs = require 'fs' +Path = require 'path' + +module.exports = (robot) -> + scriptsPath = Path.resolve __dirname, 'scripts' + Fs.exists scriptsPath, (exists) -> + if exists + robot.loadFile scriptsPath, file for file in Fs.readdirSync(scriptsPath) diff --git a/package.json b/package.json new file mode 100644 index 0000000..990ea3e --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "hubot-chrysus-dispatcher", + "version": "0.8.5", + "description": "ChatOps Chrysus Dispatcher module", + "main": "index.cofee", + "scripts": { + "test": "mocha test" + }, + "keywords": [], + "author": "Ernest G. Wilson II (https://github.com/ernestgwilsonii)", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/chrysus-io/hubot-chrysus-dispatcher.git" + }, + "devDependencies": { + "mocha": "^2.4.5" + } +} diff --git a/scripts/hubot-chrysus-dispatcher-mtr.js b/scripts/hubot-chrysus-dispatcher-mtr.js new file mode 100644 index 0000000..651fdff --- /dev/null +++ b/scripts/hubot-chrysus-dispatcher-mtr.js @@ -0,0 +1,142 @@ +// Description: +// Use Hubot to send messages to Redis for Chrysus to consume! +// +// Dependencies: +// "hubot": "latest" +// "node-slack-client": "latest" +// "redis": "latest" +// +// Configuration: +// HUBOT_SLACK_TOKEN - This is the Hubot's Slack API token key +// CHRYSUS_REDISHOST - This is the Redis IP Address or FQDN (must be resolvable from Hubot) +// CHRYSUS_REDISPORT - This is the Redis TCP port (must be reachable from Hubot, usually TCP 6379) +// CHRYSUS_REDISPASS - This is the Redis password (normally found on Redis in /etc/redis.conf set via requirepass RedisPasswordHere) +// CHRYSUS_REDISCHAN - This is the Redis Pub/Sub channel (usually 'incoming' for Chrysus) +// +// Commands: +// mtr IPAddressOrName +// +// Author: +// Ernest G. Wilson II +//////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////// +// Ensure startup configuration // +/////////////////////////////////// +// Verify that all of the needed environment variables are available to start the Chrysus Engine +// Note: These are typically set in: /etc/systemd/system/chatops.service +// For console debug and testing you may manually set these variables by: +// export CHRYSUS_REDISHOST='RedisIPAddressHere' +// export CHRYSUS_REDISPORT='6379' +// export CHRYSUS_REDISPASS='RedisPasswordHere' +// export CHRYSUS_REDISCHAN='incoming' +var ensureConfig = function () { + if (!process.env.HUBOT_SLACK_TOKEN) { + throw new Error("Error: HUBOT_SLACK_TOKEN environment variable is not set"); + } + if (!process.env.CHRYSUS_REDISHOST) { + throw new Error("Error: CHRYSUS_REDISHOST environment variable is not set"); + } + if (!process.env.CHRYSUS_REDISPORT) { + throw new Error("Error: CHRYSUS_REDISPORT environment variable is not set"); + } + if (!process.env.CHRYSUS_REDISPASS) { + throw new Error("Error: CHRYSUS_REDISPASS environment variable is not set"); + } + if (!process.env.CHRYSUS_REDISCHAN) { + throw new Error("Error: CHRYSUS_REDISCHAN environment variable is not set"); + } + return true; +}; +ensureConfig(); +//////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////// +// Global variables // +////////////////////// +var debug = true; // Controls debugging - In production turn OFF debugging! +var botKey = process.env.HUBOT_SLACK_TOKEN; // This is the unique Slack API Hubot key that identifies which bot the results will get posted as +// Require specific Node.js modules +var redis = require("redis"); +// Populate global Redis connection parameters variables based on environment variables +var redisHost = process.env.CHRYSUS_REDISHOST; // 'RedisIPAddressHere' +var redisPort = process.env.CHRYSUS_REDISPORT; // 'RedisPortHere' +var redisPass = process.env.CHRYSUS_REDISPASS; // 'RedisPasswordHere' +var redisChan = process.env.CHRYSUS_REDISCHAN; // 'RedisChannelHere' +//////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////// +module.exports = function (robot) { + + robot.respond(/mtr (.*)/i, function(msg) { + + // Optional debug is helpful for trouble shooting + if (debug === true){console.log("msg.envelope.user.name: " + msg.envelope.user.name);} // See value of msg.envelope.user.name + if (debug === true){console.log("msg.envelope.user.id: " + msg.envelope.user.id);} // See value of msg.envelope.user.id + if (debug === true){console.log("msg.message.room: " + msg.message.room);} // See value of msg.message.room + + // Logging + if (msg.envelope.user.name === msg.message.room) {console.log(msg.envelope.user.id + " " + msg.envelope.user.name + " in PRIVATE_DIRECT_MESSAGE issued " + msg.message);} + else {console.log(msg.envelope.user.id + " " + msg.envelope.user.name + " in " + msg.message.room + " issued " + msg.message);} + + // Determine which Slack channel to have the output sent to (was the request sent to Hubot in a private message or in a specific channel) + var SendToChannel; + if (msg.envelope.user.name === msg.message.room) {SendToChannel=msg.envelope.user.id;} // Send it to the human in a private response where they requested it + else {SendToChannel=msg.message.room;} // Send it to the specific channel where the human sent the request from + + // Read the human's input + var AllArguments = msg.match[0]; + var Arg = AllArguments.split(" "); + // Argument debugging + if (debug === true){console.log("AllArguments: " + AllArguments);} // See value of AllArguments + if (debug === true){console.log("Arg0: " + Arg[0]);} // See value of Arg[0] + if (debug === true){console.log("Arg1: " + Arg[1]);} // See value of Arg[1] + if (debug === true){console.log("Arg2: " + Arg[2]);} // See value of Arg[2] + if (debug === true){console.log("Arg3: " + Arg[3]);} // See value of Arg[3] + if (debug === true){console.log("Arg4: " + Arg[4]);} // See value of Arg[4] + + // Reply to the human so they know we heard the request! + msg.send("Just a moment please as I perform the requested mtr for you..."); + + // Generate the message that will be sent to Redis for Chrysus to consume + var publishMessage = { + "message": { + "tasks": [ + { + "task": "mtr", + "target": Arg[2], + "outputs": [ + { + "output": "slack", + "botKey": botKey, + "reqName": msg.envelope.user.name, + "reqRoom": SendToChannel, + "reqRawText": msg.envelope.message.rawText + } + ] + } + ] + } + } + + var publisher = redis.createClient(redisPort, redisHost); + var dbAuth = function () { + publisher.auth(redisPass); + }; + publisher.addListener('connected', dbAuth); + publisher.addListener('reconnected', dbAuth); + dbAuth(); + + var channel = redisChan; + + function myPublisher() { + publisher.publish(channel, JSON.stringify(publishMessage)); + } + + myPublisher(channel, publishMessage); + }); +}; +//////////////////////////////////////////////////////////// diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..b2d2369 --- /dev/null +++ b/test/test.js @@ -0,0 +1,9 @@ +var assert = require('assert'); +describe('Array', function() { + describe('#indexOf()', function () { + it('should return -1 when the value is not present', function () { + assert.equal(-1, [1,2,3].indexOf(5)); + assert.equal(-1, [1,2,3].indexOf(0)); + }); + }); +});