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

Simplify the setup script, capture only what is absolutely needed #576 #591

Merged
merged 11 commits into from
Sep 28, 2022
Merged
47 changes: 44 additions & 3 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,48 @@ FRONTEND_URL=https://localhost
MONGO_HOST=mongodb
MONGO_DB=praise_db
MONGO_PORT=27017
MONGO_INITDB_ROOT_USERNAME=praiseDbRootUsername
MONGO_INITDB_ROOT_USERNAME=
MONGO_INITDB_ROOT_PASSWORD=
MONGO_USERNAME=praiseDbUsername
MONGO_PASSWORD=
MONGO_USERNAME=
MONGO_PASSWORD=


# # FRONTEND ##

# Full URL to host where API is running. This variable is not currently used in production.
# Why? The frontend is built as a static website and cannot easily accept
# env variables. There are workarounds but we haven't prioritised to implement them yet.
#
# ℹ️ https://jakobzanker.de/blog/inject-environment-variables-into-a-react-app-docker-on-runtime/
REACT_APP_SERVER_URL=https://localhost

# Port number used when running frontend for development, outside of Docker
PORT=3000


## API ##
ADMINS=

## API AUTHENTICATION ##
JWT_SECRET=
# expires after 1 hour of inactivity, or 3 days
JWT_ACCESS_EXP=3600
JWT_REFRESH_EXP=25920000

## LOGGING ##
JET_LOGGER_MODE=CONSOLE
JET_LOGGER_FILEPATH=jet-logger.log
JET_LOGGER_TIMESTAMP=TRUE
JET_LOGGER_FORMAT=LINE


## DISCORD_BOT ##
DISCORD_TOKEN=
DISCORD_CLIENT_ID=
DISCORD_GUILD_ID=

## LOGGING ##
JET_LOGGER_MODE=CONSOLE
JET_LOGGER_FILEPATH=jet-logger.log
JET_LOGGER_TIMESTAMP=TRUE
JET_LOGGER_FORMAT=LINE
3 changes: 0 additions & 3 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ services:
image: ghcr.io/commons-stack/praise-dev/api:latest
env_file:
- .env
- ./packages/api/.env
restart: always
ports:
- $API_PORT:$API_PORT
Expand All @@ -29,7 +28,6 @@ services:
image: ghcr.io/commons-stack/praise-dev/discord-bot:latest
env_file:
- .env
- ./packages/discord-bot/.env
restart: always
links:
- mongodb
Expand All @@ -39,7 +37,6 @@ services:
image: ghcr.io/commons-stack/praise-dev/frontend:latest
env_file:
- .env
- ./packages/frontend/.env
restart: unless-stopped
ports:
- 80:80
Expand Down
9 changes: 3 additions & 6 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ services:
image: api
env_file:
- .env
- ./packages/api/.env
build:
build:
context: ./
dockerfile: ./packages/api/Dockerfile
restart: always
Expand All @@ -32,8 +31,7 @@ services:
image: discord-bot
env_file:
- .env
- ./packages/discord-bot/.env
build:
build:
context: ./
dockerfile: ./packages/discord-bot/Dockerfile
restart: always
Expand All @@ -45,8 +43,7 @@ services:
image: frontend
env_file:
- .env
- ./packages/frontend/.env
build:
build:
context: ./
dockerfile: ./packages/frontend/Dockerfile
restart: unless-stopped
Expand Down
3 changes: 0 additions & 3 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ services:
image: ghcr.io/commons-stack/praise/api:latest
env_file:
- .env
- ./packages/api/.env
restart: always
ports:
- $API_PORT:$API_PORT
Expand All @@ -29,7 +28,6 @@ services:
image: ghcr.io/commons-stack/praise/discord-bot:latest
env_file:
- .env
- ./packages/discord-bot/.env
restart: always
links:
- mongodb
Expand All @@ -39,7 +37,6 @@ services:
image: ghcr.io/commons-stack/praise/frontend:latest
env_file:
- .env
- ./packages/frontend/.env
restart: unless-stopped
ports:
- 80:80
Expand Down
18 changes: 0 additions & 18 deletions packages/api/.env.template

This file was deleted.

10 changes: 0 additions & 10 deletions packages/discord-bot/.env.template

This file was deleted.

13 changes: 0 additions & 13 deletions packages/frontend/.env.template

This file was deleted.

120 changes: 17 additions & 103 deletions packages/setup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
import inquirer from 'inquirer';
import * as fs from 'fs/promises';
import * as dotenv from 'dotenv';
import * as fs from 'fs/promises';
import { exit } from 'process';
import os from 'os';

/**
* Load ENV, templates first, then override with actual ENV values
* Load ENV, template first, then override with actual ENV values
* if there are any.
*/

// Top level
dotenv.config({ path: '/usr/praise/.env.template', override: true });
dotenv.config({ path: '/usr/praise/.env', override: true });

// API
dotenv.config({
path: '/usr/praise/packages/api/.env.template',
override: true,
});
dotenv.config({ path: '/usr/praise/packages/api/.env', override: true });

// Discord Bot
dotenv.config({
path: '/usr/praise/packages/discord-bot/.env.template',
override: true,
});
dotenv.config({
path: '/usr/praise/packages/discord-bot/.env',
override: true,
});

// Frontend
dotenv.config({
path: '/usr/praise/packages/frontend/.env.template',
override: true,
});
dotenv.config({ path: '/usr/praise/packages/frontend/.env', override: true });
dotenv.config({ path: '/usr/praise/.env.template', override: false });
dotenv.config({ path: '/usr/praise/.env', override: false });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Override should be true, right? The template should override any pre-existing env settings. And the "real" .env should override the template.


/**
* Welcome message
Expand Down Expand Up @@ -65,48 +41,12 @@ const questions = [
choices: ['production', 'development'],
default: process.env.NODE_ENV,
},
{
type: 'string',
name: 'MONGO_INITDB_ROOT_USERNAME',
message: 'MongoDB Root Username',
default: process.env.MONGO_INITDB_ROOT_USERNAME,
},
{
type: 'password',
name: 'MONGO_INITDB_ROOT_PASSWORD',
message: 'MongoDB Root Password',
default: process.env.MONGO_INITDB_ROOT_PASSWORD,
},
{
type: 'string',
name: 'MONGO_USERNAME',
message: 'MongoDB Praise Username',
default: process.env.MONGO_USERNAME,
},
{
type: 'password',
name: 'MONGO_PASSWORD',
message: 'MongoDB Praise Password',
default: process.env.MONGO_PASSWORD,
},
{
type: 'string',
name: 'HOST',
message: 'Server hostname',
default: process.env.HOST,
},
{
type: 'string',
name: 'API_PORT',
message: 'API port number',
default: process.env.API_PORT,
},
{
type: 'string',
name: 'PORT',
message: 'Frontend port number (Only used for development)',
default: process.env.PORT,
},
{
type: 'string',
name: 'ADMINS',
Expand Down Expand Up @@ -165,65 +105,39 @@ const run = async (): Promise<void> => {
const rootEnv = {
NODE_ENV: answers.NODE_ENV,
HOST: answers.HOST,
API_PORT: answers.API_PORT,
API_PORT: process.env.API_PORT,
SERVER_URL:
answers.NODE_ENV === 'production'
? `https://${answers.HOST as string}`
: `http://${answers.HOST as string}:${answers.API_PORT as string}`,
: `http://${answers.HOST as string}:${process.env.API_PORT as string}`,
FRONTEND_URL:
answers.NODE_ENV === 'production'
? `https://${answers.HOST as string}`
: `http://${answers.HOST as string}:${answers.PORT as string}`,
: `http://${answers.HOST as string}:${process.env.PORT as string}`,
MONGO_HOST: answers.NODE_ENV === 'production' ? 'mongodb' : 'localhost',
MONGO_INITDB_ROOT_USERNAME: answers.MONGO_INITDB_ROOT_USERNAME,
MONGO_INITDB_ROOT_PASSWORD: answers.MONGO_INITDB_ROOT_PASSWORD,
MONGO_USERNAME: answers.MONGO_USERNAME,
MONGO_PASSWORD: answers.MONGO_PASSWORD,
};
await setupAndWriteEnv(
'/usr/praise/.env.template',
'/usr/praise/.env',
rootEnv
);

const apiEnv = {
MONGO_INITDB_ROOT_USERNAME: randomString(12),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't generate random usernames for root username and username. Use the ones specified in .env.template

MONGO_INITDB_ROOT_PASSWORD: randomString(12),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passwords should not be regenerated on each run

MONGO_USERNAME: randomString(12),
MONGO_PASSWORD: randomString(12),
ADMINS: answers.ADMINS,
JWT_SECRET: process.env.JWT_SECRET || randomString(),
DISCORD_TOKEN: answers.DISCORD_TOKEN,
DISCORD_GUILD_ID: answers.DISCORD_GUILD_ID,
};
await setupAndWriteEnv(
'/usr/praise/packages/api/.env.template',
'/usr/praise/packages/api/.env',
apiEnv
);

const discordBotEnv = {
DISCORD_TOKEN: answers.DISCORD_TOKEN,
DISCORD_CLIENT_ID: answers.DISCORD_CLIENT_ID,
DISCORD_GUILD_ID: answers.DISCORD_GUILD_ID,
};
await setupAndWriteEnv(
'/usr/praise/packages/discord-bot/.env.template',
'/usr/praise/packages/discord-bot/.env',
discordBotEnv
);

const frontendEnv = {
REACT_APP_SERVER_URL:
answers.NODE_ENV === 'production'
? `https://${answers.HOST as string}`
: `http://${answers.HOST as string}:${answers.API_PORT as string}`,
PORT: answers.PORT,
: `http://${answers.HOST as string}:${process.env.API_PORT as string}`,
PORT: process.env.PORT,
};
await setupAndWriteEnv(
'/usr/praise/packages/frontend/.env.template',
'/usr/praise/packages/frontend/.env',
frontendEnv
'/usr/praise/.env.template',
'/usr/praise/.env',
rootEnv
);

console.log('\n');
console.log('🙏 ENV files have been created.');
console.log('🙏 ENV file has been created.');

exit();
};
Expand Down