#!/bin/bash # SKE Digital Signage - Management Script set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Helper functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if Docker is available check_docker() { if ! command -v docker &> /dev/null; then log_error "Docker is not installed or not in PATH" exit 1 fi if ! command -v docker-compose &> /dev/null; then log_error "Docker Compose is not installed or not in PATH" exit 1 fi } # Check if Python environment is set up check_python() { if [ ! -d "venv" ]; then log_error "Python virtual environment not found. Run './manage.sh setup' first." exit 1 fi } # Development setup setup_dev() { log_info "Setting up development environment..." # Create virtual environment if [ ! -d "venv" ]; then log_info "Creating Python virtual environment..." python3 -m venv venv fi # Activate virtual environment and install dependencies log_info "Installing Python dependencies..." source venv/bin/activate pip install -r requirements.txt # Create environment file if it doesn't exist if [ ! -f ".env" ]; then log_info "Creating .env file from template..." cp .env.example .env log_warning "Please edit .env file with your configuration" fi # Create necessary directories mkdir -p instance static/uploads static/assets logs log_success "Development environment setup complete!" log_info "Run './manage.sh dev' to start development server" } # Run development server run_dev() { check_python log_info "Starting development server..." source venv/bin/activate export FLASK_CONFIG=development export FLASK_DEBUG=true python main.py } # Build Docker image build_docker() { check_docker log_info "Building Docker image..." docker build -t ske-signage:2.0.0 . log_success "Docker image built successfully!" } # Start production with Docker Compose start_prod() { check_docker if [ ! -f ".env" ]; then log_warning "No .env file found, creating from template..." cp .env.example .env log_warning "Please edit .env file with production settings before starting" return 1 fi log_info "Starting production server with Docker Compose..." docker-compose up -d log_success "Production server started!" log_info "Access the application at http://localhost:8880" log_info "View logs with: ./manage.sh logs" } # Stop production server stop_prod() { check_docker log_info "Stopping production server..." docker-compose down log_success "Production server stopped!" } # View logs view_logs() { check_docker docker-compose logs -f } # Clean up cleanup() { log_info "Cleaning up..." # Remove Python cache find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true find . -type f -name "*.pyc" -delete 2>/dev/null || true # Remove logs rm -rf logs/*.log 2>/dev/null || true log_success "Cleanup complete!" } # Backup data backup() { BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)" mkdir -p "$BACKUP_DIR" log_info "Creating backup in $BACKUP_DIR..." # Backup database if [ -f "instance/ske_signage.db" ]; then cp instance/ske_signage.db "$BACKUP_DIR/" fi # Backup uploads if [ -d "static/uploads" ]; then cp -r static/uploads "$BACKUP_DIR/" fi # Backup assets if [ -d "static/assets" ]; then cp -r static/assets "$BACKUP_DIR/" fi log_success "Backup created in $BACKUP_DIR" } # Show help show_help() { echo "SKE Digital Signage Management Script" echo "" echo "Usage: $0 " echo "" echo "Commands:" echo " setup - Set up development environment" echo " dev - Run development server" echo " build - Build Docker image" echo " start - Start production server (Docker Compose)" echo " stop - Stop production server" echo " logs - View production logs" echo " backup - Create backup of data" echo " cleanup - Clean up temporary files" echo " help - Show this help message" echo "" echo "Examples:" echo " $0 setup # First time setup" echo " $0 dev # Development" echo " $0 start # Production" } # Main script logic case "${1:-help}" in setup) setup_dev ;; dev) run_dev ;; build) build_docker ;; start) start_prod ;; stop) stop_prod ;; logs) view_logs ;; backup) backup ;; cleanup) cleanup ;; help|--help|-h) show_help ;; *) log_error "Unknown command: $1" show_help exit 1 ;; esac