From 44f01702b00888e2bc44665cff117ab4c488ae4f Mon Sep 17 00:00:00 2001 From: DMcConnell Date: Mon, 23 Dec 2024 21:01:29 -0500 Subject: [PATCH 1/5] Centralize make and env project wide --- .github/workflows/pull_request.yaml | 2 - .gitignore | 4 +- Makefile | 62 +++++++++++++++++++++++++++++ bot/Dockerfile | 18 ++++----- bot/Makefile | 46 --------------------- bot/core.py | 14 +++---- 6 files changed, 77 insertions(+), 69 deletions(-) create mode 100644 Makefile delete mode 100644 bot/Makefile diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 204d3d1..8e4a191 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -8,7 +8,6 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v4 - name: Run tests - working-directory: bot run: make test end-to-end-test: runs-on: ubuntu-latest @@ -16,7 +15,6 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - name: build-image - working-directory: ./bot run: docker build . -t boink_bot --build-arg DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }} - name: run-tests run: docker run boink_bot test/E2E/init_server.py ${{ secrets.WEBHOOK_URL }} diff --git a/.gitignore b/.gitignore index 24326bb..591cfa3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,11 @@ *.db sqlite_test.py env/ -config.ini +bot/config.ini nohup.out __pycache__/ .pytest* .vscode map.sql logs/ -bot/.DS_Store +.DS_Store diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..99bea10 --- /dev/null +++ b/Makefile @@ -0,0 +1,62 @@ +VENV = env +BIN=$(VENV)/bin +CONFIG = bot/config.ini + +$(VENV): + @if [ ! -d env/ ]; then \ + echo "Creating a virtual environment and installing dependencies..."; \ + fi; + @python -m venv $(VENV) + @$(BIN)/pip install --upgrade -q pip + @$(BIN)/pip install -q -r bot/requirements.txt + @$(BIN)/pip install -q -r databases/requirements.txt + @$(BIN)/pip install -q -r site/requirements.txt + +setup: + @if [ ! -d databases/bot_servers ]; then \ + mkdir -p databases/bot_servers; \ + fi + @if [ ! -d databases/game_servers ]; then \ + mkdir -p databases/game_servers; \ + fi + @if [ ! -d databases/analytics ]; then \ + mkdir -p databases/analytics; \ + fi + @if [ ! -f $(CONFIG) ]; then \ + echo "[default]" > $(CONFIG); \ + echo "token = $${DISCORD_BOT_TOKEN}" >> $(CONFIG); \ + echo "database = databases/default.db" >> $(CONFIG); \ + echo "" >> $(CONFIG); \ + echo "[meta]" >> $(CONFIG); \ + echo "database = databases/meta.db" >> $(CONFIG); \ + fi + +bot-run: $(VENV) + @cd bot && $(BIN)/python core.py + +bot-lint: $(VENV) + @cd bot && $(BIN)/python -m ruff check + +bot-format: $(VENV) + @cd bot && $(BIN)/python -m ruff format + +bot-test: $(VENV) + @cd bot && $(BIN)/python -m pytest test/ -v -s --log-cli-level=DEBUG + +site-run: $(VENV) + @cd site && $(BIN)/python app.py + +clean: + @rm -rf env/ .ruff_cache/ \ + bot/__pycache__ \ + bot/test/__pycache__ \ + bot/.pytest_cache/ \ + site/__pycache__ \ + databases/__pycache__ + +reset: clean $(VENV) + +todo: + @grep -inr "todo" bot/commands/ bot/handlers/ bot/interactions/ bot/utils/ databases/ + +.PHONY: setup bot-run bot-test bot-lint bot-format dev-site clean reset todo \ No newline at end of file diff --git a/bot/Dockerfile b/bot/Dockerfile index c732e62..a1f38ea 100644 --- a/bot/Dockerfile +++ b/bot/Dockerfile @@ -2,23 +2,21 @@ FROM python:3.11.1-slim-bullseye ARG DISCORD_BOT_TOKEN -ADD . /bot +WORKDIR /app -# Uncomment this line if you are running locally -# RUN rm /bot/config.ini - -WORKDIR /bot +# Copy entire project +COPY . . # Install make RUN apt-get update && apt-get install -y make +# Setup environment and install dependencies RUN make setup DISCORD_BOT_TOKEN=$DISCORD_BOT_TOKEN -RUN pip3 install -r requirements.txt -# Create a directory for the database and set permissions -RUN mkdir -p /databases/bot_servers && chmod 777 /databases/bot_servers +# Ensure proper permissions +RUN chmod -R 755 /app -# Ensure proper permissions for the bot directory -RUN chmod -R 755 /bot +# Set working directory to bot for running the application +WORKDIR /app/bot ENTRYPOINT ["python3"] \ No newline at end of file diff --git a/bot/Makefile b/bot/Makefile deleted file mode 100644 index 4472893..0000000 --- a/bot/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -VENV = env -BIN=$(VENV)/bin -CONFIG = config.ini - -$(VENV): - @if [ ! -d env/ ]; then \ - echo "Creating a virtual environment and installing dependencies..."; \ - fi; - @python -m venv $(VENV) - @$(BIN)/pip install --upgrade -q pip - @$(BIN)/pip install -q -r requirements.txt - -setup: - @if [ ! -d databases ]; then \ - mkdir databases; \ - fi - @if [ ! -f $(CONFIG) ]; then \ - echo "[default]" > $(CONFIG); \ - echo "token = $${DISCORD_BOT_TOKEN}" >> $(CONFIG); \ - echo "database = databases/default.db" >> $(CONFIG); \ - echo "" >> $(CONFIG); \ - echo "[meta]" >> $(CONFIG); \ - echo "database = databases/meta.db" >> $(CONFIG); \ - fi - -dev: $(VENV) - @$(BIN)/python core.py - -lint: $(VENV) - @$(BIN)/python -m ruff check - -format: $(VENV) - @$(BIN)/python -m ruff format - -test: $(VENV) - @$(BIN)/python -m pytest test/ -v -s --log-cli-level=DEBUG - -clean: - @rm -rf env/ .ruff_cache/ \ - src/bot/__pycache__ \ - tests/__pycache__ .pytest_cache/ - -reset: clean $(VENV) - -todo: - @grep -inr "todo" commands/ handlers/ interactions/ utils/ databases/ diff --git a/bot/core.py b/bot/core.py index 6f341ff..d4c627d 100644 --- a/bot/core.py +++ b/bot/core.py @@ -243,15 +243,11 @@ async def run_server_database_alerts(self): logger.info("Running server database alerts") for guild in self.guilds: - try: - logger.info( - f"Config for {guild}: {read_config_str(guild.id, ConfigKeys.ALERTS, '0')}" - ) - if read_config_str(guild.id, ConfigKeys.ALERTS, "0") == "1": - await self._send_alerts_for_guild(guild) - except KeyError as e: - logger.error(f"Failed to check alerts for {guild}") - logger.error(e) + logger.info( + f"Config for {guild}: {read_config_str(guild.id, ConfigKeys.ALERTS, '0')}" + ) + if read_config_str(guild.id, ConfigKeys.ALERTS, "0") == "1": + await self._send_alerts_for_guild(guild) if __name__ == "__main__": From 2e526a5fd6e60d96cb5d8b7aef72a1ff18e3454d Mon Sep 17 00:00:00 2001 From: DMcConnell Date: Mon, 23 Dec 2024 21:07:31 -0500 Subject: [PATCH 2/5] Move docker file --- .github/workflows/pull_request.yaml | 2 +- bot/Dockerfile => Dockerfile | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename bot/Dockerfile => Dockerfile (100%) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 8e4a191..5d9889f 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -8,7 +8,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v4 - name: Run tests - run: make test + run: make bot-test end-to-end-test: runs-on: ubuntu-latest steps: diff --git a/bot/Dockerfile b/Dockerfile similarity index 100% rename from bot/Dockerfile rename to Dockerfile From b08540cd78db71ddf22125127cf036f805524052 Mon Sep 17 00:00:00 2001 From: DMcConnell Date: Mon, 23 Dec 2024 21:09:36 -0500 Subject: [PATCH 3/5] Fix bad requirements.txt --- site/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/requirements.txt b/site/requirements.txt index d0f155f..cd253ca 100644 --- a/site/requirements.txt +++ b/site/requirements.txt @@ -12,7 +12,7 @@ dash==2.13.0 dash-auth==2.0.0 dash-bootstrap-components==1.5.0 dash-core-components==2.0.0 -dash_daq=0.5.0 +dash_daq==0.5.0 dash-html-components==2.0.0 dash-table==5.0.0 discord.py==2.3.2 From 1a56d903dc6e8dac71bd6bfbed6f2e5b2815c08d Mon Sep 17 00:00:00 2001 From: DMcConnell Date: Tue, 24 Dec 2024 12:56:23 -0500 Subject: [PATCH 4/5] Resolve env issues --- Makefile | 3 ++- site/requirements.txt | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 99bea10..ddd614e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ VENV = env +PYTHON = python3.11 BIN=$(VENV)/bin CONFIG = bot/config.ini @@ -6,7 +7,7 @@ $(VENV): @if [ ! -d env/ ]; then \ echo "Creating a virtual environment and installing dependencies..."; \ fi; - @python -m venv $(VENV) + @$(PYTHON) -m venv $(VENV) @$(BIN)/pip install --upgrade -q pip @$(BIN)/pip install -q -r bot/requirements.txt @$(BIN)/pip install -q -r databases/requirements.txt diff --git a/site/requirements.txt b/site/requirements.txt index cd253ca..703ca9b 100644 --- a/site/requirements.txt +++ b/site/requirements.txt @@ -33,9 +33,9 @@ packaging==23.1 pandas==2.1.1 Pillow==10.0.1 plotly==5.17.0 -pluggy==1.3.0 +pluggy>=1.4.0 pyparsing==3.1.1 -pytest==7.4.1 +pytest>=8.2.0 python-dateutil==2.8.2 pytz==2023.3.post1 requests==2.31.0 From b54870adb3b2ab9044368826b9341094dfe7693a Mon Sep 17 00:00:00 2001 From: DMcConnell Date: Tue, 24 Dec 2024 15:32:10 -0500 Subject: [PATCH 5/5] Add .python-version --- .python-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .python-version diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..902b2c9 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.11 \ No newline at end of file