From f26759ec2425d16ac1428190ec6d82d4445d11de Mon Sep 17 00:00:00 2001 From: Till Tomczak Date: Fri, 28 Mar 2025 10:06:36 +0100 Subject: [PATCH] =?UTF-8?q?Verbessere=20Frontend-Installationsskript=20f?= =?UTF-8?q?=C3=BCr=20robustere=20Installation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Füge --no-sudo Option für Installation ohne Root-Rechte hinzu - Verbessere Node.js Installation mit NVM Fallback für Benutzer-Level - Implementiere mehrere Fallback-Mechanismen für pnpm-Installation - Füge automatische PATH-Konfiguration in .bashrc hinzu - Erstelle zusätzliches Start-Skript für einfache manuelle Ausführung - Verbessere Fehlerbehandlung und Diagnoseinformationen - Füge expliziten Abhängigkeitsinstallationsschritt hinzu 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../reservation-platform/install-frontend.sh | 383 ++++++++++++++---- 1 file changed, 310 insertions(+), 73 deletions(-) diff --git a/packages/reservation-platform/install-frontend.sh b/packages/reservation-platform/install-frontend.sh index 6bfcd46..2ab07ca 100755 --- a/packages/reservation-platform/install-frontend.sh +++ b/packages/reservation-platform/install-frontend.sh @@ -51,6 +51,8 @@ usage() { echo "OPTIONS:" echo " --auto-production Non-interactive installation in production mode" echo " --auto-development Non-interactive installation in development mode" + echo " --auto-test Non-interactive installation in test environment mode" + echo " --no-sudo Run without sudo privileges (limited functionality)" echo " --help Show this help message" echo "" echo "Example:" @@ -63,6 +65,14 @@ if [ "$1" == "--help" ]; then usage fi +# Check if we're running with no-sudo option +if [[ "$*" == *"--no-sudo"* ]]; then + NO_SUDO=true + log "Running in non-sudo mode with limited functionality" +else + NO_SUDO=false +fi + log "===== Starting MYP Frontend Installation =====" log "Installation directory: $SCRIPT_DIR" @@ -74,95 +84,202 @@ 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 -if [ "$EUID" -ne 0 ]; then - log "ERROR: This script must be run as root" +# Check for root privileges unless --no-sudo was specified +if [ "$EUID" -ne 0 ] && [ "$NO_SUDO" = "false" ]; then + log_error "This script must be run as root" + log_warning "Use --no-sudo option for limited functionality installation" + log_warning "Example: $0 --auto-development --no-sudo" exit 1 fi -# System update -log "Updating system packages..." -apt update -y >> "$LOG_FILE" 2>&1 -apt upgrade -y >> "$LOG_FILE" 2>&1 +# System update (only if running as root) +if [ "$NO_SUDO" = "false" ]; then + log "Updating system packages..." + apt update -y >> "$LOG_FILE" 2>&1 || log_error "Failed to update apt repositories" + apt upgrade -y >> "$LOG_FILE" 2>&1 || log_warning "Failed to upgrade packages, continuing anyway" -# Install required packages -log "Installing essential system packages and network tools..." -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 bash-completion \ - make build-essential libssl-dev zlib1g-dev >> "$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 + # Install required packages + log "Installing essential system packages and network tools..." + 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 bash-completion \ + make build-essential libssl-dev zlib1g-dev >> "$LOG_FILE" 2>&1 || log_warning "Some packages failed to install" else - log "Docker already installed: $(docker --version)" - log "Docker Compose already installed: $(docker-compose --version)" + log "Skipping system package installation (non-sudo mode)..." + # Check if critical tools are available + for cmd in curl git wget node npm; do + if ! command_exists "$cmd"; then + log_warning "$cmd is not installed. This might cause problems." + log_warning "Install with: sudo apt install $cmd" + fi + done +fi + +# Install Docker using the official Docker repository (skip in non-sudo mode) +if [ "$NO_SUDO" = "false" ]; then + 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 +else + log "Skipping Docker installation (non-sudo mode)..." + # Check if Docker is available + if command_exists docker; then + log "Docker already installed: $(docker --version)" + if command_exists docker-compose; then + log "Docker Compose already installed: $(docker-compose --version)" + else + log_warning "Docker Compose not installed. Some features may not work." + fi + else + log_warning "Docker not installed. Container features will not be available." + log_warning "Install Docker with: curl -fsSL https://get.docker.com | sudo sh" + fi fi # 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 2>/dev/null | cut -d. -f1 | tr -d 'v')" -lt 20 2>/dev/null ]; then log "Installing Node.js 20.x..." - curl -fsSL https://deb.nodesource.com/setup_20.x | bash - >> "$LOG_FILE" 2>&1 - apt install -y nodejs >> "$LOG_FILE" 2>&1 - log "Node.js $(node -v) installed" + if [ "$NO_SUDO" = "false" ]; then + # System-wide installation + curl -fsSL https://deb.nodesource.com/setup_20.x | bash - >> "$LOG_FILE" 2>&1 + apt install -y nodejs >> "$LOG_FILE" 2>&1 + else + # User-level installation using nvm (Node Version Manager) + log "Installing Node.js using NVM (user-level)..." + # Install NVM if not already installed + if [ ! -d "$HOME/.nvm" ]; then + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash >> "$LOG_FILE" 2>&1 + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm + else + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm + fi + + # Install Node.js 20 using NVM + nvm install 20 >> "$LOG_FILE" 2>&1 + nvm use 20 >> "$LOG_FILE" 2>&1 + fi + + # Verify installation + if command_exists node; then + log_success "Node.js $(node -v) installed" + else + log_error "Node.js installation failed" + exit 1 + fi else log "Node.js $(node -v) already installed" fi -# Install pnpm +# Install pnpm - with more robust installation approaches if ! command_exists pnpm; then log "Installing pnpm package manager..." - # Install pnpm globally to avoid home directory permission issues - npm install -g pnpm >> "$LOG_FILE" 2>&1 - # Fallback method in case npm method fails + + # First, try npm installation + if command_exists npm; then + npm install -g pnpm >> "$LOG_FILE" 2>&1 + fi + + # Check if npm method succeeded 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" + + # Update PATH to include pnpm + export PNPM_HOME="${PNPM_HOME:-$HOME/.local/share/pnpm}" + export PATH="$PNPM_HOME:$PATH" + + # Add to ~/.bashrc for future use if it's not already there + if ! grep -q "PNPM_HOME" "$HOME/.bashrc" 2>/dev/null; then + echo 'export PNPM_HOME="$HOME/.local/share/pnpm"' >> "$HOME/.bashrc" + echo 'export PATH="$PNPM_HOME:$PATH"' >> "$HOME/.bashrc" + log "Added pnpm to PATH in .bashrc for future terminal sessions" + fi + + # Try standalone installation if previous methods failed + if ! command_exists pnpm; then + log "Trying standalone installation of pnpm..." + # Create bin directory if it doesn't exist + mkdir -p "$HOME/.local/bin" + + # Download standalone PNPM + curl -fsSL https://github.com/pnpm/pnpm/releases/download/v9.12.1/pnpm-linuxstatic-x64 -o "$HOME/.local/bin/pnpm" >> "$LOG_FILE" 2>&1 + chmod +x "$HOME/.local/bin/pnpm" + + # Add to PATH + export PATH="$HOME/.local/bin:$PATH" + + # Add to ~/.bashrc if not already there + if ! grep -q "/.local/bin" "$HOME/.bashrc" 2>/dev/null; then + echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" + fi + fi + fi + + # Verify installation + if command_exists pnpm; then + log_success "pnpm $(pnpm --version) installed" + else + log_error "pnpm installation failed" + log_error "Please try to install manually with: npm install -g pnpm" + exit 1 fi - log "pnpm $(pnpm --version) installed" else log "pnpm $(pnpm --version) already installed" fi -# Add pnpm to PATH for this script session -export PATH="$HOME/.local/share/pnpm:$PATH" -export PNPM_HOME="$HOME/.local/share/pnpm" +# Make sure pnpm is in PATH for this script session +export PNPM_HOME="${PNPM_HOME:-$HOME/.local/share/pnpm}" +export PATH="$PNPM_HOME:$HOME/.local/bin:$PATH" -# Enable and start Docker -log "Ensuring Docker is running..." -systemctl enable docker >> "$LOG_FILE" 2>&1 -systemctl start docker >> "$LOG_FILE" 2>&1 +# Create a .pnpmrc file to ensure proper configuration +echo "standalone=true" > "$HOME/.pnpmrc" 2>/dev/null || log_warning "Could not create .pnpmrc file" -# Configure Docker for multi-architecture builds -log "Setting up Docker for multi-architecture builds..." -if ! docker buildx ls | grep -q "default"; then - docker buildx create --name mybuilder --use >> "$LOG_FILE" 2>&1 - log "Docker buildx configured" +# Enable and start Docker (skip in non-sudo mode) +if [ "$NO_SUDO" = "false" ]; then + log "Ensuring Docker is running..." + systemctl enable docker >> "$LOG_FILE" 2>&1 || log_warning "Failed to enable Docker service" + systemctl start docker >> "$LOG_FILE" 2>&1 || log_warning "Failed to start Docker service" + + # Configure Docker for multi-architecture builds + log "Setting up Docker for multi-architecture builds..." + if command_exists docker; then + if ! docker buildx ls 2>/dev/null | grep -q "default"; then + docker buildx create --name mybuilder --use >> "$LOG_FILE" 2>&1 || log_warning "Failed to configure Docker buildx" + log "Docker buildx configured" + else + log "Docker buildx already configured" + fi + else + log_warning "Docker not available, skipping buildx configuration" + fi else - log "Docker buildx already configured" + log "Skipping Docker service configuration (non-sudo mode)..." fi # Make sure we have the docker directory for configuration @@ -1154,29 +1271,103 @@ EOF log "Cypress example test file created" fi - # Create systemd service for development mode - log "Creating systemd service for development mode..." - cat > /etc/systemd/system/myp-frontend-dev.service << EOF + # Create systemd service for development mode (if running as root) + if [ "$NO_SUDO" = "false" ]; then + log "Creating systemd service for development mode..." + cat > /etc/systemd/system/myp-frontend-dev.service << EOF [Unit] Description=MYP Frontend Development Service After=network.target [Service] Type=simple -User=$SUDO_USER +User=${SUDO_USER:-$USER} WorkingDirectory=$SCRIPT_DIR -ExecStart=$(which pnpm) dev +ExecStart=$(which pnpm 2>/dev/null || echo "/usr/bin/pnpm") dev Restart=always RestartSec=10 -Environment=PATH=/usr/bin:/usr/local/bin:$HOME/.local/share/pnpm +Environment=PATH=/usr/bin:/usr/local/bin:${HOME}/.local/share/pnpm:${HOME}/.local/bin:${HOME}/.nvm/versions/node/$(node -v 2>/dev/null || echo "v20.0.0")/bin [Install] WantedBy=multi-user.target EOF - - # Reload systemd and enable the service - systemctl daemon-reload - systemctl enable myp-frontend-dev.service + + # Reload systemd and enable the service + systemctl daemon-reload + systemctl enable myp-frontend-dev.service + else + # Create a user-level service file for systemd if possible + log "Creating user-level service file for development mode..." + mkdir -p "$HOME/.config/systemd/user" + cat > "$HOME/.config/systemd/user/myp-frontend-dev.service" << EOF +[Unit] +Description=MYP Frontend Development Service +After=network.target + +[Service] +Type=simple +WorkingDirectory=$SCRIPT_DIR +ExecStart=$(which pnpm 2>/dev/null || echo "${HOME}/.local/bin/pnpm") dev +Restart=always +RestartSec=10 +Environment=PATH=/usr/bin:/usr/local/bin:${HOME}/.local/share/pnpm:${HOME}/.local/bin:${HOME}/.nvm/versions/node/$(node -v 2>/dev/null || echo "v20.0.0")/bin + +[Install] +WantedBy=default.target +EOF + + # Create a convenient start script + cat > "$SCRIPT_DIR/start-dev-server.sh" << 'EOF' +#!/bin/bash + +# Script to start the development server + +# Get the directory where this script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Source environment setup +if [ -f "$HOME/.bashrc" ]; then + source "$HOME/.bashrc" +fi + +# Ensure NVM is loaded if available +if [ -d "$HOME/.nvm" ]; then + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" + nvm use 20 &>/dev/null || true +fi + +# Add pnpm to PATH if not already there +if [ -d "$HOME/.local/share/pnpm" ]; then + export PNPM_HOME="$HOME/.local/share/pnpm" + export PATH="$PNPM_HOME:$PATH" +fi + +if [ -d "$HOME/.local/bin" ]; then + export PATH="$HOME/.local/bin:$PATH" +fi + +# Change to the script directory +cd "$SCRIPT_DIR" + +# Check for pnpm +if ! command -v pnpm &>/dev/null; then + echo "ERROR: pnpm not found in PATH" + echo "Try installing it with: npm install -g pnpm" + exit 1 +fi + +# Start the development server +echo "Starting MYP frontend development server..." +pnpm dev +EOF + chmod +x "$SCRIPT_DIR/start-dev-server.sh" + + log_success "Created user-level service and start script" + log "To start the development server manually, run: $SCRIPT_DIR/start-dev-server.sh" + log "To use systemd user service: systemctl --user enable --now myp-frontend-dev.service" + fi log "Installation complete!" log "" @@ -1189,6 +1380,46 @@ EOF log "The application should be accessible at http://localhost:3000 when running" fi +# Perform one-time installation of dependencies using pnpm +log "Installing project dependencies with pnpm..." +# First check that pnpm is definitely in PATH +if ! command_exists pnpm; then + log_error "pnpm still not found in PATH despite installation attempts" + log_error "PATH: $PATH" + log_error "Checking for pnpm installation locations..." + + for pnpm_location in "$HOME/.local/bin/pnpm" "$HOME/.local/share/pnpm/pnpm" "/usr/local/bin/pnpm" "/usr/bin/pnpm"; do + if [ -f "$pnpm_location" ]; then + log_warning "pnpm found at $pnpm_location but not in PATH" + log_warning "Using absolute path to install dependencies..." + + # Use absolute path to pnpm + cd "$SCRIPT_DIR" + "$pnpm_location" install >> "$LOG_FILE" 2>&1 + PNPM_STATUS=$? + + if [ $PNPM_STATUS -eq 0 ]; then + log_success "Project dependencies installed using absolute pnpm path" + else + log_error "Failed to install dependencies using absolute pnpm path" + fi + + break + fi + done +else + # pnpm is in PATH + cd "$SCRIPT_DIR" + pnpm install >> "$LOG_FILE" 2>&1 + PNPM_STATUS=$? + + if [ $PNPM_STATUS -eq 0 ]; then + log_success "Project dependencies installed" + else + log_error "Failed to install dependencies" + fi +fi + log "For issues, check the log file at: $LOG_FILE" # Add helpful commands for post-installation management log "===== Post-Installation Information =====" @@ -1199,6 +1430,12 @@ 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 "Frontend development:" +log " - Start development server: $SCRIPT_DIR/start-dev-server.sh" +log " - Install missing dependencies: cd $SCRIPT_DIR && pnpm install" +log " - Lint code: cd $SCRIPT_DIR && pnpm lint" +log " - Format code: cd $SCRIPT_DIR && npx @biomejs/biome format --write ./src" +log "" log "Docker management:" log " - List containers: docker ps -a" log " - Container logs: docker logs "