Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ Type: `String` or `Array<String>`

Servers on which the project will be deployed. Pattern must be `[email protected]` if user is not specified (`myserver.com`) the default user will be "deploy".

#### key

Type: `String`

Path to SSH key

### Usage example

```js
Expand All @@ -102,7 +108,8 @@ shipit: {
deployTo: '/tmp/deploy_to',
repositoryUrl: 'https://github.com/user/repo.git',
ignores: ['.git', 'node_modules'],
keepReleases: 2
keepReleases: 2,
key: '/path/to/key'
},
staging: {
servers: '[email protected]'
Expand Down
3 changes: 2 additions & 1 deletion lib/shipit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var isArray = require('lodash').isArray;
var defaults = require('lodash').defaults;
var extend = require('lodash').extend;
var defaults = require('lodash').defaults;
var pick = require('lodash').pick;
var childProcess = require('child_process');
var ConnectionPool = require('./ssh/connection-pool');
var LineWrapper = require('stream-line-wrapper');
Expand Down Expand Up @@ -63,7 +64,7 @@ Shipit.prototype.initialize = function () {

Shipit.prototype.initSshPool = function () {
var servers = isArray(this.config.servers) ? this.config.servers : [this.config.servers];
this.sshPool = new ConnectionPool(servers, this.options);
this.sshPool = new ConnectionPool(servers, extend({}, this.options, pick(this.config, 'key')));
return this;
};

Expand Down
14 changes: 12 additions & 2 deletions lib/ssh/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Connection.prototype.run = function (command, options, cb) {

// In sudo mode, we use a TTY channel.
var args = /^sudo/.exec(command) ? ['-tt'] : [];
args.push('-p ' + (this.options.port || 22));
args.push.apply(args, buildSSHArgs(this.options));
args.push(remote.format(this.remote));

// Escape double quotes in command.
Expand Down Expand Up @@ -82,6 +82,7 @@ Connection.prototype.run = function (command, options, cb) {
*/

Connection.prototype.copy = function (src, dest, options, cb) {

// function (src, dest, cb)
if (isFunction(options)) {
cb = options;
Expand All @@ -102,7 +103,7 @@ Connection.prototype.copy = function (src, dest, options, cb) {
var args = ['rsync'].concat(excludes).concat([
'-az',
'-e',
'"ssh -p ' + (this.options.port || 22) + '"',
'"ssh ' + (buildSSHArgs(this.options)).join(' ') + '"',
src,
dest
]);
Expand Down Expand Up @@ -133,3 +134,12 @@ function formatExcludes(excludes) {
return prev.concat(['--exclude', current]);
}, []);
}

function buildSSHArgs(options) {
var args = [];
args.push('-p ' + (options.port || 22));
if (options.key) {
args.push('-i ' + options.key);
}
return args;
}
23 changes: 23 additions & 0 deletions test/unit/ssh/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ describe('SSH Connection', function () {
'ssh -p 22 user@host "my-command2 -x"'
);
});

it('should use key if present', function () {
connection = new Connection({
remote: 'user@host',
logger: logger,
key: '/path/to/key'
});
connection.run('my-command -x', function () {});
expect(childProcess.exec).to.be.calledWith(
'ssh -p 22 -i /path/to/key user@host "my-command -x"'
);
});
});

describe('#copy', function () {
Expand All @@ -125,5 +137,16 @@ describe('SSH Connection', function () {
expect(childProcess.exec).to.be.calledWith('rsync --exclude a --exclude b -az -e ' +
'"ssh -p 22" /src/dir user@host:/dest/dir');
});

it('should use key if present', function (done) {
connection = new Connection({
remote: 'user@host',
logger: logger,
key: '/path/to/key'
});
connection.copy('/src/dir', '/dest/dir', done);
expect(childProcess.exec).to.be.calledWith('rsync -az -e "ssh -p 22 -i /path/to/key" /src/dir user@host:/dest/dir');
});

});
});