From ade95598f19dfd85ad13067d632fc98232c3ec50 Mon Sep 17 00:00:00 2001 From: Rached Ben Mustapha Date: Mon, 18 Nov 2019 11:30:40 -0800 Subject: [PATCH] feature: Make the count items job configurable * Do not hardcode the 'metadata' database * Authenticate to mongodb when needed * Support locationConfig.json file instead of the PENSIEVE collection --- countItems.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/countItems.js b/countItems.js index 3cabe683..8fea4d28 100644 --- a/countItems.js +++ b/countItems.js @@ -1,21 +1,55 @@ const { MongoClientInterface } = require('arsenal').storage.metadata.mongoclient; const { Logger } = require('werelogs'); +const fs = require('fs'); const log = new Logger('S3Utils::ScanItemCount'); const replicaSetHosts = process.env.MONGODB_REPLICASET; +const database = process.env.MONGODB_DATABASE || 'metadata'; -const config = { +function getIsLocationTransientCb() { + const locationConfigFile = 'conf/locationConfig.json'; + if (!fs.existsSync(locationConfigFile)) { + log.info('location conf file missing, falling back to PENSIEVE coll', + { filename: locationConfigFile }); + return null; + } + + const buf = fs.readFileSync(locationConfigFile); + const locationConfig = JSON.parse(buf.toString()); + + return function locationIsTransient(locationName, log, cb) { + if (!locationConfig[locationName]) { + log.error('unknown location', { locationName }); + process.nextTick(cb, null, false); + return; + } + const isTransient = Boolean(locationConfig[locationName].isTransient); + process.nextTick(cb, null, isTransient); + }; +} + +const params = { replicaSetHosts, + database, + isLocationTransient: getIsLocationTransientCb(), writeConcern: 'majority', replicaSet: 'rs0', readPreference: 'secondaryPreferred', - database: 'metadata', replicationGroupId: 'RG001', logger: log, }; -const mongoclient = new MongoClientInterface(config); +if (process.env.MONGODB_AUTH_USERNAME && + process.env.MONGODB_AUTH_PASSWORD) { + params.authCredentials = { + username: process.env.MONGODB_AUTH_USERNAME, + password: process.env.MONGODB_AUTH_PASSWORD, + }; +} + +const mongoclient = new MongoClientInterface(params); + mongoclient.setup(err => { if (err) { log.error('error connecting to mongodb', {