Skip to content

Commit 8cace6c

Browse files
mjzonerehrumesh
authored andcommitted
Implement configuration options to set dynamodb local download path (#25)
* Implement configuration options to set dynamodb local download path #12 * Change downloadDirectory to downloadFrom
1 parent 4438c29 commit 8cace6c

File tree

3 files changed

+52
-42
lines changed

3 files changed

+52
-42
lines changed

dynamodb/core.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const DOWNLOAD_PATH = 'http://dynamodb-local.s3-website-us-west-2.amazonaws.com/
1111
DB_PATH = path.dirname(__filename) + '/bin';
1212

1313
let runningProcesses = {},
14-
writeFile = function (name, data) {
15-
return new BbPromise(function (resolve, reject) {
16-
fs.writeFile(name, JSON.stringify(data, null, 4), function (err) {
14+
writeFile = function(name, data) {
15+
return new BbPromise(function(resolve, reject) {
16+
fs.writeFile(name, JSON.stringify(data, null, 4), function(err) {
1717
if (err) {
1818
reject(err);
1919
} else {
@@ -28,13 +28,14 @@ let runningProcesses = {},
2828
* @param options
2929
* @returns {Promise.<ChildProcess>}
3030
*/
31-
start: function (options, spinner) {
32-
return new BbPromise(function (resolve, reject) {
31+
start: function(options, spinner) {
32+
return new BbPromise(function(resolve, reject) {
3333
/* Dynamodb local documentation http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html */
34-
let additionalArgs = [];
34+
let additionalArgs = [],
35+
downloadFrom = options.downloadFrom || DB_PATH;
3536
options.port = options.port || 8000;
3637

37-
if (options.dbPath) { //
38+
if (options.dbPath) {
3839
additionalArgs.push('-dbPath', options.dbPath);
3940
} else {
4041
additionalArgs.push('-inMemory');
@@ -54,26 +55,26 @@ let runningProcesses = {},
5455
if (options.help) {
5556
additionalArgs.push('-help');
5657
}
57-
installer.setup(DB_PATH, DOWNLOAD_PATH, JAR, spinner)
58-
.then(function () {
58+
installer.setup(downloadFrom, DOWNLOAD_PATH, JAR, spinner)
59+
.then(function() {
5960
let args = [
60-
'-Djava.library.path=./DynamoDBLocal_lib', '-jar', JAR, '-port', options.port
61-
];
61+
'-Djava.library.path=' + downloadFrom + '/DynamoDBLocal_lib', '-jar', JAR, '-port', options.port
62+
];
6263
args = args.concat(additionalArgs);
6364
let child = spawn('java', args, {
64-
cwd: DB_PATH,
65+
cwd: downloadFrom,
6566
env: process.env,
6667
stdio: ['pipe', 'pipe', process.stderr]
6768
});
6869
if (!child.pid) {
6970
throw new Error('Unable to start DynamoDB Local process!');
7071
}
7172
child
72-
.on('error', function (err) {
73+
.on('error', function(err) {
7374
console.log('DynamoDB local start error', err);
7475
throw new Error('DynamoDB Local failed to start!');
7576
})
76-
.on('close', function (code) {
77+
.on('close', function(code) {
7778
if (code !== null && code !== 0) {
7879
console.log('DynamoDB Local failed to start with code', code);
7980
}
@@ -82,20 +83,25 @@ let runningProcesses = {},
8283
console.log('Started: Dynamodb local(pid=' + child.pid + ') ', 'via java', args.join(' '));
8384
console.log('Visit: http://localhost:' + options.port + '/shell');
8485
writeFile(DB_PATH + '/options.json', options).then(resolve, reject);
86+
writeFile(DB_PATH + '/path.json', downloadFrom);
8587
});
8688
});
8789
},
88-
stop: function (port) {
90+
stop: function(port) {
8991
if (runningProcesses[port]) {
9092
runningProcesses[port].kill('SIGKILL');
9193
delete runningProcesses[port];
9294
}
9395
},
94-
restart: function (port, db) {
96+
restart: function(port, db) {
9597
this.stop(port);
9698
this.start(port, db);
9799
},
98-
remove: installer.remove.bind(null, DB_PATH)
100+
remove: function() {
101+
let downloadedFrom = JSON.parse(fs.readFileSync(DB_PATH + '/path.json', 'utf8'));
102+
console.log("removing dynamodb from ", downloadedFrom);
103+
installer.remove(downloadedFrom);
104+
}
99105
};
100106

101107
module.exports = dynamodb;

dynamodb/installer.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ let BbPromise = require('bluebird'),
66
path = require('path'),
77
http = require('http'),
88
fs = require('fs');
9-
9+
1010

1111
let download = function (source, destination, spinner) {
12-
let createDir = function (path) {
12+
let createDir = function (path) {
1313
if (!fs.existsSync(path)) {
1414
fs.mkdirSync(path);
1515
}
@@ -24,7 +24,7 @@ let download = function (source, destination, spinner) {
2424
let len = parseInt(redirectResponse.headers['content-length'], 10),
2525
cur = 0,
2626
total = len / 1048576; //1048576 - bytes in 1Megabyte
27-
console.log("Downloading dynamodb local (Size " + total.toFixed(2) + " mb). This is one-time operation and can take several minutes ...");
27+
console.log("Downloading dynamodb local (Size " + total.toFixed(2) + " mb). This is one-time operation and can take several minutes ...");
2828
if (200 != redirectResponse.statusCode) {
2929
reject(new Error('Error getting DynamoDb local latest tar.gz location ' + response.headers.location + ': ' + redirectResponse.statusCode));
3030
}
@@ -34,15 +34,15 @@ let download = function (source, destination, spinner) {
3434
path: destination
3535
}))
3636
.on('end', function () {
37-
spinner.stop(true);
37+
spinner.stop(true);
3838
console.log("Installation complete ...");
3939
resolve();
4040
})
4141
.on('error', function (err) {
4242
reject(err);
4343
}).on("data", function (chunk) {
4444
cur += chunk.length;
45-
//process.stdout.write("Downloading " + (100.0 * cur / len).toFixed(2) + "% \r");
45+
//process.stdout.write("Downloading " + (100.0 * cur / len).toFixed(2) + "% \r");
4646
});
4747
})
4848
.on('error', function (e) {
@@ -56,10 +56,10 @@ let download = function (source, destination, spinner) {
5656
};
5757

5858
let setup = function (dbPath, downloadPath, jar, spinner) {
59-
return new BbPromise(function (resolve, reject) {
59+
return new BbPromise(function (resolve, reject) {
6060
try {
6161
if (fs.existsSync(path.join(dbPath, jar))) {
62-
spinner.stop(true);
62+
spinner.stop(true);
6363
resolve(true);
6464
} else {
6565
download(downloadPath, dbPath, spinner).then(resolve, reject);

index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const _ = require('lodash'),
55
tables = require('./tables/core'),
66
dynamodb = require('./dynamodb/core');
77

8-
module.exports = function (S) { // Always pass in the ServerlessPlugin Class
9-
const SCli = require(S.getServerlessPath('utils/cli'));
8+
module.exports = function(S) { // Always pass in the ServerlessPlugin Class
9+
const SCli = require(S.getServerlessPath('utils/cli'));
1010

1111
class DynamodbLocal extends S.classes.Plugin {
1212

@@ -33,11 +33,11 @@ module.exports = function (S) { // Always pass in the ServerlessPlugin Class
3333
option: 'new',
3434
shortcut: 'n',
3535
description: 'Create a new table & seed template for the given table name, inside the directlry given in s-project.json.'
36-
}, {
36+
}, {
3737
option: 'create',
3838
shortcut: 'c',
3939
description: 'Create dynamodb tables and run seeds'
40-
}]
40+
}]
4141
});
4242
S.addAction(this.remove.bind(this), {
4343
handler: 'dynamodbRemove',
@@ -54,35 +54,39 @@ module.exports = function (S) { // Always pass in the ServerlessPlugin Class
5454
option: 'port',
5555
shortcut: 'p',
5656
description: 'The port number that DynamoDB will use to communicate with your application. If you do not specify this option, the default port is 8000'
57-
}, {
57+
}, {
5858
option: 'cors',
5959
shortcut: 'r',
6060
description: 'Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. The default setting for -cors is an asterisk (*), which allows public access.'
61-
}, {
61+
}, {
6262
option: 'inMemory',
6363
shortcut: 'm',
6464
description: 'DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both -dbPath and -inMemory at once.'
65-
}, {
65+
}, {
6666
option: 'dbPath',
6767
shortcut: 'd',
6868
description: 'The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both -dbPath and -inMemory at once. For the path, current working directory is <projectroot>/node_modules/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.'
69-
}, {
69+
}, {
7070
option: 'sharedDb',
7171
shortcut: 'h',
7272
description: 'DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration.'
73-
}, {
73+
}, {
7474
option: 'delayTransientStatuses',
7575
shortcut: 't',
7676
description: 'Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status.)'
77-
}, {
77+
}, {
7878
option: 'optimizeDbBeforeStartup',
7979
shortcut: 'o',
8080
description: 'Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.'
81-
}, {
81+
}, {
8282
option: 'create',
8383
shortcut: 'c',
8484
description: 'After starting dynamodb local, create dynamodb tables and run seeds'
85-
}]
85+
}, {
86+
option: 'downloadFrom',
87+
shortcut: 'D',
88+
description: 'Specify the path where you want to download dynamodb. Default path is serverless-dynamodb-local/dynamodb/bin'
89+
}]
8690
});
8791

8892
return BbPromise.resolve();
@@ -101,7 +105,7 @@ module.exports = function (S) { // Always pass in the ServerlessPlugin Class
101105
}
102106

103107
table(evt) {
104-
return new BbPromise(function (resolve, reject) {
108+
return new BbPromise(function(resolve, reject) {
105109
let options = evt.options,
106110
config = S.getProject().custom.dynamodb,
107111
table = config && config.table || {},
@@ -123,22 +127,22 @@ module.exports = function (S) { // Always pass in the ServerlessPlugin Class
123127

124128
start(evt) {
125129
let self = this;
126-
return new BbPromise(function (resolve, reject) {
130+
return new BbPromise(function(resolve, reject) {
127131
let config = S.getProject().custom.dynamodb,
128132
options = _.merge({
129133
sharedDb: evt.options.sharedDb || true
130134
},
131-
evt.options,
135+
evt.options, { downloadFrom: config.downloadFrom },
132136
config && config.start
133137
),
134-
_spinner = SCli.spinner();;
138+
_spinner = SCli.spinner();
135139
if (options.create) {
136-
dynamodb.start(options).then(function () {
140+
dynamodb.start(options).then(function() {
137141
console.log(""); // seperator
138142
self.table(evt).then(resolve, reject);
139143
});
140144
} else {
141-
_spinner.start()
145+
_spinner.start();
142146
dynamodb.start(options, _spinner).then(resolve, reject);
143147
}
144148
});

0 commit comments

Comments
 (0)