From c495c35032e4e4eb8ba2202975efc571cedb653a Mon Sep 17 00:00:00 2001 From: ssendev Date: Mon, 12 Oct 2015 22:57:00 +0200 Subject: [PATCH] use ssh-tunnel port if present --- README.md | 14 +++---- index.js | 9 ++++- tests/unit/index-nodetest.js | 72 ++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8fbe372..a6b8b61 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ The Redis host. If [url](#url) is defined, then this option is not needed. The Redis port. If [url](#url) is defined, then this option is not needed. -*Default:* `6379` +*Default:* `6379` or `context.tunnel.srcPort` if present (set by [ember-cli-deploy-ssh-tunnel][7]) ### database @@ -222,18 +222,13 @@ Add set up your `deploy.js` similar to the following: ```js 'redis': { host: "localhost", - port: 49156 }, 'ssh-tunnel': { username: "your-ssh-username", host: "remote-redis-host" - srcPort: 49156 } ``` -_(NB: by default `ssh-tunnel` assigns a random port for srcPort, but we need that - to be the same for our `redis` config, so I've just hardcoded it above)_ - ### What if my Redis server is only accessible *from* my remote server? Sometimes you need to SSH into a server (a "bastion" server) and then run @@ -245,12 +240,10 @@ your Redis host as the destination host, like so: ```js 'redis': { host: "localhost", - port: 49156 }, 'ssh-tunnel': { username: "your-ssh-username", host: "remote-redis-host" - srcPort: 49156, dstHost: "location-of-your-elasticache-node-or-remote-redis" } ``` @@ -265,6 +258,10 @@ The following properties are expected to be present on the deployment `context` - `commandLineArgs.revisionKey` (provided by [ember-cli-deploy][5]) - `deployEnvironment` (provided by [ember-cli-deploy][5]) +The following properties are used if present on the deployment `context` object: + +- `tunnel.srcPort` (provided by [ember-cli-deploy-ssh-tunnel][7]) + ## Running Tests - `npm test` @@ -275,3 +272,4 @@ The following properties are expected to be present on the deployment `context` [4]: https://github.com/ember-cli-deploy/ember-cli-deploy-build "ember-cli-deploy-build" [5]: https://github.com/ember-cli/ember-cli-deploy "ember-cli-deploy" [6]: https://github.com/ember-cli-deploy/ember-cli-deploy-revision-data "ember-cli-deploy-revision-data" +[7]: https://github.com/ember-cli-deploy/ember-cli-deploy-ssh-tunnel "ember-cli-deploy-ssh-tunnel" diff --git a/index.js b/index.js index 199e37e..b84cb6c 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,13 @@ module.exports = { name: options.name, defaultConfig: { host: 'localhost', - port: 6379, + port: function(context) { + if (context.tunnel && context.tunnel.srcPort) { + return context.tunnel.srcPort; + } else { + return 6379; + } + }, filePattern: 'index.html', distDir: function(context) { return context.distDir; @@ -42,6 +48,7 @@ module.exports = { }, redisDeployClient: function(context) { var redisOptions = this.pluginConfig; + redisOptions.port = this.readConfig('port'); var redisLib = context._redisLib; return new Redis(redisOptions, redisLib); diff --git a/tests/unit/index-nodetest.js b/tests/unit/index-nodetest.js index 9a536ca..07583ff 100644 --- a/tests/unit/index-nodetest.js +++ b/tests/unit/index-nodetest.js @@ -92,6 +92,78 @@ describe('redis plugin', function() { assert.equal(redisLib.createdClient.options.database, 4); }); + describe('resolving port from the pipeline', function() { + it('uses the config data if it already exists', function() { + var plugin = subject.createDeployPlugin({ + name: 'redis' + }); + + var config = { + host: 'somehost', + port: 1234, + }; + var context = { + ui: mockUi, + project: stubProject, + config: { + redis: config + }, + tunnel: { + srcPort: '2345' + } + }; + + plugin.beforeHook(context); + plugin.configure(context); + assert.equal(plugin.readConfig('port'), '1234'); + }); + + it('uses the context value if it exists and config doesn\'t', function() { + var plugin = subject.createDeployPlugin({ + name: 'redis' + }); + + var config = { + host: 'somehost', + }; + var context = { + ui: mockUi, + project: stubProject, + config: { + redis: config + }, + tunnel: { + srcPort: '2345' + } + }; + + plugin.beforeHook(context); + plugin.configure(context); + assert.equal(plugin.readConfig('port'), '2345'); + }); + + it('uses the default port if config and context don\'t exist', function() { + var plugin = subject.createDeployPlugin({ + name: 'redis' + }); + + var config = { + host: 'somehost', + }; + var context = { + ui: mockUi, + project: stubProject, + config: { + redis: config + } + }; + + plugin.beforeHook(context); + plugin.configure(context); + assert.equal(plugin.readConfig('port'), '6379'); + }); + }); + describe('resolving revisionKey from the pipeline', function() { it('uses the config data if it already exists', function() { var plugin = subject.createDeployPlugin({