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

[Deployment]: Ops files to deploy to kubernetes #45

Closed
Closed
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
65 changes: 47 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
FROM node:20-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the entire monorepo into the container
FROM node:18-alpine AS base

FROM base AS builder
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
# Replace <your-major-version> with the major version installed in your repository. For example:
# RUN yarn global add turbo@^2
RUN yarn global add turbo@latest
COPY . .

# Install dependencies

# Generate a partial monorepo with a pruned lockfile for a target workspace.
# Assuming "web" is the name entered in the project's package.json: { name: "web" }
RUN turbo prune web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install

# Generate Prisma client
RUN cd packages/db && npx prisma generate

# Expose ports for both applications
EXPOSE 3000

WORKDIR /usr/src/app

# Command to start both services
CMD ["yarn", "run", "dev:docker"]
# Build the project
COPY --from=builder /app/out/full/ .
RUN yarn turbo run build --filter=web...

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=installer /app/apps/web/next.config.js .
COPY --from=installer /app/apps/web/package.json .

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public

CMD node apps/web/server.js
21 changes: 21 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:20-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the entire monorepo into the container
COPY . .

# Install dependencies
RUN yarn install

# Generate Prisma client
RUN cd packages/db && npx prisma generate

# Expose ports for both applications
EXPOSE 3000

WORKDIR /usr/src/app

# Command to start both services
CMD ["yarn", "run", "dev:docker"]
1 change: 1 addition & 0 deletions apps/leaderboard-generator/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONTEST_ID="##CONTEST_ID##"
43 changes: 43 additions & 0 deletions apps/leaderboard-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM node:18-alpine AS base

FROM base AS builder
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
# Replace <your-major-version> with the major version installed in your repository. For example:
# RUN yarn global add turbo@^2
RUN yarn global add turbo@latest
COPY . .

# Generate a partial monorepo with a pruned lockfile for a target workspace.
# Assuming "web" is the name entered in the project's package.json: { name: "web" }
RUN turbo prune leaderboard-generator --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install

# Build the project
COPY --from=builder /app/out/full/ .
RUN yarn turbo run build --filter=leaderboard-generator...

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs

COPY --from=installer /app/apps/leaderboard-generator/dist .

CMD node index.js
7 changes: 6 additions & 1 deletion apps/leaderboard-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"leaderboard:create": "CONTEST_ID=clx19tibu00054lu9fpb3c3zx ts-node src/index.ts"
"build": "esbuild ./src/index.ts --bundle --platform=node --outfile=dist/index.js --sourcemap",
"leaderboard:create": "ts-node src/index.ts"
},
"dependencies": {
"@repo/db": "*",
"esbuild": "^0.21.5"
},
"keywords": [],
"author": "",
Expand Down
7 changes: 3 additions & 4 deletions apps/leaderboard-generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
import prisma from "@repo/db/src";

async function main(contestId: string) {
const userPoints = new Map<string, number>();
Expand All @@ -16,15 +15,15 @@ async function main(contestId: string) {
if (userPoints.has(submission.userId)) {
userPoints.set(
submission.userId,
userPoints.get(submission.userId)! + submission.points,
userPoints.get(submission.userId)! + submission.points
);
} else {
userPoints.set(submission.userId, submission.points);
}
});

const sortedUserPoints = Array.from(userPoints.entries()).sort(
(a, b) => b[1] - a[1],
(a, b) => b[1] - a[1]
);

// clean existing leaderboard
Expand Down
4 changes: 2 additions & 2 deletions apps/leaderboard-generator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"rootDir": "./src", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
Expand Down Expand Up @@ -55,7 +55,7 @@
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand Down
43 changes: 43 additions & 0 deletions apps/sweeper/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM node:18-alpine AS base

FROM base AS builder
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
# Replace <your-major-version> with the major version installed in your repository. For example:
# RUN yarn global add turbo@^2
RUN yarn global add turbo@latest
COPY . .

# Generate a partial monorepo with a pruned lockfile for a target workspace.
# Assuming "web" is the name entered in the project's package.json: { name: "web" }
RUN turbo prune leaderboard-generator --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install

# Build the project
COPY --from=builder /app/out/full/ .
RUN yarn turbo run build --filter=leaderboard-generator...

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs

COPY --from=installer /app/apps/leaderboard-generator/dist .

CMD node index.js
1 change: 1 addition & 0 deletions apps/sweeper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"dependencies": {
"@repo/db": "*",
"esbuild": "^0.21.5",
"nodemon": "^3.1.3"
},
"scripts": {
Expand Down
18 changes: 0 additions & 18 deletions apps/sweeper/src/db/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/sweeper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prisma } from "@prisma/client";
import { db } from "./db";
import db from "@repo/db/src";
import { updateContest, updateMemoryAndExecutionTime } from "./utils";
type SubmissionWithTestcases = Prisma.SubmissionGetPayload<{
include: {
Expand Down
2 changes: 1 addition & 1 deletion apps/sweeper/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prisma } from "@prisma/client";
import { db } from "./db";
import db from "@repo/db/src";
import { getPoints } from "./points";

type SubmissionWithTestcases = Prisma.SubmissionGetPayload<{
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/submission/bulk/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { db } from "../../../db";
import db from "@repo/db/client";
import { getServerSession } from "next-auth";
import { authOptions } from "../../../lib/auth";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/submission/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubmissionInput } from "@repo/common/zod";
import { getProblem } from "../../lib/problems";
import axios from "axios";
import { LANGUAGE_MAPPING } from "@repo/common/language";
import { db } from "../../db";
import db from "@repo/db/client";
import { getServerSession } from "next-auth";
import { authOptions } from "../../lib/auth";
import { rateLimit } from "../../lib/rateLimit";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/db/contest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getServerSession } from "next-auth";
import { db } from ".";
import db from "@repo/db/client";
import { authOptions } from "../lib/auth";

export const getContest = async (contestId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/db/contestPoints.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getServerSession } from "next-auth";
import { authOptions } from "../lib/auth";
import { db } from "./index";
import db from "@repo/db/client";

export const getContestPoints = async (
contestId: string,
Expand Down
18 changes: 0 additions & 18 deletions apps/web/app/db/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/app/db/problem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { db } from ".";
import db from "@repo/db/client";

export const getProblem = async (problemId: string, contestId?: string) => {
if (contestId) {
Expand Down
1 change: 1 addition & 0 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @type {import('next').NextConfig} */
module.exports = {
transpilePackages: ["@repo/ui"],
output: 'standalone'
};
2 changes: 2 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-slot": "^1.0.2",
"@repo/ui": "*",
"@repo/common": "*",
"@repo/db": "*",
"@types/bcrypt": "^5.0.2",
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
app:
build:
context: .
dockerfile: Dockerfile
dockerfile: Dockerfile.dev
container_name: app-container
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
Expand Down
20 changes: 20 additions & 0 deletions packages/ops/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.5'

services:
app:
image: 100xdevs/algo-arena:latest
container_name: algo-arena
environment:
- NEXTAUTH_URL=
- NEXTAUTH_SECRET=
# - NEXTAUTH_URL_INTERNAL=
- DATABASE_URL=
- REDIS_URL=
- JUDGE0_URI=
- MOUNT_PATH=/problems
- NODE_ENV=production
- PORT=443
ports:
- '443:443' # Next.js app
volumes:
- ~/algorithmic-arena/apps/problems/:/problems
13 changes: 13 additions & 0 deletions packages/ops/prometh-conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
data:
prometheus.yml: |
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'redis'
static_configs:
- targets: ['redis-exporter:9121']

Loading