import ApiStatusCodes from '../api/ApiStatusCodes' ; import CaptainConstants from '../utils/CaptainConstants' ; import Logger from '../utils/Logger' ; import DockerApi from './DockerApi' ; import SshClientImport = require ( 'ssh2' ) ; const SshClient = SshClientImport . Client ; export default class DockerUtils { static joinDockerNode ( dockerApi , sshUser , sshPort , captainIpAddress , isManager , remoteNodeIpAddress , privateKey ) { const remoteUserName = sshUser ; return Promise . resolve ( ) . then ( function ( ) { return dockerApi . getJoinToken ( isManager ) ; } ) . then ( function ( token ) { return new Promise < void > ( function ( resolve , reject ) { const conn = new SshClient ( ) ; conn . on ( 'error' , function ( err ) { Logger . e ( err ) ; reject ( ApiStatusCodes . createError ( ApiStatusCodes . STATUS_ERROR_GENERIC , 'SSH Connection error!!' ) ) ; } ) . on ( 'ready' , function ( ) { Logger . d ( 'SSH Client :: ready' ) ; conn . exec ( `${ CaptainConstants . disableFirewallCommand } ${ dockerApi . createJoinCommand ( captainIpAddress , token , remoteNodeIpAddress ) }` , function ( err , stream ) { if ( err ) { Logger . e ( err ) ; reject ( ApiStatusCodes . createError ( ApiStatusCodes . STATUS_ERROR_GENERIC , 'SSH Running command failed!!' ) ) ; return ; } let hasExisted = false ; stream . on ( 'close' , function ( code , signal ) { Logger . d ( `Stream :: close :: code: ${ code }, signal: ${ signal }` ) ; conn . end ( ) ; if ( hasExisted ) { return ; } hasExisted = true ; resolve ( ) ; } ) . on ( 'data' , function ( data ) { Logger . d ( `STDOUT: ${ data }` ) ; } ) . stderr . on ( 'data' , function ( data ) { Logger . e ( `STDERR: ${ data }` ) ; if ( hasExisted ) { return ; } hasExisted = true ; reject ( ApiStatusCodes . createError ( ApiStatusCodes . STATUS_ERROR_GENERIC , `Error during setup: ${ data }` ) ) ; } ) ; } ) ; } ) . connect ( { host : remoteNodeIpAddress , port : sshPort , username : remoteUserName , privateKey : privateKey , } ) ; } ) ; } ) ; } }