diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..0a7a659 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,8 @@ +**/__pycache__ +**/*.pyc +**/*.pyo +**/*.pyd +*.git +.dockerignore +Dockerfile* +*.md diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..887fea4 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,73 @@ +# For x86 architecture, use the following base image: +FROM python:3.9.13-slim + +# Set environment variables +ENV PYTHONUNBUFFERED=1 +ENV POSTGRES_USER=username +ENV POSTGRES_PASSWORD=password +ENV POSTGRES_DB=databasename + +# Set the working directory +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + build-essential \ + libpq-dev \ + postgresql \ + wget \ + unzip \ + tar \ + git \ + g++ \ + make \ + && rm -rf /var/lib/apt/lists/* + +# Download the Certabo software +RUN wget https://www.certabo.com/wp-content/uploads/SOFTWARE/Release/Ubuntu/Certabo_Tabutronic_Ubuntu_4.5.zip + +# Extract the Certabo software +RUN unzip /app/Certabo_Tabutronic_Ubuntu_4.5.zip -d /app + +# Install the Certabo software +RUN dpkg -i /app/Certabo_Tabutronic_Ubuntu_4.5/Certabo_Tabutronic_Ubuntu_4.5.deb || true +RUN apt-get install -f + +# Clone Stockfish and compile it +RUN git clone https://github.com/official-stockfish/Stockfish.git && \ + cd Stockfish/src && \ + make build ARCH=armv8 && \ + cp stockfish /usr/local/bin/ && \ + chmod +x /usr/local/bin/stockfish && \ + cd ../.. && \ + rm -rf Stockfish + +# Start PostgreSQL service and setup database +USER postgres +RUN service postgresql start && \ + psql -c "CREATE ROLE $POSTGRES_USER WITH LOGIN PASSWORD '$POSTGRES_PASSWORD'; ALTER ROLE $POSTGRES_USER CREATEDB;" + +RUN service postgresql start && \ + psql -c "CREATE DATABASE $POSTGRES_DB;" && \ + psql -c "GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER;" + +USER root + +# Copy only the requirements file +COPY requirements.txt . + +# Install Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the start script and the rest of the application code +COPY start.sh . +COPY . . + +# Give execute permissions to the start script +RUN chmod +x start.sh + +# Expose the port the app runs on +EXPOSE 5000 + +# Command to run the start script +CMD ["./start.sh"] \ No newline at end of file diff --git a/backend/server.py b/backend/server.py index d59eed5..61c8f5e 100644 --- a/backend/server.py +++ b/backend/server.py @@ -2,7 +2,6 @@ import pathlib from flask import Flask from flask_socketio import SocketIO -from config import STOCKFISH_PATH from chessLogic.chessLogic import ChessLogic from certaboHelper.certabo import Certabo from certaboHelper.initCertabo import InitializeCertabo @@ -25,7 +24,9 @@ conf = ''.join([str(loc),STOCKFISH_PATH]) pStockfish = conf elif platform.system() == 'Darwin': #Darwin for MacOS - pStockfish = STOCKFISH_PATH + pStockfish = "/opt/homebrew/bin/stockfish" +else: #Default to Linux + pStockfish ="/usr/local/bin/stockfish" chess_logic = ChessLogic(pStockfish) diff --git a/backend/start.sh b/backend/start.sh new file mode 100644 index 0000000..3f51a8d --- /dev/null +++ b/backend/start.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +DEVICE_PATH="/dev/ttyUSB0" +if [ -c "$DEVICE_PATH" ] +then + echo -e "\n\n\tDevice $DEVICE_PATH is accessible.\n\n" +else + echo -e "\nError: Device $DEVICE_PATH is not accessible.\n\nPlease check the device path or connection.\n\nMaybe you forgot to mount the device?\n\nExample docker run command: \n\tdocker run --device=/dev/ttyUSB0:/dev/ttyUSB0 -p 5000:5000 chess-backend\n\n" + exit 1 +fi + +# Start the Python application +echo -e "\n\n\tStarting Python application...\n\n" +python server.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0f3cc60 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3' +services: + stockfish: + image: tchorwat/stockfish + ports: + - "23249:23249" + + postgres: + image: postgres:14 + environment: + POSTGRES_PASSWORD: password + ports: + - "5432:5432" + + backend: + build: + context: ./backend + dockerfile: Dockerfile + ports: + - "5000:5000" + depends_on: + - postgres + - stockfish + + frontend: + build: + context: ./frontend + dockerfile: Dockerfile + ports: + - "3000:3000" + depends_on: + - backend \ No newline at end of file