Skip to content

Commit

Permalink
Add Postgres container and use it. Run Teamspeak recorder in separate…
Browse files Browse the repository at this point in the history
… process as Django manage.py command in run.sh script.
  • Loading branch information
Bensge committed Jan 6, 2025
1 parent 8b36e9e commit 9705e84
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,6 @@ GitHub.sublime-settings

# frontend
node_modules/
frontend/output
frontend/output

secrets/
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ ENV PYTHONUNBUFFERED=1

EXPOSE 8000

# Install mysqlclient debian package dependencies
RUN apt-get update && apt-get install -y --no-install-recommends python3-dev default-libmysqlclient-dev build-essential pkg-config && rm -rf /var/lib/apt/lists/*
RUN pip install uv
COPY pyproject.toml pyproject.toml
RUN uv sync
Expand Down
2 changes: 1 addition & 1 deletion Spybot2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', False)

ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct']
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct', '192.168.59.100']

CSRF_TRUSTED_ORIGINS = [f"https://{SERVER_IP}"]

Expand Down
17 changes: 15 additions & 2 deletions docker-compose-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
restart: unless-stopped
volumes:
- static_files:/spybot_static
- $PWD/.env:/.env
- $PWD/.env:/app/.env
expose:
- 8000
caddy:
Expand All @@ -18,13 +18,26 @@ services:
# - "443:443"
# - "443:443/udp"
volumes:
- $PWD/Caddyfile:/etc/caddy/Caddyfile
- $PWD/infrastructure/Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
- static_files:/spybot_static
db:
image: postgres:17.2
restart: always
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
volumes:
- pg_data:/var/lib/postgresql/data
secrets:
- db_password
volumes:
caddy_data:
caddy_config:
static_files:
pg_data:
secrets:
db_password:
file: secrets/db_password.txt


12 changes: 12 additions & 0 deletions infrastructure/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ services:
- caddy_data:/data
- caddy_config:/config
- static_files:/spybot_static
db:
image: postgres:17.2
restart: always
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
volumes:
- spybot_pg_data:/var/lib/postgresql/data
secrets:
- db_password
volumes:
caddy_data:
caddy_config:
static_files:
secrets:
db_password:
file: secrets/db_password.txt


2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies = [
"Django ~=5.1",
"ts3 ~=2.0.0b3",
"django-environ ~=0.11.2",
"mysqlclient ~=2.2",
"django-crontab ~=0.7.1",
"num2words ~=0.5.12",
"requests ~=2.31",
Expand All @@ -22,6 +21,7 @@ dependencies = [
"django-bootstrap5 ~=24.2",
"sentry-sdk>=2.13.0",
"gunicorn>=23.0.0",
"psycopg2-binary>=2.9.10",
]


Expand Down
5 changes: 5 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ ls -a
# run DB migrations if necessary
.venv/bin/python manage.py migrate

# start recorder in background and terminate on script exit
.venv/bin/python manage.py recorder &
RECORDER_JOB=$!
trap 'kill $RECORDER_JOB' EXIT HUP TERM INT

# run django app
.venv/bin/gunicorn -w 2 --bind 0.0.0.0:8000 Spybot2.wsgi
3 changes: 0 additions & 3 deletions spybot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@ class SpybotConfig(AppConfig):

def ready(self):
if os.environ.get('RUN_MAIN'):
from spybot.recorder.recorder import Recorder
print("spybot app ready")
rec = Recorder()
rec.start()
14 changes: 14 additions & 0 deletions spybot/management/commands/recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.core.management import BaseCommand, CommandError


class Command(BaseCommand):
def handle(self, *args, **options):
try:
from spybot.recorder.recorder import Recorder
from Spybot2 import settings
if settings.RECORDER_ENABLED:
print('Starting recorder')
rec = Recorder()
rec.run()
except Exception as e:
raise CommandError(f'Error in recorder command: {e}')
5 changes: 0 additions & 5 deletions spybot/recorder/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ def __init__(self):
self.ts = TS()
self.client = Client(self.ts)

def start(self):
if settings.RECORDER_ENABLED:
thread = Thread(target=self.run, daemon=True)
thread.start()

def run(self):

while True:
Expand Down
58 changes: 45 additions & 13 deletions uv.lock

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

0 comments on commit 9705e84

Please sign in to comment.