From c3b212b45a6cb2fc372d2824829f85f7e4816ca9 Mon Sep 17 00:00:00 2001 From: Nicolas Humbert Date: Wed, 13 Nov 2024 11:19:20 +0100 Subject: [PATCH] BB-616 use the backbeatClient with the same settings as in production --- bin/backbeat-cli.js | 41 +++++++++++++++++++++++++++++++++++------ docs/backbeat-cli.md | 6 +----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bin/backbeat-cli.js b/bin/backbeat-cli.js index e51b11790..dffd5a4e8 100755 --- a/bin/backbeat-cli.js +++ b/bin/backbeat-cli.js @@ -1,7 +1,10 @@ #!/usr/bin/env node const program = require('commander'); -const BackbeatClient = require('../lib/clients/BackbeatClient.js'); +const url = require('url'); +const https = require('https'); +const http = require('http'); +const { createBackbeatClient } = require('../lib/clients/utils.js'); const pkg = require('../package.json'); const werelogs = require('werelogs'); @@ -15,12 +18,38 @@ werelogs.configure({ dump: config.log.dumpLevel, }); +function parseEndpoint(endpoint) { + try { + const parsedUrl = new url.URL(endpoint); + const transport = parsedUrl.protocol.slice(0, -1); + const host = parsedUrl.hostname; + const port = parsedUrl.port; + + return { transport, host, port }; + } catch (error) { + throw new Error(`Invalid endpoint URL: ${endpoint}`); + } +} + function createClient() { - return new BackbeatClient({ - endpoint: process.env.BACKBEAT_ENDPOINT || 'http://127.0.0.1:8000', - region: process.env.AWS_REGION || 'us-east-1', - accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'accessKey1', - secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'verySecretKey1', + const { transport, host, port } = parseEndpoint(process.env.BACKBEAT_ENDPOINT || 'http://127.0.0.1:8000'); + + let agent; + if (transport === 'https') { + agent = new https.Agent({ keepAlive: true }); + } else { + agent = new http.Agent({ keepAlive: true }); + } + + return createBackbeatClient({ + transport, + host, + port, + credentials: { + accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'accessKey1', + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'verySecretKey1', + }, + agent, }); } diff --git a/docs/backbeat-cli.md b/docs/backbeat-cli.md index eade1e1ce..041ce2c77 100644 --- a/docs/backbeat-cli.md +++ b/docs/backbeat-cli.md @@ -53,7 +53,7 @@ npm install 4. **Create an alias for easy access** ```bash -alias backbeat-cli='node bin/backbeat.cli.js' +alias backbeat-cli='node bin/backbeat-cli.js' ``` ## Configuration @@ -65,9 +65,6 @@ Before using the Backbeat CLI, configure the necessary environment variables to - `BACKBEAT_ENDPOINT`: The endpoint URL for the Backbeat service. *Default*: `http://127.0.0.1:8000` -- `AWS_REGION`: The AWS region to use. - *Default*: `us-east-1` - - `AWS_ACCESS_KEY_ID`: Your AWS access key ID. *Default*: `accessKey1` @@ -78,7 +75,6 @@ You can set these variables in your shell or include them in a configuration fil ```bash export BACKBEAT_ENDPOINT='http://your-backbeat-endpoint.com' -export AWS_REGION='your-region' export AWS_ACCESS_KEY_ID='your-access-key-id' export AWS_SECRET_ACCESS_KEY='your-secret-access-key' ```