Dockerize
This commit is contained in:
parent
407a9e4794
commit
94117aa844
7
.dockerignore
Normal file
7
.dockerignore
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.git
|
||||||
|
.venv
|
||||||
|
venv
|
||||||
|
Dockerfile
|
||||||
|
docker-compose.yaml
|
||||||
|
__pycache__
|
||||||
|
.idea
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
.venv
|
27
Dockerfile
Normal file
27
Dockerfile
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
FROM docker.io/python:3.12-slim as builder
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE 1
|
||||||
|
ENV PYTHONUNBUFFERED 1
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt
|
||||||
|
|
||||||
|
FROM docker.io/python:3.12-slim
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE 1
|
||||||
|
ENV PYTHONUNBUFFERED 1
|
||||||
|
|
||||||
|
RUN useradd --no-create-home --system user && mkdir /data && chown user:user /data
|
||||||
|
|
||||||
|
ENV DB_PATH /data/presence-tracker.sqlite
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /app/wheels /wheels
|
||||||
|
COPY --from=builder /app/requirements.txt .
|
||||||
|
|
||||||
|
RUN pip install --no-cache /wheels/*
|
||||||
|
USER user
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
ENTRYPOINT ["python3", "/app/main.py"]
|
@ -4,7 +4,7 @@ from loguru import logger
|
|||||||
from datetime_utils import to_unix
|
from datetime_utils import to_unix
|
||||||
|
|
||||||
|
|
||||||
def get_db() -> sqlite3.Connection:
|
def get_db(db_path) -> sqlite3.Connection:
|
||||||
SCHEMA = """
|
SCHEMA = """
|
||||||
create table if not exists activities (
|
create table if not exists activities (
|
||||||
id integer primary key autoincrement,
|
id integer primary key autoincrement,
|
||||||
@ -25,13 +25,13 @@ def get_db() -> sqlite3.Connection:
|
|||||||
foreign key (activity_name_id) references activities(id)
|
foreign key (activity_name_id) references activities(id)
|
||||||
);"""
|
);"""
|
||||||
|
|
||||||
db = sqlite3.connect('presence-tracker.sqlite')
|
db = sqlite3.connect(db_path)
|
||||||
db.executescript(SCHEMA)
|
db.executescript(SCHEMA)
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
|
||||||
class PresenceTracker:
|
class PresenceTracker:
|
||||||
def __init__(self, db: sqlite3.Connection = get_db()):
|
def __init__(self, db: sqlite3.Connection):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
def log_activity(self, user_id: int, activity_name: str, start_time: datetime, end_time: datetime | None = None):
|
def log_activity(self, user_id: int, activity_name: str, start_time: datetime, end_time: datetime | None = None):
|
||||||
|
15
docker-compose.yaml
Normal file
15
docker-compose.yaml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
services:
|
||||||
|
discord-presence-tracker:
|
||||||
|
image: https://gitea.demb.uk/norohind/discord-presence-tracker.git
|
||||||
|
restart: unless-stopped
|
||||||
|
read_only: true
|
||||||
|
environment:
|
||||||
|
- LOGURU_LEVEL=INFO
|
||||||
|
- TOKEN
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
- discord-presence-tracker-vol:/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
discord-presence-tracker-vol:
|
||||||
|
external: true
|
6
main.py
6
main.py
@ -7,7 +7,7 @@ from typing import Any
|
|||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from PresenceTracker import PresenceTracker
|
from PresenceTracker import PresenceTracker, get_db
|
||||||
import discord
|
import discord
|
||||||
from discord.ext.commands import Bot as BotBase
|
from discord.ext.commands import Bot as BotBase
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -104,7 +104,9 @@ async def async_main():
|
|||||||
intents.presences = True
|
intents.presences = True
|
||||||
intents.members = True
|
intents.members = True
|
||||||
|
|
||||||
activity_tracker = PresenceTracker()
|
db = get_db(os.environ['DB_PATH'])
|
||||||
|
|
||||||
|
activity_tracker = PresenceTracker(db)
|
||||||
bot = Bot(intents=intents, activity_tracker=activity_tracker, command_prefix='')
|
bot = Bot(intents=intents, activity_tracker=activity_tracker, command_prefix='')
|
||||||
|
|
||||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user