src/ssh-client.js
/**
     * Executes a set of commands sequentially, and returns the results of
     * command execution. Command set execution will only continue after each
     * command in the set has completed successfully. Execution will be aborted
     * if a failure is encountered.
     *
     * Command execution will be treated as a failure if the `exec()` method
     * on the ssh client fails, or if the command exits with a non zero error
     * code.
     *
     * @private
     * @param {Object} client A reference to the ssh client to use to execute
     *        the commands. It is assumed that the client has successfully
     *        established a connection to the remote host.
     * @param {String[]} commands An array of strings representing the commands
     *        to be executed on the remote host.
     *
     * @return {Promise<ExecutionResults[]>} An array of execution results for
     *         each command that was executed. If commands are not executed due
     *         to a failure in an earlier command, no execution result will be
     *         provided for that command.
     *         Note that this method always resolves its promise even if a
     *         command execution fails.
     */
Executes a set of commands sequentially, and returns the results of
command execution. Command set execution will only continue after each
command in the set has completed successfully. Execution will be aborted
if a failure is encountered.

Command execution will be treated as a failure if the `exec()` method
on the ssh client fails, or if the command exits with a non zero error
code.

@private
@param {Object} client A reference to the ssh client to use to execute
the commands. It is assumed that the client has successfully
established a connection to the remote host.
@param {String[]} commands An array of strings representing the commands
to be executed on the remote host.

@return {Promise} An array of execution results for
each command that was executed. If commands are not executed due
to a failure in an earlier command, no execution result will be
provided for that command.
Note that this method always resolves its promise even if a
command execution fails.
Executes a set of commands sequentially and returns the results of command execution . Command set execution will only continue after each command in the set has completed successfully . Execution will be aborted if a failure is encountered . Command execution will be treated as a failure if the ` exec () ` method on the ssh client fails or if the command exits with a non zero error code . @private @param { Object } client A reference to the ssh client to use to execute the commands . It is assumed that the client has successfully established a connection to the remote host . @param { String [] } commands An array of strings representing the commands to be executed on the remote host . @return { Promise } An array of execution results for each command that was executed . If commands are not executed due to a failure in an earlier command no execution result will be provided for that command . Note that this method always resolves its promise even if a command execution fails .
_runCommandSet(client, commands) {
        const results = [];
        let _finishMethod = undefined;
        client.on('continue', () => {
            this.logger.trace('Continuing command execution');
            if (_finishMethod) {
                _finishMethod();
            }
        });
        return Promise.mapSeries(commands, (command) => {
            return new Promise((resolve, reject) => {
                _finishMethod = undefined;
                this.logger.trace('Executing command', { command });
                const callNext = client.exec(command, (err, stream) => {
                    const result = {
                        command,
                        success: true,
                        stdout: '',
                        stderr: '',
                    };
                    results.push(result);
                    if (err) {
                        this.logger.trace(err, 'Error executing command');
                        result.success = false;
                        result.error = err;
                        reject(err);
                        return;
                    }
                    stream
                        .on('close', (code, signal) => {
                            result.exitCode = code;
                            if (code !== 0) {
                                const err = new Error(
                                    'Program exited with non zero exit code'
                                );
                                result.error = err;
                                result.success = false;
                                reject(err);
                            } else if (callNext) {
                                this.logger.trace(
                                    'Command execution successful. Invoking next'
                                );
                                resolve(result);
                            } else {
                                this.logger.trace(
                                    'Command execution successful, but client not ready. Waiting'
                                );
                                _finishMethod = () => {
                                    resolve(result);
                                    _finishMethod = undefined;
                                };
                            }
                        })
                        .on('data', (data) => {
                            result.stdout += data;
                        })
                        .stderr.on('data', (data) => {
                            result.stderr += data;
                        });
                });
            });
        })
            .then(() => {
                this.logger.trace('Command set executed successfully');
                return results;
            })
            .catch((err) => {
                this.logger.error(err, 'Error running command set');
                return results;
            });
    }
_runCommandSet ( client , commands ) { const results = [ ] ; let _finishMethod = undefined ; client . on ( 'continue' , ( ) => { this . logger . trace ( 'Continuing command execution' ) ; if ( _finishMethod ) { _finishMethod ( ) ; } } ) ; return Promise . mapSeries ( commands , ( command ) => { return new Promise ( ( resolve , reject ) => { _finishMethod = undefined ; this . logger . trace ( 'Executing command' , { command } ) ; const callNext = client . exec ( command , ( err , stream ) => { const result = { command , success : true , stdout : '' , stderr : '' , } ; results . push ( result ) ; if ( err ) { this . logger . trace ( err , 'Error executing command' ) ; result . success = false ; result . error = err ; reject ( err ) ; return ; } stream . on ( 'close' , ( code , signal ) => { result . exitCode = code ; if ( code !== 0 ) { const err = new Error ( 'Program exited with non zero exit code' ) ; result . error = err ; result . success = false ; reject ( err ) ; } else if ( callNext ) { this . logger . trace ( 'Command execution successful. Invoking next' ) ; resolve ( result ) ; } else { this . logger . trace ( 'Command execution successful, but client not ready. Waiting' ) ; _finishMethod = ( ) => { resolve ( result ) ; _finishMethod = undefined ; } ; } } ) . on ( 'data' , ( data ) => { result . stdout += data ; } ) . stderr . on ( 'data' , ( data ) => { result . stderr += data ; } ) ; } ) ; } ) ; } ) . then ( ( ) => { this . logger . trace ( 'Command set executed successfully' ) ; return results ; } ) . catch ( ( err ) => { this . logger . error ( err , 'Error running command set' ) ; return results ; } ) ; }
Executes a set of commands sequentially, and returns the results of
 command execution.
Executes a set of commands sequentially and returns the results of command execution .
// Resolution complete. Do no further processing.