From dee736bd54902b4bfef1f93a9a346d79d14a3cd1 Mon Sep 17 00:00:00 2001 From: Benjamin Morgan Date: Thu, 25 Dec 2025 22:48:27 -0700 Subject: [PATCH] Containerize --- .gitignore | 4 +++- Dockerfile | 14 ++++++++++++++ docker-compose.yml | 8 ++++++++ docker-entrypoint.sh | 15 +++++++++++++++ mstbot.py | 8 +------- requirements.txt | 15 +++++++++++++++ schema.sql | 3 +++ 7 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100755 docker-entrypoint.sh create mode 100644 requirements.txt create mode 100644 schema.sql diff --git a/.gitignore b/.gitignore index 76024f8..c02f1a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.env -*.db \ No newline at end of file +*.db +secrets/* +data/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..45e977f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.13-slim + +WORKDIR /app + +COPY docker-entrypoint.sh ./entrypoint.sh +COPY requirements.txt . +COPY schema.sql . +COPY mstbot.py . + +RUN apt update && apt install sqlite3 +RUN pip install --upgrade pip && pip install -r requirements.txt + +ENTRYPOINT [ "/app/entrypoint.sh" ] +CMD [ "python3", "mstbot.py" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..126db91 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +services: + bot: + image: registry.benrmorgan.com/minecraftstatus:latest + env_file: + - ./secrets/.env + volumes: + - ./data:/data + restart: unless-stopped \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..df261b1 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -e + +DB_PATH="/data/mstbot.db" +SCHEMA="/app/schema.sql" + +if [ ! -f "$DB_PATH" ]; then + echo "SQLite DB not found, initializing…" + mkdir -p "$(dirname "$DB_PATH")" + sqlite3 "$DB_PATH" < "$SCHEMA" +else + echo "SQLite DB exists, skipping init" +fi + +exec "$@" diff --git a/mstbot.py b/mstbot.py index fe7bfda..3d835e0 100644 --- a/mstbot.py +++ b/mstbot.py @@ -1,6 +1,4 @@ # TODO: Change hours column to boolean, drop times table, make it a column in servers -# TODO: Containerize, include sql script to initialize empty database if one does not exist in volume -# TODO: CICD deploy over live instance via compose # TODO: prevent sql injection # TODO: annotate return types everywhere # TODO: split cogs into separate files, add utils file, remove globals @@ -21,12 +19,8 @@ from typing import Union, List import sqlite3 import discord from discord.ext import commands -from dotenv import load_dotenv from mcstatus import JavaServer -# Load environment variables -load_dotenv('.env') - # Bot Initialization intents = discord.Intents.default() intents.messages = True @@ -356,7 +350,7 @@ async def getStatus(serv: JavaServer): def connect(): - mydb = sqlite3.connect('mstbot.db') + mydb = sqlite3.connect('/data/mstbot.db') cursor = mydb.cursor() return mydb, cursor diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1d62989 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +aiohappyeyeballs==2.6.1 +aiohttp==3.13.2 +aiosignal==1.4.0 +asyncio-dgram==2.2.0 +attrs==25.4.0 +audioop-lts==0.2.2 +discord.py==2.6.4 +dnspython==2.8.0 +frozenlist==1.8.0 +idna==3.11 +mcstatus==12.0.6 +multidict==6.7.0 +propcache==0.4.1 +setuptools==80.9.0 +yarl==1.22.0 diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..18bf241 --- /dev/null +++ b/schema.sql @@ -0,0 +1,3 @@ +CREATE TABLE names (id INTEGER NOT NULL, name TEXT NOT NULL, UNIQUE (id, name)); +CREATE TABLE times (id INTEGER NOT NULL PRIMARY KEY, time NOT NULL DEFAULT 0) WITHOUT ROWID; +CREATE TABLE servers (id INTEGER PRIMARY KEY, ip TEXT NOT NULL, port INTEGER NOT NULL DEFAULT 25565, last_query TIMESTAMP, hours REAL NOT NULL DEFAULT 0, announce_joins BOOLEAN NOT NULL DEFAULT FALSE, announce_joins_id INTEGER) WITHOUT ROWID; \ No newline at end of file