Skip to content

Commit

Permalink
chore(core-database-postgres): use token in database name (#1992)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Jan 18, 2019
1 parent aa0718e commit ea4f775
Show file tree
Hide file tree
Showing 28 changed files with 102 additions and 43 deletions.
18 changes: 18 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ version: 2
jobs:
test-node10-0:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:10-browsers'
- image: 'postgres:alpine'
Expand Down Expand Up @@ -95,6 +98,9 @@ jobs:
command: ./node_modules/.bin/codecov
test-node11-0:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:11-browsers'
- image: 'postgres:alpine'
Expand Down Expand Up @@ -188,6 +194,9 @@ jobs:
command: ./node_modules/.bin/codecov
test-node10-1:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:10-browsers'
- image: 'postgres:alpine'
Expand Down Expand Up @@ -287,6 +296,9 @@ jobs:
command: ./node_modules/.bin/codecov
test-node10-2:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:10-browsers'
- image: 'postgres:alpine'
Expand Down Expand Up @@ -383,6 +395,9 @@ jobs:
command: ./node_modules/.bin/codecov
test-node11-1:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:11-browsers'
- image: 'postgres:alpine'
Expand Down Expand Up @@ -482,6 +497,9 @@ jobs:
command: ./node_modules/.bin/codecov
test-node11-2:
working_directory: ~/core
environment:
CORE_DB_DATABASE: core_development
CORE_DB_USERNAME: core
docker:
- image: 'circleci/node:11-browsers'
- image: 'postgres:alpine'
Expand Down
8 changes: 8 additions & 0 deletions .circleci/configTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"jobs": {
"test-node10-0": {
"working_directory": "~/core",
"environment": {
"CORE_DB_DATABASE": "core_development",
"CORE_DB_USERNAME": "core"
},
"docker": [
{
"image": "circleci/node:10-browsers"
Expand Down Expand Up @@ -82,6 +86,10 @@
},
"test-node11-0": {
"working_directory": "~/core",
"environment": {
"CORE_DB_DATABASE": "core_development",
"CORE_DB_USERNAME": "core"
},
"docker": [
{
"image": "circleci/node:11-browsers"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"snyk": "./node_modules/.bin/snyk protect",
"version": "cross-env-shell ./scripts/version.sh",
"release": "cross-env-shell ./scripts/release.sh",
"updates": "yarn lerna run updates"
"updates": "yarn lerna run updates",
"generateDocker": "node ./scripts/docker/generate-docker.js"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand Down
6 changes: 4 additions & 2 deletions packages/core-container/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ export class Container implements container.IContainer {
this.shuttingDown = true;

const logger = this.resolvePlugin<Logger.ILogger>("logger");
logger.suppressConsoleOutput(this.silentShutdown);
logger.info("Ark Core is trying to gracefully shut down to avoid data corruption :pizza:");
if (logger) {
logger.suppressConsoleOutput(this.silentShutdown);
logger.info("Core is trying to gracefully shut down to avoid data corruption :pizza:");
}

try {
/* TODO: core-database-postgres has a dep on core-container. Yet we have code in core-container fetching a reference to core-database-postgres.
Expand Down
2 changes: 2 additions & 0 deletions packages/core-container/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class Environment {
* @return {void}
*/
private exportVariables() {
process.env.CORE_TOKEN = this.variables.token;

// Don't pollute the test environment!
if (process.env.NODE_ENV === "test") {
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/core-database-postgres/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const defaults = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
};
4 changes: 2 additions & 2 deletions packages/core-test-utils/src/config/testnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || "core_development",
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_development`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core-test-utils/src/config/unitnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || "core_development",
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_development`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/devnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/mainnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/testnet.1/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}1`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}1`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/testnet.2/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}2`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}2`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/testnet.live/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}live`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}live`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/config/testnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
connection: {
host: process.env.CORE_DB_HOST || "localhost",
port: process.env.CORE_DB_PORT || 5432,
database: process.env.CORE_DB_DATABASE || `core_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || "core",
database: process.env.CORE_DB_DATABASE || `${process.env.CORE_TOKEN}_${process.env.CORE_NETWORK_NAME}`,
user: process.env.CORE_DB_USERNAME || process.env.CORE_TOKEN,
password: process.env.CORE_DB_PASSWORD || "password",
},
},
Expand Down
28 changes: 28 additions & 0 deletions scripts/docker/generate-docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require("fs");
const { ensureDirSync } = require("fs-extra");

const regex = new RegExp("{token}", "g");
const templateRoot = './scripts/docker/templates'
const templateDirs = fs.readdirSync(templateRoot);

if (process.argv.length !== 3) {
throw new Error("Expected 1 argument for the token name");
}

const token = process.argv[2];

console.log(`Generating docker files for '${token}':`)

templateDirs.forEach(templateDir => {
ensureDirSync(`./docker/${templateDir}`)
const templateFiles = fs.readdirSync(`${templateRoot}/${templateDir}`)
templateFiles.forEach(templateFile => {
const template = fs.readFileSync(`${templateRoot}/${templateDir}/${templateFile}`, { encoding: "utf8" })
const target = `./docker/${templateDir}/${templateFile}`
console.log(`${target}`)
fs.writeFileSync(target, template.replace(regex, token));
if (templateFile.endsWith(".sh")) {
fs.chmodSync(target, "755");
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ services:

postgres:
image: "postgres:alpine"
container_name: core-mainnet-postgres
container_name: {token}-development-postgres
ports:
- '127.0.0.1:5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: core_mainnet
POSTGRES_USER: core
POSTGRES_DB: {token}_development
POSTGRES_USER: {token}

volumes:
postgres:
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env sh

docker stop ark-development-postgres
docker rm -v ark-development-postgres
docker stop {token}-development-postgres
docker rm -v {token}-development-postgres
docker volume rm development_postgres
docker network rm development_default
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ services:

postgres:
image: "postgres:alpine"
container_name: core-devnet-postgres
container_name: {token}-devnet-postgres
ports:
- '127.0.0.1:5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: core_devnet
POSTGRES_USER: core
POSTGRES_DB: {token}_devnet
POSTGRES_USER: {token}

core:
build: .
image: core
container_name: core-devnet-core
container_name: {token}-devnet-core
ports:
- "4002:4002"
- "4003:4003"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

sysctl -w net.ipv4.conf.all.route_localnet=1

POSTGRES=$(ping -c 1 ark-testnet-postgres | awk -F'[()]' '/PING/{print $2}')
CORE=$(ping -c 1 ark-testnet-core | awk -F'[()]' '/PING/{print $2}')
POSTGRES=$(ping -c 1 {token}-devnet-postgres | awk -F'[()]' '/PING/{print $2}')
CORE=$(ping -c 1 {token}-devnet-core | awk -F'[()]' '/PING/{print $2}')
iptables -I OUTPUT -t nat -o lo -d localhost -p tcp --dport 5432 -j DNAT --to-destination ${POSTGRES}:5432
iptables -I POSTROUTING -t nat -p tcp --dport 5432 -d ${POSTGRES} -j SNAT --to ${CORE}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DOCKER_DB_NAME="$(docker-compose ps -q postgres)"
DB_HOSTNAME=postgres
DB_USER=ark
DB_USER={token}
LOCAL_DUMP_PATH="snapshot.dump"

docker-compose up -d postgres
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ services:

postgres:
image: "postgres:alpine"
container_name: core-development-postgres
container_name: {token}-mainnet-postgres
ports:
- '127.0.0.1:5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: core_development
POSTGRES_USER: core
POSTGRES_DB: {token}_mainnet
POSTGRES_USER: {token}

volumes:
postgres:
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ services:

postgres:
image: "postgres:alpine"
container_name: core-testnet-postgres
container_name: {token}-testnet-postgres
ports:
- '127.0.0.1:5432:5432'
volumes:
- 'postgres:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: core_testnet
POSTGRES_USER: core
POSTGRES_DB: {token}_testnet
POSTGRES_USER: {token}

core:
build: .
image: core
container_name: core-testnet-core
container_name: {token}-testnet-core
ports:
- "4000:4000"
- "4003:4003"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

sysctl -w net.ipv4.conf.all.route_localnet=1

POSTGRES=$(ping -c 1 ark-devnet-postgres | awk -F'[()]' '/PING/{print $2}')
CORE=$(ping -c 1 ark-devnet-core | awk -F'[()]' '/PING/{print $2}')
POSTGRES=$(ping -c 1 {token}-testnet-postgres | awk -F'[()]' '/PING/{print $2}')
CORE=$(ping -c 1 {token}-testnet-core | awk -F'[()]' '/PING/{print $2}')
iptables -I OUTPUT -t nat -o lo -d localhost -p tcp --dport 5432 -j DNAT --to-destination ${POSTGRES}:5432
iptables -I POSTROUTING -t nat -p tcp --dport 5432 -d ${POSTGRES} -j SNAT --to ${CORE}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DOCKER_DB_NAME="$(docker-compose ps -q postgres)"
DB_HOSTNAME=postgres
DB_USER=ark
DB_USER={token}
LOCAL_DUMP_PATH="snapshot.dump"

docker-compose up -d postgres
Expand Down

0 comments on commit ea4f775

Please sign in to comment.