diff --git a/README.md b/README.md index da304f0..7592b77 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,12 @@ Type: `String` or `Array` Servers on which the project will be deployed. Pattern must be `user@myserver.com` if user is not specified (`myserver.com`) the default user will be "deploy". +#### key + +Type: `String` + +Path to SSH key + ### Usage example ```js @@ -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: 'user@myserver.com' diff --git a/lib/shipit.js b/lib/shipit.js index 75d9f31..1f27b3f 100644 --- a/lib/shipit.js +++ b/lib/shipit.js @@ -63,7 +63,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; }; diff --git a/lib/ssh/connection.js b/lib/ssh/connection.js index 92d8550..0dc20b7 100644 --- a/lib/ssh/connection.js +++ b/lib/ssh/connection.js @@ -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. @@ -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; @@ -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 ]); @@ -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; +} diff --git a/package.json b/package.json index 2e73d72..822e3f5 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "repository": { "type": "git", - "url": "git://github.com/neoziro/grunt-shipit.git" + "url": "https://github.com/sharathprabhal/grunt-shipit" }, "keywords": [ "gruntplugin", @@ -17,7 +17,7 @@ "author": "Greg Bergé ", "license": "MIT", "bugs": { - "url": "https://github.com/neoziro/grunt-shipit/issues" + "url": "https://github.com/sharathprabhal/grunt-shipit/issues" }, "dependencies": { "async": "^0.9.0", diff --git a/test/unit/ssh/connection.js b/test/unit/ssh/connection.js index b70fd9c..79d9f46 100644 --- a/test/unit/ssh/connection.js +++ b/test/unit/ssh/connection.js @@ -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 () { @@ -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'); + }); + }); }); \ No newline at end of file