Skip to content

Commit

Permalink
Fix connection limit
Browse files Browse the repository at this point in the history
  • Loading branch information
markwylde committed May 13, 2023
1 parent 495eac7 commit 61da1c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
33 changes: 20 additions & 13 deletions lib/utils/createSshConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ function execRemote(conn, command, onData, config, silent) {
return;
}

// if (!silent) {
log(kleur.white('out:'), data.toString());
// }
log(kleur.white('out:'), data.toString());
output += data || '';
})

stream.on('exit', (code, signal) => {
stream.end(); // Close the stream on exit
if (code !== 0) {
reject({ code, signal, output });
return;
}

resolve(output);
});

stream.stderr.on('data', function(data) {
log(kleur.white('err:'), data.toString());
output += data || '';
});
}
});
});
Expand Down Expand Up @@ -92,18 +96,21 @@ export async function createSshConnection({ host, username, port = 22, privateKe

await connectionPromise;

const script = (scriptContent, onData, sudo) => {
const tempScriptFilename = `/tmp/script_${Date.now()}.sh`;
const scriptRunner = `
echo '${scriptContent.replace(/'/g, "'\\''")}' | sudo tee ${tempScriptFilename} > /dev/null &&
sudo chmod +x ${tempScriptFilename} &&
${sudo ? 'sudo ' : ''}${tempScriptFilename} &&
sudo rm ${tempScriptFilename}
`;
return execRemote(conn, scriptRunner, onData, config, true);
};

return {
sftp: conn.sftp.bind(conn),
script: (scriptContent, onData) => {
const tempScriptFilename = `/tmp/script_${Date.now()}.sh`;
const scriptRunner = `
echo '${scriptContent.replace(/'/g, "'\\''")}' | sudo tee ${tempScriptFilename} > /dev/null &&
sudo chmod +x ${tempScriptFilename} &&
${tempScriptFilename} &&
sudo rm ${tempScriptFilename}
`;
return execRemote(conn, scriptRunner, onData, config, true);
},
script: (scriptContent, onData) => script(scriptContent, onData, false),
sudoScript: (scriptContent, onData) => script(scriptContent, onData, true),
exec: (command, onData) => execRemote(conn, command, onData, config),
close: () => {
return new Promise(resolve => {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/sshCopyFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function sshCopyFile(connection, source, destination, options) {

writeStream.on('close', () => {
resolve(`Uploaded: ${sourceFile} -> ${destinationFile}`);
sftp.end();
});

readStream.pipe(writeStream);
Expand Down

0 comments on commit 61da1c0

Please sign in to comment.