|
|
@ -1,15 +1,15 @@
|
|
|
|
const fs = require('fs');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const node_ssh = require('node-ssh');
|
|
|
|
const sdk = require('matrix-js-sdk');
|
|
|
|
const sdk = require('matrix-js-sdk');
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
|
|
|
|
|
|
|
|
const config = yaml.safeLoad(fs.readFileSync('config.yaml'));
|
|
|
|
const config = yaml.safeLoad(fs.readFileSync('config.yaml'));
|
|
|
|
|
|
|
|
|
|
|
|
const matrixclient = sdk.createClient({
|
|
|
|
const matrixclient = sdk.createClient({
|
|
|
|
baseUrl: config.baseurl,
|
|
|
|
baseUrl: config.baseurl,
|
|
|
|
accessToken: config.accesstoken,
|
|
|
|
accessToken: config.accesstoken,
|
|
|
|
userId: config.userid
|
|
|
|
userId: config.userid
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const ssh = new node_ssh();
|
|
|
|
|
|
|
|
|
|
|
|
// Autojoin for the bot (keep commented out when not needed to prevent abuse)
|
|
|
|
// Autojoin for the bot (keep commented out when not needed to prevent abuse)
|
|
|
|
/*matrixclient.on('RoomMember.membership', function(event, member) {
|
|
|
|
/*matrixclient.on('RoomMember.membership', function(event, member) {
|
|
|
@ -20,6 +20,34 @@ const matrixclient = sdk.createClient({
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});*/
|
|
|
|
});*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason) => {
|
|
|
|
|
|
|
|
console.log('Reason: ' + reason);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function remoterun(command, args) {
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
|
|
|
|
ssh.connect({
|
|
|
|
|
|
|
|
host: config.ssh.host,
|
|
|
|
|
|
|
|
username: config.ssh.user,
|
|
|
|
|
|
|
|
privateKey: config.ssh.key
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.then(function(){
|
|
|
|
|
|
|
|
ssh.execCommand('/usr/bin/sudo -- ' + command, {cwd: config.infrastructure})
|
|
|
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
|
|
|
if(result.code === 0) {
|
|
|
|
|
|
|
|
resolve(result)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reject(result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, function(result) {
|
|
|
|
|
|
|
|
reject(result)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}, function(result) {
|
|
|
|
|
|
|
|
reject(result)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const commands = {};
|
|
|
|
const commands = {};
|
|
|
|
|
|
|
|
|
|
|
@ -27,15 +55,17 @@ commands['rebuild'] = {};
|
|
|
|
commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/;
|
|
|
|
commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/;
|
|
|
|
commands['rebuild'].usage = '!rebuild <container>';
|
|
|
|
commands['rebuild'].usage = '!rebuild <container>';
|
|
|
|
commands['rebuild'].exec = function(args, room, event) {
|
|
|
|
commands['rebuild'].exec = function(args, room, event) {
|
|
|
|
let container = args[1];
|
|
|
|
const container = args[1];
|
|
|
|
|
|
|
|
|
|
|
|
matrixclient.sendNotice(room.roomId, 'rebuilding container ' + container);
|
|
|
|
matrixclient.sendNotice(room.roomId, 'rebuilding container ' + container);
|
|
|
|
console.log('sudo ./rebuild.sh ' + container);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let script = spawn('/bin/bash', ['./rebuild.sh', container]);
|
|
|
|
remoterun('./rebuild.sh ' + container)
|
|
|
|
script.on('close', function(exitcode) {
|
|
|
|
.then(function(result) {
|
|
|
|
matrixclient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container + '\nexitcode: ' + exitcode);
|
|
|
|
matrixclient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container);
|
|
|
|
});
|
|
|
|
}, function(result) {
|
|
|
|
|
|
|
|
matrixclient.sendNotice(room.roomId, event.sender.userId + '\nrebuild of container ' + container + ' failed.\ncode: ' + result.code + '\nstdout: \n```\n' + result.stdout + '```\nstderr: \n```\n' + result.stderr + '\n```');
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|