move remoterun to remoteExec.js

master
lub 7 years ago
parent 9e433f5f6a
commit 0681060c51

@ -1,5 +1,5 @@
const fs = require('fs'); const fs = require('fs');
const nodeSsh = require('node-ssh'); const remoteExec = require('./remoteExec.js');
const sdk = require('matrix-js-sdk'); const sdk = require('matrix-js-sdk');
const yaml = require('js-yaml'); const yaml = require('js-yaml');
@ -9,7 +9,6 @@ const matrixClient = sdk.createClient({
accessToken: config.matrix.accesstoken, accessToken: config.matrix.accesstoken,
userId: config.matrix.userid userId: config.matrix.userid
}); });
const ssh = new nodeSsh();
// 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) {
@ -25,30 +24,6 @@ process.on('unhandledRejection', (reason) => {
console.log('Reason: ' + 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 = {};
@ -60,7 +35,7 @@ commands['rebuild'].exec = function(args, room, event) {
matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container); matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container);
remoterun('./rebuild.sh ' + container) remoteExec('./rebuild.sh ' + container, config)
.then(function(result) { .then(function(result) {
matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container); matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container);
}, function(result) { }, function(result) {

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