Last active
May 17, 2019 20:40
-
-
Save seahyc/ace574d448a900f4dc3b15b89e6f471a to your computer and use it in GitHub Desktop.
Listens to Rancher websocket events, and notify through slack webhook when application is upgraded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Setup: | |
npm install ws | |
Usage: | |
Create an API key in Rancher and start up with: | |
node socket.js address.of.rancher:8080 access_key secret_key project_id | |
*/ | |
var WebSocket = require('ws'); | |
var slackWebhook = require('slack-webhook'); | |
var keys = require('./keys.json'); | |
var env = process.argv[2]; | |
var host = keys[env]['host'] | |
var accessKey = keys[env]['accessKey']; | |
var secretKey = keys[env]['secretKey']; | |
var projectId = keys[env]['projectId']; | |
var slackWebHookURI = keys[env]['slackWebHookURI'] | |
var slack = new slackWebhook(slackWebHookURI); | |
var url = 'wss://'+accessKey+':'+secretKey+'@'+host+'/v1/projects/'+projectId+'/subscribe?eventNames=resource.change'; | |
var socket = new WebSocket(url); | |
socket.on('open', function() { | |
console.log('Socket opened'); | |
}); | |
socket.on('message', function(messageStr) { | |
try { | |
var message = JSON.parse(messageStr); | |
if ( message.name === 'resource.change' && message.data ) { | |
var resource = message.data.resource; | |
if ( resource.state === 'upgrading' ) { | |
console.log(message.data); | |
slack.send('The service *"' + resource.name + '-' + env + '"* is _upgrading_ :construction_worker::skin-tone-4:'); | |
} | |
else if ( resource.state === 'upgraded' ) { | |
console.log(message.data); | |
slack.send('The service *"' + resource.name + '-' + env + '"* is _upgraded_ Please complete upgrade, ma boys! :yanrong:'); | |
} | |
} | |
} | |
catch (e) { | |
console.log(e); | |
} | |
}); | |
socket.on('close', function() { | |
console.log('Socket closed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment