Compare commits

..

3 Commits

Author SHA1 Message Date
fa3a2209ad Merge branch 'main' of https://git.cnull.net/core/Projektarbeit-MYP 2025-03-26 13:28:36 +01:00
2ab4c4c3e2 Improve frontend installation script for robust installation process
- Add non-interactive installation mode with command-line options
- Implement robust error handling and recovery mechanisms
- Fix pnpm installation with fallback methods and PATH configuration
- Add CORS headers to allow communication with backend
- Improve Docker build and compose process with verification steps
- Add colored log output for better readability
- Add dependency installation retry mechanism for better reliability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-26 13:23:18 +01:00
8366a9295e Enhance frontend installation script with improved system packages and static backend URL
- Add essential system packages and networking tools
- Install official Docker CE with proper configuration
- Configure backend connection to static IP 192.168.0.105
- Generate secure random AUTH_SECRET during installation
- Add environment example file for version control
- Improve post-installation information and helpful commands

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-26 13:20:44 +01:00

View File

@ -3,14 +3,37 @@
# MYP Frontend Installation Script for Debian # MYP Frontend Installation Script for Debian
# This script installs and configures the MYP frontend reservation platform # This script installs and configures the MYP frontend reservation platform
set -e # Exit immediately if a command exits with non-zero status # We'll handle errors ourselves rather than exiting immediately
# set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LOG_FILE="$SCRIPT_DIR/frontend-install.log" LOG_FILE="$SCRIPT_DIR/frontend-install.log"
# Function for logging with timestamps # Function for logging with timestamps and colors
log() { log() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S") local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "[$timestamp] $1" | tee -a "$LOG_FILE" local message="[$timestamp] $1"
echo -e "$message" | tee -a "$LOG_FILE"
}
# Function for logging errors with timestamps and colors
log_error() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local message="[$timestamp] ERROR: $1"
echo -e "\033[0;31m$message\033[0m" | tee -a "$LOG_FILE"
}
# Function for logging warnings with timestamps and colors
log_warning() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local message="[$timestamp] WARNING: $1"
echo -e "\033[0;33m$message\033[0m" | tee -a "$LOG_FILE"
}
# Function for logging success with timestamps and colors
log_success() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local message="[$timestamp] SUCCESS: $1"
echo -e "\033[0;32m$message\033[0m" | tee -a "$LOG_FILE"
} }
# Function to check if a command exists # Function to check if a command exists
@ -21,9 +44,36 @@ command_exists() {
# Clear log file # Clear log file
> "$LOG_FILE" > "$LOG_FILE"
# Add script usage info
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "OPTIONS:"
echo " --auto-production Non-interactive installation in production mode"
echo " --auto-development Non-interactive installation in development mode"
echo " --help Show this help message"
echo ""
echo "Example:"
echo " $0 --auto-production"
exit 1
}
# Check for help flag
if [ "$1" == "--help" ]; then
usage
fi
log "===== Starting MYP Frontend Installation =====" log "===== Starting MYP Frontend Installation ====="
log "Installation directory: $SCRIPT_DIR" log "Installation directory: $SCRIPT_DIR"
# Display system information
log "System information:"
uname -a >> "$LOG_FILE" 2>&1
lsb_release -a >> "$LOG_FILE" 2>&1 || cat /etc/os-release >> "$LOG_FILE" 2>&1
log "CPU: $(grep "model name" /proc/cpuinfo | head -n1 | cut -d':' -f2 | sed 's/^ *//')"
log "Memory: $(free -h | grep "Mem:" | awk '{print $2}')"
log "Disk space: $(df -h / | awk 'NR==2 {print $4}') free on /"
# Check for root privileges # Check for root privileges
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
log "ERROR: This script must be run as root" log "ERROR: This script must be run as root"
@ -36,8 +86,39 @@ apt update -y >> "$LOG_FILE" 2>&1
apt upgrade -y >> "$LOG_FILE" 2>&1 apt upgrade -y >> "$LOG_FILE" 2>&1
# Install required packages # Install required packages
log "Installing required system packages..." log "Installing essential system packages and network tools..."
apt install -y curl git docker.io docker-compose xz-utils sqlite3 >> "$LOG_FILE" 2>&1 apt install -y curl git wget htop net-tools iptables iputils-ping traceroute nmap tcpdump nftables \
netcat-openbsd dnsutils whois vim nano rsync zip unzip xz-utils sqlite3 \
apt-transport-https ca-certificates gnupg lsb-release >> "$LOG_FILE" 2>&1
# Install Docker using the official Docker repository
log "Installing Docker from official repository..."
if ! command_exists docker; then
# Remove any old Docker versions
apt remove -y docker docker-engine docker.io containerd runc >> "$LOG_FILE" 2>&1 || true
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg >> "$LOG_FILE" 2>&1
# Set up the stable repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update apt and install Docker
apt update -y >> "$LOG_FILE" 2>&1
apt install -y docker-ce docker-ce-cli containerd.io >> "$LOG_FILE" 2>&1
# Install Docker Compose
log "Installing Docker Compose..."
curl -L "https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose >> "$LOG_FILE" 2>&1
chmod +x /usr/local/bin/docker-compose >> "$LOG_FILE" 2>&1
ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose >> "$LOG_FILE" 2>&1
else
log "Docker already installed: $(docker --version)"
log "Docker Compose already installed: $(docker-compose --version)"
fi
# Install Node.js 20.x # Install Node.js 20.x
if ! command_exists node || [ $(node -v | cut -d. -f1 | tr -d 'v') -lt 20 ]; then if ! command_exists node || [ $(node -v | cut -d. -f1 | tr -d 'v') -lt 20 ]; then
@ -52,13 +133,23 @@ fi
# Install pnpm # Install pnpm
if ! command_exists pnpm; then if ! command_exists pnpm; then
log "Installing pnpm package manager..." log "Installing pnpm package manager..."
curl -fsSL https://get.pnpm.io/install.sh | sh - >> "$LOG_FILE" 2>&1 # Install pnpm globally to avoid home directory permission issues
source ~/.bashrc npm install -g pnpm >> "$LOG_FILE" 2>&1
# Fallback method in case npm method fails
if ! command_exists pnpm; then
log "Trying alternative pnpm installation method..."
curl -fsSL https://get.pnpm.io/install.sh | bash - >> "$LOG_FILE" 2>&1
export PATH="$HOME/.local/share/pnpm:$PATH"
fi
log "pnpm $(pnpm --version) installed" log "pnpm $(pnpm --version) installed"
else else
log "pnpm $(pnpm --version) already installed" log "pnpm $(pnpm --version) already installed"
fi fi
# Add pnpm to PATH for this script session
export PATH="$HOME/.local/share/pnpm:$PATH"
export PNPM_HOME="$HOME/.local/share/pnpm"
# Enable and start Docker # Enable and start Docker
log "Ensuring Docker is running..." log "Ensuring Docker is running..."
systemctl enable docker >> "$LOG_FILE" 2>&1 systemctl enable docker >> "$LOG_FILE" 2>&1
@ -100,6 +191,11 @@ if [ ! -f "$SCRIPT_DIR/docker/caddy/Caddyfile" ]; then
X-Content-Type-Options "nosniff" X-Content-Type-Options "nosniff"
# Clickjacking protection # Clickjacking protection
X-Frame-Options "SAMEORIGIN" X-Frame-Options "SAMEORIGIN"
# Allow backend API access (CORS)
Access-Control-Allow-Origin "http://192.168.0.105:5000"
Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Access-Control-Allow-Headers "Origin, Content-Type, Accept, Authorization"
Access-Control-Allow-Credentials "true"
} }
# Log access # Log access
@ -118,29 +214,59 @@ if [ ! -d "/srv/myp-env" ]; then
fi fi
if [ ! -f "/srv/myp-env/github.env" ]; then if [ ! -f "/srv/myp-env/github.env" ]; then
log "Creating GitHub OAuth credentials template..." log "Creating GitHub OAuth credentials file with static backend URL..."
cat > /srv/myp-env/github.env << EOF cat > /srv/myp-env/github.env << EOF
# GitHub OAuth Credentials # GitHub OAuth Credentials
# Replace these with your actual GitHub OAuth app credentials # Replace these with your actual GitHub OAuth app credentials
AUTH_GITHUB_ID=your_github_client_id AUTH_GITHUB_ID=your_github_client_id
AUTH_GITHUB_SECRET=your_github_client_secret AUTH_GITHUB_SECRET=your_github_client_secret
AUTH_SECRET=generate_a_secure_random_string AUTH_SECRET=$(openssl rand -hex 32)
AUTH_TRUST_HOST=true AUTH_TRUST_HOST=true
NEXT_PUBLIC_BACKEND_URL=http://backend:5000 NEXT_PUBLIC_BACKEND_URL=http://192.168.0.105:5000
# Add additional environment variables as needed # Add additional environment variables as needed
EOF EOF
log "ATTENTION: GitHub OAuth credentials file created with placeholder values" log "ATTENTION: GitHub OAuth credentials file created with generated AUTH_SECRET"
log " Backend URL set to http://192.168.0.105:5000"
log " Please edit /srv/myp-env/github.env with your actual GitHub OAuth app credentials" log " Please edit /srv/myp-env/github.env with your actual GitHub OAuth app credentials"
fi fi
# Create a copy of the environment file in the git repository
log "Creating a copy of the environment file for version control..."
if [ ! -f "$SCRIPT_DIR/.env.example" ]; then
cat > "$SCRIPT_DIR/.env.example" << EOF
# GitHub OAuth Credentials Example
# This is a template for the required environment variables
AUTH_GITHUB_ID=your_github_client_id
AUTH_GITHUB_SECRET=your_github_client_secret
AUTH_SECRET=random_string_generated_during_installation
AUTH_TRUST_HOST=true
NEXT_PUBLIC_BACKEND_URL=http://192.168.0.105:5000
# Add additional environment variables as needed
EOF
log "Environment example file created at $SCRIPT_DIR/.env.example"
fi
# Create database directory if it doesn't exist # Create database directory if it doesn't exist
log "Setting up database directory..." log "Setting up database directory..."
mkdir -p /srv/MYP-DB mkdir -p /srv/MYP-DB
# Check if we need to run in development or production mode # Determine if we should run in non-interactive mode
read -p "Do you want to set up the frontend in production mode? (y/n): " production_mode if [ -n "$1" ] && [ "$1" == "--auto-production" ]; then
log "Running in automatic production mode (non-interactive)..."
production_mode="y"
elif [ -n "$1" ] && [ "$1" == "--auto-development" ]; then
log "Running in automatic development mode (non-interactive)..."
production_mode="n"
else
# Interactive mode
log "Running in interactive mode..."
# Check if we need to run in development or production mode
read -p "Do you want to set up the frontend in production mode? (y/n): " production_mode
fi
if [ "$production_mode" = "y" ] || [ "$production_mode" = "Y" ]; then if [ "$production_mode" = "y" ] || [ "$production_mode" = "Y" ]; then
# Production mode - using Docker # Production mode - using Docker
@ -148,7 +274,7 @@ if [ "$production_mode" = "y" ] || [ "$production_mode" = "Y" ]; then
# Create docker-compose file if it doesn't exist # Create docker-compose file if it doesn't exist
if [ ! -f "$SCRIPT_DIR/docker/compose.yml" ]; then if [ ! -f "$SCRIPT_DIR/docker/compose.yml" ]; then
log "Creating docker-compose.yml file..." log "Creating docker-compose.yml file with network configuration..."
cat > "$SCRIPT_DIR/docker/compose.yml" << EOF cat > "$SCRIPT_DIR/docker/compose.yml" << EOF
version: '3.8' version: '3.8'
@ -164,6 +290,7 @@ services:
- /srv/MYP-DB:/app/.next/cache/drizzle - /srv/MYP-DB:/app/.next/cache/drizzle
networks: networks:
- myp-network - myp-network
- backend-network # Network for communicating with backend
caddy: caddy:
image: caddy:2.8 image: caddy:2.8
@ -183,32 +310,134 @@ services:
networks: networks:
myp-network: myp-network:
driver: bridge driver: bridge
backend-network: # Network for connecting to the backend at 192.168.0.105
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/24
gateway: 192.168.0.1
volumes: volumes:
caddy_data: caddy_data:
caddy_config: caddy_config:
EOF EOF
log "docker-compose.yml created" log "docker-compose.yml created with backend network configuration"
fi fi
# Build Docker image # Build Docker image with error handling
log "Building Docker image (this may take a while)..." log "Building Docker image (this may take a while)..."
cd "$SCRIPT_DIR/docker"
if [ -f "build.sh" ]; then # Check available disk space before starting build
bash build.sh >> "$LOG_FILE" 2>&1 available_space=$(df -m / | awk 'NR==2 {print $4}')
log "Docker image built successfully" if [ "$available_space" -lt 2000 ]; then
log "WARNING: Low disk space (${available_space}MB). Docker build may fail."
log "Consider freeing up at least 2GB of space before continuing."
read -p "Continue anyway? (y/n): " continue_build
if [ "$continue_build" != "y" ] && [ "$continue_build" != "Y" ]; then
log "Docker build aborted due to low disk space."
exit 1
fi
fi
# Test Docker is working properly
if ! docker info >> "$LOG_FILE" 2>&1; then
log "ERROR: Docker is not running or the current user doesn't have permission to use Docker."
log "Try running 'sudo systemctl restart docker' or add your user to the docker group with:"
log "'sudo usermod -aG docker $USER' and then log out and back in."
exit 1
fi
# Try to use existing build script if available
if [ -f "$SCRIPT_DIR/docker/build.sh" ]; then
log "Using existing build script..."
cd "$SCRIPT_DIR/docker"
if bash build.sh >> "$LOG_FILE" 2>&1; then
log "Docker image built successfully using build.sh"
else
log "ERROR: Docker build failed using build.sh script"
log "Attempting direct build as fallback..."
cd "$SCRIPT_DIR"
if docker build -t myp-rp:latest . >> "$LOG_FILE" 2>&1; then
log "Docker image built successfully using direct method"
else
log "ERROR: Docker build failed. Check the log at $LOG_FILE for details."
exit 1
fi
fi
else else
# Direct build method
log "Building Docker image manually..." log "Building Docker image manually..."
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
docker build -t myp-rp:latest . >> "$LOG_FILE" 2>&1 if docker build -t myp-rp:latest . >> "$LOG_FILE" 2>&1; then
log "Docker image built" log "Docker image built successfully"
else
log "ERROR: Docker build failed. Check the log at $LOG_FILE for details."
exit 1
fi
fi fi
# Start the application # Verify the image was created
if docker image inspect myp-rp:latest >> /dev/null 2>&1; then
log "Verified: Docker image myp-rp:latest exists"
else
log "ERROR: Docker image myp-rp:latest does not exist after build"
exit 1
fi
# Start the application with error handling
log "Starting the application using Docker Compose..." log "Starting the application using Docker Compose..."
cd "$SCRIPT_DIR/docker" cd "$SCRIPT_DIR/docker"
docker-compose -f compose.yml up -d
# Ensure we have the compose file
if [ ! -f "compose.yml" ]; then
log "ERROR: compose.yml not found in $(pwd)"
log "Looking for compose file in alternative locations..."
find "$SCRIPT_DIR" -name "compose.yml" -o -name "docker-compose.yml" 2>/dev/null | while read compose_file; do
log "Found compose file at: $compose_file"
done
exit 1
fi
# Test docker-compose to make sure it's working
if ! docker-compose version >> "$LOG_FILE" 2>&1; then
log "WARNING: docker-compose command failed. Trying with docker compose (with space)..."
if ! docker compose version >> "$LOG_FILE" 2>&1; then
log "ERROR: Both docker-compose and docker compose commands failed."
log "Please check Docker and Docker Compose installation."
exit 1
else
# Use docker compose instead
log "Using docker compose (with space) command..."
if docker compose -f compose.yml up -d >> "$LOG_FILE" 2>&1; then
log "Application started successfully with docker compose!"
else
log "ERROR: Failed to start application with docker compose."
log "Check logs with: docker compose -f compose.yml logs"
exit 1
fi
fi
else
# Use docker-compose
if docker-compose -f compose.yml up -d >> "$LOG_FILE" 2>&1; then
log "Application started successfully with docker-compose!"
else
log "ERROR: Failed to start application with docker-compose."
log "Check logs with: docker-compose -f compose.yml logs"
exit 1
fi
fi
# Verify containers are running
log "Verifying containers are running..."
sleep 5 # Give containers a moment to start
running_containers=$(docker ps --format '{{.Names}}' | grep -c "myp\|frontend\|caddy" || echo "0")
if [ "$running_containers" -gt 0 ]; then
log "Detected $running_containers running containers related to MYP."
else
log "WARNING: No running containers detected for MYP frontend."
log "Check container status with: docker ps -a"
fi
log "Frontend installed and running in production mode!" log "Frontend installed and running in production mode!"
log "The application should be accessible at http://localhost" log "The application should be accessible at http://localhost"
@ -221,29 +450,69 @@ else
# Install dependencies # Install dependencies
log "Installing project dependencies with pnpm..." log "Installing project dependencies with pnpm..."
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
# Source pnpm if needed
source ~/.bashrc
export PATH="$HOME/.local/share/pnpm:$PATH"
pnpm install >> "$LOG_FILE" 2>&1 # Make sure we're in the right directory and pnpm is available
log "Dependencies installed" log "Current directory: $(pwd)"
log "Checking pnpm path: $(which pnpm 2>/dev/null || echo 'pnpm not found in PATH')"
# Install dependencies with retry mechanism
max_attempts=3
attempt=1
success=false
while [ $attempt -le $max_attempts ] && [ "$success" = "false" ]; do
log "Attempt $attempt of $max_attempts to install dependencies..."
if command_exists pnpm; then
if pnpm install >> "$LOG_FILE" 2>&1; then
log "Dependencies installed successfully!"
success=true
else
log "WARNING: pnpm install failed on attempt $attempt"
fi
else
log "ERROR: pnpm not found in PATH. Trying to fix..."
export PATH="$PATH:$HOME/.local/share/pnpm:$HOME/.pnpm:$(npm global bin)"
if ! command_exists pnpm; then
log "Attempting to reinstall pnpm..."
npm install -g pnpm >> "$LOG_FILE" 2>&1
fi
fi
attempt=$((attempt+1))
# If we've failed but have more attempts to go, wait a bit before trying again
if [ "$success" = "false" ] && [ $attempt -le $max_attempts ]; then
log "Waiting 5 seconds before retry..."
sleep 5
fi
done
if [ "$success" = "false" ]; then
log "ERROR: Failed to install dependencies after $max_attempts attempts."
log "Please check the log file at $LOG_FILE for details."
log "You may need to run 'pnpm install' manually in $SCRIPT_DIR"
else
log "Dependencies successfully installed."
fi
# Create .env.local file for development # Create .env.local file for development
if [ ! -f "$SCRIPT_DIR/.env.local" ]; then if [ ! -f "$SCRIPT_DIR/.env.local" ]; then
log "Creating development environment file..." log "Creating development environment file with static backend URL..."
cat > "$SCRIPT_DIR/.env.local" << EOF cat > "$SCRIPT_DIR/.env.local" << EOF
# Development Environment Variables # Development Environment Variables
# GitHub OAuth # GitHub OAuth
AUTH_GITHUB_ID=your_github_client_id AUTH_GITHUB_ID=your_github_client_id
AUTH_GITHUB_SECRET=your_github_client_secret AUTH_GITHUB_SECRET=your_github_client_secret
AUTH_SECRET=generate_a_secure_random_string AUTH_SECRET=$(openssl rand -hex 32)
AUTH_TRUST_HOST=true AUTH_TRUST_HOST=true
# Backend URL # Backend URL - Static IP for backend
NEXT_PUBLIC_BACKEND_URL=http://localhost:5000 NEXT_PUBLIC_BACKEND_URL=http://192.168.0.105:5000
EOF EOF
log "ATTENTION: Development environment file created with placeholder values" log "ATTENTION: Development environment file created with generated AUTH_SECRET"
log " Backend URL set to http://192.168.0.105:5000"
log " Please edit $SCRIPT_DIR/.env.local with your actual GitHub OAuth credentials" log " Please edit $SCRIPT_DIR/.env.local with your actual GitHub OAuth credentials"
fi fi
@ -283,4 +552,24 @@ EOF
fi fi
log "For issues, check the log file at: $LOG_FILE" log "For issues, check the log file at: $LOG_FILE"
log "Done!" # Add helpful commands for post-installation management
log "===== Post-Installation Information ====="
log "Here are some helpful commands for managing your installation:"
log ""
log "System management:"
log " - Check system status: htop, free -h, df -h"
log " - Network status: ip a, netstat -tulpn, ss -tulpn"
log " - View logs: tail -f /var/log/syslog, journalctl -f"
log ""
log "Docker management:"
log " - List containers: docker ps -a"
log " - Container logs: docker logs <container_id>"
log " - Stop containers: docker-compose -f $SCRIPT_DIR/docker/compose.yml down"
log " - Start containers: docker-compose -f $SCRIPT_DIR/docker/compose.yml up -d"
log " - Restart containers: docker-compose -f $SCRIPT_DIR/docker/compose.yml restart"
log ""
log "Backend connection:"
log " - Test backend connection: curl -I http://192.168.0.105:5000/api/test"
log " - Check backend accessibility: ping 192.168.0.105"
log ""
log "Installation Complete! 🎉"