initial ssh support - closes #5

master
lub 7 years ago
parent 430b4178ac
commit e2647b2f31

@ -1,3 +1,9 @@
baseurl: 'https://example.org' baseurl: 'https://example.org'
accesstoken: 't0ps3cr3t' accesstoken: 't0ps3cr3t'
userid: '@jd:example.org' userid: '@jd:example.org'
infrastructure: '/home/qwer/scripts'
ssh:
key: '/home/qwer/.ssh/id_rsa'
host: 'srv01.example.org'
user: 'heimdall'

@ -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```');
})
} }

@ -15,6 +15,7 @@
"dependencies": { "dependencies": {
"babel-runtime": "^6.26.0", "babel-runtime": "^6.26.0",
"js-yaml": "^3.10.0", "js-yaml": "^3.10.0",
"matrix-js-sdk": "^0.8.4" "matrix-js-sdk": "^0.8.4",
"node-ssh": "^5.0.0"
} }
} }