2019-11-23 02:47:00 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const util = require('util');
|
|
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
|
|
|
|
if (!process.env.NODE_ENV) {
|
|
|
|
console.error('Please set "NODE_ENV" variable, e.g.: "NODE_ENV=test"');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-12-16 23:32:32 +08:00
|
|
|
let configFileName = process.env.NODE_ENV;
|
|
|
|
if (process.env.CARTO_WINDSHAFT_ENV_BASED_CONF) {
|
|
|
|
// we override the file with the one with env vars
|
|
|
|
configFileName = 'config';
|
|
|
|
}
|
2019-11-23 02:47:00 +08:00
|
|
|
|
2020-12-16 23:32:32 +08:00
|
|
|
const environment = require(`../config/environments/${configFileName}.js`);
|
2019-11-23 02:47:00 +08:00
|
|
|
const REDIS_PORT = environment.redis.port;
|
2020-12-16 23:32:32 +08:00
|
|
|
const REDIS_HOST = environment.redis.host;
|
|
|
|
|
|
|
|
const PGHOST = environment.postgres.host;
|
|
|
|
const PGPORT = environment.postgres.port;
|
2019-11-23 02:47:00 +08:00
|
|
|
|
|
|
|
const TEST_USER_ID = 1;
|
|
|
|
const TEST_USER = environment.postgres_auth_user.replace('<%= user_id %>', TEST_USER_ID);
|
|
|
|
const TEST_PASSWORD = environment.postgres_auth_pass.replace('<%= user_id %>', TEST_USER_ID);
|
|
|
|
const PUBLIC_USER = environment.postgres.user;
|
|
|
|
const PUBLIC_USER_PASSWORD = environment.postgres.password;
|
|
|
|
const TEST_DB = `${TEST_USER}_db`;
|
|
|
|
|
|
|
|
async function dropDatabase () {
|
|
|
|
await exec(`dropdb --if-exists ${TEST_DB}`, {
|
2020-12-16 23:32:32 +08:00
|
|
|
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
|
2019-11-23 02:47:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createDatabase () {
|
|
|
|
await exec(`createdb -T template_postgis -EUTF8 "${TEST_DB}"`, {
|
2020-12-16 23:32:32 +08:00
|
|
|
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
|
2019-11-23 02:47:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createDatabaseExtension () {
|
|
|
|
await exec(`psql -c "CREATE EXTENSION IF NOT EXISTS cartodb CASCADE;" ${TEST_DB}`, {
|
2020-12-16 23:32:32 +08:00
|
|
|
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
|
2019-11-23 02:47:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function populateDatabase () {
|
|
|
|
const filenames = [
|
|
|
|
'analysis_catalog',
|
|
|
|
'windshaft.test',
|
|
|
|
'gadm4',
|
|
|
|
'countries_null_values',
|
|
|
|
'ported/populated_places_simple_reduced',
|
|
|
|
'cdb_analysis_check',
|
|
|
|
'cdb_invalidate_varnish'
|
2019-11-29 01:07:44 +08:00
|
|
|
].map(filename => `${__dirname}/support/sql/${filename}.sql`);
|
|
|
|
|
|
|
|
const populateDatabaseCmd = `
|
|
|
|
cat ${filenames.join(' ')} |
|
|
|
|
sed -e "s/:PUBLICUSER/${PUBLIC_USER}/g" |
|
|
|
|
sed -e "s/:PUBLICPASS/${PUBLIC_USER_PASSWORD}/g" |
|
|
|
|
sed -e "s/:TESTUSER/${TEST_USER}/g" |
|
|
|
|
sed -e "s/:TESTPASS/${TEST_PASSWORD}/g" |
|
|
|
|
PGOPTIONS='--client-min-messages=WARNING' psql -q -v ON_ERROR_STOP=1 ${TEST_DB}
|
|
|
|
`;
|
|
|
|
|
|
|
|
await exec(populateDatabaseCmd, {
|
2020-12-16 23:32:32 +08:00
|
|
|
env: Object.assign({ PGUSER: 'postgres', PGHOST: PGHOST, PGPORT: PGPORT }, process.env)
|
2019-11-29 01:07:44 +08:00
|
|
|
});
|
2019-11-23 02:47:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function populateRedis () {
|
2019-11-29 00:30:06 +08:00
|
|
|
const commands = `
|
2020-04-29 20:32:08 +08:00
|
|
|
FLUSHALL
|
|
|
|
|
2019-11-29 00:30:06 +08:00
|
|
|
HMSET rails:users:localhost \
|
|
|
|
id ${TEST_USER_ID} \
|
|
|
|
database_name "${TEST_DB}" \
|
2020-12-16 23:32:32 +08:00
|
|
|
database_host "${PGHOST}" \
|
2019-11-29 00:30:06 +08:00
|
|
|
map_key 1234
|
|
|
|
|
|
|
|
HMSET rails:users:cartodb250user \
|
|
|
|
id ${TEST_USER_ID} \
|
|
|
|
database_name "${TEST_DB}" \
|
2020-12-16 23:32:32 +08:00
|
|
|
database_host "${PGHOST}" \
|
2019-11-29 00:30:06 +08:00
|
|
|
database_password "${TEST_PASSWORD}" \
|
|
|
|
map_key 4321
|
|
|
|
|
|
|
|
HMSET api_keys:localhost:1234 \
|
|
|
|
user "localhost" \
|
|
|
|
type "master" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "${TEST_USER}" \
|
|
|
|
database_password "${TEST_PASSWORD}"
|
|
|
|
|
|
|
|
HMSET api_keys:localhost:default_public \
|
|
|
|
user "localhost" \
|
|
|
|
type "default" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
|
|
|
|
HMSET api_keys:localhost:regular1 \
|
|
|
|
user "localhost" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_regular1" \
|
|
|
|
database_password "regular1"
|
|
|
|
|
|
|
|
HMSET api_keys:localhost:regular2 \
|
|
|
|
user "localhost" \
|
|
|
|
type "regular" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "false" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
|
|
|
|
HMSET api_keys:cartodb250user:4321 \
|
|
|
|
user "localhost" \
|
|
|
|
type "master" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "${TEST_USER}" \
|
|
|
|
database_password "${TEST_PASSWORD}"
|
|
|
|
|
|
|
|
HMSET api_keys:cartodb250user:default_public \
|
|
|
|
user "localhost" \
|
|
|
|
type "default" \
|
|
|
|
grants_sql "true" \
|
|
|
|
grants_maps "true" \
|
|
|
|
database_role "test_windshaft_publicuser" \
|
|
|
|
database_password "public"
|
|
|
|
`;
|
|
|
|
|
2020-12-16 23:32:32 +08:00
|
|
|
await exec(`echo "${commands}" | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -n 5`);
|
2019-11-23 02:47:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main (args) {
|
|
|
|
let code = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (args[0]) {
|
|
|
|
case 'setup':
|
|
|
|
await populateRedis();
|
|
|
|
await dropDatabase();
|
|
|
|
await createDatabase();
|
|
|
|
await createDatabaseExtension();
|
|
|
|
await populateDatabase();
|
|
|
|
break;
|
|
|
|
case 'teardown':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Missing "mode" argument. Valid ones: "setup" or "teardown"');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
code = 1;
|
|
|
|
} finally {
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main(process.argv.slice(2));
|