Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestgwilsonii committed Aug 23, 2016
1 parent d4a9897 commit 65c3e39
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <Ernest.Wilson@ctl.io> (https://github.com/ernestgwilsonii)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/chrysus-io/hubot-chrysus-dispatcher.git"
},
"devDependencies": {
"mocha": "^2.4.5"
}
}
142 changes: 142 additions & 0 deletions scripts/hubot-chrysus-dispatcher-mtr.js
Original file line number Diff line number Diff line change
@@ -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 <ErnestGWilsonII@gmail.com>
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// 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);
});
};
////////////////////////////////////////////////////////////
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
});

0 comments on commit 65c3e39

Please sign in to comment.