Skip to content

A self-hosted media server and file management system

8.3 KB
Raw Blame
#!/bin/bash

# --- Configuration ---
PUID=$(grep "^PUID=" "$ENV_FILE" | cut -d'=' -f2)
PGID=$(grep "^PGID=" "$ENV_FILE" | cut -d'=' -f2)
PUID=${PUID:-1000}
PGID=${PGID:-1000}

SHARED_VOLUME_NAME="mediaserver-shared-env"
ENV_FILE="./.env" # Path to your .env file
FALLBACK_DEFAULT_DOMAIN="app.test" # Default if APP_URL is not found in .env
# --- End Configuration ---

# --- Define color variables ---
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
RESET='\033[0m' # Reset color
# --- End color variables ---

echo
echo "============================================"
echo "          mediaServer Docker Setup          "
echo "============================================"
echo

# --- Check for CI/CD or auto-default mode ---
IS_CI_MODE=false
if [[ "$1" == "--auto-default" ]]; then
    IS_CI_MODE=true
    echo "${BLUE}[INFO]${RESET} Auto-default mode enabled via command-line argument."
    shift # Consume the argument if it's explicitly passed
elif [[ -n "$CI" || -n "$GITHUB_ACTIONS" || -n "$GITLAB_CI" || -n "$JENKINS_URL" ]]; then
    IS_CI_MODE=true
    echo "${BLUE}[INFO]${RESET} Auto-default mode enabled (CI/CD environment detected)."
fi
echo
# --- End CI/CD check ---

echo -e "${YELLOW}[STEP 1/5]${RESET} Verifying required files and folders..."
echo

# Check for docker-compose.yaml
if [[ ! -f "docker-compose.yaml" ]]; then
    echo -e "${RED}[ERROR]${RESET} Missing 'docker-compose.yaml' file."
    echo "Please ensure this file is present in the root directory."
    exit 1
else
    echo -e "${GREEN}[FOUND]${RESET} 'docker-compose.yaml' file."
fi
echo

# Check for .env.docker
if [[ ! -f "docker/.env.docker" ]]; then
    echo -e "${RED}[ERROR]${RESET} Missing 'docker/.env.docker' file."
    echo "Please ensure this file is present in the correct directory."
    exit 1
fi

# Create .env if it doesn't exist
if [[ ! -f "$ENV_FILE" ]]; then
    echo -e "${BLUE}[INFO]${RESET} '.env' file not found. Creating from '.env.docker'..."
    cp "docker/.env.docker" "$ENV_FILE"
    if [[ $? -ne 0 ]]; then
        echo -e "${RED}[ERROR]${RESET} Failed to create '.env' file."
        exit 1
    fi
    echo -e "${GREEN}[SUCCESS]${RESET} '.env' file created."
else
    echo -e "${GREEN}[FOUND]${RESET} '.env' file."
fi
echo

# Ensure data/app subdirectories exists
if [[ ! -d "data/media" ]] || [[ ! -d "data/thumbnails" ]] || [[ ! -d "app" ]]; then
    echo -e "${BLUE}[INFO]${RESET} One or more 'data/app' subdirectories are missing. Creating them..."
    echo
    mkdir -p data/media data/thumbnails app
    echo -e "${GREEN}[SUCCESS]${RESET} 'data/app' subdirectories created."
else
    echo -e "${GREEN}[FOUND]${RESET} 'data/app' subdirectories."
fi
echo

# Ensure permissions are set for data directories
sudo chown -R ${PUID}:${PGID} data app
sudo chmod -R 775 data app
if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to create 'data/app' subdirectories or set permissions."
    exit 1
fi

# Ensure logs directory exists
if [[ ! -d "logs" ]]; then
    echo -e "${BLUE}[INFO]${RESET} Missing 'logs' directory. Creating it..."
    echo
    mkdir -p "logs/mediaServer"
    mkdir -p "logs/nginx"
    sudo chown -R ${PUID}:${PGID} ./logs/nginx
    sudo chown -R ${PUID}:${PGID} ./logs/mediaServer
    sudo chmod -R 775 logs
    if [[ $? -ne 0 ]]; then
        echo -e "${RED}[ERROR]${RESET} Failed to create 'logs' directory."
        exit 1
    fi
    echo -e "${GREEN}[SUCCESS]${RESET} 'logs' directory created."
else
    echo -e "${GREEN}[FOUND]${RESET} 'logs' directory."
fi
echo

echo -e "${YELLOW}[STEP 2/5]${RESET} Setting up user config..."
echo

# Get existing APP_HOST and APP_PORT or use defaults
CURRENT_APP_HOST=""
CURRENT_APP_PORT=""
if [[ -f "$ENV_FILE" ]]; then
    CURRENT_APP_HOST=$(grep "^APP_HOST=" "$ENV_FILE" | sed -E 's/^APP_HOST="?([^"]*)"?/\1/')
    CURRENT_APP_PORT=$(grep "^APP_PORT=" "$ENV_FILE" | sed -E 's/^APP_PORT="?([^"]*)"?/\1/')
fi

CURRENT_DOMAIN_DEFAULT="${FALLBACK_DEFAULT_DOMAIN}"
if [[ -n "$CURRENT_APP_HOST" ]]; then
    CURRENT_DOMAIN_DEFAULT="$CURRENT_APP_HOST"
fi

if $IS_CI_MODE; then
    USER_DOMAIN="$CURRENT_DOMAIN_DEFAULT"
    echo "${BLUE}[INFO]${RESET} Automatically using default domain: ${USER_DOMAIN}"
    echo
else
    # Ask for domain name with a default
    echo -e "${YELLOW}[CONFIG]${RESET} Enter your app domain (URL)..."
    echo -e "${YELLOW}[CONFIG]${RESET} Current value: ${CURRENT_DOMAIN_DEFAULT} (Press enter to use this)..."
    echo
    read -p "Domain: " USER_DOMAIN
    echo
fi

APP_HOST="${USER_DOMAIN:-$CURRENT_DOMAIN_DEFAULT}" # Use current default if user input is empty
APP_PORT="${CURRENT_APP_PORT:-8080}" # Use current default if user input is empty

echo -e "${BLUE}[INFO]${RESET} Setting APP_HOST to: ${APP_HOST}"
echo

# Update APP_HOST in .env file using sed
sed -i "s|^APP_HOST=.*|APP_HOST=\"${APP_HOST}\"|" "$ENV_FILE"
if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to update APP_HOST in ${ENV_FILE}."
    echo "Please check the .env file format or script permissions."
    exit 1
else
    echo -e "${GREEN}[SUCCESS]${RESET} APP_HOST updated in ${ENV_FILE}."
fi
echo

echo -e "${YELLOW}[STEP 3/5]${RESET} Stopping and cleaning up Existing mediaServer Docker containers..."
echo

docker compose down
if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to bring down Docker containers."
    echo "Please check your Docker setup and try again."
    exit 1
fi
echo -e "${GREEN}[SUCCESS]${RESET} Docker containers stopped and cleaned up."
echo

# echo -e "${YELLOW}[STEP 4/5]${RESET} Pruning Docker volumes..."
# echo
# docker volume prune -f
# if [[ $? -ne 0 ]]; then
#     echo -e "${RED}[ERROR]${RESET} Failed to prune Docker volumes."
#     echo "Please check your Docker setup and try again."
#     exit 1
# fi
# echo -e "${GREEN}[SUCCESS]${RESET} Docker volumes pruned."
# echo

echo -e "${YELLOW}[STEP 4/5]${RESET} Pulling latest Docker images..."
echo
docker compose pull
if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to pull Docker images."
    echo "Please check your internet connection and Docker setup."
    exit 1
else
    echo -e "${GREEN}[SUCCESS]${RESET} Docker images pulled successfully."
    echo
fi

echo -e "${YELLOW}[STEP 5/5]${RESET} Building docker compose..."
echo

# Check for shared docker volume
if ! docker volume inspect "$SHARED_VOLUME_NAME" &>/dev/null; then
    echo -e "${BLUE}[INFO]${RESET} Shared volume '$SHARED_VOLUME_NAME' does not exist. Creating it..."
    docker volume create "$SHARED_VOLUME_NAME"
    if [[ $? -ne 0 ]]; then
        echo -e "${RED}[ERROR]${RESET} Failed to create shared volume '$SHARED_VOLUME_NAME'."
        echo "Please check Docker daemon status and permissions."
        exit 1
    else
        echo -e "${GREEN}[SUCCESS]${RESET} Shared volume '$SHARED_VOLUME_NAME' created."
    fi
else
    echo -e "${GREEN}[FOUND]${RESET} Shared volume '$SHARED_VOLUME_NAME' exists."
fi
echo


# Create a temporary container to set permissions on shared volume
echo -e "${BLUE}[INFO]${RESET} Setting permissions on shared volume '$SHARED_VOLUME_NAME'..."
echo
docker run --rm \
  -v "${SHARED_VOLUME_NAME}:/shared" \
  alpine sh -c "mkdir -p /shared && chown -R ${PUID}:${PGID} /shared && chmod 775 /shared && echo 'Volume permissions set.'"

if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to set shared volume permissions."
    echo "Please check your Docker volume and user/group ID configurations."
    exit 1
fi
echo -e "${GREEN}[SUCCESS]${RESET} Shared volume permissions set."
echo

docker compose up -d

if [[ $? -ne 0 ]]; then
    echo -e "${RED}[ERROR]${RESET} Failed to build docker compose."
    echo "Please check your configuration."
    exit 1
fi

echo "============================================"
echo -e "${GREEN}          SETUP COMPLETED SUCCESSFULLY!${RESET}"
echo "============================================"
echo
echo "Your mediaServer will be available at https://$APP_HOST or http://$APP_HOST:$APP_PORT"
echo
echo "To add audio or video to your server, put the files in ./data/media organised by /LIBRARY/FOLDER/VIDEO.mp4"

if [[ "$APP_HOST" == "app.test" ]]; then
    echo
    echo "${YELLOW}[WARNING]${RESET} The domain '$APP_HOST' is not publicly resolvable."
    echo "You may need to add the following line to your /etc/hosts file:"
    echo
    echo "  127.0.0.1    $APP_HOST"
    echo
    echo "Run the following command with sudo if desired:"
    echo "echo '127.0.0.1 $APP_HOST' | sudo tee -a /etc/hosts"
    echo
fi