diff --git a/backend/install-backend.sh b/backend/install-backend.sh new file mode 100755 index 0000000..ea28359 --- /dev/null +++ b/backend/install-backend.sh @@ -0,0 +1,162 @@ +#!/bin/bash + +# MYP Backend Installation Script for Debian +# This script installs and configures the MYP backend on a Debian-based system + +set -e # Exit immediately if a command exits with non-zero status +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +LOG_FILE="$SCRIPT_DIR/backend-install.log" + +# Function for logging with timestamps +log() { + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo -e "[$timestamp] $1" | tee -a "$LOG_FILE" +} + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Clear log file +> "$LOG_FILE" + +log "===== Starting MYP Backend Installation =====" +log "Installation directory: $SCRIPT_DIR" + +# Check for root privileges +if [ "$EUID" -ne 0 ]; then + log "ERROR: This script must be run as root" + exit 1 +fi + +# System update +log "Updating system packages..." +apt update -y >> "$LOG_FILE" 2>&1 +apt upgrade -y >> "$LOG_FILE" 2>&1 + +# Install required packages +log "Installing required packages..." +apt install -y python3 python3-venv python3-pip sqlite3 >> "$LOG_FILE" 2>&1 + +# Create Python virtual environment +log "Creating Python virtual environment..." +cd "$SCRIPT_DIR" +if [ -d "venv" ]; then + log "Found existing virtual environment, removing..." + rm -rf venv +fi + +python3 -m venv venv >> "$LOG_FILE" 2>&1 +source venv/bin/activate +log "Upgrading pip..." +pip install --upgrade pip >> "$LOG_FILE" 2>&1 + +# Install Python dependencies +log "Installing Python dependencies..." +if [ -f "requirements.txt" ]; then + pip install -r requirements.txt >> "$LOG_FILE" 2>&1 +else + log "ERROR: requirements.txt not found" + exit 1 +fi + +# Create database directory if it doesn't exist +log "Setting up database directories..." +mkdir -p instance/backups + +# Check if .env file exists +if [ ! -f "$SCRIPT_DIR/.env" ]; then + log "Creating .env file template (IMPORTANT: Edit with your configuration)..." + cat > "$SCRIPT_DIR/.env" << EOF +# MYP Backend Environment Configuration +# IMPORTANT: Replace these values with your actual configuration! + +SECRET_KEY=generate_a_secure_random_key +DATABASE_PATH=instance/myp_backend.db + +# Tapo P115 Smart Plug credentials +TAPO_USERNAME=your_tapo_email +TAPO_PASSWORD=your_tapo_password + +# Printer to Smart Plug mapping (JSON format) +# Format: {"Printer Name": "192.168.x.x", "Another Printer": "192.168.x.y"} +PRINTERS={"Example Printer": "192.168.1.100"} +EOF + log "ATTENTION: .env file has been created with placeholder values" + log " Please edit .env with your actual configuration before continuing" + read -p "Press Enter to continue after editing .env..." +fi + +# Initialize the database +log "Initializing database..." +if [ -f "$SCRIPT_DIR/development/initialize_myp_database.sh" ]; then + bash "$SCRIPT_DIR/development/initialize_myp_database.sh" >> "$LOG_FILE" 2>&1 + log "Database initialized successfully" +else + log "WARNING: initialize_myp_database.sh not found, manual setup may be required" + # Create empty database + touch instance/myp_backend.db + log "Created empty database file" +fi + +# Setup cron job +log "Setting up cron job for maintenance tasks..." +CURRENT_USER=$(who am i | awk '{print $1}') +CRON_JOB="*/5 * * * * cd $SCRIPT_DIR && source venv/bin/activate && flask check-jobs" + +# Create a temporary file with the current crontab plus our new job +TEMP_CRON=$(mktemp) +crontab -l 2>/dev/null | grep -v "$SCRIPT_DIR.*check-jobs" > "$TEMP_CRON" || true +echo "$CRON_JOB" >> "$TEMP_CRON" +crontab "$TEMP_CRON" +rm "$TEMP_CRON" + +log "Cron job installed. Maintenance tasks will run every 5 minutes" + +# Test the application +log "Testing backend application..." +source venv/bin/activate +cd "$SCRIPT_DIR" +if python -c "import app" 2>> "$LOG_FILE"; then + log "Import test successful" +else + log "WARNING: Import test failed, check the log file for details" +fi + +# Generate systemd service file +log "Creating systemd service..." +cat > /etc/systemd/system/myp-backend.service << EOF +[Unit] +Description=MYP Backend Service +After=network.target + +[Service] +User=$CURRENT_USER +WorkingDirectory=$SCRIPT_DIR +ExecStart=$SCRIPT_DIR/venv/bin/gunicorn --bind 0.0.0.0:5000 app:app +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target +EOF + +# Reload systemd and enable the service +systemctl daemon-reload +systemctl enable myp-backend.service + +log "Installation complete!" +log "" +log "To start the backend service, run: systemctl start myp-backend" +log "To check service status, run: systemctl status myp-backend" +log "To view logs, run: journalctl -u myp-backend -f" +log "" +log "Configuration file is at: $SCRIPT_DIR/.env" +log "Make sure to configure your Tapo smart plug credentials and printer mapping in this file" +log "" +log "For development mode, run: cd $SCRIPT_DIR && source venv/bin/activate && python app.py" +log "" +log "To run tests, use: cd $SCRIPT_DIR && source venv/bin/activate && python -m unittest development/tests/tests.py" +log "" +log "For issues, check the log file at: $LOG_FILE" \ No newline at end of file diff --git a/packages/reservation-platform/install-frontend.sh b/packages/reservation-platform/install-frontend.sh new file mode 100755 index 0000000..7aa1a0a --- /dev/null +++ b/packages/reservation-platform/install-frontend.sh @@ -0,0 +1,286 @@ +#!/bin/bash + +# MYP Frontend Installation Script for Debian +# This script installs and configures the MYP frontend reservation platform + +set -e # Exit immediately if a command exits with non-zero status +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +LOG_FILE="$SCRIPT_DIR/frontend-install.log" + +# Function for logging with timestamps +log() { + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo -e "[$timestamp] $1" | tee -a "$LOG_FILE" +} + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Clear log file +> "$LOG_FILE" + +log "===== Starting MYP Frontend Installation =====" +log "Installation directory: $SCRIPT_DIR" + +# Check for root privileges +if [ "$EUID" -ne 0 ]; then + log "ERROR: This script must be run as root" + exit 1 +fi + +# System update +log "Updating system packages..." +apt update -y >> "$LOG_FILE" 2>&1 +apt upgrade -y >> "$LOG_FILE" 2>&1 + +# Install required packages +log "Installing required system packages..." +apt install -y curl git docker.io docker-compose xz-utils sqlite3 >> "$LOG_FILE" 2>&1 + +# Install Node.js 20.x +if ! command_exists node || [ $(node -v | cut -d. -f1 | tr -d 'v') -lt 20 ]; 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" +else + log "Node.js $(node -v) already installed" +fi + +# Install pnpm +if ! command_exists pnpm; then + log "Installing pnpm package manager..." + curl -fsSL https://get.pnpm.io/install.sh | sh - >> "$LOG_FILE" 2>&1 + source ~/.bashrc + log "pnpm $(pnpm --version) installed" +else + log "pnpm $(pnpm --version) already installed" +fi + +# Enable and start Docker +log "Ensuring Docker is running..." +systemctl enable docker >> "$LOG_FILE" 2>&1 +systemctl start docker >> "$LOG_FILE" 2>&1 + +# 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" +else + log "Docker buildx already configured" +fi + +# Make sure we have the docker directory for configuration +mkdir -p "$SCRIPT_DIR/docker/caddy" + +# Check if Caddyfile exists +if [ ! -f "$SCRIPT_DIR/docker/caddy/Caddyfile" ]; then + log "Creating Caddyfile template..." + cat > "$SCRIPT_DIR/docker/caddy/Caddyfile" << EOF +# Caddyfile for MYP Frontend +# Replace example.com with your actual domain + +:80 { + # Automatic HTTPS will be enabled if you use a domain name + # For local development or internal network, HTTP is fine + + # Reverse proxy to frontend app + reverse_proxy frontend:3000 + + # Basic headers for security + header { + # Enable HSTS + Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" + # Enable XSS protection + X-XSS-Protection "1; mode=block" + # Prevent content type sniffing + X-Content-Type-Options "nosniff" + # Clickjacking protection + X-Frame-Options "SAMEORIGIN" + } + + # Log access + log { + output file /var/log/caddy/access.log + } +} +EOF + log "Caddyfile created. Edit it to configure your domain if needed." +fi + +# Create GitHub OAuth credentials file +if [ ! -d "/srv/myp-env" ]; then + log "Creating directory for environment variables..." + mkdir -p /srv/myp-env +fi + +if [ ! -f "/srv/myp-env/github.env" ]; then + log "Creating GitHub OAuth credentials template..." + cat > /srv/myp-env/github.env << EOF +# GitHub OAuth Credentials +# Replace these with your actual GitHub OAuth app credentials + +AUTH_GITHUB_ID=your_github_client_id +AUTH_GITHUB_SECRET=your_github_client_secret +AUTH_SECRET=generate_a_secure_random_string +AUTH_TRUST_HOST=true +NEXT_PUBLIC_BACKEND_URL=http://backend:5000 + +# Add additional environment variables as needed +EOF + log "ATTENTION: GitHub OAuth credentials file created with placeholder values" + log " Please edit /srv/myp-env/github.env with your actual GitHub OAuth app credentials" +fi + +# Create database directory if it doesn't exist +log "Setting up database directory..." +mkdir -p /srv/MYP-DB + +# 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 + +if [ "$production_mode" = "y" ] || [ "$production_mode" = "Y" ]; then + # Production mode - using Docker + log "Setting up in production mode using Docker..." + + # Create docker-compose file if it doesn't exist + if [ ! -f "$SCRIPT_DIR/docker/compose.yml" ]; then + log "Creating docker-compose.yml file..." + cat > "$SCRIPT_DIR/docker/compose.yml" << EOF +version: '3.8' + +services: + frontend: + image: myp-rp:latest + restart: unless-stopped + environment: + - NODE_ENV=production + env_file: + - /srv/myp-env/github.env + volumes: + - /srv/MYP-DB:/app/.next/cache/drizzle + networks: + - myp-network + + caddy: + image: caddy:2.8 + restart: unless-stopped + ports: + - "80:80" + - "443:443" + volumes: + - ./caddy/Caddyfile:/etc/caddy/Caddyfile + - caddy_data:/data + - caddy_config:/config + networks: + - myp-network + depends_on: + - frontend + +networks: + myp-network: + driver: bridge + +volumes: + caddy_data: + caddy_config: +EOF + log "docker-compose.yml created" + fi + + # Build Docker image + log "Building Docker image (this may take a while)..." + cd "$SCRIPT_DIR/docker" + + if [ -f "build.sh" ]; then + bash build.sh >> "$LOG_FILE" 2>&1 + log "Docker image built successfully" + else + log "Building Docker image manually..." + cd "$SCRIPT_DIR" + docker build -t myp-rp:latest . >> "$LOG_FILE" 2>&1 + log "Docker image built" + fi + + # Start the application + log "Starting the application using Docker Compose..." + cd "$SCRIPT_DIR/docker" + docker-compose -f compose.yml up -d + + log "Frontend installed and running in production mode!" + log "The application should be accessible at http://localhost" + log "Check logs with: docker-compose -f $SCRIPT_DIR/docker/compose.yml logs -f" + +else + # Development mode + log "Setting up in development mode..." + + # Install dependencies + log "Installing project dependencies with pnpm..." + cd "$SCRIPT_DIR" + # Source pnpm if needed + source ~/.bashrc + export PATH="$HOME/.local/share/pnpm:$PATH" + + pnpm install >> "$LOG_FILE" 2>&1 + log "Dependencies installed" + + # Create .env.local file for development + if [ ! -f "$SCRIPT_DIR/.env.local" ]; then + log "Creating development environment file..." + cat > "$SCRIPT_DIR/.env.local" << EOF +# Development Environment Variables + +# GitHub OAuth +AUTH_GITHUB_ID=your_github_client_id +AUTH_GITHUB_SECRET=your_github_client_secret +AUTH_SECRET=generate_a_secure_random_string +AUTH_TRUST_HOST=true + +# Backend URL +NEXT_PUBLIC_BACKEND_URL=http://localhost:5000 +EOF + log "ATTENTION: Development environment file created with placeholder values" + log " Please edit $SCRIPT_DIR/.env.local with your actual GitHub OAuth credentials" + fi + + # Create systemd service for development mode + 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 +WorkingDirectory=$SCRIPT_DIR +ExecStart=$(which pnpm) dev +Restart=always +RestartSec=10 +Environment=PATH=/usr/bin:/usr/local/bin:$HOME/.local/share/pnpm + +[Install] +WantedBy=multi-user.target +EOF + + # Reload systemd and enable the service + systemctl daemon-reload + systemctl enable myp-frontend-dev.service + + log "Installation complete!" + log "" + log "To start the frontend development service, run: systemctl start myp-frontend-dev" + log "To check service status, run: systemctl status myp-frontend-dev" + log "To view logs, run: journalctl -u myp-frontend-dev -f" + log "" + log "For manual development startup, run: cd $SCRIPT_DIR && pnpm dev" + log "" + log "The application should be accessible at http://localhost:3000 when running" +fi + +log "For issues, check the log file at: $LOG_FILE" +log "Done!" \ No newline at end of file