Skip to content

Commit

Permalink
dockerize even more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexinthesky committed Dec 18, 2024
1 parent 6348e59 commit b655ad2
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ nohup.out
.env.production.local
.env
config.json
apps/*/config/default.js
# apps/*/config/default.js
.patreon-credentials.json
install.config

File renamed without changes.
2 changes: 1 addition & 1 deletion apps/bot/ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
apps: [
{
name: 'Craig',
script: 'dist/sharding/index.js',
script: 'dist/index.js',
wait_ready: true,
kill_timeout: 3000,
env: {
Expand Down
5 changes: 5 additions & 0 deletions apps/bot/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ if (process.env.SHARD_ID !== undefined && process.env.SHARD_COUNT !== undefined)
})
});
}
if (process.env.DISCORD_APP_ID !== undefined ) {
dexareConfig.applicationID = process.env.DISCORD_APP_ID
}


export const client = new CraigBot(dexareConfig);

process.once('SIGINT', async () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/bot/src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Redis, { RedisOptions } from 'ioredis';

const redisConfig: RedisOptions = config.get('redis');
export const client = new Redis({
host: redisConfig.host || 'localhost',
port: redisConfig.port || 6379,
host: process.env.REDIS_HOST || redisConfig.host || 'localhost',
port: process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT, 10) : (redisConfig.port || 6379),
keyPrefix: redisConfig.keyPrefix || 'craig:',
lazyConnect: true
});
Expand Down
22 changes: 11 additions & 11 deletions apps/dashboard/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ function validateEnv<T extends string = string>(key: keyof NodeJS.ProcessEnv, de

export const config = {
cookieName: 'token',
clientId: validateEnv('CLIENT_ID'),
clientSecret: validateEnv('CLIENT_SECRET'),
patreonClientId: validateEnv('PATREON_CLIENT_ID'),
patreonClientSecret: validateEnv('PATREON_CLIENT_SECRET'),
patreonWebhookSecret: validateEnv('PATREON_WEBHOOK_SECRET'),
clientId: validateEnv('CLIENT_ID',""),
clientSecret: validateEnv('CLIENT_SECRET',""),
patreonClientId: validateEnv('PATREON_CLIENT_ID',""),
patreonClientSecret: validateEnv('PATREON_CLIENT_SECRET',""),
patreonWebhookSecret: validateEnv('PATREON_WEBHOOK_SECRET',""),
patreonTierMap: validateEnv('PATREON_TIER_MAP', '{}'),
googleClientId: validateEnv('GOOGLE_CLIENT_ID'),
googleClientSecret: validateEnv('GOOGLE_CLIENT_SECRET'),
microsoftClientId: validateEnv('MICROSOFT_CLIENT_ID'),
microsoftClientSecret: validateEnv('MICROSOFT_CLIENT_SECRET'),
dropboxClientId: validateEnv('DROPBOX_CLIENT_ID'),
dropboxClientSecret: validateEnv('DROPBOX_CLIENT_SECRET'),
googleClientId: validateEnv('GOOGLE_CLIENT_ID',""),
googleClientSecret: validateEnv('GOOGLE_CLIENT_SECRET',""),
microsoftClientId: validateEnv('MICROSOFT_CLIENT_ID',""),
microsoftClientSecret: validateEnv('MICROSOFT_CLIENT_SECRET',""),
dropboxClientId: validateEnv('DROPBOX_CLIENT_ID',""),
dropboxClientSecret: validateEnv('DROPBOX_CLIENT_SECRET',""),
appUri: validateEnv('APP_URI', 'http://localhost:3000'),
jwtSecret: validateEnv('JWT_SECRET', 'this is a development value that should be changed in production!!!!!')
} as const;
58 changes: 58 additions & 0 deletions apps/ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module.exports = {
apps: [
{
name: 'Craig',
cwd: '/app/apps/bot',
script: 'dist/sharding/index.js',
wait_ready: true,
kill_timeout: 3000,
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'Craig Dashboard',
cwd: '/app/apps/dashboard',
script: 'npm',
args: 'start',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'craig.horse',
cwd: '/app/apps/download',
script: 'dist/index.js',
instances: '8',
exec_mode: 'cluster',
wait_ready: true,
listen_timeout: 10000,
kill_timeout: 3000,
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'Craig Tasks',
cwd: '/app/apps/tasks',
script: 'dist/index.js',
wait_ready: true,
kill_timeout: 3000,
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}
]
};
File renamed without changes.
4 changes: 2 additions & 2 deletions apps/tasks/src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Redis, { RedisOptions } from 'ioredis';

const redisConfig: RedisOptions = config.get('redis');
export const client = new Redis({
host: redisConfig.host || 'localhost',
port: redisConfig.port || 6379,
host: process.env.REDIS_HOST || redisConfig.host || 'localhost',
port: process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT, 10) : (redisConfig.port || 6379),
keyPrefix: redisConfig.keyPrefix || 'craig:',
lazyConnect: true
});
Expand Down
45 changes: 45 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: demo
POSTGRES_DB: craig
POSTGRES_USER: craig
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U craig"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis
restart: always
ports:
- "6379:6379"
craig:
build: .
ports:
- "3000:3000"
- "5029:5029"
environment:
REDIS_HOST: redis
REDIS_PORT: "6379"
DISCORD_BOT_TOKEN: "dummy"
DISCORD_APP_ID: "dummy"
CLIENT_ID: "dummy"
CLIENT_SECRET: "dummy"
DATABASE_URL: "postgresql://craig:demo@db:5432/craig?schema=public"
API_HOMEPAGE: "http://localhost:5029"
API_HOST: "0.0.0.0"
ENNUIZEL_BASE: "https://ez.craig.horse/"
NODE_ENV: 'development'
depends_on:
db:
condition: service_healthy
restart: true
redis:
condition: service_started
# command: sleep infinity
18 changes: 13 additions & 5 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use an official Ubuntu base image
FROM ubuntu:22.04
FROM node:18.20.5

# Remove interactive prompts
ENV DEBIAN_FRONTEND=noninteractive
Expand All @@ -9,7 +9,7 @@ RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y \
# cook
make inkscape ffmpeg flac fdkaac vorbis-tools opus-tools zip unzip \
make inkscape ffmpeg flac vorbis-tools opus-tools zip \
wget \
# redis
lsb-release curl gpg \
Expand All @@ -19,7 +19,7 @@ RUN apt-get update && \
# install
sed coreutils build-essential \
# Other dependencies
sudo git && \
sudo git vim && \
# Cleanup
apt-get -y autoremove

Expand All @@ -28,14 +28,22 @@ WORKDIR /app
# Copy all changes, particularly environment variables with discord API keys
COPY . .
# Run first-time setup for faster restarts
RUN ./install.sh

RUN scripts/buildCook.sh
RUN scripts/downloadCookBuilds.sh
RUN npm install -g pm2

RUN yarn install
RUN yarn prisma:generate
RUN yarn run build
RUN yarn run sync

# Expose app port
EXPOSE 3000
# Expose API port
EXPOSE 5029
# Start Craig
CMD ["sh", "-c", "/app/start.sh"]
CMD ["sh", "-c", "/app/start_docker.sh"]


# Usage:
Expand Down
14 changes: 14 additions & 0 deletions start_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -x
env
yarn prisma:deploy
yarn run sync

DOWNLOAD_DOMAIN=$(echo $API_HOMEPAGE|awk -F'://' '{print $2}')
sed -z -E -i'' "s/(dexare:.*token:\s*)('')(.*applicationID:\s*)('')(.*downloadDomain:\s*)('localhost:5029')/\
\1'${DISCORD_BOT_TOKEN}'\3'${DISCORD_APP_ID}'\5'${DOWNLOAD_DOMAIN//\//\\/}'/" \
"/app/apps/bot/config/default.js"

pm2 start /app/apps/ecosystem.config.js
pm2 save
pm2 logs

0 comments on commit b655ad2

Please sign in to comment.