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

Prod mode #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion backend/atlas_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def start_challenge(request, challenge_id):

container_id, password = client.run_container(
challenge.docker_image,
port=22,
port=challenge.port,
container_name=f"{request.user.team.name.replace(' ', '_')}-{challenge.title.replace(' ', '_')}"
)

Expand Down
16 changes: 9 additions & 7 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["*"]

DOCKER_HOST_IP="localhost"
DOCKER_HOST_IP="13.233.255.203"

# Application definition

Expand Down Expand Up @@ -59,14 +59,16 @@
]


CORS_ALLOW_ALL_ORIGINS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000", # React development server
"http://13.233.255.203", # React development server
"*",
]

CSRF_TRUSTED_ORIGINS = [
"http://localhost:3000", # React development server
"http://13.233.255.203", # React development server
"*",
]

ROOT_URLCONF = "backend.urls"
Expand Down Expand Up @@ -196,4 +198,4 @@
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
}
84 changes: 84 additions & 0 deletions dev-docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
services:
db:
image: postgres:16.4-bullseye
container_name: atlas_db
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 3s
timeout: 60s
retries: 5
restart : always

pgadmin:
image: dpage/pgadmin4
container_name: atlas_pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
volumes:
- pgadmin_data:/var/lib/pgadmin
networks:
- app-network
restart: always
depends_on:
- db

frontend:
build:
context: ./frontend
dockerfile: Dockerfile
container_name: atlas_frontend
ports:
- "3000:3000"
# volumes:
# - ./frontend:/app
# - /app/node_modules
environment:
- VITE_API_URL=http://localhost:8000
networks:
- app-network
restart: always
depends_on:
- backend

backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: atlas_backend
ports:
- "8000:8000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_HOST=db
- DB_PORT=5432
- DEBUG=1
networks:
- app-network
restart: always
depends_on:
- db

volumes:
postgres_data:
pgadmin_data:

networks:
app-network:
driver: bridge
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
dockerfile: Dockerfile
container_name: atlas_frontend
ports:
- "3000:3000"
- "80:4173"
# volumes:
# - ./frontend:/app
# - /app/node_modules
Expand All @@ -68,7 +68,6 @@ services:
- DB_PASSWORD=${DB_PASSWORD}
- DB_HOST=db
- DB_PORT=5432
- DEBUG=1
networks:
- app-network
restart: always
Expand All @@ -81,4 +80,4 @@ volumes:

networks:
app-network:
driver: bridge
driver: bridge
6 changes: 4 additions & 2 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ RUN pnpm install
# Copy rest of the application
COPY . .

RUN pnpm run build

# Expose port for frontend
EXPOSE 3000
EXPOSE 4173

# Start frontend
CMD ["pnpm","run","dev","--host"]
CMD ["pnpm","run","preview","--host"]
23 changes: 23 additions & 0 deletions frontend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Use node.js as base image
FROM node:23.1-alpine3.19

# Install pnpm
RUN npm install -g pnpm

# Set working directory
WORKDIR /app

# Copy package files and pnpm lock
COPY package.json pnpm-lock.yaml ./

# INstall dependencies
RUN pnpm install

# Copy rest of the application
COPY . .

# Expose port for frontend
EXPOSE 3000

# Start frontend
CMD ["pnpm","run","dev","--host"]
2 changes: 1 addition & 1 deletion frontend/src/api/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';

export const API_URL = 'http://localhost:8000';
export const API_URL = 'http://ec2-13-233-255-203.ap-south-1.compute.amazonaws.com:8000';

const apiClient = axios.create({
baseURL: API_URL,
Expand Down
24 changes: 24 additions & 0 deletions remove-containers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Get the list of container IDs and names that have been running for more than 10 minutes
containers=$(sudo docker ps --filter "status=running" --format "{{.ID}} {{.Names}} {{.RunningFor}}" | grep -E '([0-9]+) (minutes|hours|days)' | awk '$3 > 10 && $4 == "minutes" {print $1, $2}')

# Check if there are any containers to delete
if [ -z "$containers" ]; then
echo "No containers running for more than 10 minutes."
else
# Stop and remove the containers, ignoring specific atlas containers
while read -r container; do
container_id=$(echo $container | awk '{print $1}')
container_name=$(echo $container | awk '{print $2}')
if [[ $container_name != "atlas_backend" && $container_name != "atlas_frontend" && $container_name != "atlas_pgadmin" && $container_name != "atlas_db" ]]; then
echo "Stopping and removing container $container_id ($container_name)"
sudo docker stop $container_id
sudo docker rm $container_id
else
echo "Ignoring container $container_id ($container_name)"
fi
done <<< "$containers"
fi

psql -h localhost -U postgres -d atlas_db -c '\x' -c "DELETE FROM atlas_backend_container where (created_at < now() - interval '10 minutes');"