Skip to content

Instantly share code, notes, and snippets.

@dziamid
Created August 18, 2016 10:08
Show Gist options
  • Save dziamid/90eb02628ba0b02b92a0342dfe5af22f to your computer and use it in GitHub Desktop.
Save dziamid/90eb02628ba0b02b92a0342dfe5af22f to your computer and use it in GitHub Desktop.
var ssh = require('ssh2');
var Promise = require('bluebird');
var sshConnectionReady = false;
var sshConnection;
const EventEmitter = require('eventemitter3');
const emitter = new EventEmitter();
function connect() {
sshConnection = new ssh.Client();
sshConnection.connect({
host: config['QN_Address'],
port: 22,
username: 'ec2-user',
privateKey: require('fs').readFileSync(config['QN_Key'])
});
sshConnection.once('ready', () => {
emitter.emit('ready');
sshConnectionReady = true;
});
sshConnection.once('error', () => {
sshConnectionReady = false;
});
}
function getConfig() {
connect();
}
function getConnection() {
return new Promise((resolve, reject) => {
if (sshConnectionReady) {
resolve(sshConnection);
} else {
emitter.once('ready', () => resolve(sshConnection))
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment