initial ssh support - closes #5

develop
lub 7 years ago
parent 6186516cdb
commit e570db34ef

@ -1,3 +1,9 @@
baseurl: 'https://example.org'
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 node_ssh = require('node-ssh');
const sdk = require('matrix-js-sdk');
const spawn = require('child_process').spawn;
const yaml = require('js-yaml');
const config = yaml.safeLoad(fs.readFileSync('config.yaml'));
const matrixclient = sdk.createClient({
baseUrl: config.baseurl,
accessToken: config.accesstoken,
userId: config.userid
});
const ssh = new node_ssh();
/*matrixclient.on('RoomMember.membership', function(event, member) {
if(member.membership === 'invite' && member.userId === userid) {
@ -19,6 +19,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 = {};
@ -26,15 +54,17 @@ commands['rebuild'] = {};
commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/;
commands['rebuild'].usage = '!rebuild <container>';
commands['rebuild'].exec = function(args, room, event) {
let container = args[1];
const container = args[1];
matrixclient.sendNotice(room.roomId, 'rebuilding container ' + container);
console.log('sudo ./rebuild.sh ' + container);
let script = spawn('/bin/bash', ['./rebuild.sh', container]);
script.on('close', function(exitcode) {
matrixclient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container + '\nexitcode: ' + exitcode);
});
remoterun('./rebuild.sh ' + container)
.then(function(result) {
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": {
"babel-runtime": "^6.26.0",
"js-yaml": "^3.10.0",
"matrix-js-sdk": "^0.8.4"
"matrix-js-sdk": "^0.8.4",
"node-ssh": "^5.0.0"
}
}