Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Env var derivation + running application #20

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: GQL_PORT=$PORT sqd serve:prod
worker: sqd process:prod
web: GQL_PORT=$PORT npm run serve:prod
worker: npm run process:prod
9 changes: 9 additions & 0 deletions deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Deploy Helpers

Folder holding scripts to support the API deployment process.

### Run script

Currently support deriving the database_url environment variable into expected ones for the processor and graphQL-server (e.g. DB_HOST, DB_PASS), then it starts the target applications with this new vars available.

> Check the script [here](./run) for more details.
65 changes: 65 additions & 0 deletions deploy/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env node

/**
* @file Manage to derive the DATABASE_URL env var
* into several ones, followed by starting the correct npm script.
* It will set the new vars prepending its declaration prior
* calling the npm-script defined by the caller.
* The main intent is to solve the problem when the cloud provider changes
* the DATABASE_URL due to maintenance that leads to manual derivation
* of such values.
*
* @author Bruno Menezes <brunodmenezes@gmail.com>
*/

/**
* @typedef {Object} Config
* @property {string} DB_HOST - Database host
* @property {string} DB_NAME - Database name
* @property {string} DB_USER - Database user
* @property {string} DB_PASS - Database password
* @property {string} DB_PORT - Database port
*/

const url = require('node:url');
const shell = require('shelljs');
const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');

const argv = yargs(hideBin(process.argv)).argv;

if (!argv.npmScriptName) {
console.error('Npm script name is required!\n');
process.exitCode = 1;
}

/**
* @type Config
*/
const config = {};

const scriptName = argv.npmScriptName;
const DATABASE_URL = process.env.DATABASE_URL;
const db = url.parse(DATABASE_URL);
const auth = db.auth.split(':');
config.DB_HOST = db.hostname;
config.DB_NAME = db.pathname.substring(1);
config.DB_USER = auth[0];
config.DB_PASS = auth[1];
config.DB_PORT = db.port;

/**
*
* @param {string} scriptName Existing npm-script name.
* @param {Config} config Database environment variables to be injected.
*/
function runScript(scriptName, config) {
const vars = [];
for (const key in config) {
vars.push(`${key}=${config[key]}`);
}

shell.exec(`${vars.join(' ')} npm run ${scriptName}`);
}

runScript(scriptName, config);
32 changes: 7 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
"sqd:down": "sqd down",
"sqd:migration": "sqd migration",
"sqd:process": "sqd process",
"sqd:process:prod": "sqd process:prod",
"sqd:graphql": "sqd serve",
"sqd:graphql:prod": "sqd serve:prod",
"sqd:auth": "sqd auth -k $SQD_API_KEY",
"sqd:deploy": "sqd deploy -o cartesi -m",
"sqd:deploy:mainnet": "run-s \"sqd:deploy -- {@} .\" -- \"squid-mainnet.yaml\"",
"sqd:deploy:sepolia": "run-s \"sqd:deploy -- {@} .\" -- \"squid-sepolia.yaml\"",
"tsc": "tsc"
"tsc": "tsc",
"process:prod": "node deploy/run --npmScriptName=sqd:process:prod",
"serve:prod": "node deploy/run --npmScriptName=sqd:graphql:prod"
},
"dependencies": {
"@cartesi/rollups": "1.0.0",
Expand All @@ -34,8 +38,10 @@
"dotenv": "^16.1.4",
"ethers": "^6.5.1",
"pg": "^8.11.0",
"shelljs": "^0.8.5",
"type-graphql": "^1.2.0-rc.1",
"typeorm": "^0.3.16"
"typeorm": "^0.3.16",
"yargs": "^17.7.2"
},
"devDependencies": {
"@subsquid/cli": "latest",
Expand Down