Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How execute multiple commands on SSH2 using NodeJS

I'm trying to deploy from GitHub using I want to execute more than one command, in order of the array. The code I'm using now is included below.

async.series([
...
// Deploy from GitHub
function (callback) {
    // Console shizzle:
    console.log('');
    console.log('Deploying...'.red.bold);
    console.log();
    console.log();

    var deployFunctions = [
        {
            command: 'cd ' + envOptions.folder + ' && pwd',
            log: false
        },
        {
            command: 'pwd'
        },
        {
            command: 'su ' + envOptions.user,
            log: false
        },
        {
            command: 'git pull'
        },
        {
            command: 'chmod 0777 * -R',
            log: false
        }
    ];
    async.eachSeries(deployFunctions, function (item, callback) {
        deployment.ssh2.exec(item.command, function (err, stream) {
            deployment.logExec(item);
            stream.on('data', function (data, extended) {
                console.log(data.toString().trim());
                console.log();
            });
            function done() {
                callback(err);
            }

            stream.on('exit', done);
            stream.on('end', done);
        });
    }, function () {
        callback();
    });
},
...);

But, after I cd'ed to the right directory, it forgets where it was and starts all over again.

$ cd /some/folder && pwd
/some/folder

$ pwd
/root
like image 526
jeroenvisser101 Avatar asked Jan 03 '14 15:01

jeroenvisser101


2 Answers

@robertklep is correct about why your cd doesn't persist. Each command invokes a distinct shell instance which starts in its initial state. You could prefix each command with cd /home/jansenstok/domains/alcoholtesterwinkel.com/public_html/ && as a quick fix, but really you are setting yourself up for pain. What you want is a shell script with all the power of multiple lines as opposed to a list of individual disconnected commands.

Look at using ssh2's sftp function to transfer a complete shell script to the remote machine as step 1, execute it via exec (/bin/bash /tmp/your_deploy_script.sh) as step 2, and then delete the script as step 3.

like image 87
Peter Lyons Avatar answered Sep 28 '22 06:09

Peter Lyons


I know this is a super old question, but I ran into this problem while trying to manage an ACE through my Node server. The answer didn't work for me, but several searches later led me to a wrapper that worked really well for me. Just wanted to share here because this was the top link in my Google search. It's called ssh2shell and can be found here: https://www.npmjs.com/package/ssh2shell

It's very simple to use, just pass an array of commands and they run one by one waiting for each command to complete before moving on to the next.

like image 31
codysmith001 Avatar answered Sep 28 '22 07:09

codysmith001