-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (123 loc) · 4.03 KB
/
Copy pathindex.js
File metadata and controls
135 lines (123 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const fs = require('fs-promise');
const dateFormat = require('dateformat');
const node_ssh = require('node-ssh');
const pm2 = require('pm2');
const SlackBot = require('slackbots');
//Stuff that will eventually be in a config file
const host = 'sockrpgtest.sockdrawer.io';
const botList = ['sockbot', 'zoidberg'];
const pullList = ['/usr/local/sockbot/SockBot'];
const slackToken = process.env.SLACK_TOKEN || 'invalid';
const slackbotName = 'Rover';
const slackChannel = '#cd-project';
const logfile = '/home/rover/hooksreceived.log';
const logfileRemote = logfile;
const keyfile = '/home/rover/.ssh/id_rsa';
/**
* Log a message
* @method log
* @param {} msg The message to log
* @return Promise that resolves when the log is done
*/
function log(msg) {
const timestamp = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
const line = `[${timestamp}] ${msg} \n`;
return fs.appendFile(logfile, line);
}
module.exports = {
ssh: new node_ssh(),
slackbot: undefined,
/**
* Connect to PM2
* @method pm2_connect
* @return promise that resolves to a connected PM2 instance
*/
pm2_connect: function() {
let promise = new Promise((resolve, reject) => {
pm2.connect(function(err) {
if (err) return reject(err);
return resolve(pm2);
});
});
return promise;
},
/**
* Restart pm2
* @method pm2_restart
* @param {} process the process to restart
* @return promise that resolves to the restarted process
*/
pm2_restart: function(process) {
let promise = new Promise((resolve, reject) => {
pm2.restart(process, function(err, proc) {
if (err) return reject(err);
return resolve(proc);
});
});
return promise;
},
/**
* Handle an incoming call
* @method handle
* @param {} body The body of the request
* @return Promise resolves when the handler is ready for another request
*/
handle: function(body) {
const zen = body.zen;
const ssh = module.exports.ssh;
/**
* Restart a single bot
* @method restartBot
* @param {} name The name of the bot to restart
* @return Promise a promise that resolves when the bot is restarted
*/
function restartBot(name) {
return module.exports.pm2_restart(name)
.then(() => log(`Restarted ${name}`));
}
/**
* Execute a git pull on a directory
* @method pull
* @param {} dir The diretory to pull
* @return Promise A promise that resolves when the pull is done
*/
function pull (dir) {
return ssh.exec('git pull', [], { cwd: dir});
}
if (!module.exports.slackbot) {
module.exports.slackbot = new SlackBot({
token: slackToken,
name: slackbotName
});
}
return log(zen)
.catch((err) => console.error(err))
.then(() => ssh.connect({
host: host,
username: 'rover',
privateKey: keyfile
}))
.then(() => module.exports.pm2_connect())
.then(() => Promise.all(botList.map(restartBot)))
.then(() => pm2.disconnect())
.then(() => ssh.putFile(logfile, logfileRemote))
.then(() => Promise.all(pullList.map(pull)))
.then(() => module.exports.slackbot.postMessageToChannel(slackChannel, "Sockbot updated!"))
.catch((err) => {
return log(`ERROR: ${err.toString()}`);
});
}
};
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/updated', function (req, res, next) {
return module.exports.handle(req.body)
.then(() => res.sendStatus(200))
.catch(next);
});
app.listen(3000, function () {
console.log('Listening on port 3000!');
});