From e2647b2f318ea8afb1cb48c4d9a2e8d87f1a6858 Mon Sep 17 00:00:00 2001 From: lub Date: Sat, 14 Oct 2017 17:54:58 +0200 Subject: [PATCH] initial ssh support - closes #5 --- config.sample.yaml | 8 +++++++- index.js | 46 ++++++++++++++++++++++++++++++++++++++-------- package.json | 3 ++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/config.sample.yaml b/config.sample.yaml index 6139599..7f9165a 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -1,3 +1,9 @@ baseurl: 'https://example.org' accesstoken: 't0ps3cr3t' -userid: '@jd:example.org' \ No newline at end of file +userid: '@jd:example.org' + +infrastructure: '/home/qwer/scripts' +ssh: + key: '/home/qwer/.ssh/id_rsa' + host: 'srv01.example.org' + user: 'heimdall' \ No newline at end of file diff --git a/index.js b/index.js index d1d88c4..d0d5212 100644 --- a/index.js +++ b/index.js @@ -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(); // Autojoin for the bot (keep commented out when not needed to prevent abuse) /*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 = {}; @@ -27,15 +55,17 @@ commands['rebuild'] = {}; commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/; commands['rebuild'].usage = '!rebuild '; 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```'); + }) + } diff --git a/package.json b/package.json index a90f97d..fcae46c 100644 --- a/package.json +++ b/package.json @@ -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" } }