Skip to content

Commit

Permalink
fix custom db name handling (#386)
Browse files Browse the repository at this point in the history
Signed-off-by: David Huffman <[email protected]>
  • Loading branch information
dshuffma-ibm authored Feb 23, 2023
1 parent fa14227 commit d5d6985
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion common/deploy_scripts/test_run_athena.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# export these values so the subshell running athena will be able to see them
export HOST_URL="${HOST_URL:=http://localhost:3000}"
export DB_SYSTEM="${DB_SYSTEM:=athena-systems}"
export DB_SYSTEM="${DB_SYSTEM:=athena-system}"
export DB_CONNECTION_STRING="${DB_CONNECTION_STRING:=http://localhost:5984}"

echo "HOST_URL=$HOST_URL"
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
environment:
- CONFIGURE_FILE=./env/config.yaml
- DB_CONNECTION_STRING=http://admin:password@console-couchdb:5984
- DB_SYSTEM=athena-systems
- DB_SYSTEM=athena-system
networks:
- test

Expand Down
4 changes: 2 additions & 2 deletions packages/athena/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Create a file called `dev.json` in the `env` directory with the following conten
```json
{
"DB_CONNECTION_STRING": "http://localhost:5984",
"DB_SYSTEM": "athena-systems",
"DB_SYSTEM": "athena-system",
"CONFIGURE_FILE": "./env/my_config.yaml"
}
```
Expand Down Expand Up @@ -426,7 +426,7 @@ are included in the Athena repository as git submodules.
> cat env/dev.json
{
"DB_CONNECTION_STRING": "http://localhost:5984",
"DB_SYSTEM": "athena-systems",
"DB_SYSTEM": "athena-system",
"CONFIGURE_FILE": "./env/my_config.yaml"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/athena/docs/db_backup_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Get the contents of a backup doc including attachments.
"last_sequence": "...",
"docs": []
},
"athena-systems": {
"athena-system": {
"start_timestamp": 1592922858740,
"finish_timestamp": 1592922861261,
"last_sequence": "...",
Expand Down
2 changes: 1 addition & 1 deletion packages/athena/docs/other_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ See public Optools configuration settings.
"CLUSTER_DATA": {
"type": "paid"
},
"DB_SYSTEM": "athena-systems", // name of settings/tx database
"DB_SYSTEM": "athena-system", // name of settings/tx database
"DEPLOYER_TIMEOUT": 90000, // deployer http timeout in ms
"DEPLOYER_URL": "something",
"DOMAIN": null, // domain athena is using for cookies, should match url in browser
Expand Down
6 changes: 3 additions & 3 deletions packages/athena/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __Example:__
"DB_CONNECTION_STRING": "https://username:password@localhost:5984", // required

// The name of the database that contains the settings doc
"DB_SYSTEM": "athena-systems", // required unless "db_custom_names" is used in config file
"DB_SYSTEM": "athena-system", // required unless "db_custom_names" is used in config file


// ****** Optional Fields ******
Expand Down Expand Up @@ -328,7 +328,7 @@ __default_settings_doc.json:__
"db_custom_names": {
"DB_COMPONENTS": "athena-components",
"DB_SESSIONS": "athena-sessions",
"DB_SYSTEM": "athena-systems"
"DB_SYSTEM": "athena-system"
},
// default database configuration - do not modify
Expand Down Expand Up @@ -359,7 +359,7 @@ __default_settings_doc.json:__
"../json_docs/default_settings_doc.json",
"../json_docs/system_design_doc.json"
],
"name": "athena-systems"
"name": "athena-system"
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/athena/env/dev.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"DB_CONNECTION_STRING": "http://admin:admin@localhost:5984",
"DB_SYSTEM": "athena-systems",
"DB_SYSTEM": "athena-system",
"CONFIGURE_FILE": "./env/my_config.yaml"
}
2 changes: 1 addition & 1 deletion packages/athena/json_docs/default_settings_doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"db_custom_names": {
"DB_COMPONENTS": "athena-components",
"DB_SESSIONS": "athena-sessions",
"DB_SYSTEM": "athena-systems"
"DB_SYSTEM": "athena-system"
},
"db_defaults": {
"DB_COMPONENTS": {
Expand Down
5 changes: 2 additions & 3 deletions packages/athena/libs/migration_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,8 @@ module.exports = function (logger, ev, t) {
}
}

// dsh todo remove me
logger.silly('settings doc:', JSON.stringify(doc, null, 2));

logger.silly('built settings "db_custom_names":', JSON.stringify(doc.db_custom_names, null, 2));
logger.silly('built settings "db_defaults":', JSON.stringify(doc.db_defaults, null, 2));
break;
}
}
Expand Down
28 changes: 21 additions & 7 deletions packages/athena/libs/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = function (logger, t, noInterval, noAutoRun) {
settings.HOST_URL = process.env.HOST_URL || athena.host_url; // the external url to reach this application
settings.REGION = process.env.REGION || athena.region;
settings.db_defaults = athena.db_defaults;
load_database_names(athena.db_defaults); // load all db names here
load_database_names(athena); // load all db names here
settings.ENFORCE_BACKEND_SSL = athena.enforce_backend_ssl; // should we check certs or not, server side
settings.ACCESS_LIST = lowercase_key_values(athena.access_list); // athena access list
settings.INITIAL_ADMIN = athena.initial_admin;
Expand Down Expand Up @@ -561,12 +561,26 @@ module.exports = function (logger, t, noInterval, noAutoRun) {
}

// load each database name
function load_database_names(dbs) {
// this sets setting fields: DB_COMPONENTS, & DB_SESSIONS
function load_database_names(settings_doc) {
const dbs = settings_doc.db_defaults;
const db_custom_names = settings_doc.db_custom_names;
if (dbs) {
for (let db_const in dbs) {
if (dbs[db_const].name) { // set it if if a db name is set
settings[db_const] = dbs[db_const].name;
db_names.push(dbs[db_const].name);
for (let db_name in dbs) {
if (db_name !== 'DB_SYSTEM') { // the system db should be left driven by process.env.DB_SYSTEM
if (dbs[db_name].name) {
let name2use = dbs[db_name].name;
if (db_custom_names && db_custom_names[db_name] && typeof db_custom_names[db_name] === 'string') {
name2use = db_custom_names[db_name];
}

if (settings[db_name] !== name2use) { // if its changed, log it
logger.debug('[settings] custom db name:', db_name, '->', db_custom_names[db_name]);
}

settings[db_name] = name2use;
db_names.push(name2use);
}
}
}
}
Expand Down Expand Up @@ -710,7 +724,7 @@ module.exports = function (logger, t, noInterval, noAutoRun) {
logger.error('---------------------------------------------------------\n');
return cb({ error: 'missing vars', details: errors });
} else {
logger.info('[settings] final env variables look good!');
//logger.info('[settings] final env variables look good!');
return cb(null);
}
}
Expand Down
40 changes: 17 additions & 23 deletions packages/athena/scripts/create_databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,34 @@ module.exports = (logger, t) => {
const couch_lib = t.couch_lib;

setup.createDatabasesAndDesignDocs = (cb) => {
const config_map = require('../json_docs/default_settings_doc.json').db_defaults;
const config_map = require('../json_docs/default_settings_doc.json').db_defaults; // init with the defaults
const db_custom_names = require('../json_docs/default_settings_doc.json').db_custom_names;

const errs = [];

// check if config file db names are replacing names in the default file
for (let db in config_map) { // copy config file setting -> default doc
let name2use = '';

// first load custom names from settings file
// first look for custom names in default settings file
if (db_custom_names && db_custom_names[db] && typeof db_custom_names[db] === 'string') {
if (db === 'DB_SYSTEM') { // if its not already set, stuff it into env too
if (!process.env['DB_SYSTEM']) { // (env setting should override config)
logger.debug('[default file] custom db name:', db, '=', db_custom_names[db]);
process.env['DB_SYSTEM'] = db_custom_names[db];
}
config_map['DB_SYSTEM'].name = process.env['DB_SYSTEM'];
} else {
logger.debug('[default file] custom db name:', db, '=', db_custom_names[db]);
config_map[db].name = db_custom_names[db];
}
name2use = db_custom_names[db];
}

// then config file
// then look for custom names in the config file
if (t.config_file && t.config_file.db_custom_names && t.config_file.db_custom_names[db] && typeof t.config_file.db_custom_names[db] === 'string') {
if (db === 'DB_SYSTEM') { // if its not already set, stuff it into env too
if (!process.env['DB_SYSTEM']) { // (env setting should override config)
logger.debug('[config file] custom db name:', db, '=', t.config_file.db_custom_names[db]);
process.env['DB_SYSTEM'] = t.config_file.db_custom_names[db];
}
config_map['DB_SYSTEM'].name = process.env['DB_SYSTEM'];
} else {
logger.debug('[config file] custom db name:', db, '=', t.config_file.db_custom_names[db]);
config_map[db].name = t.config_file.db_custom_names[db];
}
name2use = t.config_file.db_custom_names[db];
}

// then env
if (process.env[db]) {
name2use = process.env[db]; // an env setting should override config
}

if (name2use) {
logger.debug('[db startup] custom db name:', db, '=', name2use);
config_map[db].name = name2use;
process.env[db] = name2use; // stuff it into env too
}
}

Expand Down

0 comments on commit d5d6985

Please sign in to comment.