diff --git a/.circleci/config.yml b/.circleci/config.yml index 52017f71c4..f9cda3cd47 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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' @@ -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' @@ -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' @@ -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' @@ -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' @@ -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' diff --git a/.circleci/configTemplate.json b/.circleci/configTemplate.json index 744c7e6312..d0b19f0275 100644 --- a/.circleci/configTemplate.json +++ b/.circleci/configTemplate.json @@ -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" @@ -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" diff --git a/package.json b/package.json index c949cc368d..00832ffbaa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core-container/src/container.ts b/packages/core-container/src/container.ts index 1f6d3874c3..378c4c3323 100644 --- a/packages/core-container/src/container.ts +++ b/packages/core-container/src/container.ts @@ -239,8 +239,10 @@ export class Container implements container.IContainer { this.shuttingDown = true; const logger = this.resolvePlugin("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. diff --git a/packages/core-container/src/environment.ts b/packages/core-container/src/environment.ts index eb7e886e70..04f8231ed2 100644 --- a/packages/core-container/src/environment.ts +++ b/packages/core-container/src/environment.ts @@ -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; diff --git a/packages/core-database-postgres/src/defaults.ts b/packages/core-database-postgres/src/defaults.ts index 10df9569ae..566937c84c 100644 --- a/packages/core-database-postgres/src/defaults.ts +++ b/packages/core-database-postgres/src/defaults.ts @@ -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", }, }; diff --git a/packages/core-test-utils/src/config/testnet/plugins.js b/packages/core-test-utils/src/config/testnet/plugins.js index 231dae805b..662f1de692 100644 --- a/packages/core-test-utils/src/config/testnet/plugins.js +++ b/packages/core-test-utils/src/config/testnet/plugins.js @@ -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", }, }, diff --git a/packages/core-test-utils/src/config/unitnet/plugins.js b/packages/core-test-utils/src/config/unitnet/plugins.js index 231dae805b..662f1de692 100644 --- a/packages/core-test-utils/src/config/unitnet/plugins.js +++ b/packages/core-test-utils/src/config/unitnet/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/devnet/plugins.js b/packages/core/src/config/devnet/plugins.js index c5190ec015..cfd80f5cf9 100644 --- a/packages/core/src/config/devnet/plugins.js +++ b/packages/core/src/config/devnet/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/mainnet/plugins.js b/packages/core/src/config/mainnet/plugins.js index a4def3c1a1..fd61cb3d97 100644 --- a/packages/core/src/config/mainnet/plugins.js +++ b/packages/core/src/config/mainnet/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/testnet.1/plugins.js b/packages/core/src/config/testnet.1/plugins.js index c88e34de79..27198e2e07 100644 --- a/packages/core/src/config/testnet.1/plugins.js +++ b/packages/core/src/config/testnet.1/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/testnet.2/plugins.js b/packages/core/src/config/testnet.2/plugins.js index 2a0110f6ef..d9c1dc3ad2 100644 --- a/packages/core/src/config/testnet.2/plugins.js +++ b/packages/core/src/config/testnet.2/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/testnet.live/plugins.js b/packages/core/src/config/testnet.live/plugins.js index fd0ca40650..3b60234719 100644 --- a/packages/core/src/config/testnet.live/plugins.js +++ b/packages/core/src/config/testnet.live/plugins.js @@ -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", }, }, diff --git a/packages/core/src/config/testnet/plugins.js b/packages/core/src/config/testnet/plugins.js index 16d6e243f4..efe107fe78 100644 --- a/packages/core/src/config/testnet/plugins.js +++ b/packages/core/src/config/testnet/plugins.js @@ -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", }, }, diff --git a/scripts/docker/generate-docker.js b/scripts/docker/generate-docker.js new file mode 100644 index 0000000000..596087633a --- /dev/null +++ b/scripts/docker/generate-docker.js @@ -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"); + } + }) +}) diff --git a/docker/mainnet/docker-compose.yml b/scripts/docker/templates/development/docker-compose.yml similarity index 73% rename from docker/mainnet/docker-compose.yml rename to scripts/docker/templates/development/docker-compose.yml index 96509f867a..08ff9230a6 100644 --- a/docker/mainnet/docker-compose.yml +++ b/scripts/docker/templates/development/docker-compose.yml @@ -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: diff --git a/docker/development/purge.sh b/scripts/docker/templates/development/purge.sh similarity index 53% rename from docker/development/purge.sh rename to scripts/docker/templates/development/purge.sh index 9849a43d90..446bfc96f1 100755 --- a/docker/development/purge.sh +++ b/scripts/docker/templates/development/purge.sh @@ -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 diff --git a/docker/devnet/Dockerfile b/scripts/docker/templates/devnet/Dockerfile similarity index 100% rename from docker/devnet/Dockerfile rename to scripts/docker/templates/devnet/Dockerfile diff --git a/docker/devnet/docker-compose.yml b/scripts/docker/templates/devnet/docker-compose.yml similarity index 80% rename from docker/devnet/docker-compose.yml rename to scripts/docker/templates/devnet/docker-compose.yml index 4f96099482..fb9e5ea730 100644 --- a/docker/devnet/docker-compose.yml +++ b/scripts/docker/templates/devnet/docker-compose.yml @@ -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" diff --git a/docker/testnet/entrypoint.sh b/scripts/docker/templates/devnet/entrypoint.sh similarity index 73% rename from docker/testnet/entrypoint.sh rename to scripts/docker/templates/devnet/entrypoint.sh index 4b787358df..3e8a3ef710 100755 --- a/docker/testnet/entrypoint.sh +++ b/scripts/docker/templates/devnet/entrypoint.sh @@ -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} diff --git a/docker/devnet/purge_all.sh b/scripts/docker/templates/devnet/purge_all.sh similarity index 100% rename from docker/devnet/purge_all.sh rename to scripts/docker/templates/devnet/purge_all.sh diff --git a/docker/testnet/restore.sh b/scripts/docker/templates/devnet/restore.sh similarity index 94% rename from docker/testnet/restore.sh rename to scripts/docker/templates/devnet/restore.sh index ef8a357f39..efd5ae4c03 100755 --- a/docker/testnet/restore.sh +++ b/scripts/docker/templates/devnet/restore.sh @@ -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 diff --git a/docker/development/docker-compose.yml b/scripts/docker/templates/mainnet/docker-compose.yml similarity index 74% rename from docker/development/docker-compose.yml rename to scripts/docker/templates/mainnet/docker-compose.yml index dde65f39f1..f5c6c70d8f 100644 --- a/docker/development/docker-compose.yml +++ b/scripts/docker/templates/mainnet/docker-compose.yml @@ -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: diff --git a/docker/testnet/Dockerfile b/scripts/docker/templates/testnet/Dockerfile similarity index 100% rename from docker/testnet/Dockerfile rename to scripts/docker/templates/testnet/Dockerfile diff --git a/docker/testnet/docker-compose.yml b/scripts/docker/templates/testnet/docker-compose.yml similarity index 80% rename from docker/testnet/docker-compose.yml rename to scripts/docker/templates/testnet/docker-compose.yml index e23ad52105..9e9d611e1c 100644 --- a/docker/testnet/docker-compose.yml +++ b/scripts/docker/templates/testnet/docker-compose.yml @@ -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" diff --git a/docker/devnet/entrypoint.sh b/scripts/docker/templates/testnet/entrypoint.sh similarity index 72% rename from docker/devnet/entrypoint.sh rename to scripts/docker/templates/testnet/entrypoint.sh index 4a0a373661..77d8976835 100755 --- a/docker/devnet/entrypoint.sh +++ b/scripts/docker/templates/testnet/entrypoint.sh @@ -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} diff --git a/docker/testnet/purge_all.sh b/scripts/docker/templates/testnet/purge_all.sh similarity index 100% rename from docker/testnet/purge_all.sh rename to scripts/docker/templates/testnet/purge_all.sh diff --git a/docker/devnet/restore.sh b/scripts/docker/templates/testnet/restore.sh similarity index 94% rename from docker/devnet/restore.sh rename to scripts/docker/templates/testnet/restore.sh index ef8a357f39..efd5ae4c03 100755 --- a/docker/devnet/restore.sh +++ b/scripts/docker/templates/testnet/restore.sh @@ -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