YUKI Hiroshi
null+****@clear*****
Fri Nov 9 11:47:21 JST 2012
YUKI Hiroshi 2012-11-09 11:47:21 +0900 (Fri, 09 Nov 2012) New Revision: 12870a7b3f9af7ac9fa378ed676864543bfa6a4d https://github.com/groonga/gcs/commit/12870a7b3f9af7ac9fa378ed676864543bfa6a4d Log: Add gcs-run-scenarios command based on tools/run-scenarios.js Added files: bin/gcs-run-scenarios Modified files: lib/client.js lib/command-line.js package.json Added: bin/gcs-run-scenarios (+82 -0) 100755 =================================================================== --- /dev/null +++ bin/gcs-run-scenarios 2012-11-09 11:47:21 +0900 (0619cb4) @@ -0,0 +1,82 @@ +#!/usr/bin/env node + +/* + Set these environment variables: + * AWS_ACCESS_KEY_ID + * AWS_SECRET_ACCESS_KEY +*/ + +var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface; +var Client = require(__dirname + '/../lib/client').Client; +var fs = require('fs'); +var path = require('path'); + +var commandLine = new CLI(); +commandLine + .usage('--scenarios <path to a directory> [options]') + .option('--scenarios <path to a directory>', + 'Path to the scenarios directory which includes "scenarios.json".', + String) + .option('--document-endpoint <host:port>', + 'The host and port number of the documents/batch API.', + String) + .option('--acs', + 'Process requests for Amazon CloudSearch.', + String) + .parseClient(); + +var client = new Client(commandLine); +if (commandLine.options.documentEndpoint) + client.docEndpoint = commandLine.options.documentEndpoint; + +var scenariosDir = path.resolve(process.cwd(), commandLine.options.scenarios); +var scenariosFile = path.resolve(scenariosDir, 'scenarios.json'); +if (!commandLine.options.scenarios || + !path.existsSync(scenariosFile)) + client.raiseFatalError('You must specify the path to a scenarios directory ' + + 'by "--scenarios" option.'); +} + +var scenarios = JSON.parse(fs.readFileSync(scenariosFile)); + +cs = new CloudSearch({ + accessKeyId: process.env.AWS_ACCESS_KEY_ID, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, +}); + +CloudSearch.prototype.extractBody = function(options) { + return 'blob'; +}; + +var statusCodeTable = { + 404: 'Not Found', + 400: 'Bad Request', + 200: 'OK' +}; + +var resultsDirName = commandLine.options.acs ? 'results-acs' : 'results'; +var resultsDir.resolve(resultsDirName, resultsDirectoryName); +scenarios.forEach(function(scenario) { + console.log('Processing ' + scenario.name); + var filename = scenario.name.replace(/[^a-zA-Z0-9]+/g, '-') + '.txt'; + cs[scenario.params.Action].call(cs, scenario.params, function(error, data) { + var response = error || data; + + var statusCode = response.StatusCode; + if (!statusCodeTable[statusCode]) { + throw "Unknown status code " + statusCode; + } + + var output = ''; + output += 'HTTP/1.1 ' + statusCode + ' ' + statusCodeTable[statusCode] + '\r\n'; + for (var key in response.Headers) { + output += key + ': ' + response.Headers[key] + '\r\n'; + }; + output += '\r\n'; + output += response.Body.toString(); + + var resultPath = resultsDir.resolve(resultsDir, filename); + fs.writeFile(resultPath, output); + console.log('Wrote ' + resultPath); + }); +}); Modified: lib/client.js (+7 -4) =================================================================== --- lib/client.js 2012-11-08 19:13:17 +0900 (4d6cfab) +++ lib/client.js 2012-11-09 11:47:21 +0900 (7b238cd) @@ -8,13 +8,16 @@ function Client(options) { this.host = options.host; this.port = options.port; this.docEndpoint = options.docEndpoint; + + this.accessKeyId = options.accessKeyId; + this.secretAccessKey = options.secretAccessKey; } Client.prototype = { get configurationAPI() { if (!this._configurationAPI) { this._configurationAPI = new CloudSearch({ - accessKeyId: 'dummy-access-key-id', - secretAccessKey: 'dummy-access-key' + accessKeyId: this.accessKeyId, + secretAccessKey: this.secretAccessKey }); var self = this; this._configurationAPI.host = function() { @@ -64,8 +67,8 @@ Client.prototype = { }, createDocumentService: function(options) { var service = new DocumentService({ - accessKeyId: 'dummy-access-key-id', - secretAccessKey: 'dummy-access-key', + accessKeyId: this.accessKeyId, + secretAccessKey: this.secretAccessKey, domainName: options.domainName, domainId: options.domainId, region: amazon.US_EAST_1 Modified: lib/command-line.js (+28 -0) =================================================================== --- lib/command-line.js 2012-11-08 19:13:17 +0900 (b342b42) +++ lib/command-line.js 2012-11-09 11:47:21 +0900 (8a94c09) @@ -52,6 +52,24 @@ CommandLineInterface.prototype = { return this.host + ':' + this.port; }, + get accessKeyId() { + return this.options.accessKeyId || + process.env.AWS_ACCESS_KEY_ID || + 'dummy-access-key-id'; + }, + set accessKeyId(value) { + return this.options.accessKeyId = value; + }, + + get secretAccessKey() { + return this.options.secretAccessKey || + process.env.AWS_SECRET_ACCESS_KEY || + 'dummy-access-key'; + }, + set secretAccessKey(value) { + return this.options.secretAccessKey = value; + }, + get databasePath() { return this.options.databasePath || defaultDatabasePath; }, @@ -74,6 +92,16 @@ CommandLineInterface.prototype = { if (asClient) { this.program + .option('-a, --access-key <access key id>', + 'Your AWS access key. Used in conjunction with --secret-key. ' + + 'Must be specified if you do not use an AWS credential file. ' + + '[' + process.env.AWS_ACCESS_KEY_ID + ']', + String) + .option('-k, --secret-key <secret key>', + 'Your AWS secret key. Used in conjunction with --access-key. ' + + 'Must be specified if you do not use an AWS credential file. ' + + '[' + process.env.AWS_SECRET_ACCESS_KEY + ']', + String) .option('-e, --endpoint <hostname:port>', 'The host name (and port) to access the configuration API ' + '[' + defaultEndpoint + ']', Modified: package.json (+2 -1) =================================================================== --- package.json 2012-11-08 19:13:17 +0900 (9632aee) +++ package.json 2012-11-09 11:47:21 +0900 (5561102) @@ -45,7 +45,8 @@ "gcs-index-documents": "./bin/gcs-index-documents", "gcs-import-examples": "./bin/gcs-import-examples", "gcs-configure-default-search-field": "./bin/gcs-configure-default-search-field", - "gcs-post-sdf": "./bin/gcs-post-sdf" + "gcs-post-sdf": "./bin/gcs-post-sdf", + "gcs-run-scenarios": "./bin/gcs-run-scenarios" }, "main": "./lib/server", "engines": { -------------- next part -------------- HTML����������������������������...ダウンロード