Skip to content

Commit c457364

Browse files
authored
Merge pull request #139 from SmoFlaDru/dev-benno
Add Postgres container and use it. Run Teamspeak recorder in separate…
2 parents e49b93a + 6ce2187 commit c457364

13 files changed

+351
-308
lines changed

.env.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DB_URL=mysql://dummy:[email protected]:3306/dummy
1+
DB_URL=postgres://dummy:[email protected]:5432/dummy
22
SECRET_KEY=notasecuresecret
33
TS_USER=username
44
TS_PASSWORD=notarealpassword

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,6 @@ GitHub.sublime-settings
142142

143143
# frontend
144144
node_modules/
145-
frontend/output
145+
frontend/output
146+
147+
secrets/

Dockerfile

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ ENV PYTHONUNBUFFERED=1
1414

1515
EXPOSE 8000
1616

17-
# Install mysqlclient debian package dependencies
18-
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/*
1917
RUN pip install uv
2018
COPY pyproject.toml pyproject.toml
2119
RUN uv sync

Spybot2/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# SECURITY WARNING: don't run with debug turned on in production!
5151
DEBUG = env.bool('DEBUG', False)
5252

53-
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct']
53+
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct', '192.168.59.100']
5454

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

docker-compose-local.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
restart: unless-stopped
66
volumes:
77
- static_files:/spybot_static
8-
- $PWD/.env:/.env
8+
- $PWD/.env:/app/.env
99
expose:
1010
- 8000
1111
caddy:
@@ -18,13 +18,26 @@ services:
1818
# - "443:443"
1919
# - "443:443/udp"
2020
volumes:
21-
- $PWD/Caddyfile:/etc/caddy/Caddyfile
21+
- $PWD/infrastructure/Caddyfile:/etc/caddy/Caddyfile
2222
- caddy_data:/data
2323
- caddy_config:/config
2424
- static_files:/spybot_static
25+
db:
26+
image: postgres:17.2
27+
restart: always
28+
environment:
29+
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
30+
volumes:
31+
- pg_data:/var/lib/postgresql/data
32+
secrets:
33+
- db_password
2534
volumes:
2635
caddy_data:
2736
caddy_config:
2837
static_files:
38+
pg_data:
39+
secrets:
40+
db_password:
41+
file: secrets/db_password.txt
2942

3043

infrastructure/docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ services:
1919
- caddy_data:/data
2020
- caddy_config:/config
2121
- static_files:/spybot_static
22+
db:
23+
image: postgres:17.2
24+
restart: always
25+
environment:
26+
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
27+
volumes:
28+
- spybot_pg_data:/var/lib/postgresql/data
29+
secrets:
30+
- db_password
2231
volumes:
2332
caddy_data:
2433
caddy_config:
2534
static_files:
35+
secrets:
36+
db_password:
37+
file: secrets/db_password.txt
2638

2739

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dependencies = [
1313
"Django ~=5.1",
1414
"ts3 ~=2.0.0b3",
1515
"django-environ ~=0.11.2",
16-
"mysqlclient ~=2.2",
1716
"django-crontab ~=0.7.1",
1817
"num2words ~=0.5.12",
1918
"requests ~=2.31",
@@ -22,13 +21,14 @@ dependencies = [
2221
"django-bootstrap5 ~=24.2",
2322
"sentry-sdk>=2.13.0",
2423
"gunicorn>=23.0.0",
24+
"psycopg2-binary>=2.9.10",
2525
]
2626

2727

2828
[tool.uv]
2929
dev-dependencies = [
3030
"unittest-xml-reporting ~=3.2",
31-
"testcontainers[mysql]>=4.8.0",
31+
"testcontainers[postgres]>=4.9.0",
3232
]
3333

3434
[tool.setuptools]

run.sh

+5
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ ls -a
1010
# run DB migrations if necessary
1111
.venv/bin/python manage.py migrate
1212

13+
# start recorder in background and terminate on script exit
14+
.venv/bin/python manage.py recorder &
15+
RECORDER_JOB=$!
16+
trap 'kill $RECORDER_JOB' EXIT HUP TERM INT
17+
1318
# run django app
1419
.venv/bin/gunicorn -w 2 --bind 0.0.0.0:8000 Spybot2.wsgi

spybot/apps.py

-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ class SpybotConfig(AppConfig):
99

1010
def ready(self):
1111
if os.environ.get('RUN_MAIN'):
12-
from spybot.recorder.recorder import Recorder
1312
print("spybot app ready")
14-
rec = Recorder()
15-
rec.start()
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.core.management import BaseCommand, CommandError
2+
3+
4+
class Command(BaseCommand):
5+
def handle(self, *args, **options):
6+
try:
7+
from spybot.recorder.recorder import Recorder
8+
from Spybot2 import settings
9+
if settings.RECORDER_ENABLED:
10+
print('Starting recorder')
11+
rec = Recorder()
12+
rec.run()
13+
except Exception as e:
14+
raise CommandError(f'Error in recorder command: {e}')

spybot/recorder/recorder.py

-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ def __init__(self):
1515
self.ts = TS()
1616
self.client = Client(self.ts)
1717

18-
def start(self):
19-
if settings.RECORDER_ENABLED:
20-
thread = Thread(target=self.run, daemon=True)
21-
thread.start()
22-
2318
def run(self):
2419

2520
while True:
+9-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
from testcontainers.mysql import MySqlContainer
1+
from testcontainers.postgres import PostgresContainer
22

33
from Spybot2 import settings
44

55
from xmlrunner.extra.djangotestrunner import XMLTestRunner
66

77

88
class TestContainerRunner(XMLTestRunner):
9-
mysql_container: MySqlContainer = None
9+
postgres_container: PostgresContainer = None
1010

1111
def setup_databases(self, **kwargs):
12-
self.mysql_container = MySqlContainer(image="mariadb:11.0")
13-
self.mysql_container.start()
12+
self.postgres_container = PostgresContainer(image="postgres:17.2")
13+
self.postgres_container.start()
1414

1515
db_connection_settings = {
16-
'USER': 'root',
17-
'PASSWORD': self.mysql_container.root_password,
18-
'HOST': self.mysql_container.get_container_host_ip().replace("localhost", "127.0.0.1"),
19-
'PORT': self.mysql_container.get_exposed_port(3306),
16+
'USER': self.postgres_container.username,
17+
'PASSWORD': self.postgres_container.password,
18+
'HOST': self.postgres_container.get_container_host_ip().replace("localhost", "127.0.0.1"),
19+
'PORT': self.postgres_container.get_exposed_port(self.postgres_container.port),
2020
}
2121
settings.DATABASES['default'].update(db_connection_settings)
2222

@@ -25,4 +25,4 @@ def setup_databases(self, **kwargs):
2525
def teardown_databases(self, old_config, **kwargs):
2626
super().teardown_databases(old_config, **kwargs)
2727

28-
self.mysql_container.stop()
28+
self.postgres_container.stop()

0 commit comments

Comments
 (0)