diff --git a/bbbeasy-frontend/public/index.html b/bbbeasy-frontend/public/index.html index 97914997..3a9dd4f3 100644 --- a/bbbeasy-frontend/public/index.html +++ b/bbbeasy-frontend/public/index.html @@ -9,8 +9,9 @@ BBBEasy +
- \ No newline at end of file + diff --git a/docker-compose.yml b/docker-compose.yml index a3a010d7..cfd749ad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,52 +4,57 @@ networks: bbbeasy-network: ipam: config: - - subnet: 172.55.0.0/24 + - subnet: 172.25.0.0/24 services: - webserver: + bbbeasy-frontend: restart: always - image: nginx:1.23.3-alpine - container_name: bbbeasy_nginx + image: waelsan/bbbeasy-frontend:v1.0.0 + container_name: bbbeasy-frontend depends_on: - - bbbeasy + - bbbeasy-backend ports: - "80:80" - "443:443" networks: bbbeasy-network: - ipv4_address: 172.55.0.10 + ipv4_address: 172.25.0.20 volumes: - - bbbeasy-repo:/var/www/html - - ./docker/bbbeasy.conf:/etc/nginx/conf.d/default.conf:ro - - ./docker/phinx.yml:/var/www/html/bbbeasy-backend/phinx.yml:ro - - ./docker/default.ini:/var/www/html/bbbeasy-backend/app/config/default.ini:ro - - ./docker/config-production.ini:/var/www/html/bbbeasy-backend/app/config/config-production.ini:ro - - ./docker/logs/backend:/var/www/html/bbbeasy-backend/logs/:rw - - ./docker/logs/nginx:/var/log/nginx/:rw - - ./docker/certs/fullchain.pem:/etc/nginx/certs/fullchain.pem:ro - - ./docker/certs/privkey.pem:/etc/nginx/certs/privkey.pem:ro - bbbeasy: + - ./docker/logs/nginx:/var/log/nginx:rw + - ./docker/certbot/www:/var/www/certbot/:ro + - ./docker/certbot/conf/:/etc/nginx/ssl/:ro + - ./docker/bbbe-frontend.conf:/etc/nginx/conf.d/bbbe-frontend.conf + bbbeasy-backend: restart: always - image: riadvice/bbbeasy - container_name: bbbeasy_app + image: waelsan/bbbeasy-backend:v1.0.0 + container_name: bbbeasy-backend depends_on: - db - cache - ports: - - "9000:9000" networks: bbbeasy-network: - ipv4_address: 172.55.0.20 + ipv4_address: 172.25.0.21 volumes: - - bbbeasy-repo:/var/www/html - ./docker/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro - ./docker/phinx.yml:/var/www/html/bbbeasy-backend/phinx.yml:ro - ./docker/www-bbbeasy.conf:/usr/local/etc/php-fpm.d/www-bbbeasy.conf:ro - ./docker/default.ini:/var/www/html/bbbeasy-backend/app/config/default.ini:ro - - ./docker/config-production.ini:/var/www/html/bbbeasy-backend/app/config/config-production.ini:ro + - ./docker/config-production.ini:/var/www/html/bbbeasy-backend/app/config/config-production.ini:rw - ./docker/logs/backend:/var/www/html/bbbeasy-backend/logs/:rw + - ./docker/logs/nginx:/var/log/nginx:rw - ./docker/cache:/var/www/html/bbbeasy-backend/tmp/:rw + bbbeasy-docs: + restart: always + image: waelsan/bbbeasy-docs:v1.0.0 + container_name: bbbeasy-docs + depends_on: + - db + - cache + networks: + bbbeasy-network: + ipv4_address: 172.25.0.22 + volumes: + - ./docker/logs/nginx:/var/log/nginx:rw db: restart: always image: postgres:15.2-alpine3.17 @@ -62,9 +67,9 @@ services: - POSTGRES_PASSWORD=bbbeasy networks: bbbeasy-network: - ipv4_address: 172.55.0.50 + ipv4_address: 172.25.0.50 volumes: - - postgresdata:/var/lib/postgresql/data + - ./docker/data/postgres:/var/lib/postgresql/data cache: restart: always image: redis:7.0.9-alpine3.17 @@ -73,13 +78,12 @@ services: - "6379:6379" networks: bbbeasy-network: - ipv4_address: 172.55.0.60 + ipv4_address: 172.25.0.60 + certbot: + restart: always + image: certbot/certbot:latest + container_name: certbot + volumes: + - ./docker/certbot/www:/var/www/certbot/:rw + - ./docker/certbot/conf/:/etc/letsencrypt/:rw -volumes: - bbbeasy-repo: - postgresdata: - driver: local - driver_opts: - type: none - o: bind - device: ./docker/data/postgres diff --git a/docker/bbbeasy.conf b/docker/bbbeasy.conf deleted file mode 100644 index 007ed6d4..00000000 --- a/docker/bbbeasy.conf +++ /dev/null @@ -1,140 +0,0 @@ -upstream bbbeasy_app-upstream -{ - server bbbeasy_app:9000; -} - -server -{ - listen 80; - listen [::]:80; - server_name bbbeasy.test; - return 301 https://bbbeasy.test$request_uri; -} - -server -{ - charset utf-8; - sendfile off; - - # port 443 IPv4 with http2 and ssl - listen 443 ssl http2; - # port 443 IPv6 with http2 and ssl - listen [::]:443 ssl http2; - - ssl_certificate /etc/nginx/certs/fullchain.pem; - ssl_certificate_key /etc/nginx/certs/privkey.pem; - ssl_session_cache shared:SSL:10m; - ssl_session_timeout 10m; - ssl_protocols TLSv1.2; - ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; - - server_name bbbeasy.test; - - root /var/www/html/bbbeasy-backend/public; - index index.html; - - access_log /var/log/nginx/bbbeasy-access.log; - error_log /var/log/nginx/bbbeasy-error.log; - - location /api - { - root /var/www/html/bbbeasy-backend/public; - # Checks whether the requested url exists as a file ($uri) or directory ($uri/) in the root, else redirect to /index.php. - try_files $uri $uri/ @redirects; - # try_files $uri /index.php?$query_string; - } - # Redirect mail bounce and ping - location ~ ^/mailer-ping/(.*$) - { - try_files $uri $uri/ @redirects; - } - - location @redirects - { - rewrite ^ /index.php; - } - - location ~ \.php$ - { - try_files $uri =404; - include fastcgi_params; - fastcgi_pass bbbeasy_app-upstream; - fastcgi_index index.php; - fastcgi_read_timeout 60; - fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - - access_log /var/log/nginx/bbbeasy-backend-access.log; - error_log /var/log/nginx/bbbeasy-backend-error.log; - } - - location /docs - { - root /var/www/html/bbbeasy-docs; - index index.html; - try_files $uri $uri/ /index.html; - - access_log /var/log/nginx/bbbeasy-docs-access.log; - error_log /var/log/nginx/bbbeasy-docs-error.log; - } - - location / - { - root /var/www/html/bbbeasy-installer; - index index.html; - try_files $uri $uri/ =404; - - access_log /var/log/nginx/bbbeasy-installer-access.log; - error_log /var/log/nginx/bbbeasy-installer-error.log; - } - - # Don't pollute the logs with common requests - location = /robots.txt - { - access_log off; log_not_found off; - } - location = /favicon.ico - { - access_log off; log_not_found off; - } - - ########################## - # Security - ########################## - # Hide Nginx version in headers - server_tokens off; - - # Deny access to dot-files. - location ~ /\. - { - deny all; - access_log off; - log_not_found off; - } - - # Enable this if you want custom error pages - # error_page 500 502 503 504 /50x.html; - # location = /50x.html { - # root html; - # } - - ########################## - # Additional Nginx Tweaks - # Read more: https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration - # Read more: https://www.nginx.com/blog/9-tips-for-improving-wordpress-performance-with-nginx/ - ########################## - # Buffers - client_body_buffer_size 10K; # Handles the client buffer size, meaning any POST actions like form submissions. - client_header_buffer_size 1k; # Handles the client header size. 1K is a decent size. - client_max_body_size 50M; # The maximum allowed size of a request. If exceeded, Nginx throws a 413 Request Entity Too Large. - large_client_header_buffers 4 512k; # The max number and size of buffers for large client headers. - - # Timeouts - client_body_timeout 12; # Time a server will wait for a client body to be sent after request. - client_header_timeout 12; # Time a server will wait for a client header to be sent after request. - keepalive_timeout 15; # Timeout for keep-alive connections. After this period of time, the connection is closed. - send_timeout 10; # If after this time, the client will take nothing, then Nginx shuts down the connection. - - # Gzip compression - gzip on; -} diff --git a/install.sh b/install.sh index 48229e17..ac5f52d8 100755 --- a/install.sh +++ b/install.sh @@ -62,48 +62,83 @@ if [[ "$DISTRIB_ID" != "Ubuntu" && "$DISTRIB_RELEASE" != "22.04" ]]; then fi # Setup default values -HV_HOST=$(hostname) +#HV_HOST=$(hostname) INSTALL_TYPE="git" INSTALL_DIR="/opt/bbbeasy" ADMIN_EMAIL=$(grep '^root:' /etc/passwd | awk -F'[<>]' '{print $2}') DB_NAME="bbbeasy" +ENVIRONMENT="production" # Additional values BACKEND_DIR="$INSTALL_DIR/bbbeasy-backend" +# Docker bbbeasy default names +DOCKER_FRONTEND=bbbeasy-frontend +DOCKER_DOCS=bbbeasy-docs +DOCKER_BACKEND=bbbeasy-backend +DOCKER_CACHE=bbbeasy_cache +DOCKER_DB=bbbeasy_db + +say() { + echo "bbbeasy-install: $1" +} + +err() { + say "$1" >&2 + exit 1 +} + +check_root() { + if [ $EUID != 0 ]; then + err "You must run this command as root." + exit 0 + fi +} + +usage_err() { + say "$1" >&2 + # usage + # TODO: prepare usage function + exit 1 +} + # Read CLI options read_options() { - while [[ $# -gt 0 ]]; do - case $1 in - -h | --host) - HV_HOST="$2" - shift + while builtin getopts "h:t:d:e:x" opt "${@}"; do + case "${opt}" in + h) + HV_HOST="${OPTARG}" ;; - -t | --type) - case "$2" in + t) + case "${OPTARG}" in docker | git) - INSTALL_TYPE="$2" - shift + INSTALL_TYPE="${OPTARG}" + #shift ;; *) echo "Error: option --type only accepts \"docker\" or \"git\"" exit 1 ;; esac - shift ;; - -d | --dir) - INSTALL_DIR="$2" - shift + d) + INSTALL_DIR="${OPTARG}" + ;; + e) + ADMIN_EMAIL="${OPTARG}" + ;; + x) + LETS_ENCRYPT_OPTIONS=(--manual --preferred-challenges dns) ;; - *) + :) + err "Missing option argument for -$OPTARG" + ;; + \?) echo "Unknown option: $1" - exit 1 + usage_err "Invalid option: -$OPTARG" >&2 ;; esac - shift done - # TODO: capture admin email option echo "---- INSTALL OPTIONS ----" echo "Hostname : $HV_HOST" echo "Type : $INSTALL_TYPE" @@ -113,6 +148,9 @@ read_options() { } install_docker_deps() { + echo "Install basic dependencies" + apt-get install -y git wget + mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo \ @@ -208,24 +246,37 @@ install_docker() { # Download docker configuration files wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/default.ini wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/config-production.ini - wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/bbbeasy.conf wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/phinx.yml wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/php.ini wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker/www-bbbeasy.conf + wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/docker-compose.yml + wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/package/ressources/BBBE-FRONTEND/config/bbbe-frontend.conf cd "$INSTALL_DIR" + # Create tools directory + mkdir -p "tools" + + cd "tools" + # Download bbbeasy bianry + wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/tools/bbbeasy + cd "$INSTALL_DIR" generate_passwords - setup_host - generate_ssl + + ####################################### + #TODO Prepare certbot for SSL connexion + ####################################### + #setup_host_docker + #generate_ssl # Create ssl certificates directory - mkdir -p "$INSTALL_DIR/docker/certs" - ln -s /etc/letsencrypt/live/meetings.riadvice.ovh/fullchain.pem docker/certs/fullchain.pem - ln -s /etc/letsencrypt/live/meetings.riadvice.ovh/privkey.pem docker/certs/privkey.pem + #mkdir -p "$INSTALL_DIR/docker/certs" + #ln -s /etc/letsencrypt/live/meetings.riadvice.ovh/fullchain.pem docker/certs/fullchain.pem + #ln -s /etc/letsencrypt/live/meetings.riadvice.ovh/privkey.pem docker/certs/privkey.pem } generate_passwords() { if [[ "$INSTALL_TYPE" == "docker" ]]; then + cd "$INSTALL_DIR" CURRENT_PASSWORD=$(grep "POSTGRES_PASSWORD=" docker-compose.yml | cut -d= -f2) # If docker-compose.yml has not been configured if [[ "$CURRENT_PASSWORD" == "bbbeasy" ]]; then @@ -260,8 +311,24 @@ setup_host() { } generate_ssl() { - apt-get install -y certbot - certbot certonly --standalone --non-interactive --preferred-challenges http -d $HV_HOST --email $ADMIN_EMAIL --agree-tos -n + if [[ "$INSTALL_TYPE" == "docker" ]]; then + cd "$INSTALL_DIR" + docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ --email $ADMIN_EMAIL -d $HV_HOST --agree-tos --non-interactive + # Create Crontab for auto renw of the SSL + (crontab -l; echo "30 2 * * 1 docker compose -f $INSTALL_DIR/docker-compose.yml run --rm certbot renew") | crontab - + (crontab -l; echo "35 2 * * 1 docker restart $DOCKER_FRONTEND") | crontab - + LIVE_DIR="$INSTALL_DIR/docker/certbot/conf/live/$HV_HOST" + if [ -n "$(find "$LIVE_DIR" -mindepth 1 -print -quit)" ]; then + cd "docker" + wget -nc https://raw.githubusercontent.com/riadvice/bbbeasy/master/package/ressources/BBBE-FRONTEND/config/bbbe-frontend-ssl.conf + cd "$INSTALL_DIR" + sed -e "s/bbbeasy.test/$HV_HOST/g" -i "docker/bbbe-frontend-ssl.conf" + sed -i 's/bbbe-frontend-ssl.conf:/bbbe-frontend.conf:/g' docker-compose.yml + fi + else + apt-get install -y certbot + certbot certonly --standalone --non-interactive --preferred-challenges http -d $HV_HOST --email $ADMIN_EMAIL --agree-tos -n + fi } clone_repo() { @@ -276,21 +343,41 @@ clone_repo() { } build_apps() { + # Define Installation Type on /etc/default/bbbeasy + if [[ "$INSTALL_TYPE" == "docker" ]]; then + echo "INSTALL_TYPE=docker" > /etc/default/bbbeasy + elif [[ "$INSTALL_TYPE" == "git" ]]; then + echo "INSTALL_TYPE=git" > /etc/default/bbbeasy + fi + "$INSTALL_DIR/tools/./bbbeasy" -si - cp "$INSTALL_DIR/package/templates/nginx/bbbeasy.conf" /etc/nginx/sites-available/bbbeasy - ln -s /etc/nginx/sites-available/bbbeasy /etc/nginx/sites-enabled/bbbeasy - sed -i "s/server_name.*/server_name $HV_HOST;/g" /etc/nginx/sites-available/bbbeasy - sed -i "s|return 301 https.*|return 301 https://$HV_HOST\$request_uri;|g" /etc/nginx/sites-available/bbbeasy - sed -i "s|HV_HOST|$HV_HOST|g" /etc/nginx/sites-available/bbbeasy - bbbeasy -d - bbbeasy -ei + if [[ "$INSTALL_TYPE" == "docker" ]]; then + #TODO Edit bbbeasy-frontend nginx configruation for check ssl + bbbeasy -rc + echo "► Waiting until containers UP" + sleep 30 + echo "► Running database migration" + sudo docker exec -it bbbeasy-backend vendor/bin/phinx migrate -e "$ENVIRONMENT" + elif [[ "$INSTALL_TYPE" == "git" ]]; then + cp "$INSTALL_DIR/package/templates/nginx/bbbeasy.conf" /etc/nginx/sites-available/bbbeasy + ln -s /etc/nginx/sites-available/bbbeasy /etc/nginx/sites-enabled/bbbeasy + sed -i "s/server_name.*/server_name $HV_HOST;/g" /etc/nginx/sites-available/bbbeasy + sed -i "s|return 301 https.*|return 301 https://$HV_HOST\$request_uri;|g" /etc/nginx/sites-available/bbbeasy + sed -i "s|HV_HOST|$HV_HOST|g" /etc/nginx/sites-available/bbbeasy + bbbeasy -d + bbbeasy -ei + service nginx restart + fi } install() { + check_root read_options "$@" + if [ -z "$HV_HOST" ]; then + HV_HOST=$(hostname) + fi if [[ "$INSTALL_TYPE" == "docker" ]]; then echo "-- Installing docker version --" - install_common_deps install_docker_deps install_docker elif [[ "$INSTALL_TYPE" == "git" ]]; then @@ -298,12 +385,13 @@ install() { install_common_deps install_deps service nginx stop - generate_ssl clone_repo generate_passwords - build_apps - service nginx restart fi + if [ -n "$ADMIN_EMAIL" ] && [ -n "$HV_HOST" ]; then + generate_ssl + fi + build_apps } install "$@" 2>&1 | tee -a "/tmp/bbbeasy-install-$NOW.log" diff --git a/package/bbbeasy-backend.Dockerfile b/package/bbbeasy-backend.Dockerfile new file mode 100644 index 00000000..3ff4a7cb --- /dev/null +++ b/package/bbbeasy-backend.Dockerfile @@ -0,0 +1,81 @@ +FROM php:8.2.3-fpm-alpine3.17 + +LABEL authors="ghazi.triki@riadvice.tn,wael.bouslama@riadvice.tn,o.moussi@arribatt.com" + +# Environment Variables +ENV TERM="xterm" + +# Update headers +RUN apk add --update linux-headers + +# Minimal packages +RUN apk update && apk upgrade && apk add --no-cache --virtual .persistent-deps \ + acl \ + bash \ + git \ + curl \ + icu-data-full \ + icu-libs \ + libpq-dev \ + libxml2-dev \ + openssh-client \ + oniguruma-dev \ + libzip \ + zlib \ + nginx + +# PHP extensions +RUN set -xe \ + && apk add --no-cache --virtual .build-deps \ + $PHPIZE_DEPS \ + icu-data-full \ + icu-dev \ + libtool \ + openssl-dev \ + zlib-dev \ + zip \ + unzip \ + libzip-dev \ + && pecl install xdebug-3.2.0 \ + && pecl install -o -f redis \ + && docker-php-ext-install \ + bcmath \ + intl \ + pgsql \ + pdo_pgsql \ + mbstring \ + opcache \ + sockets \ + zip \ + && wget https://github.com/FriendsOfPHP/pickle/releases/latest/download/pickle.phar \ + && mv pickle.phar /usr/local/bin/pickle \ + && chmod +x /usr/local/bin/pickle \ + && pickle install uploadprogress \ + && docker-php-ext-enable \ + pgsql \ + pdo_pgsql \ + redis \ + xdebug \ + zip \ + && apk del .build-deps + +# Conf PHP +#COPY . /var/www/html +RUN mkdir -p /var/www/html/bbbeasy-backend +COPY bbbeasy-backend/. /var/www/html/bbbeasy-backend + + +WORKDIR /var/www/html/bbbeasy-backend + +# Copy the entrypoint.sh script +COPY package/bbbeasy-backend.entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +#preparing Nginx +RUN rm /etc/nginx/http.d/* +COPY package/ressources/BBBE-BACKEND/config/nginx/bbbe-backend.conf /etc/nginx/http.d/ + + +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] + +EXPOSE 8000 diff --git a/package/bbbeasy-backend.dockerignore b/package/bbbeasy-backend.dockerignore new file mode 100644 index 00000000..a4f26879 --- /dev/null +++ b/package/bbbeasy-backend.dockerignore @@ -0,0 +1,3 @@ +bbbeasy-backend/app/config/config-*.ini +bbbeasy-backend/app/config/default.ini +bbbeasy-backend/logs/*.log diff --git a/package/bbbeasy-backend.entrypoint.sh b/package/bbbeasy-backend.entrypoint.sh new file mode 100644 index 00000000..6536340c --- /dev/null +++ b/package/bbbeasy-backend.entrypoint.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +PHP_FPM_LISTEN=8000 + +# Change PHP-FPM listen address +sed -i "s/listen = 9000/listen = ${PHP_FPM_LISTEN}/g" /usr/local/etc/php-fpm.d/zz-docker.conf + +# Set permissions for the web directory +sed -i '4i \chown -R nginx:nginx /var/www/html && chmod -R 755 /var/www/html' /usr/local/bin/docker-php-entrypoint +sed -i '3i nginx -g '\''daemon on;'\''' /usr/local/bin/docker-php-entrypoint + +# Start Nginx +nginx -g 'daemon on;' + +# Start PHP-FPM +php vendor/bin/phinx migrate -e production && php-fpm diff --git a/package/bbbeasy-docs.Dockerfile b/package/bbbeasy-docs.Dockerfile new file mode 100644 index 00000000..b029fe12 --- /dev/null +++ b/package/bbbeasy-docs.Dockerfile @@ -0,0 +1,17 @@ +FROM nginx:latest + +LABEL authors="ghazi.triki@riadvice.tn,wael.bouslama@riadvice.tn,o.moussi@arribatt.com" + +RUN mkdir -p /var/www/html/bbbeasy-docs/build +COPY bbbeasy-docs/build/. /var/www/html/bbbeasy-docs/build/ + +WORKDIR /var/www/html/bbbeasy-docs + +#Configure Nginx +RUN rm /etc/nginx/conf.d/default.conf +COPY package/ressources/BBBE-DOCS/config/bbbe-docs.conf /etc/nginx/conf.d/ + +#Permission for webapp folder +RUN chown -R nginx: /var/www/html/ + +CMD ["nginx", "-g", "daemon off;"] diff --git a/package/bbbeasy-docs.dockerignore b/package/bbbeasy-docs.dockerignore new file mode 100644 index 00000000..a4f26879 --- /dev/null +++ b/package/bbbeasy-docs.dockerignore @@ -0,0 +1,3 @@ +bbbeasy-backend/app/config/config-*.ini +bbbeasy-backend/app/config/default.ini +bbbeasy-backend/logs/*.log diff --git a/package/bbbeasy-frontend.Dockerfile b/package/bbbeasy-frontend.Dockerfile new file mode 100644 index 00000000..80b87ca9 --- /dev/null +++ b/package/bbbeasy-frontend.Dockerfile @@ -0,0 +1,23 @@ +FROM nginx:latest + +LABEL authors="ghazi.triki@riadvice.tn,wael.bouslama@riadvice.tn,o.moussi@arribatt.com" + +RUN mkdir -p /var/www/html/bbbeasy-frontend/build +COPY bbbeasy-frontend/build/. /var/www/html/bbbeasy-frontend/build/ +COPY bbbeasy-frontend/.env /var/www/html/bbbeasy-frontend/ + +WORKDIR /var/www/html/bbbeasy-frontend + +#Linking Frontend and Backend +RUN sed -i 's#REACT_APP_API_URL=http://bbbeasy\.test/api#REACT_APP_API_URL=http://bbbeasy_app/api#' .env +RUN sed -i 's#REACT_APP_URL=http://bbbeasy\.test:3300#REACT_APP_URL=http://bbbeasy-frontend_app#' .env + +#Permission for installer folder and webapp +RUN chown -R nginx: /var/www/html + +#Configure Nginx +COPY package/ressources/BBBE-FRONTEND/config/bbbe-frontend.conf /etc/nginx/conf.d/ +RUN rm /etc/nginx/conf.d/default.conf + + +CMD ["nginx", "-g", "daemon off; load_module modules/ngx_http_js_module.so;"] diff --git a/package/bbbeasy-frontend.dockerignore b/package/bbbeasy-frontend.dockerignore new file mode 100644 index 00000000..a4f26879 --- /dev/null +++ b/package/bbbeasy-frontend.dockerignore @@ -0,0 +1,3 @@ +bbbeasy-backend/app/config/config-*.ini +bbbeasy-backend/app/config/default.ini +bbbeasy-backend/logs/*.log diff --git a/package/ressources/BBBE-BACKEND/config/app-config/config-production.ini b/package/ressources/BBBE-BACKEND/config/app-config/config-production.ini new file mode 100755 index 00000000..7815cbe4 --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/app-config/config-production.ini @@ -0,0 +1,38 @@ +; BBBEasy open source platform - https://riadvice.tn/ +; +; Copyright (c) 2022-2023 RIADVICE SUARL and by respective authors (see below). +; +; This program is free software; you can redistribute it and/or modify it under the +; terms of the GNU Lesser General Public License as published by the Free Software +; Foundation; either version 3.0 of the License, or (at your option) any later +; version. +; +; BBBEasy is distributed in the hope that it will be useful, but WITHOUT ANY +; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +; PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +; +; You should have received a copy of the GNU Lesser General Public License along +; with BBBEasy; if not, see . + +[globals] +; Stack trace verbosity. Assign values 1 to 3 for increasing verbosity levels. Zero (0) suppresses the stack trace. +; This is the default value and it should be the assigned setting on a production server. +DEBUG = 0 + +; database settings +db.driver = pgsql +db.dsn = pgsql:host=bbbeasy_db;port=5432;dbname=bbbeasy +db.username = bbbeasy +db.password = bbbeasy +; Settings +ORGANISATION = RIADVICE + +; time to cache any errors which occur; x-cache is installed on the server +error.ttl = 60 + +; set to development|testing|staging|production|local or whatever you like +application.environment = production + +; log session queries +log.level = info +config.extension = -install diff --git a/package/ressources/BBBE-BACKEND/config/app-config/default.ini b/package/ressources/BBBE-BACKEND/config/app-config/default.ini new file mode 100755 index 00000000..68b7fd9f --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/app-config/default.ini @@ -0,0 +1,86 @@ +; BBBEasy open source platform - https://riadvice.tn/ +; +; Copyright (c) 2022-2023 RIADVICE SUARL and by respective authors (see below). +; +; This program is free software; you can redistribute it and/or modify it under the +; terms of the GNU Lesser General Public License as published by the Free Software +; Foundation; either version 3.0 of the License, or (at your option) any later +; version. +; +; BBBEasy is distributed in the hope that it will be useful, but WITHOUT ANY +; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +; PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +; +; You should have received a copy of the GNU Lesser General Public License along +; with BBBEasy; if not, see . + +[globals] +; Stack trace verbosity. Assign values 1 to 3 for increasing verbosity levels. Zero (0) suppresses the stack trace. +; This is the default value and it should be the assigned setting on a production server. +DEBUG = 0 + +; The PHP Package to show in requests +PACKAGE = BBBEasy Backend + +;Location of custom logs. +LOGS = ../logs/ + +; Temporary folder for cache, filesystem locks, compiled F3 templates, etc. Default is the tmp/ folder outside the Web root. +; Adjust accordingly to conform to your site's security policies. +TEMP = ../tmp/ + +; Directory where file uploads are saved. +UPLOADS = ../uploads/ + +; Search path for user interface files used by the View and Template classes' render() method. Default value is the Web root. +; Accepts a pipe (|), comma (,), or semi-colon (;) as separator for multiple paths. +UI = templates/;../public/; + +; Location of the language dictionaries. +LOCALES = i18n/ | 3600 +ENCODING = UTF-8 + +; Current active language. Value is used to load the appropriate language translation file in the folder pointed to by LOCALES. +; If set to NULL, language is auto-detected from the HTTP Accept-Language request header. +LANGUAGE = en-GB +FALLBACK = en-GB + +; cache configuration +CACHE = redis=bbbeasy_cache + +; Cache seed +SEED = bbbeasy + +; turn javascript and css minification on and off +MINIFY_JS = TRUE +MINIFY_CSS = TRUE + +; timezone settings +TZ = Africa/Tunis + +; set to development|testing|staging|production|local or whatever you like +application.environment = production + +; log session queries +log.session = false + +; session parameters +session.table = users_sessions + +; pagination +pagination.limit = 12 + +; default view to render +view.default = website + +; log clean after x days +log.keep = 14 + +; BigBlueButton +bbb.shared_secret = unsecure_shared_secret_to_change_immediately +bbb.server = unsecure_server_to_change_immediately + +webapps.allowed = + +; server host for command line actions +server.host = bbbeasy_cache diff --git a/package/ressources/BBBE-BACKEND/config/nginx/bbbe-backend.conf b/package/ressources/BBBE-BACKEND/config/nginx/bbbe-backend.conf new file mode 100644 index 00000000..d6f58ed7 --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/nginx/bbbe-backend.conf @@ -0,0 +1,45 @@ +server { + listen 80; + listen [::]:80; + server_name bbbeasy-backend_app; + + root /var/www/html/bbbeasy-backend/public; + + index index.php index.html; + + # Hide version + server_tokens off; + + # disable cache control + add_header Cache-Control no-cache; + expires modified +2d; + sendfile off; + + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options sameorigin; + add_header "X-XSS-Protection" "1; mode=block"; + add_header X-Permitted-Cross-Domain-Policies master-only; + + location / { + root /var/www/html/bbbeasy-backend/public; + try_files $uri $uri/ @redirects; + } + + location @redirects { + rewrite ^ /index.php; + } + + location ~ \.php$ { + fastcgi_pass 127.0.0.1:8000; + try_files $uri =404; + include fastcgi_params; + fastcgi_index index.php; + fastcgi_read_timeout 60; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + access_log /var/log/nginx/bbbeasy-backend-access.log; + error_log /var/log/nginx/bbbeasy-backend-error.log; + +} diff --git a/package/ressources/BBBE-BACKEND/config/phinx/phinx.yml b/package/ressources/BBBE-BACKEND/config/phinx/phinx.yml new file mode 100755 index 00000000..2789a472 --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/phinx/phinx.yml @@ -0,0 +1,35 @@ +paths: + migrations: '%%PHINX_CONFIG_DIR%%/db/migrations' + seeds: '%%PHINX_CONFIG_DIR%%/db/seeds' + +environments: + default_migration_table: phinxlog + default_database: development + production: + adapter: pgsql + host: bbbeasy_db + name: bbbeasy + user: bbbeasy + pass: W274VlyVDF45EDXzQqsqPnRqDO5iJkrb + port: 5432 + charset: utf8 + + development: + adapter: pgsql + host: bbbeasy_db + name: bbbeasy + user: bbbeasy + pass: 'bbbeasy' + port: 5432 + charset: utf8 + + testing: + adapter: pgsql + host: bbbeasy_db + name: bbbeasy_test + user: bbbeasy_test + pass: 'bbbeasy_test' + port: 5432 + charset: utf8 + +version_order: creation diff --git a/package/ressources/BBBE-BACKEND/config/php/custom.ini b/package/ressources/BBBE-BACKEND/config/php/custom.ini new file mode 100644 index 00000000..77130c46 --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/php/custom.ini @@ -0,0 +1,21 @@ +apc.enable_cli = 1 +date.timezone = Africa/Tunis +session.auto_start = Off +short_open_tag = Off +post_max_size = 10M +upload_max_filesize = 5M + +# http://symfony.com/doc/current/performance.html +opcache.max_accelerated_files = 20000 +opcache.memory_consumption = 256 +realpath_cache_size = 4096K +realpath_cache_ttl = 600 + +# hide version number php Http Header +expose_php = Off + +#XDEBUG Config +xdebug.start_with_request=no +xdebug.discover_client_host=false +xdebug.mode=off +xdebug.idekey=PHPSTORM diff --git a/package/ressources/BBBE-BACKEND/config/php/www-bbbeasy.conf b/package/ressources/BBBE-BACKEND/config/php/www-bbbeasy.conf new file mode 100644 index 00000000..38a36adb --- /dev/null +++ b/package/ressources/BBBE-BACKEND/config/php/www-bbbeasy.conf @@ -0,0 +1,8 @@ +[www] +user = root +group = root +pm.max_children = 27 +pm.start_servers = 8 +pm.min_spare_servers = 4 +pm.max_spare_servers = 12 +pm.max_requests = 64 diff --git a/package/ressources/BBBE-DOCS/config/bbbe-docs.conf b/package/ressources/BBBE-DOCS/config/bbbe-docs.conf new file mode 100644 index 00000000..32fb3e79 --- /dev/null +++ b/package/ressources/BBBE-DOCS/config/bbbe-docs.conf @@ -0,0 +1,29 @@ +server +{ + listen 80; + listen [::]:80; + server_name bbbeasy-docs_app; + + root /var/www/html/bbbeasy-docs/build; + + # Hide version + server_tokens off; + + # disable cache control + add_header Cache-Control no-cache; + expires modified +2d; + sendfile off; + + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options sameorigin; + add_header "X-XSS-Protection" "1; mode=block"; + add_header X-Permitted-Cross-Domain-Policies master-only; + + location / { + index index.html; + try_files $uri $uri/ =404; + } + + access_log /var/log/nginx/bbbeasy-docs-access.log; + error_log /var/log/nginx/bbbeasy-docs-error.log; +} diff --git a/package/ressources/BBBE-FRONTEND/config/bbbe-frontend-ssl.conf b/package/ressources/BBBE-FRONTEND/config/bbbe-frontend-ssl.conf new file mode 100644 index 00000000..93ecaac2 --- /dev/null +++ b/package/ressources/BBBE-FRONTEND/config/bbbe-frontend-ssl.conf @@ -0,0 +1,123 @@ +server +{ + listen 80; + listen [::]:80; + server_name bbbeasy.test; + return 301 https://bbbeasy.test$request_uri; +} + +server +{ + charset utf-8; + + # port 443 IPv4,IPv6 with http2 and ssl + http2 on; + listen 443 ssl; + listen [::]:443 ssl ipv6only=on; + + ssl_certificate /etc/nginx/ssl/live/bbbeasy.test/fullchain.pem; + ssl_certificate_key /etc/nginx/ssl/live/bbbeasy.test/privkey.pem; + ssl_session_cache shared:SSL:10m; + ssl_session_timeout 10m; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; + ssl_session_tickets off; + ssl_stapling on; + ssl_stapling_verify on; + resolver 8.8.8.8 8.8.4.4 valid=300s; + resolver_timeout 5s; + ssl_buffer_size 4k; + + + server_name bbbeasy.test; + + root /var/www/html/bbbeasy-frontend/build/webapp; + + ########################## + # Security Configuration + ########################## + # Hide version + server_tokens off; + + # disable cache control + add_header Cache-Control no-cache; + expires modified +2d; + sendfile off; + + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options sameorigin; + add_header "X-XSS-Protection" "1; mode=block"; + add_header X-Permitted-Cross-Domain-Policies master-only; + + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } + + ########################################################## + # Locations Configuration (frontend,docs,backend and api) + ########################################################## + location / { + index index.html; + try_files $uri /index.html; + } + + location /docs { + proxy_pass http://bbbeasy-docs/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + allow all; + } + + location /api { + proxy_pass http://bbbeasy-backend/api; + allow all; + } + + location ~ ^/mailer-ping/(.*$) { + try_files $uri $uri/ @redirects; + } + + location @redirects { + rewrite ^ /index.php; + } + + location = /robots.txt { + access_log off; + log_not_found off; + } + + location = /favicon.ico { + access_log off; + log_not_found off; + } + + ########################## + # Logging Configuration + ########################## + access_log /var/log/nginx/bbbeasy-frontend-access.log; + error_log /var/log/nginx/bbbeasy-frontend-error.log; + + ########################## + # Additional Nginx Tweaks + # Read more: https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration + # Read more: https://www.nginx.com/blog/9-tips-for-improving-wordpress-performance-with-nginx/ + ########################## + # Buffers + client_body_buffer_size 10K; # Handles the client buffer size, meaning any POST actions like form submissions. + client_header_buffer_size 1k; # Handles the client header size. 1K is a decent size. + client_max_body_size 50M; # The maximum allowed size of a request. If exceeded, Nginx throws a 413 Request Entity Too Large. + large_client_header_buffers 4 512k; # The max number and size of buffers for large client headers. + + # Timeouts + client_body_timeout 12; # Time a server will wait for a client body to be sent after request. + client_header_timeout 12; # Time a server will wait for a client header to be sent after request. + keepalive_timeout 15; # Timeout for keep-alive connections. After this period of time, the connection is closed. + send_timeout 10; # If after this time, the client will take nothing, then Nginx shuts down the connection. + + # Gzip compression + gzip on; + +} diff --git a/package/ressources/BBBE-FRONTEND/config/bbbe-frontend.conf b/package/ressources/BBBE-FRONTEND/config/bbbe-frontend.conf new file mode 100644 index 00000000..79546a5e --- /dev/null +++ b/package/ressources/BBBE-FRONTEND/config/bbbe-frontend.conf @@ -0,0 +1,95 @@ +server +{ + listen 80; + listen [::]:80; + server_name bbbeasy-frontend; + root /var/www/html/bbbeasy-frontend/build/webapp; + + ########################## + # Security Configuration + ########################## + # Hide version + server_tokens off; + + # disable cache control + add_header Cache-Control no-cache; + expires modified +2d; + sendfile off; + + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options sameorigin; + add_header "X-XSS-Protection" "1; mode=block"; + add_header X-Permitted-Cross-Domain-Policies master-only; + + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } + + ########################################################## + # Locations Configuration (frontend,docs,backend and api) + ########################################################## + location / { + index index.html; + try_files $uri /index.html; + } + + location /docs { + proxy_pass http://bbbeasy-docs/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + allow all; + } + + location /api { + proxy_pass http://bbbeasy-backend/api; + allow all; + } + + location ~ ^/mailer-ping/(.*$) { + try_files $uri $uri/ @redirects; + } + + location @redirects { + rewrite ^ /index.php; + } + + location = /robots.txt { + access_log off; + log_not_found off; + } + + location = /favicon.ico { + access_log off; + log_not_found off; + } + + ########################## + # Logging Configuration + ########################## + access_log /var/log/nginx/bbbeasy-frontend-access.log; + error_log /var/log/nginx/bbbeasy-frontend-error.log; + + ########################## + # Additional Nginx Tweaks + # Read more: https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration + # Read more: https://www.nginx.com/blog/9-tips-for-improving-wordpress-performance-with-nginx/ + ########################## + # Buffers + client_body_buffer_size 10K; # Handles the client buffer size, meaning any POST actions like form submissions. + client_header_buffer_size 1k; # Handles the client header size. 1K is a decent size. + client_max_body_size 50M; # The maximum allowed size of a request. If exceeded, Nginx throws a 413 Request Entity Too Large. + large_client_header_buffers 4 512k; # The max number and size of buffers for large client headers. + + # Timeouts + client_body_timeout 12; # Time a server will wait for a client body to be sent after request. + client_header_timeout 12; # Time a server will wait for a client header to be sent after request. + keepalive_timeout 15; # Timeout for keep-alive connections. After this period of time, the connection is closed. + send_timeout 10; # If after this time, the client will take nothing, then Nginx shuts down the connection. + + # Gzip compression + gzip on; + +} diff --git a/package/templates/nginx/bbbeasy.conf b/package/templates/nginx/bbbeasy.conf index e6b9d226..a71600ec 100644 --- a/package/templates/nginx/bbbeasy.conf +++ b/package/templates/nginx/bbbeasy.conf @@ -14,12 +14,11 @@ server server { charset utf-8; - sendfile off; - # port 443 IPv4 with http2 and ssl - listen 443 ssl http2; - # port 443 IPv6 with http2 and ssl - listen [::]:443 ssl http2; + # port 443 IPv4,IPv6 with http2 and ssl + http2 on; + listen 443 ssl; + listen [::]:443 ssl ipv6only=on; ssl_certificate /etc/letsencrypt/live/HV_HOST/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/HV_HOST/privkey.pem; @@ -27,6 +26,12 @@ server ssl_session_timeout 10m; ssl_protocols TLSv1.2; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; + ssl_session_tickets off; + ssl_stapling on; + ssl_stapling_verify on; + resolver 8.8.8.8 8.8.4.4 valid=300s; + resolver_timeout 5s; + ssl_buffer_size 4k; server_name bbbeasy.test; @@ -82,7 +87,7 @@ server { root /opt/bbbeasy/bbbeasy-frontend/build/installer; index index.html; - try_files $uri $uri/ =404; + try_files $uri /index.html; access_log /var/log/nginx/bbbeasy-installer-access.log; error_log /var/log/nginx/bbbeasy-installer-error.log; @@ -104,6 +109,16 @@ server # Hide Nginx version in headers server_tokens off; + # disable cache control + add_header Cache-Control no-cache; + expires modified +2d; + sendfile off; + + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options sameorigin; + add_header "X-XSS-Protection" "1; mode=block"; + add_header X-Permitted-Cross-Domain-Policies master-only; + # Deny access to dot-files. location ~ /\. { diff --git a/tools/bbbeasy b/tools/bbbeasy index aa07ea92..725951fa 100755 --- a/tools/bbbeasy +++ b/tools/bbbeasy @@ -46,6 +46,16 @@ GIT_BRANCH=$(git --git-dir="$BASEDIR/../.git" branch | sed -n '/\* /s///p') # Git tag, commits ahead & commit id under format '0.4-160-g3bb256c' GIT_VERSION=$(git --git-dir="$BASEDIR/../.git" describe --tags --always HEAD) +# Setup default values +INSTALL_DIR="/opt/bbbeasy" + +# Docker bbbeasy default names +DOCKER_FRONTEND=bbbeasy-frontend +DOCKER_DOCS=bbbeasy-docs +DOCKER_BACKEND=bbbeasy-backend +DOCKER_CACHE=bbbeasy_cache +DOCKER_DB=bbbeasy_db + # # Display usage # @@ -111,11 +121,15 @@ need_production() { # Display installed BBBEasy & servers version # display_version() { - echo "■ BBBEasy : $GIT_VERSION" - echo "■ Nginx : $(nginx -v)" - echo "■ PHP : $(php -v | sed -n 1p)" - echo "■ Redis : $(redis-server --version)" - echo "■ PostgreSQL : $(psql -V)" + echo "■ BBBEasy : $GIT_VERSION" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker images + else + echo "■ Nginx : $(nginx -v)" + echo "■ PHP : $(php -v | sed -n 1p)" + echo "■ Redis : $(redis-server --version)" + echo "■ PostgreSQL : $(psql -V)" + fi } # @@ -134,31 +148,39 @@ self_install() { # Clean server status # display_status() { - units="nginx php-fpm postgresql redis" - echo - line='————————————————————►' - for unit in $units; do - if [ $(pgrep -c "$unit") != 0 ]; then - printf "%s %s [✔ - UP]\n" $unit "${line:${#unit}}" - else - printf "%s %s [✘ - DOWN]\n" $unit "${line:${#unit}}" - fi - done - echo + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker ps + else + units="nginx php-fpm postgresql redis" + echo + line='————————————————————►' + for unit in $units; do + if [ $(pgrep -c "$unit") != 0 ]; then + printf "%s %s [✔ - UP]\n" $unit "${line:${#unit}}" + else + printf "%s %s [✘ - DOWN]\n" $unit "${line:${#unit}}" + fi + done + echo + fi } # # Clean BBBEasy cache # clean_cache() { - echo "► Deleting cache" - find "$BASEDIR/../bbbeasy-backend/bin/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; - find "$BASEDIR/../bbbeasy-backend/tmp/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; - find "$BASEDIR/../bbbeasy-backend/tmp/cache/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; - find "$BASEDIR/../bbbeasy-backend/tmp/mail/" -name '*.eml' -type f -exec rm -v {} \; - find "$BASEDIR/../bbbeasy-backend/uploads/tmp/" -type f -exec rm -v {} \; - find "$BASEDIR/../bbbeasy-backend/public/minified/" ! -name '.gitkeep' -type f -exec rm -v {} \; - echo "✔ Cache deleted" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + exit 0 + else + echo "► Deleting cache" + find "$BASEDIR/../bbbeasy-backend/bin/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; + find "$BASEDIR/../bbbeasy-backend/tmp/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; + find "$BASEDIR/../bbbeasy-backend/tmp/cache/" ! -name '.gitkeep' -type f -exec rm -rfv {} \; + find "$BASEDIR/../bbbeasy-backend/tmp/mail/" -name '*.eml' -type f -exec rm -v {} \; + find "$BASEDIR/../bbbeasy-backend/uploads/tmp/" -type f -exec rm -v {} \; + find "$BASEDIR/../bbbeasy-backend/public/minified/" ! -name '.gitkeep' -type f -exec rm -v {} \; + echo "✔ Cache deleted" + fi } # @@ -166,9 +188,11 @@ clean_cache() { # clean_logs() { echo "► Cleaning logs" - - find "$BASEDIR/../bbbeasy-backend/logs/" ! -name '.gitkeep' -type f -exec rm -v {} \; - + if [[ "$INSTALL_TYPE" == "docker" ]]; then + find "$BASEDIR/../docker/logs/backend/" ! -name '.gitkeep' -type f -exec rm -v {} \; + else + find "$BASEDIR/../bbbeasy-backend/logs/" ! -name '.gitkeep' -type f -exec rm -v {} \; + fi echo "✔ Cleaned logs folder" } @@ -176,8 +200,19 @@ clean_logs() { # Clean sessions from the database # clean_sessions() { - sudo -u postgres psql -d bbbeasy -c "SELECT setval('users_sessions_id_seq'::regclass, 1);" - sudo -u postgres psql -d bbbeasy -c "TRUNCATE users_sessions;" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + DB_USER=$(grep "POSTGRES_USER" $BASEDIR/../docker-compose.yml | awk -F "=" '{ print $2 }') + DB_PASSWORD=$(grep "POSTGRES_PASSWORD" $BASEDIR/../docker-compose.yml | awk -F "=" '{ print $2 }') + DB_NAME=$(grep "POSTGRES_DB" $BASEDIR/../docker-compose.yml | awk -F "=" '{ print $2 }') + DB_URL="postgresql://${DB_NAME}/${DB_PASSWORD}@localhost:5432/${DB_NAME}" + sudo docker exec -it $DOCKER_DB psql "${DB_URL}" <>"$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini" - sudo sed -i 's/webapp/installer/g' /etc/nginx/sites-available/bbbeasy - sudo service nginx reload - printf "%s %s [✔ - DONE]\n" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo sed -i 's/;config.extension.*/config.extension = -install/g' "$BASEDIR/../docker/config-$ENVIRONMENT.ini" + sudo sed -i 's/webapp/installer/g' "$BASEDIR/../docker/bbbe-frontend.conf" + sudo docker restart DOCKER_FRONTEND + else + if grep -q -v "config.extension" "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini"; then + echo "config.extension = -install" >>"$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini" + sudo sed -i 's/webapp/installer/g' /etc/nginx/sites-available/bbbeasy + sudo service nginx reload + fi fi + printf "%s %s [✔ - DONE]\n" else if [ $(grep -c "config.extension =" "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini") != 0 ]; then #c cmd to replace the matched line in one go @@ -235,12 +285,17 @@ enable_installer() { enable_web() { echo "► Enabling backend webapp" if [ $ENVIRONMENT == "production" ]; then - sed -i '/^config\.extension/d' "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini" - sudo sed -i 's/installer/webapp/g' /etc/nginx/sites-available/bbbeasy - sudo service nginx reload + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo sed -i 's/config.extension/;config.extension/g' "$BASEDIR/../docker/config-$ENVIRONMENT.ini" + sudo sed -i 's/installer/webapp/g' "$BASEDIR/../docker/bbbe-frontend.conf" + sudo docker restart DOCKER_FRONTEND + else + sed -i '/^config\.extension/d' "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini" + sudo sed -i 's/installer/webapp/g' /etc/nginx/sites-available/bbbeasy + sudo service nginx reload + fi printf "%s %s [✔ - DONE]\n" else - echo "► Enabling backend web app" if [ $(grep -c "config.extension = -install" "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini") != 0 ]; then sed -i '/config.extension = -install/ c config.extension =' "$BASEDIR/../bbbeasy-backend/app/config/config-$ENVIRONMENT.ini" printf "%s %s [✔ - DONE]\n" @@ -254,20 +309,25 @@ enable_web() { # Enable unit tests # enable_tests() { - echo "► Enabling unit tests" - - rm -rfv "$BASEDIR/../bbbeasy-backend/public/statera/" - mkdir -pv "$BASEDIR/../bbbeasy-backend/public/statera/coverage/" - mkdir -pv "$BASEDIR/../bbbeasy-backend/public/statera/result/" - cp -Rv "$BASEDIR/../bbbeasy-backend/tools/statera.php" "$BASEDIR/../bbbeasy-backend/public/statera/index.php" - cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/css" "$BASEDIR/../bbbeasy-backend/public/statera/" - cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/images" "$BASEDIR/../bbbeasy-backend/public/statera/" - cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/css" "$BASEDIR/../bbbeasy-backend/public/statera/result" - cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/images" "$BASEDIR/../bbbeasy-backend/public/statera/result" - cp "$BASEDIR/../bbbeasy-backend/vendor/bcosca/fatfree-core/code.css" "$BASEDIR/../bbbeasy-backend/public/statera/css/code.css" - cp "$BASEDIR/../bbbeasy-backend/vendor/bcosca/fatfree-core/code.css" "$BASEDIR/../bbbeasy-backend/public/statera/result/css/code.css" - - echo "✔ Unit tests enabled" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + echo "Not yet implemented for docker version" + exit 0 + else + echo "► Enabling unit tests" + + rm -rfv "$BASEDIR/../bbbeasy-backend/public/statera/" + mkdir -pv "$BASEDIR/../bbbeasy-backend/public/statera/coverage/" + mkdir -pv "$BASEDIR/../bbbeasy-backend/public/statera/result/" + cp -Rv "$BASEDIR/../bbbeasy-backend/tools/statera.php" "$BASEDIR/../bbbeasy-backend/public/statera/index.php" + cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/css" "$BASEDIR/../bbbeasy-backend/public/statera/" + cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/images" "$BASEDIR/../bbbeasy-backend/public/statera/" + cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/css" "$BASEDIR/../bbbeasy-backend/public/statera/result" + cp -Rv "$BASEDIR/../bbbeasy-backend/tests/ui/images" "$BASEDIR/../bbbeasy-backend/public/statera/result" + cp "$BASEDIR/../bbbeasy-backend/vendor/bcosca/fatfree-core/code.css" "$BASEDIR/../bbbeasy-backend/public/statera/css/code.css" + cp "$BASEDIR/../bbbeasy-backend/vendor/bcosca/fatfree-core/code.css" "$BASEDIR/../bbbeasy-backend/public/statera/result/css/code.css" + + echo "✔ Unit tests enabled" + fi } # @@ -317,10 +377,14 @@ run_tests() { } deploy_apps() { - install_dependencies - chown_folders - chmod_folders - run_migrations + if [[ "$INSTALL_TYPE" == "docker" ]]; then + exit 1 + else + install_dependencies + chown_folders + chmod_folders + run_migrations + fi } # @@ -371,15 +435,36 @@ install_dependencies() { # run_migrations() { echo "► Running database migration" - cd "$(dirname "$BASEDIR")/bbbeasy-backend/" - vendor/bin/phinx migrate -e "$ENVIRONMENT" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker restart DOCKER_BACKEND + else + cd "$(dirname "$BASEDIR")/bbbeasy-backend/" + vendor/bin/phinx migrate -e "$ENVIRONMENT" + fi +} + +# +# Run Containers +# +run_containers() { + if [[ "$INSTALL_TYPE" == "docker" ]]; then + echo "► Running Containers" + sudo docker-compose up -d -f "$BASEDIR/../docker-compose.yml" + else + exit 1 + fi } # # Generate code quality metrics # generate_metrics() { - vendor/bin/phpmetrics --report-html="$BASEDIR/../bbbeasy-backend/public/metrics" "$BASEDIR/../bbbeasy-backend/app/src/" + if [[ "$INSTALL_TYPE" == "docker" ]]; then + echo "Not implemented yet for docker version" + exit 1 + else + vendor/bin/phpmetrics --report-html="$BASEDIR/../bbbeasy-backend/public/metrics" "$BASEDIR/../bbbeasy-backend/app/src/" + fi } chown_folders() { @@ -405,30 +490,42 @@ chmod_folders() { # Start services # start_services() { - sudo service postgresql start - sudo service redis start - sudo service nginx start - sudo service php8.2-fpm start + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker-compose -f "$BASEDIR/../docker-compose.yml" start + else + sudo service postgresql start + sudo service redis start + sudo service nginx start + sudo service php8.2-fpm start + fi } # # Stop services # stop_services() { - sudo service postgresql stop - sudo service redis stop - sudo service nginx stop - sudo service php8.2-fpm stop + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker-compose -f "$BASEDIR/../docker-compose.yml" stop + else + sudo service postgresql stop + sudo service redis stop + sudo service nginx stop + sudo service php8.2-fpm stop + fi } # # Restart services # restart_services() { - sudo service postgresql restart - sudo service redis restart - sudo service nginx restart - sudo service php8.2-fpm restart + if [[ "$INSTALL_TYPE" == "docker" ]]; then + sudo docker-compose -f "$BASEDIR/../docker-compose.yml" restart + else + sudo service postgresql restart + sudo service redis restart + sudo service nginx restart + sudo service php8.2-fpm restart + fi } run() { @@ -446,7 +543,14 @@ run() { ENVIRONMENT="production" fi + if [ $(grep -c "INSTALL_TYPE=docker" /etc/default/bbbeasy) -eq 1 ]; + INSTALL_TYPE="docker" + else + INSTALL_TYPE="git" + fi + echo "► Detected environment: \`$ENVIRONMENT\`" + echo "► Detected installation type: \`$INSTALL_TYPE\`" while [[ $# -gt 0 ]]; do @@ -508,6 +612,12 @@ run() { continue fi + if [ "$1" = "--containers" -o "$1" = "-containers" -o "$1" = "-rc" ]; then + run_containers + shift + continue + fi + if [ "$1" = "--metrics" -o "$1" = "-metrics" -o "$1" = "-me" ]; then generate_metrics shift