diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f568e03 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,46 @@ +# Git files +.git +.gitignore +.github + +# Documentation +README.md +CHANGELOG.md +AUTHORS +LICENSE +SECURITY.md +*.md + +# Development files +vagrant/ +wsl/ +INSTALL/ +.idea +.vscode +.DS_Store + +# Dependencies (will be installed in container) +vendor/ +node_modules/ + +# Data directories (will be mounted as volumes) +data/ + +# Build artifacts +public/css/ +public/js/ +public/img/ +public/flags/ +public/views/ + +# Logs +*.log +npm-debug.log + +# Cache +*.cache +.docker-initialized + +# Environment files (should be configured separately) +.env +.env.dev diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..1981b09 --- /dev/null +++ b/.env.dev @@ -0,0 +1,39 @@ +# MariaDB Configuration +DBHOST=fodb +DBPORT_HOST=3307 +DBPASSWORD_ADMIN=root +DBNAME_COMMON=monarc_common +DBNAME_CLI=monarc_cli +DBUSER_MONARC=sqlmonarcuser +DBPASSWORD_MONARC=sqlmonarcuser +# Set USE_BO_COMMON=1 and DBHOST to the BackOffice DB host to reuse monarc_common. +USE_BO_COMMON=1 + +# Shared Docker network name (must exist when using external network) +MONARC_NETWORK_NAME=monarc-network + +# Node.js major version for frontend build tooling +NODE_MAJOR=16 + +# Optional: set to 0/false/no to disable Xdebug in the image build +XDEBUG_ENABLED=1 +XDEBUG_MODE=debug +XDEBUG_START_WITH_REQUEST=trigger +# On Linux, set XDEBUG_CLIENT_HOST to your Docker bridge (often 172.17.0.1). +XDEBUG_CLIENT_HOST=host.docker.internal +XDEBUG_CLIENT_PORT=9003 +XDEBUG_IDEKEY=IDEKEY +XDEBUG_DISCOVER_CLIENT_HOST=0 + +# Stats Service Configuration +STATS_HOST=0.0.0.0 +STATS_PORT=5005 +STATS_DB_NAME=statsservice +STATS_DB_USER=sqlmonarcuser +STATS_DB_PASSWORD=sqlmonarcuser +# Generate a random secret key for production use: +# openssl rand -hex 32 +STATS_SECRET_KEY=changeme_generate_random_secret_key_for_production +# Stats API Key - Leave empty on first run, it will be generated by the stats service +# After first run, get it from the stats service logs or container +STATS_API_KEY= diff --git a/.gitignore b/.gitignore index 9fb0019..606ef8d 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,7 @@ bin/ data/* !data/fonts .docker/mariaDb/data/* +.env +.docker-initialized +docker/db_data +.vscode diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..27e423b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,115 @@ +FROM ubuntu:22.04 + +ARG XDEBUG_ENABLED=1 +ARG XDEBUG_MODE=debug +ARG XDEBUG_START_WITH_REQUEST=trigger +ARG XDEBUG_CLIENT_HOST=host.docker.internal +ARG XDEBUG_CLIENT_PORT=9003 +ARG XDEBUG_IDEKEY=IDEKEY +ARG XDEBUG_DISCOVER_CLIENT_HOST=0 +ARG NODE_MAJOR=16 + +# Prevent interactive prompts during package installation +ENV DEBIAN_FRONTEND=noninteractive +ENV LANGUAGE=en_US.UTF-8 +ENV LANG=en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 + +# Install system dependencies +RUN apt-get update && apt-get upgrade -y && \ + packages="vim zip unzip git gettext curl gsfonts mariadb-client apache2 php8.1 php8.1-cli \ + php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml \ + php8.1-bcmath php8.1-intl php8.1-imagick locales wget ca-certificates gnupg \ + build-essential python3 pkg-config libcairo2-dev libpango1.0-dev libjpeg-dev \ + libgif-dev librsvg2-dev libpixman-1-dev"; \ + if [ "$XDEBUG_ENABLED" = "1" ] || [ "$XDEBUG_ENABLED" = "true" ] || [ "$XDEBUG_ENABLED" = "yes" ]; then \ + packages="$packages php8.1-xdebug"; \ + fi; \ + apt-get install -y $packages && \ + locale-gen en_US.UTF-8 && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Configure PHP +RUN sed -i 's/upload_max_filesize = .*/upload_max_filesize = 200M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/post_max_size = .*/post_max_size = 50M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/max_execution_time = .*/max_execution_time = 100/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/max_input_time = .*/max_input_time = 223/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/memory_limit = .*/memory_limit = 512M/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\.gc_maxlifetime = .*/session.gc_maxlifetime = 604800/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\.gc_probability = .*/session.gc_probability = 1/' /etc/php/8.1/apache2/php.ini && \ + sed -i 's/session\.gc_divisor = .*/session.gc_divisor = 1000/' /etc/php/8.1/apache2/php.ini + +# Configure Xdebug for development +RUN if [ "$XDEBUG_ENABLED" = "1" ] || [ "$XDEBUG_ENABLED" = "true" ] || [ "$XDEBUG_ENABLED" = "yes" ]; then \ + echo "zend_extension=xdebug.so" > /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.mode=${XDEBUG_MODE}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.start_with_request=${XDEBUG_START_WITH_REQUEST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.client_host=${XDEBUG_CLIENT_HOST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.client_port=${XDEBUG_CLIENT_PORT}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.discover_client_host=${XDEBUG_DISCOVER_CLIENT_HOST}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini && \ + echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /etc/php/8.1/apache2/conf.d/20-xdebug.ini; \ + fi + +# Enable Apache modules +RUN a2enmod rewrite ssl headers + +# Set global ServerName to avoid AH00558 warning +RUN echo "ServerName localhost" > /etc/apache2/conf-available/servername.conf \ + && a2enconf servername + +# Install Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +# Install Node.js and npm +RUN mkdir -p /etc/apt/keyrings && \ + curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \ + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \ + apt-get update && \ + apt-get install -y nodejs && \ + npm install -g grunt-cli && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Set working directory +WORKDIR /var/www/html/monarc + +# Configure Apache +RUN echo '\n\ + ServerName localhost\n\ + DocumentRoot /var/www/html/monarc/public\n\ +\n\ + \n\ + DirectoryIndex index.php\n\ + AllowOverride All\n\ + Require all granted\n\ + \n\ +\n\ + \n\ + Header always set X-Content-Type-Options nosniff\n\ + Header always set X-XSS-Protection "1; mode=block"\n\ + Header always set X-Robots-Tag none\n\ + Header always set X-Frame-Options SAMEORIGIN\n\ + \n\ +\n\ + SetEnv APP_ENV development\n\ + SetEnv APP_DIR /var/www/html/monarc\n\ +' > /etc/apache2/sites-available/000-default.conf + +# Allow Apache override to all +RUN sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf + +# Create necessary directories +RUN mkdir -p /var/www/html/monarc/data/cache \ + /var/www/html/monarc/data/LazyServices/Proxy \ + /var/www/html/monarc/data/DoctrineORMModule/Proxy \ + /var/www/html/monarc/data/import/files + +# Copy entrypoint script +COPY docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + +EXPOSE 80 + +ENTRYPOINT ["docker-entrypoint.sh"] +CMD ["apachectl", "-D", "FOREGROUND"] diff --git a/Dockerfile.stats b/Dockerfile.stats new file mode 100644 index 0000000..a2b9bee --- /dev/null +++ b/Dockerfile.stats @@ -0,0 +1,38 @@ +FROM ubuntu:22.04 + +ENV DEBIAN_FRONTEND=noninteractive +ENV LANGUAGE=en_US.UTF-8 +ENV LANG=en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 +ENV FLASK_APP=runserver.py +ENV STATS_CONFIG=production.py + +# Install dependencies +RUN apt-get update && \ + apt-get install -y \ + git \ + curl \ + python3 \ + python3-pip \ + python3-venv \ + nodejs \ + npm \ + postgresql-client \ + locales && \ + locale-gen en_US.UTF-8 && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Install Poetry +RUN curl -sSL https://install.python-poetry.org | python3 - && \ + ln -s /root/.local/bin/poetry /usr/local/bin/poetry + +WORKDIR /var/www/stats-service + +# Copy entrypoint script +COPY docker-stats-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-stats-entrypoint.sh + +EXPOSE 5005 + +ENTRYPOINT ["docker-stats-entrypoint.sh"] diff --git a/INSTALL/INSTALL.docker.md b/INSTALL/INSTALL.docker.md new file mode 100644 index 0000000..365bcd1 --- /dev/null +++ b/INSTALL/INSTALL.docker.md @@ -0,0 +1,188 @@ +# Docker Installation Guide for MONARC FrontOffice + +This document provides installation instructions for setting up MONARC FrontOffice using Docker for development purposes. + +## Quick Installation + +```bash +# Clone the repository +git clone https://github.com/monarc-project/MonarcAppFO +cd MonarcAppFO + +# Start with the Makefile (recommended) +make start + +# Or use docker compose directly +cp .env.dev .env +docker compose -f docker-compose.dev.yml up -d --build +``` + +## What Gets Installed + +The Docker setup includes: + +1. **MONARC FrontOffice Application** + - Ubuntu 22.04 base + - PHP 8.1 with all required extensions + - Apache web server + - Composer for PHP dependencies + - Node.js for frontend + - All MONARC modules and dependencies + +2. **MariaDB 10.11** + - Database for MONARC application data + - Pre-configured with proper character sets + +3. **PostgreSQL 15** + - Database for the statistics service + +4. **Stats Service** + - Python/Flask application + - Poetry for dependency management + - Integrated with MONARC FrontOffice + +5. **MailCatcher** + - Email testing interface + - SMTP server for development + +## First Time Setup + +When you start the environment for the first time: + +1. Docker images will be built (5-10 minutes) +2. Dependencies will be installed automatically +3. Databases will be created and initialized +4. Frontend repositories will be cloned and built +5. Initial admin user will be created + +## Access URLs + +After startup, access the application at: + +- **MONARC FrontOffice**: http://localhost:5001 +- **Stats Service**: http://localhost:5005 +- **MailCatcher**: http://localhost:1080 + +## Default Credentials + +- **Username**: admin@admin.localhost +- **Password**: admin + +⚠️ **Security Warning**: Change these credentials before deploying to production! + +## System Requirements + +- Docker Engine 20.10 or later +- Docker Compose V2 +- At least 8GB RAM available for Docker +- At least 20GB free disk space +- Linux, macOS, or Windows with WSL2 + +## Configuration + +Environment variables are defined in the `.env` file (copied from `.env.dev`): + +- Database credentials +- Service ports +- Secret keys + +You can customize these before starting the environment. + +## Troubleshooting + +### Port Conflicts + +If ports 5001, 5005, 3307, 5432, or 1080 are already in use: + +1. Edit `docker-compose.dev.yml` +2. Change the host port (left side of the port mapping) +3. Example: Change `"5001:80"` to `"8001:80"` + +You can also change the MariaDB host port by setting `DBPORT_HOST` in `.env`. + +### Permission Issues + +If you encounter permission errors: + +```bash +docker exec -it monarc-fo-app bash +chown -R www-data:www-data /var/www/html/monarc/data +chmod -R 775 /var/www/html/monarc/data +``` + +### Service Not Starting + +Check the logs: + +```bash +make logs +# or +docker compose -f docker-compose.dev.yml logs +``` + +### Reset Everything + +To start completely fresh: + +```bash +make reset +# or +docker compose -f docker-compose.dev.yml down -v +``` + +## Common Tasks + +### View Logs +```bash +make logs +``` + +### Access Container Shell +```bash +make shell +``` + +### Access Database +```bash +make db +``` + +### Stop Services +```bash +make stop +``` + +### Restart Services +```bash +make restart +``` + +## Comparison with Other Installation Methods + +| Method | Complexity | Time | Best For | +|--------|-----------|------|----------| +| Docker | Low | Fast (2-5 min startup) | Development | +| Vagrant | Medium | Slow (10-15 min startup) | Development | +| Manual | High | Slow (30+ min) | Production | +| VM | Low | Medium | Testing | + +## Next Steps + +After installation: + +1. Read the [full Docker documentation](README.docker.md) +2. Review the [MONARC documentation](https://www.monarc.lu/documentation) +3. Configure the stats API key (optional) +4. Start developing! + +## Getting Help + +- **Documentation**: [README.docker.md](README.docker.md) +- **MONARC Website**: https://www.monarc.lu +- **GitHub Issues**: https://github.com/monarc-project/MonarcAppFO/issues +- **Community**: https://www.monarc.lu/community + +## License + +This project is licensed under the GNU Affero General Public License version 3. +See [LICENSE](LICENSE) for details. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..551882a --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ +.DEFAULT_GOAL := help + +SHELL := /bin/bash + +ENV ?= dev +COMPOSE_FILE ?= docker-compose.$(ENV).yml +COMPOSE := docker compose -f $(COMPOSE_FILE) + +GREEN := \033[0;32m +YELLOW := \033[1;33m +RED := \033[0;31m +NC := \033[0m + +.PHONY: help check-env start stop restart logs logs-app logs-stats shell shell-stats db reset status stats-key + +help: + @printf "%b\n" "$(GREEN)MONARC FrontOffice Docker Development Environment Manager$(NC)" + @printf "\n%b\n" "Usage: make " + @printf "%b\n" "Environment: ENV= (default: dev, uses docker-compose..yml)" + @printf "\n%b\n" "Commands:" + @printf " %-12s %s\n" "start" "Start all services (builds on first run)" + @printf " %-12s %s\n" "stop" "Stop all services" + @printf " %-12s %s\n" "restart" "Restart all services" + @printf " %-12s %s\n" "logs" "View logs from all services" + @printf " %-12s %s\n" "logs-app" "View logs from MONARC application" + @printf " %-12s %s\n" "logs-stats" "View logs from stats service" + @printf " %-12s %s\n" "shell" "Open a shell in the MONARC container" + @printf " %-12s %s\n" "shell-stats" "Open a shell in the stats service container" + @printf " %-12s %s\n" "db" "Open MySQL client in the database" + @printf " %-12s %s\n" "stats-key" "Show the stats API key" + @printf " %-12s %s\n" "reset" "Reset everything (removes all data)" + @printf " %-12s %s\n" "status" "Show status of all services" + +check-env: + @if [ ! -f .env ]; then \ + printf "%b\n" "$(YELLOW)No .env file found. Creating from .env.dev...$(NC)"; \ + cp .env.dev .env; \ + printf "%b\n" "$(GREEN).env file created. You can edit it to customize configuration.$(NC)"; \ + fi + +start: check-env + @printf "%b\n" "$(GREEN)Starting MONARC FrontOffice development environment...$(NC)" + @$(COMPOSE) up -d --build + @printf "%b\n" "$(GREEN)Services started!$(NC)" + @printf "%b\n" "MONARC FrontOffice: http://localhost:5001" + @printf "%b\n" "Stats Service: http://localhost:5005" + @printf "%b\n" "MailCatcher: http://localhost:1080" + @printf "\n%b\n" "$(YELLOW)To view logs: make logs ENV=$(ENV)$(NC)" + +stop: + @printf "%b\n" "$(YELLOW)Stopping all services...$(NC)" + @$(COMPOSE) stop + @printf "%b\n" "$(GREEN)Services stopped.$(NC)" + +restart: + @printf "%b\n" "$(YELLOW)Restarting all services...$(NC)" + @$(COMPOSE) restart + @printf "%b\n" "$(GREEN)Services restarted.$(NC)" + +logs: + @$(COMPOSE) logs -f + +logs-app: + @$(COMPOSE) logs -f monarc + +logs-stats: + @$(COMPOSE) logs -f stats-service + +shell: + @printf "%b\n" "$(GREEN)Opening shell in MONARC container...$(NC)" + @docker exec -it monarc-fo-app bash + +shell-stats: + @printf "%b\n" "$(GREEN)Opening shell in stats service container...$(NC)" + @docker exec -it monarc-fo-stats bash + +db: + @printf "%b\n" "$(GREEN)Opening MySQL client...$(NC)" + @if [ -f .env ]; then \ + export $$(grep -v '^#' .env | xargs); \ + fi; \ + export MYSQL_PWD="$${DBPASSWORD_MONARC:-sqlmonarcuser}"; \ + docker exec -it monarc-fo-db mysql -u"$${DBUSER_MONARC:-sqlmonarcuser}" "$${DBNAME_COMMON:-monarc_common}" + +stats-key: + @printf "%b\n" "$(GREEN)Retrieving stats API key...$(NC)" + @$(COMPOSE) logs stats-service | grep "Token:" | tail -1 + @printf "\n%b\n" "$(YELLOW)Note: Update this key in your .env file and restart MONARC service:$(NC)" + @printf "%b\n" " 1. Edit .env and set STATS_API_KEY=" + @printf "%b\n" " 2. Run: docker compose -f \"$(COMPOSE_FILE)\" restart monarc" + +reset: + @printf "%b\n" "$(RED)WARNING: This will remove all data!$(NC)"; \ + read -p "Are you sure? (yes/no): " confirm; \ + if [ "$$confirm" = "yes" ]; then \ + printf "%b\n" "$(YELLOW)Stopping and removing all containers, volumes, and data...$(NC)"; \ + $(COMPOSE) down -v; \ + printf "%b\n" "$(GREEN)Reset complete. Run 'make start' to start fresh.$(NC)"; \ + else \ + printf "%b\n" "$(GREEN)Reset cancelled.$(NC)"; \ + fi + +status: + @$(COMPOSE) ps diff --git a/README.docker.md b/README.docker.md new file mode 100644 index 0000000..1c5ceac --- /dev/null +++ b/README.docker.md @@ -0,0 +1,464 @@ +# Docker Development Environment for MONARC FrontOffice + +This guide explains how to set up a local development environment for MONARC FrontOffice using Docker. + +## Prerequisites + +- Docker Engine 20.10 or later +- Docker Compose V2 (comes with Docker Desktop) +- At least 8GB of RAM available for Docker +- At least 20GB of free disk space + +## Quick Start + +### Option 1: Using the Makefile (Recommended) + +1. **Clone the repository** (if you haven't already): + ```bash + git clone https://github.com/monarc-project/MonarcAppFO + cd MonarcAppFO + ``` + +2. **Start the development environment**: + ```bash + make start + ``` + + This will automatically: + - Create `.env` file from `.env.dev` if it doesn't exist + - Build and start all services + - Display access URLs + + The first run will take several minutes as it: + - Builds the Docker images + - Installs all dependencies (PHP, Node.js, Python) + - Clones frontend repositories + - Initializes databases + - Sets up the stats service + - Creates the initial admin user + +3. **Access the application**: + - MONARC FrontOffice: http://localhost:5001 + - Stats Service: http://localhost:5005 + - MailCatcher (email testing): http://localhost:1080 + +4. **Login credentials**: + - Username: `admin@admin.localhost` + - Password: `admin` + +### Option 2: Using Docker Compose Directly + +1. **Clone the repository** (if you haven't already): + ```bash + git clone https://github.com/monarc-project/MonarcAppFO + cd MonarcAppFO + ``` + +2. **Copy the environment file**: + ```bash + cp .env.dev .env + ``` + +3. **Customize environment variables** (optional): + Edit `.env` file to change default passwords and configuration: + ```bash + # Generate a secure secret key for stats service + openssl rand -hex 32 + # Update STATS_SECRET_KEY in .env with the generated value + ``` + +4. **Start the development environment**: + ```bash + docker compose -f docker-compose.dev.yml up --build + ``` + +5. **Access the application**: + - MONARC FrontOffice: http://localhost:5001 + - Stats Service: http://localhost:5005 + - MailCatcher (email testing): http://localhost:1080 + +6. **(Optional) Configure Stats API Key**: + The stats service generates an API key on first run. To retrieve it: + ```bash + # Using Makefile + make stats-key + + # Or manually check logs + docker compose -f docker-compose.dev.yml logs stats-service | grep "Token:" + + # Or create a new client and get the key + docker exec -it monarc-fo-stats poetry run flask client_create --name monarc + + # Update the .env file with the API key and restart the monarc service + docker compose -f docker-compose.dev.yml restart monarc + ``` + +7. **Login credentials**: + - Username: `admin@admin.localhost` + - Password: `admin` + +## Services + +The development environment includes the following services: + +| Service | Description | Port | Container Name | +|---------|-------------|------|----------------| +| monarc | Main FrontOffice application (PHP/Apache) | 5001 | monarc-fo-app | +| db | MariaDB database | 3307 | monarc-fo-db | +| postgres | PostgreSQL database (for stats) | 5432 | monarc-fo-postgres | +| stats-service | MONARC Statistics Service | 5005 | monarc-fo-stats | +| mailcatcher | Email testing tool | 1080 (web), 1025 (SMTP) | monarc-fo-mailcatcher | + +## Development Workflow + +### Makefile Commands + +The Makefile provides convenient commands for managing the development environment. +Use `ENV=` to select `docker compose..yml` (default: `dev`). + +```bash +make start # Start all services +make stop # Stop all services +make restart # Restart all services +make logs # View logs from all services +make logs-app # View logs from MONARC application +make logs-stats # View logs from stats service +make shell # Open a shell in the MONARC container +make shell-stats # Open a shell in the stats service container +make db # Open MySQL client +make status # Show status of all services +make stats-key # Show the stats API key +make reset # Reset everything (removes all data) +``` + +### Live Code Editing + +The application source code is mounted as a volume, so changes you make on your host machine will be immediately reflected in the container. After making changes: + +1. **PHP/Backend changes**: Apache automatically reloads modified files +2. **Frontend changes**: You may need to rebuild the frontend: + ```bash + docker exec -it monarc-fo-app bash + cd /var/www/html/monarc + ./scripts/update-all.sh -d + ``` + +### Accessing the Container + +Using Makefile: +```bash +make shell # MONARC application +make shell-stats # Stats service +``` + +Or directly with docker: +```bash +docker exec -it monarc-fo-app bash # MONARC application +docker exec -it monarc-fo-stats bash # Stats service +``` + +### Database Access + +Using Makefile: +```bash +make db # Connect to MariaDB +``` + +Or directly with docker: +```bash +# Connect to MariaDB +docker exec -it monarc-fo-db mysql -usqlmonarcuser -psqlmonarcuser monarc_common + +# Connect to PostgreSQL +docker exec -it monarc-fo-postgres psql -U sqlmonarcuser -d statsservice +``` + +### Viewing Logs + +Using Makefile: +```bash +make logs # All services +make logs-app # MONARC application only +make logs-stats # Stats service only +``` + +Or directly with docker compose: +```bash +# View logs for all services +docker compose -f docker-compose.dev.yml logs -f + +# View logs for a specific service +docker compose -f docker-compose.dev.yml logs -f monarc +docker compose -f docker-compose.dev.yml logs -f stats-service +``` + +### Restarting Services + +Using Makefile: +```bash +make restart # Restart all services +``` + +Or directly with docker compose: +```bash +# Restart all services +docker compose -f docker-compose.dev.yml restart + +# Restart a specific service +docker compose -f docker-compose.dev.yml restart monarc +``` + +### Stopping the Environment + +Using Makefile: +```bash +make stop # Stop all services (keeps data) +make reset # Stop and remove everything including data +``` + +Or directly with docker compose: +```bash +# Stop all services (keeps data) +docker compose -f docker-compose.dev.yml stop + +# Stop and remove containers (keeps volumes/data) +docker compose -f docker-compose.dev.yml down + +# Stop and remove everything including data +docker compose -f docker-compose.dev.yml down -v +``` + +## Common Tasks + +### Using BackOffice monarc_common + +To reuse the BackOffice `monarc_common` database: + +1. Set `USE_BO_COMMON=1` in `.env`. +2. Point `DBHOST` to the BackOffice MariaDB host (for example, `monarc-bo-db` on a shared Docker network). +3. Ensure the BackOffice database already has `monarc_common` and grants for `DBUSER_MONARC`. + +### Shared Docker Network + +To connect FrontOffice and BackOffice containers, use a shared external network: + +1. Create the network once: `docker network create monarc-network` +2. Set `MONARC_NETWORK_NAME=monarc-network` in both projects' `.env` files. +3. Ensure the BackOffice compose file also uses the same external network name. + +### Resetting the Database + +To completely reset the databases: +```bash +docker compose -f docker-compose.dev.yml down -v +docker compose -f docker-compose.dev.yml up --build +``` + +### Installing New PHP Dependencies + +```bash +docker exec -it monarc-fo-app bash +composer require package/name +``` + +### Installing New Node Dependencies + +```bash +docker exec -it monarc-fo-app bash +cd node_modules/ng_client # or ng_anr +npm install package-name +``` + +### Running Database Migrations + +```bash +docker exec -it monarc-fo-app bash +php ./vendor/robmorgan/phinx/bin/phinx migrate -c ./module/Monarc/FrontOffice/migrations/phinx.php +``` + +### Creating Database Seeds + +```bash +docker exec -it monarc-fo-app bash +php ./vendor/robmorgan/phinx/bin/phinx seed:run -c ./module/Monarc/FrontOffice/migrations/phinx.php +``` + +### Rebuilding Frontend + +```bash +docker exec -it monarc-fo-app bash +cd /var/www/html/monarc +./scripts/update-all.sh -d +``` + +## Debugging + +### Xdebug Configuration + +Xdebug is enabled by default in the development image. To disable it, set +`XDEBUG_ENABLED=0` in `.env` and rebuild the image (for example, `make start`). +You can also tune the connection behavior via `.env`: +`XDEBUG_START_WITH_REQUEST`, `XDEBUG_CLIENT_HOST`, and `XDEBUG_CLIENT_PORT`. + +When enabled, Xdebug is pre-configured. To use it: + +1. Configure your IDE to listen on port 9003 +2. Set the IDE key to `IDEKEY` +3. Start debugging in your IDE +4. Trigger a request to the application + +For PhpStorm: +- Go to Settings → PHP → Debug +- Set Xdebug port to 9003 +- Enable "Can accept external connections" +- Set the path mappings: `/var/www/html/monarc` → your local project path + +### Checking Service Health + +```bash +# Check if all services are running +docker compose -f docker-compose.dev.yml ps + +# Check specific service health +docker compose -f docker-compose.dev.yml ps monarc +``` + +## Troubleshooting + +### Port Conflicts + +If you get port conflicts, you can change the ports in the `docker-compose.dev.yml` file: +```yaml +ports: + - "5001:80" # Change 5001 to another available port +``` + +To change the MariaDB host port without editing the compose file, set +`DBPORT_HOST` in `.env` (default: `3307`). + +### Permission Issues + +If you encounter permission issues with mounted volumes: +```bash +docker exec -it monarc-fo-app bash +chown -R www-data:www-data /var/www/html/monarc/data +chmod -R 775 /var/www/html/monarc/data +``` + +### Database Connection Issues + +Check if the database is healthy: +```bash +docker compose -f docker-compose.dev.yml ps db +``` + +If needed, restart the database: +```bash +docker compose -f docker-compose.dev.yml restart db +``` + +### Stats Service Issues + +Check stats service logs: +```bash +docker compose -f docker-compose.dev.yml logs stats-service +``` + +Restart the stats service: +```bash +docker compose -f docker-compose.dev.yml restart stats-service +``` + +### Rebuilding from Scratch + +If something goes wrong and you want to start fresh: +```bash +# Stop everything +docker compose -f docker-compose.dev.yml down -v + +# Remove all related containers, images, and volumes +docker system prune -a + +# Rebuild and start +docker compose -f docker-compose.dev.yml up --build +``` + +## Performance Optimization + +For better performance on macOS and Windows: + +1. **Use Docker volume mounts for dependencies**: The compose file already uses named volumes for `vendor` and `node_modules` to improve performance. + +2. **Allocate more resources**: In Docker Desktop settings, increase: + - CPUs: 4 or more + - Memory: 8GB or more + - Swap: 2GB or more + +3. **Enable caching**: The Dockerfile uses apt cache and composer optimizations. + +## Comparison with Vagrant + +| Feature | Docker | Vagrant | +|---------|--------|---------| +| Startup time | Fast (~2-3 min) | Slow (~10-15 min) | +| Resource usage | Lower | Higher | +| Isolation | Container-level | VM-level | +| Portability | Excellent | Good | +| Live code reload | Yes | Yes | +| Learning curve | Moderate | Low | + +## Environment Variables Reference + +All environment variables are defined in the `.env` file: + +| Variable | Description | Default | +|----------|-------------|---------| +| `DBHOST` | MariaDB host for MONARC | `db` | +| `DBPORT_HOST` | MariaDB host port (published) | `3307` | +| `DBPASSWORD_ADMIN` | MariaDB root password | `root` | +| `DBNAME_COMMON` | Common database name | `monarc_common` | +| `DBNAME_CLI` | CLI database name | `monarc_cli` | +| `DBUSER_MONARC` | Database user | `sqlmonarcuser` | +| `DBPASSWORD_MONARC` | Database password | `sqlmonarcuser` | +| `USE_BO_COMMON` | Use BackOffice `monarc_common` (`1/0`, `true/false`, `yes/no`) | `0` | +| `MONARC_NETWORK_NAME` | Shared Docker network name | `monarc-network` | +| `NODE_MAJOR` | Node.js major version for frontend tools | `16` | +| `STATS_HOST` | Stats service host | `0.0.0.0` | +| `STATS_PORT` | Stats service port | `5005` | +| `STATS_DB_NAME` | Stats database name | `statsservice` | +| `STATS_DB_USER` | Stats database user | `sqlmonarcuser` | +| `STATS_DB_PASSWORD` | Stats database password | `sqlmonarcuser` | +| `STATS_SECRET_KEY` | Stats service secret key | `changeme_generate_random_secret_key_for_production` | +| `XDEBUG_ENABLED` | Enable Xdebug in the build (`1/0`, `true/false`, `yes/no`) | `1` | +| `XDEBUG_MODE` | Xdebug modes (`debug`, `develop`, etc.) | `debug` | +| `XDEBUG_START_WITH_REQUEST` | Start mode (`trigger` or `yes`) | `trigger` | +| `XDEBUG_CLIENT_HOST` | Host IDE address | `host.docker.internal` | +| `XDEBUG_CLIENT_PORT` | IDE port | `9003` | +| `XDEBUG_IDEKEY` | IDE key | `IDEKEY` | +| `XDEBUG_DISCOVER_CLIENT_HOST` | Auto-detect client host (`1/0`) | `0` | + +## Security Notes + +⚠️ **Important**: The default credentials provided are for development only. Never use these in production! + +For production deployments: +1. Change all default passwords +2. Generate a secure `STATS_SECRET_KEY` using `openssl rand -hex 32` +3. Use proper SSL/TLS certificates +4. Follow security best practices + +## Additional Resources + +- [MONARC Website](https://www.monarc.lu) +- [MONARC Documentation](https://www.monarc.lu/documentation) +- [GitHub Repository](https://github.com/monarc-project/MonarcAppFO) +- [MonarcAppBO (BackOffice)](https://github.com/monarc-project/MonarcAppBO) + +## Getting Help + +If you encounter issues: + +1. Check the [troubleshooting section](#troubleshooting) +2. Review the logs: `docker compose -f docker-compose.dev.yml logs` +3. Open an issue on [GitHub](https://github.com/monarc-project/MonarcAppFO/issues) +4. Join the MONARC community discussions diff --git a/README.md b/README.md index 21a8a85..4e35f9e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,22 @@ For installation instructions see You can also use the provided [Virtual Machine](https://vm.monarc.lu). +Development Environment +----------------------- + +For local development, you can use either: + +- **Docker** (recommended): See [README.docker.md](README.docker.md) for instructions + ```bash + make start + ``` + +- **Vagrant**: See [vagrant/README.rst](vagrant/README.rst) for instructions + ```bash + cd vagrant && vagrant up + ``` + + Contributing ------------ diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..26c562a --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,140 @@ +services: + # MariaDB database for MONARC + fodb: + image: mariadb:10.11 + container_name: monarc-fo-db + environment: + MYSQL_ROOT_PASSWORD: ${DBPASSWORD_ADMIN} + MYSQL_DATABASE: ${DBNAME_COMMON} + MYSQL_USER: ${DBUSER_MONARC} + MYSQL_PASSWORD: ${DBPASSWORD_MONARC} + command: + - --character-set-server=utf8mb4 + - --collation-server=utf8mb4_general_ci + - --bind-address=0.0.0.0 + ports: + - "${DBPORT_HOST:-3307}:3306" + volumes: + - db_data:/var/lib/mysql + - ./docker/db-init:/docker-entrypoint-initdb.d:ro + networks: + - monarc-network + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DBPASSWORD_ADMIN}"] + interval: 10s + timeout: 5s + retries: 5 + + # PostgreSQL database for stats service + # postgres: + # image: postgres:15 + # container_name: monarc-fo-postgres + # environment: + # POSTGRES_USER: ${STATS_DB_USER} + # POSTGRES_PASSWORD: ${STATS_DB_PASSWORD} + # POSTGRES_DB: ${STATS_DB_NAME} + # ports: + # - "5432:5432" + # volumes: + # - postgres_data:/var/lib/postgresql/data + # networks: + # - monarc-network + # healthcheck: + # test: ["CMD-SHELL", "pg_isready -U ${STATS_DB_USER}"] + # interval: 10s + # timeout: 5s + # retries: 5 + + # # Stats service + # stats-service: + # build: + # context: . + # dockerfile: Dockerfile.stats + # container_name: monarc-fo-stats + # environment: + # STATS_HOST: ${STATS_HOST} + # STATS_PORT: ${STATS_PORT} + # STATS_DB_HOST: postgres + # STATS_DB_NAME: ${STATS_DB_NAME} + # STATS_DB_USER: ${STATS_DB_USER} + # STATS_DB_PASSWORD: ${STATS_DB_PASSWORD} + # STATS_SECRET_KEY: ${STATS_SECRET_KEY} + # ports: + # - "5005:5005" + # depends_on: + # postgres: + # condition: service_healthy + # networks: + # - monarc-network + # volumes: + # - stats_data:/var/www/stats-service + + # MailCatcher for email testing + mailcatcher: + image: sj26/mailcatcher:latest + container_name: monarc-fo-mailcatcher + ports: + - "1080:1080" # Web interface + - "1025:1025" # SMTP + networks: + - monarc-network + + # Main MONARC FrontOffice application + monarcfoapp: + build: + context: . + dockerfile: Dockerfile + args: + NODE_MAJOR: ${NODE_MAJOR:-16} + XDEBUG_ENABLED: ${XDEBUG_ENABLED:-1} + XDEBUG_MODE: ${XDEBUG_MODE:-debug} + XDEBUG_START_WITH_REQUEST: ${XDEBUG_START_WITH_REQUEST:-trigger} + XDEBUG_CLIENT_HOST: ${XDEBUG_CLIENT_HOST:-host.docker.internal} + XDEBUG_CLIENT_PORT: ${XDEBUG_CLIENT_PORT:-9003} + XDEBUG_IDEKEY: ${XDEBUG_IDEKEY:-IDEKEY} + XDEBUG_DISCOVER_CLIENT_HOST: ${XDEBUG_DISCOVER_CLIENT_HOST:-0} + container_name: monarc-fo-app + environment: + DBHOST: ${DBHOST:-fodb} + DBNAME_COMMON: ${DBNAME_COMMON} + DBNAME_CLI: ${DBNAME_CLI} + DBUSER_MONARC: ${DBUSER_MONARC} + DBPASSWORD_MONARC: ${DBPASSWORD_MONARC} + DBPASSWORD_ADMIN: ${DBPASSWORD_ADMIN} + USE_BO_COMMON: ${USE_BO_COMMON:-0} + STATS_API_KEY: ${STATS_API_KEY} + APP_ENV: development + APP_DIR: /var/www/html/monarc + ports: + - "5001:80" + depends_on: + fodb: + condition: service_healthy + # stats-service: + # condition: service_started + networks: + - monarc-network + volumes: + # Mount the application code for live development + - ./:/var/www/html/monarc + # Preserve vendor directory in named volume for better performance + - vendor_data:/var/www/html/monarc/vendor + - node_modules_data:/var/www/html/monarc/node_modules + working_dir: /var/www/html/monarc + +networks: + monarc-network: + name: ${MONARC_NETWORK_NAME:-monarc-network} + external: true + +volumes: + db_data: + driver: local + driver_opts: + type: none + device: ./docker/db_data + o: bind + postgres_data: + stats_data: + vendor_data: + node_modules_data: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..99acc57 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,185 @@ +#!/bin/bash +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +echo -e "${GREEN}Starting MONARC FrontOffice setup...${NC}" + +# Wait for database to be ready +echo -e "${YELLOW}Waiting for MariaDB to be ready...${NC}" +while ! mysqladmin ping -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" --silent 2>/dev/null; do + echo "Waiting for MariaDB..." + sleep 2 +done +echo -e "${GREEN}MariaDB is ready!${NC}" + +is_true() { + case "$1" in + 1|true|yes|on) return 0 ;; + *) return 1 ;; + esac +} + +# Check if this is the first run +if [ ! -f "/var/www/html/monarc/.docker-initialized" ]; then + echo -e "${GREEN}First run detected, initializing application...${NC}" + + cd /var/www/html/monarc + + # Install composer dependencies (always, to ensure binaries like Phinx are present) + echo -e "${YELLOW}Installing Composer dependencies...${NC}" + composer install --ignore-platform-req=php --no-interaction + + # Create module symlinks + echo -e "${YELLOW}Creating module symlinks...${NC}" + mkdir -p module/Monarc + cd module/Monarc + ln -sfn ./../../vendor/monarc/core Core + ln -sfn ./../../vendor/monarc/frontoffice FrontOffice + cd /var/www/html/monarc + + # Clone frontend repositories + echo -e "${YELLOW}Setting up frontend repositories...${NC}" + mkdir -p node_modules + cd node_modules + + if [ ! -d "ng_client" ]; then + git clone --config core.fileMode=false https://github.com/monarc-project/ng-client.git ng_client + fi + + if [ ! -d "ng_anr" ]; then + git clone --config core.fileMode=false https://github.com/monarc-project/ng-anr.git ng_anr + fi + + cd /var/www/html/monarc + + # Check if CLI database exists and create databases if needed + echo -e "${YELLOW}Setting up databases...${NC}" + DB_EXISTS=$(mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "SHOW DATABASES LIKE '${DBNAME_CLI}';" | grep -c "${DBNAME_CLI}" || true) + USE_BO_COMMON_ENABLED=0 + if is_true "${USE_BO_COMMON}"; then + USE_BO_COMMON_ENABLED=1 + fi + + if [ "$DB_EXISTS" -eq 0 ]; then + echo -e "${YELLOW}Creating databases...${NC}" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "CREATE DATABASE IF NOT EXISTS ${DBNAME_CLI} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" + + if [ "$USE_BO_COMMON_ENABLED" -eq 0 ]; then + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "CREATE DATABASE IF NOT EXISTS ${DBNAME_COMMON} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" + + echo -e "${YELLOW}Populating common database...${NC}" + export MYSQL_PWD="${DBPASSWORD_MONARC}" + mysql -h"${DBHOST}" -u"${DBUSER_MONARC}" ${DBNAME_COMMON} < db-bootstrap/monarc_structure.sql + mysql -h"${DBHOST}" -u"${DBUSER_MONARC}" ${DBNAME_COMMON} < db-bootstrap/monarc_data.sql + else + echo -e "${YELLOW}USE_BO_COMMON is enabled; skipping monarc_common creation and bootstrap.${NC}" + fi + fi + + echo -e "${YELLOW}Ensuring privileges for ${DBUSER_MONARC}...${NC}" + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "GRANT ALL PRIVILEGES ON ${DBNAME_CLI}.* TO '${DBUSER_MONARC}'@'%';" + if [ "$USE_BO_COMMON_ENABLED" -eq 0 ]; then + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "GRANT ALL PRIVILEGES ON ${DBNAME_COMMON}.* TO '${DBUSER_MONARC}'@'%';" + fi + mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "FLUSH PRIVILEGES;" + + if [ "$USE_BO_COMMON_ENABLED" -eq 1 ]; then + COMMON_EXISTS=$(mysql -h"${DBHOST}" -u"root" -p"${DBPASSWORD_ADMIN}" -e "SHOW DATABASES LIKE '${DBNAME_COMMON}';" | grep -c "${DBNAME_COMMON}" || true) + if [ "$COMMON_EXISTS" -eq 0 ]; then + echo -e "${RED}USE_BO_COMMON is enabled, but ${DBNAME_COMMON} was not found on ${DBHOST}.${NC}" + echo -e "${RED}Ensure the BackOffice database is reachable and contains ${DBNAME_COMMON}.${NC}" + exit 1 + fi + fi + + # Generate local config (always override to match container DB) + echo -e "${YELLOW}Creating local configuration...${NC}" + cat > config/autoload/local.php < [ + 'connection' => [ + 'orm_default' => [ + 'params' => [ + 'host' => '${DBHOST}', + 'user' => '${DBUSER_MONARC}', + 'password' => '${DBPASSWORD_MONARC}', + 'dbname' => '${DBNAME_COMMON}', + ], + ], + 'orm_cli' => [ + 'params' => [ + 'host' => '${DBHOST}', + 'user' => '${DBUSER_MONARC}', + 'password' => '${DBPASSWORD_MONARC}', + 'dbname' => '${DBNAME_CLI}', + ], + ], + ], + ], + + 'activeLanguages' => array('fr','en','de','nl','es','ro','it','ja','pl','pt','ru','zh'), + + 'appVersion' => \$package_json['version'], + + 'checkVersion' => false, + 'appCheckingURL' => 'https://version.monarc.lu/check/MONARC', + + 'email' => [ + 'name' => 'MONARC', + 'from' => 'info@monarc.lu', + ], + + 'mospApiUrl' => 'https://objects.monarc.lu/api/', + + 'monarc' => [ + 'ttl' => 60, // timeout + 'salt' => '', // private salt for password encryption + ], + + 'statsApi' => [ + 'baseUrl' => 'http://stats-service:5005', + 'apiKey' => '${STATS_API_KEY:-}', + ], + + 'import' => [ + 'uploadFolder' => '$appdir/data/import/files', + 'isBackgroundProcessActive' => false, + ], +]; +EOF + + # Update and build frontend and run DB migrations + echo -e "${YELLOW}Building frontend and running DB migrations...${NC}" + ./scripts/update-all.sh + + # Seed database with initial user + echo -e "${YELLOW}Creating initial user and client...${NC}" + php ./vendor/robmorgan/phinx/bin/phinx seed:run -c ./module/Monarc/FrontOffice/migrations/phinx.php + + # Set permissions + echo -e "${YELLOW}Setting permissions...${NC}" + chown -R www-data:www-data /var/www/html/monarc/data + chmod -R 775 /var/www/html/monarc/data + + # Mark initialization as complete + touch /var/www/html/monarc/.docker-initialized + echo -e "${GREEN}Initialization complete!${NC}" +else + echo -e "${GREEN}Application already initialized, starting services...${NC}" +fi + +# Execute the main command +exec apachectl -D FOREGROUND diff --git a/docker-stats-entrypoint.sh b/docker-stats-entrypoint.sh new file mode 100755 index 0000000..7127751 --- /dev/null +++ b/docker-stats-entrypoint.sh @@ -0,0 +1,97 @@ +#!/bin/bash +set -e + +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +echo -e "${GREEN}Starting MONARC Stats Service setup...${NC}" + +# Wait for PostgreSQL to be ready +echo -e "${YELLOW}Waiting for PostgreSQL to be ready...${NC}" +until PGPASSWORD=$STATS_DB_PASSWORD psql -h "$STATS_DB_HOST" -U "$STATS_DB_USER" -d postgres -c '\q' 2>/dev/null; do + echo "Waiting for PostgreSQL..." + sleep 2 +done +echo -e "${GREEN}PostgreSQL is ready!${NC}" + +# Check if this is the first run +if [ ! -f "/var/www/stats-service/.docker-initialized" ]; then + echo -e "${GREEN}First run detected, initializing stats service...${NC}" + + # Clone the stats service repository + if [ ! -d "/var/www/stats-service/.git" ]; then + echo -e "${YELLOW}Cloning stats-service repository...${NC}" + cd /var/www + git clone https://github.com/monarc-project/stats-service stats-service-tmp + cp -r stats-service-tmp/* stats-service/ + cp -r stats-service-tmp/.git stats-service/ + rm -rf stats-service-tmp + cd /var/www/stats-service + fi + + # Install npm dependencies + echo -e "${YELLOW}Installing npm dependencies...${NC}" + npm ci + + # Install Python dependencies with Poetry + echo -e "${YELLOW}Installing Python dependencies with Poetry...${NC}" + poetry install --only=main + + # Create instance directory and configuration + echo -e "${YELLOW}Creating configuration...${NC}" + mkdir -p instance + cat > instance/production.py <