diff --git a/.dockerignore b/.dockerignore new file mode 100755 index 00000000..e0cc78f0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,27 @@ +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/.gitattributes b/.gitattributes old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 38411563..62389ce5 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .DS_Store __pycache__/ **/__pycache__/ +.vscode/launch.json +.vscode/tasks.json *.pyc + diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100755 index 00000000..16d04702 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "docker.imageBuildContextPath": "${workspaceFolder}" + //"docker.imageBuildContextPath": "/MainPool/Main/Vital_Data/Scripts/Dispatcharr/Dispatcharr" + +} \ No newline at end of file diff --git a/dispatcharr/settings.py b/dispatcharr/settings.py index ee9db5c8..abfe40a1 100644 --- a/dispatcharr/settings.py +++ b/dispatcharr/settings.py @@ -68,8 +68,8 @@ DATABASES = { 'NAME': os.environ.get('POSTGRES_DB', 'dispatcharr'), 'USER': os.environ.get('POSTGRES_USER', 'dispatch'), 'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'secret'), - 'HOST': os.environ.get('POSTGRES_HOST', 'db'), - 'PORT': os.environ.get('POSTGRES_PORT', 5432), + 'HOST': os.environ.get('POSTGRES_HOST', 'localhost'), + 'PORT': int(os.environ.get('POSTGRES_PORT', 5432)), } } @@ -108,4 +108,4 @@ MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = '/media/' -#SERVER_IP = "10.0.0.107" +SERVER_IP = "127.0.0.1" diff --git a/docker/DockerfileAIO b/docker/DockerfileAIO new file mode 100755 index 00000000..fa9fa40a --- /dev/null +++ b/docker/DockerfileAIO @@ -0,0 +1,45 @@ +FROM python:3.10-slim + +# Add PostgreSQL repository +RUN apt-get update && apt-get install -y wget gnupg2 && \ + echo "deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \ + wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - + +# Install required packages +RUN apt-get update && apt-get install -y \ + ffmpeg \ + streamlink \ + vlc \ + libpq-dev \ + gcc \ + postgresql-14 \ + postgresql-contrib-14 \ + redis-server \ + && rm -rf /var/lib/apt/lists/* + +# Set the working directory +WORKDIR /app + +# Copy requirements.txt from the parent directory +COPY requirements.txt /app/ + +# Install Python dependencies +RUN pip install --no-cache-dir -r /app/requirements.txt + +# Copy the application source code from the parent directory +COPY . /app/ + +# Set environment variables +ENV DJANGO_SETTINGS_MODULE=dispatcharr.settings +ENV PYTHONUNBUFFERED=1 +ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt + +# Copy the entrypoint script +COPY docker/entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +# Expose the port +EXPOSE 9191 + +# Command to run the startup script +CMD ["/app/entrypoint.sh"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index df7ded16..b644ea3e 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -49,6 +49,8 @@ services: db: image: postgres:14 container_name: dispatcharr_db + ports: + - "5436:5432" environment: - POSTGRES_DB=dispatcharr - POSTGRES_USER=dispatch diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 00000000..f91b062d --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,120 @@ +#!/bin/sh + +set -e # Exit immediately if a command exits with a non-zero status + +# Function to echo with timestamp +echo_with_timestamp() { + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" +} + +# Set PostgreSQL environment variables +export POSTGRES_DB=${POSTGRES_DB:-dispatcharr} +export POSTGRES_USER=${POSTGRES_USER:-dispatch} +export POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-secret} +export POSTGRES_HOST=${POSTGRES_HOST:-localhost} +export POSTGRES_PORT=${POSTGRES_PORT:-5432} +export PGDATA=${PGDATA:-/app/data/db} +export PG_BINDIR="/usr/lib/postgresql/14/bin" + +# Echo environment variables for debugging +echo_with_timestamp "POSTGRES_DB: $POSTGRES_DB" +echo_with_timestamp "POSTGRES_USER: $POSTGRES_USER" +echo_with_timestamp "POSTGRES_PASSWORD: $POSTGRES_PASSWORD" +echo_with_timestamp "POSTGRES_HOST: $POSTGRES_HOST" +echo_with_timestamp "POSTGRES_PORT: $POSTGRES_PORT" + +# Create group if it doesn't exist +if ! getent group "$PGID" >/dev/null 2>&1; then + groupadd -g "$PGID" mygroup +fi +# Create user if it doesn't exist +if ! getent passwd $PUID > /dev/null 2>&1; then + useradd -u $PUID -g $PGID -m $POSTGRES_USER +else + existing_user=$(getent passwd $PUID | cut -d: -f1) + if [ "$existing_user" != "$POSTGRES_USER" ]; then + usermod -l $POSTGRES_USER -g $PGID "$existing_user" + fi +fi + +# Initialize PostgreSQL database +if [ -z "$(ls -A "$PGDATA")" ]; then + echo_with_timestamp "Initializing PostgreSQL database..." + mkdir -p "$PGDATA" + chown -R postgres:postgres "$PGDATA" + chmod 700 "$PGDATA" + + # Initialize PostgreSQL + su - postgres -c "$PG_BINDIR/initdb -D $PGDATA" + # Configure PostgreSQL + echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf" + echo "listen_addresses='*'" >> "$PGDATA/postgresql.conf" +fi + +# Start PostgreSQL +su - postgres -c "$PG_BINDIR/pg_ctl -D $PGDATA start -w -t 300 -o '-c port=${POSTGRES_PORT}'" + +# Wait for PostgreSQL to be ready +until su - postgres -c "$PG_BINDIR/pg_isready -h ${POSTGRES_HOST} -p ${POSTGRES_PORT}" >/dev/null 2>&1; do + echo_with_timestamp "Waiting for PostgreSQL to be ready..." + sleep 1 +done + +# Setup database if needed +if ! su - postgres -c "psql -p ${POSTGRES_PORT} -tAc \"SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB';\"" | grep -q 1; then + # Create PostgreSQL database + echo_with_timestamp "Creating PostgreSQL database..." + su - postgres -c "createdb -p ${POSTGRES_PORT} ${POSTGRES_DB}" + + # Create user, set ownership, and grant privileges + echo_with_timestamp "Creating PostgreSQL user..." + su - postgres -c "psql -p ${POSTGRES_PORT} -d ${POSTGRES_DB}" </dev/null 2>&1; then + echo_with_timestamp "ERROR: PostgreSQL is running but the database is not accessible. Exiting..." + exit 1 +else + echo_with_timestamp "PostgreSQL database is accessible." +fi + +# Start Redis +echo_with_timestamp "Starting Redis..." +service redis-server start + +# Run Django commands +echo_with_timestamp "Running Django commands..." +python manage.py collectstatic --noinput || true +python manage.py migrate --noinput || true + +# Start Celery +echo_with_timestamp "Starting Celery..." +celery -A dispatcharr worker --loglevel=info & + +# Start Gunicorn +echo_with_timestamp "Starting Gunicorn..." +gunicorn --workers=4 --worker-class=gevent --timeout=300 --bind 0.0.0.0:9191 dispatcharr.wsgi:application \ No newline at end of file