Created
August 18, 2016 10:08
-
-
Save dziamid/90eb02628ba0b02b92a0342dfe5af22f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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