Skip to content

Commit

Permalink
Merge pull request #139 from SmoFlaDru/dev-benno
Browse files Browse the repository at this point in the history
Add Postgres container and use it. Run Teamspeak recorder in separate…
  • Loading branch information
Bensge authored Jan 6, 2025
2 parents e49b93a + 6ce2187 commit c457364
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 308 deletions.
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_URL=mysql://dummy:[email protected]:3306/dummy
DB_URL=postgres://dummy:[email protected]:5432/dummy
SECRET_KEY=notasecuresecret
TS_USER=username
TS_PASSWORD=notarealpassword
Expand Down
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


4 changes: 2 additions & 2 deletions 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,13 +21,14 @@ dependencies = [
"django-bootstrap5 ~=24.2",
"sentry-sdk>=2.13.0",
"gunicorn>=23.0.0",
"psycopg2-binary>=2.9.10",
]


[tool.uv]
dev-dependencies = [
"unittest-xml-reporting ~=3.2",
"testcontainers[mysql]>=4.8.0",
"testcontainers[postgres]>=4.9.0",
]

[tool.setuptools]
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
18 changes: 9 additions & 9 deletions spybot/tests/runner_testcontainers_db.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from testcontainers.mysql import MySqlContainer
from testcontainers.postgres import PostgresContainer

from Spybot2 import settings

from xmlrunner.extra.djangotestrunner import XMLTestRunner


class TestContainerRunner(XMLTestRunner):
mysql_container: MySqlContainer = None
postgres_container: PostgresContainer = None

def setup_databases(self, **kwargs):
self.mysql_container = MySqlContainer(image="mariadb:11.0")
self.mysql_container.start()
self.postgres_container = PostgresContainer(image="postgres:17.2")
self.postgres_container.start()

db_connection_settings = {
'USER': 'root',
'PASSWORD': self.mysql_container.root_password,
'HOST': self.mysql_container.get_container_host_ip().replace("localhost", "127.0.0.1"),
'PORT': self.mysql_container.get_exposed_port(3306),
'USER': self.postgres_container.username,
'PASSWORD': self.postgres_container.password,
'HOST': self.postgres_container.get_container_host_ip().replace("localhost", "127.0.0.1"),
'PORT': self.postgres_container.get_exposed_port(self.postgres_container.port),
}
settings.DATABASES['default'].update(db_connection_settings)

Expand All @@ -25,4 +25,4 @@ def setup_databases(self, **kwargs):
def teardown_databases(self, old_config, **kwargs):
super().teardown_databases(old_config, **kwargs)

self.mysql_container.stop()
self.postgres_container.stop()
Loading

0 comments on commit c457364

Please sign in to comment.