From 0681060c51ac09af1e89610c6aae56321e429fa1 Mon Sep 17 00:00:00 2001 From: lub Date: Sun, 15 Oct 2017 17:57:41 +0200 Subject: [PATCH] move remoterun to remoteExec.js --- index.js | 29 ++--------------------------- remoteExec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 remoteExec.js diff --git a/index.js b/index.js index 5c1d1c6..06e92bf 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const nodeSsh = require('node-ssh'); +const remoteExec = require('./remoteExec.js'); const sdk = require('matrix-js-sdk'); const yaml = require('js-yaml'); @@ -9,7 +9,6 @@ const matrixClient = sdk.createClient({ accessToken: config.matrix.accesstoken, userId: config.matrix.userid }); -const ssh = new nodeSsh(); // Autojoin for the bot (keep commented out when not needed to prevent abuse) /*matrixClient.on('RoomMember.membership', function(event, member) { @@ -25,30 +24,6 @@ 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 = {}; @@ -60,7 +35,7 @@ commands['rebuild'].exec = function(args, room, event) { matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container); - remoterun('./rebuild.sh ' + container) + remoteExec('./rebuild.sh ' + container, config) .then(function(result) { matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container); }, function(result) { diff --git a/remoteExec.js b/remoteExec.js new file mode 100644 index 0000000..370226a --- /dev/null +++ b/remoteExec.js @@ -0,0 +1,32 @@ +const nodeSsh = require('node-ssh'); +const ssh = new nodeSsh(); + + +module.exports = (command, config) => { + console.log('l6'); + 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) { + console.log('l17'); + resolve(result); + } else { + console.log('l20'); + reject(result); + } + }, function(result) { + console.log('l24'); + reject(result); + }) + }, function(result) { + console.log('l28'); + reject(result); + }); + }); +}; \ No newline at end of file