diff --git a/.docker/magento/Dockerfile b/.docker/magento/Dockerfile new file mode 100644 index 0000000..bcd8fb0 --- /dev/null +++ b/.docker/magento/Dockerfile @@ -0,0 +1,78 @@ +ARG PHP_VERSION=8.2 + +FROM php:${PHP_VERSION}-apache + +SHELL ["/bin/bash", "-c"] + +RUN apt-get update && apt-get install -y \ + mariadb-client \ + libfreetype6-dev \ + libjpeg62-turbo-dev \ + libmcrypt-dev \ + libedit-dev \ + libedit2 \ + libpng-dev \ + libjpeg-dev \ + libicu-dev \ + libxslt1-dev \ + libzip-dev \ + libssl-dev \ + git \ + wget \ + curl \ + lynx \ + psmisc \ + unzip \ + tar \ + cron \ + libpcre3 \ + libpcre3-dev \ + && pecl install \ + xdebug \ + redis \ + oauth \ + && echo "extension=oauth.so" > /usr/local/etc/php/conf.d/docker-php-ext-oauth.ini \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install \ + opcache \ + bcmath \ + gd \ + intl \ + mysqli \ + pdo_mysql \ + soap \ + sockets \ + xsl \ + zip \ + && apt-get clean && rm -rf /var/lib/apt/lists/* \ + && docker-php-ext-enable redis xdebug + +# Install composer +COPY --from=composer:2 /usr/bin/composer /usr/bin/composer + +# Enable apache modules +RUN a2enmod rewrite +RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf + +COPY ./docker-php-m2.ini /usr/local/etc/php/conf.d/docker-php-m2.ini + +COPY ./m2-entrypoint.sh /usr/local/bin/m2-entrypoint +RUN chmod +x /usr/local/bin/m2-entrypoint + +ARG M2_VERSION +ARG M2_REPO_KEY +ARG M2_REPO_SECRET + +RUN mkdir -p /Sequra/Core && chown -R www-data:www-data /Sequra && chmod -R 755 /Sequra + +WORKDIR /var/www/html + +RUN composer config -g http-basic.repo.magento.com $M2_REPO_KEY $M2_REPO_SECRET + +RUN composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=$M2_VERSION . \ + && composer require mageplaza/magento-2-spanish-language-pack:dev-master mageplaza/magento-2-portuguese-language-pack:dev-master mageplaza/magento-2-french-language-pack:dev-master mageplaza/magento-2-italian-language-pack:dev-master \ + && chmod -R 755 ./ \ + && chown -R www-data:www-data ./ + +# Modify base entry point to run our custom setup script +RUN sed -i 's/exec "$@"/\/usr\/local\/bin\/m2-entrypoint \&\& exec "$@"/g' /usr/local/bin/docker-php-entrypoint diff --git a/.docker/magento/docker-php-m2.ini b/.docker/magento/docker-php-m2.ini new file mode 100644 index 0000000..3db3955 --- /dev/null +++ b/.docker/magento/docker-php-m2.ini @@ -0,0 +1,11 @@ +# Magento 2 requirements +date.timezone=Europe/Madrid +memory_limit=2G +realpath_cache_size=10M +realpath_cache_ttl=7200 + +# XDebug +xdebug.mode=debug +xdebug.start_with_request=yes +xdebug.client_host=localhost.sequrapi.com +xdebug.log_level=1 \ No newline at end of file diff --git a/.docker/magento/m2-entrypoint.sh b/.docker/magento/m2-entrypoint.sh new file mode 100755 index 0000000..6deb100 --- /dev/null +++ b/.docker/magento/m2-entrypoint.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +# Check if need to install WordPress +if [ ! -f /var/www/html/.post-install-complete ]; then + + function handle_failure { + touch /var/www/html/.post-install-failed + echo "❌ Magento 2 installation failed" + exit 1 + } + + rm -f /var/www/html/.post-install-failed + + cd /var/www/html + export XDEBUG_MODE=off + + # Override WP_URL if PUBLIC_URL is set + if [ -n "$PUBLIC_URL" ]; then + M2_URL="$PUBLIC_URL" + fi + + session_save="--session-save=$M2_SESSION_SAVE" + if [ "$M2_SESSION_SAVE" == 'redis' ]; then + session_save="$session_save\ + --session-save-redis-host=$M2_SESSION_SAVE_REDIS_HOST \ + --session-save-redis-port=$M2_SESSION_SAVE_REDIS_PORT \ + --session-save-redis-password=$M2_SESSION_SAVE_REDIS_PASSWORD \ + --session-save-redis-timeout=$M2_SESSION_SAVE_REDIS_TIMEOUT \ + --session-save-redis-persistent-id=$M2_SESSION_SAVE_REDIS_PERSISTENT_IDENTIFIER \ + --session-save-redis-db=$M2_SESSION_SAVE_REDIS_DB \ + --session-save-redis-compression-threshold=$M2_SESSION_SAVE_REDIS_COMPRESSION_THRESHOLD \ + --session-save-redis-compression-lib=$M2_SESSION_SAVE_REDIS_COMPRESSION_LIB \ + --session-save-redis-log-level=$M2_SESSION_SAVE_REDIS_LOG_LEVEL \ + --session-save-redis-max-concurrency=$M2_SESSION_SAVE_REDIS_MAX_CONCURRENCY \ + --session-save-redis-break-after-frontend=$M2_SESSION_SAVE_REDIS_BREAK_AFTER_FRONTEND \ + --session-save-redis-break-after-adminhtml=$M2_SESSION_SAVE_REDIS_BREAK_AFTER_ADMINHTML \ + --session-save-redis-first-lifetime=$M2_SESSION_SAVE_REDIS_FIRST_LIFETIME \ + --session-save-redis-bot-first-lifetime=$M2_SESSION_SAVE_REDIS_BOT_FIRST_LIFETIME \ + --session-save-redis-bot-lifetime=$M2_SESSION_SAVE_REDIS_BOT_LIFETIME \ + --session-save-redis-disable-locking=$M2_SESSION_SAVE_REDIS_DISABLE_LOCKING \ + --session-save-redis-min-lifetime=$M2_SESSION_SAVE_REDIS_MIN_LIFETIME \ + --session-save-redis-max-lifetime=$M2_SESSION_SAVE_REDIS_MAX_LIFETIME \ + --session-save-redis-sentinel-master=$M2_SESSION_SAVE_REDIS_SENTINEL_MASTER \ + --session-save-redis-sentinel-servers=$M2_SESSION_SAVE_REDIS_SENTINEL_SERVERS \ + --session-save-redis-sentinel-verify-master=$M2_SESSION_SAVE_REDIS_SENTINEL_VERIFY_MASTER \ + --session-save-redis-sentinel-connect-retries=$M2_SESSION_SAVE_REDIS_SENTINEL_CONNECT_RETRIES" + fi + + disable_modules="" + if [ -n "$M2_DISABLE_MODULES" ]; then + disable_modules="--disable-modules=$M2_DISABLE_MODULES" + fi + + # Install Magento 2 + su -s /bin/bash www-data -c "bin/magento setup:install \ + --base-url=$M2_URL \ + --db-host=$M2_DB_HOST \ + --db-name=$M2_DB_NAME \ + --db-user=$M2_DB_USER \ + --db-password=$M2_DB_PASSWORD \ + --skip-db-validation \ + --backend-frontname=$M2_BACKEND_FRONTNAME \ + --admin-firstname=$M2_ADMIN_FIRSTNAME \ + --admin-lastname=$M2_ADMIN_LASTNAME \ + --admin-email=$M2_ADMIN_EMAIL \ + --admin-user=$M2_ADMIN_USER \ + --admin-password=$M2_ADMIN_PASSWORD \ + --language=$M2_LANGUAGE \ + --currency=$M2_CURRENCY \ + --timezone=$M2_TIMEZONE \ + --use-rewrites=1 \ + --search-engine=elasticsearch7 \ + --elasticsearch-host=$M2_ELASTICSEARCH_HOST \ + --elasticsearch-port=$M2_ELASTICSEARCH_PORT \ + --elasticsearch-enable-auth=0 \ + --elasticsearch-index-prefix=$M2_ELASTICSEARCH_INDEX_PREFIX \ + --elasticsearch-timeout=$M2_ELASTICSEARCH_TIMEOUT \ + $session_save $disable_modules" \ + && su -s /bin/bash www-data -c "composer config http-basic.repo.magento.com $M2_COMPOSER_REPO_KEY $M2_COMPOSER_REPO_SECRET" \ + && su -s /bin/bash www-data -c "bin/magento deploy:mode:set developer" \ + && su -s /bin/bash www-data -c "bin/magento sampledata:deploy && bin/magento setup:upgrade && bin/magento cache:flush" \ + || handle_failure + + # Set auto increment to current timestamp for Order Sequence tables + echo "ALTER TABLE sequence_order_0 AUTO_INCREMENT = $(date +%s);ALTER TABLE sequence_order_1 AUTO_INCREMENT = $(date +%s);" \ + | mysql -h $M2_DB_HOST -P 3306 -u $M2_DB_USER -p$M2_DB_PASSWORD $M2_DB_NAME || handle_failure + + # Install seQura plugin + su -s /bin/bash www-data -c "composer config repositories.sequra/magento2-core path /Sequra/Core" \ + && su -s /bin/bash www-data -c "COMPOSER_MIRROR_PATH_REPOS=1 composer require sequra/magento2-core:^2.5" \ + && su -s /bin/bash www-data -c "bin/magento config:set dev/template/allow_symlink $M2_ALLOW_SYMLINK" \ + && su -s /bin/bash www-data -c "bin/magento module:enable Sequra_Core" \ + && su -s /bin/bash www-data -c "bin/magento setup:upgrade" \ + && su -s /bin/bash www-data -c "bin/magento sequra:configure --merchant_ref="$SQ_MERCHANT_REF" --username="$SQ_USER_NAME" --password="$SQ_USER_SECRET" --assets_key="$SQ_ASSETS_KEY" --endpoint="$SQ_ENDPOINT"" || handle_failure + + touch /var/www/html/.post-install-complete && echo "✅ Magento 2 installed and configured." +fi \ No newline at end of file diff --git a/.env.sample b/.env.sample index 8ce3698..77303fb 100644 --- a/.env.sample +++ b/.env.sample @@ -1,48 +1,123 @@ -MAGENTO_VERSION=2 +# The version of MariaDB to use. See available tags at https://hub.docker.com/_/mariadb/tags +MARIADB_TAG=10.6 -SQ_M2_CORE_VERSION=* -# Use "local" to run code in local directory -#SQ_M2_CORE_VERSION=local +# The name of the database to create +MARIADB_DATABASE=magentodb +# The name of the database user to create +MARIADB_USER=magentouser +# The password for the database user +MARIADB_PASSWORD=magentopass +# The password for the root user +MARIADB_ROOT_PASSWORD=root +# The port to expose the Database service +MARIADB_PORT=3328 + +# The version of Redis to use. See available tags at https://hub.docker.com/_/redis/tags +REDIS_TAG=7.2 +# The port to expose the Redis service +REDIS_PORT=6380 + +# The version of Elasticsearch to use. See available tags at https://hub.docker.com/_/elasticsearch/tags +# ELASTICSEARCH_TAG=8.11.4 # Use this version for Magento 2.4.7 +ELASTICSEARCH_TAG=7.16.3 # Use this version for Magento 2.4.3, 2.4.4, 2.4.5, 2.4.6 + +# The port to expose the Elasticsearch service +ELASTICSEARCH_API_PORT=9200 +ELASTICSEARCH_INTERNODE_PORT=9300 + +# The version of PHP to use. +PHP_VERSION=8.2 # Compatible with Magento 2.4.7, 2.4.6 +# PHP_VERSION=8.1 # Compatible with Magento 2.4.5, 2.4.4 +# PHP_VERSION=7.4 # Compatible with Magento 2.4.3 + +# The version of Magento 2 to use. +# M2_VERSION=2.4.3-p3 +# M2_VERSION=2.4.4-p11 +# M2_VERSION=2.4.5-p10 +# M2_VERSION=2.4.6-p8 +M2_VERSION=2.4.7-p3 + +# The hostname of the database server +M2_DB_HOST=mariadb +# Same as MARIADB_USER +M2_DB_USER=$MARIADB_USER +# Same as MARIADB_PASSWORD +M2_DB_PASSWORD=$MARIADB_PASSWORD +#Same as MARIADB_DATABASE +M2_DB_NAME=$MARIADB_DATABASE +# The port to expose the Magento 2 service +M2_HTTP_PORT=8018 +M2_HTTP_HOST=localhost.sequrapi.com +# The site URL +M2_URL=http://$M2_HTTP_HOST:$M2_HTTP_PORT + +M2_BACKEND_FRONTNAME="admin" +M2_ADMIN_USER="admin" +M2_ADMIN_PASSWORD="Admin123" +M2_ADMIN_EMAIL="magento@sequra.com" +M2_ADMIN_FIRSTNAME="Admin" +M2_ADMIN_LASTNAME="Admin" +M2_LANGUAGE="en_US" +M2_CURRENCY="EUR" +M2_TIMEZONE="Europe/Madrid" + +M2_ELASTICSEARCH_HOST=elasticsearch +M2_ELASTICSEARCH_PORT=9200 +M2_ELASTICSEARCH_INDEX_PREFIX=m2 +M2_ELASTICSEARCH_TIMEOUT=60 + +M2_DISABLE_MODULES="Magento_TwoFactorAuth" +# M2_DISABLE_MODULES="Magento_TwoFactorAuth,Magento_AdminAdobeIms,Magento_AdminAnalytics,Magento_AdobeIms,Magento_AdobeImsApi,Magento_AdobeStockAdminUi,Magento_AdobeStockClient,Magento_AdobeStockClientApi,Magento_AdobeStockImage,Magento_AdobeStockImageApi,Magento_AdobeStockImageAdminUi,Magento_Analytics,Magento_ApplicationPerformanceMonitor,Magento_ApplicationPerformanceMonitorNewRelic,Magento_Backup,Magento_CardinalCommerce,Magento_Captcha,Magento_Dhl,Magento_Fedex,Magento_GoogleAdwords,Magento_GoogleAnalytics,Magento_GoogleGtag,Magento_GoogleOptimizer,Magento_Paypal,Magento_PaypalCaptcha,Magento_PaypalGraphQl,Magento_PaymentServicesPaypal,Magento_PaymentServicesPaypalGraphQl,PayPal_Braintree,PayPal_BraintreeCustomerBalance,PayPal_BraintreeGiftCardAccount,PayPal_BraintreeGiftWrapping,PayPal_BraintreeGraphQl" + +# Choose the session save handler. Options are files, redis, db. +# For redis, the REDIS_TAG must be set and you must provide the +# extra environment variables having M2_SESSION_SAVE_REDIS_ prefix. +M2_SESSION_SAVE=files +# M2_SESSION_SAVE=db +# M2_SESSION_SAVE=redis +# M2_SESSION_SAVE_REDIS_HOST=redis +# M2_SESSION_SAVE_REDIS_PORT=6379 +# M2_SESSION_SAVE_REDIS_PASSWORD='' # Default is empty +# M2_SESSION_SAVE_REDIS_TIMEOUT=4 # Default is 4 +# M2_SESSION_SAVE_REDIS_PERSISTENT_IDENTIFIER='' # Default is empty +# M2_SESSION_SAVE_REDIS_DB=2 # Default is 2 +# M2_SESSION_SAVE_REDIS_COMPRESSION_THRESHOLD=2048 # Default is 2048 +# M2_SESSION_SAVE_REDIS_COMPRESSION_LIB=gzip # Default is gzip +# M2_SESSION_SAVE_REDIS_LOG_LEVEL=3 # Default is 1 +# M2_SESSION_SAVE_REDIS_MAX_CONCURRENCY=25 # Default is 6 +# M2_SESSION_SAVE_REDIS_BREAK_AFTER_FRONTEND=5 # Default is 5 +# M2_SESSION_SAVE_REDIS_BREAK_AFTER_ADMINHTML=30 # Default is 30 +# M2_SESSION_SAVE_REDIS_FIRST_LIFETIME=600 # Default is 600 +# M2_SESSION_SAVE_REDIS_BOT_FIRST_LIFETIME=60 # Default is 60 +# M2_SESSION_SAVE_REDIS_BOT_LIFETIME=7200 # Default is 7200 +# M2_SESSION_SAVE_REDIS_DISABLE_LOCKING=1 # Default is 0 +# M2_SESSION_SAVE_REDIS_MIN_LIFETIME=7776000 # Default is 60 +# M2_SESSION_SAVE_REDIS_MAX_LIFETIME=2592000 # Default is 2592000 +# M2_SESSION_SAVE_REDIS_SENTINEL_MASTER='' # Default is empty +# M2_SESSION_SAVE_REDIS_SENTINEL_SERVERS='' # Default is empty +# M2_SESSION_SAVE_REDIS_SENTINEL_VERIFY_MASTER=0 # Default is 0 +# M2_SESSION_SAVE_REDIS_SENTINEL_CONNECT_RETRIES=5 # Default is 5 + +M2_ALLOW_SYMLINK=1 + +# Public and private keys for Magento 2 composer repository +M2_COMPOSER_REPO_KEY= +M2_COMPOSER_REPO_SECRET= + +# SeQura configuration SQ_MERCHANT_REF=dummy SQ_USER_NAME=dummy SQ_USER_SECRET=ZqbjrN6bhPYVIyram3wcuQgHUmP1C4 SQ_ASSETS_KEY=ADc3ZdOLh4 SQ_ENDPOINT=https://sandbox.sequrapi.com/orders -MAGENTO_HTTP_PORT=8018 -MAGENTO_EXTERNAL_HTTP_PORT_NUMBER=${MAGENTO_HTTP_PORT} -MAGENTO_HTTPS_PORT=8019 -MAGENTO_EXTERNAL_HTTPS_PORT_NUMBER=${MAGENTO_HTTPS_PORT} -MAGENTO_MYSQL_PORT=8020 - -# More options available at https://hub.docker.com/r/bitnami/magento -MAGENTO_HOST=localhost -MAGENTO_DATABASE_HOST=mariadb -MAGENTO_DATABASE_PORT_NUMBER=3306 -MAGENTO_DATABASE_USER=bn_magento -MAGENTO_DATABASE_NAME=bitnami_magento -ELASTICSEARCH_HOST=elasticsearch -ELASTICSEARCH_PORT_NUMBER=9200 -MAGENTO_EMAIL=magento@sequra.com -MAGENTO_FIRST_NAME=admin_first_name -MAGENTO_LAST_NAME=admin_last_name -MAGENTO_USERNAME=admin -MAGENTO_PASSWORD=password123 -MAGENTO_FRONTNAME=admin -MAGENTO_COUNTRY=ES - -MAGENTO_LOCALE=es_ES -MAGENTO_CURRENCY=EUR -MAGENTO_TIMEZONE=Europe/Berlin -MAGENTO_SAMPLEDATA=yes -MAGENTO_EXTRA_INSTALL_ARGS="--currency=${MAGENTO_CURRENCY} --timezone=${MAGENTO_TIMEZONE} --language=${MAGENTO_LOCALE} --cleanup-database" -MAGENTO_ENABLE_HTTP_CACHE=no - -# ALLOW_EMPTY_PASSWORD is recommended only for development. -ALLOW_EMPTY_PASSWORD=yes -#MAGENTO_DATABASE_PASSWORD=supersecret -COMPOSER_AUTH='{"http-basic":{"repo.magento.com":{"username":"","password":""}}}' -# Outside /bitnami/magento/ -COMPOSER_CACHE_DIR=/opt/.composer/cache - -BITNAMI_DEBUG=false \ No newline at end of file +# Set with the ngrok authtoken (get it from https://dashboard.ngrok.com/) +NGROK_AUTHTOKEN= +# The port to expose the ngrok service to the host. +NGROK_PORT=4740 +# The name of the ngrok container +NGROK_CONTAINER_NAME=magento-ngrok +# This is the URL where the website will be accessible on the internet. +# It is automatically generated by ngrok and will be overwritten by the script. +# Do not modify this value manually. +PUBLIC_URL= \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 46030d2..126607f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,49 +1,87 @@ +x-env_file: &env_file + env_file: + - .env + services: - mariadb: - image: docker.io/bitnami/mariadb:10.6 - environment: - # ALLOW_EMPTY_PASSWORD is recommended only for development. - - ALLOW_EMPTY_PASSWORD=yes - - MARIADB_USER=bn_magento - - MARIADB_DATABASE=bitnami_magento - volumes: - - "mariadb_data:/bitnami/mariadb" - ports: - - "${MAGENTO_MYSQL_PORT}:3306" - env_file: - - path: ./.env.sample - required: true - - path: ./.env - required: false magento: - container_name: ${COMPOSE_PROJECT_NAME:-sequra}-magento - image: docker.io/bitnami/magento:${MAGENTO_VERSION:-latest} + image: sequra/magento:$M2_VERSION-$PHP_VERSION + build: + dockerfile: Dockerfile + context: ./.docker/magento + args: + PHP_VERSION: ${PHP_VERSION} + M2_VERSION: ${M2_VERSION} + M2_REPO_KEY: ${M2_COMPOSER_REPO_KEY} + M2_REPO_SECRET: ${M2_COMPOSER_REPO_SECRET} + platforms: + - linux/amd64 + depends_on: + mariadb: + condition: service_healthy + elasticsearch: + condition: service_healthy + redis: + condition: service_healthy ports: - - "${MAGENTO_HTTP_PORT}:8080" - - "${MAGENTO_HTTPS_PORT}:8443" - env_file: - - path: ./.env.sample - required: true - - path: ./.env - required: false + - $M2_HTTP_PORT:80 volumes: - - "magento_data:/bitnami/magento" - - "./docker-entrypoint-init.d:/docker-entrypoint-init.d" - - ".composer_cache:${COMPOSER_CACHE_DIR}" - - "./:/Sequra/Core" + - m2_html:/var/www/html + - .:/Sequra/Core:ro extra_hosts: - "localhost.sequrapi.com:host-gateway" - depends_on: - - mariadb - - elasticsearch + <<: *env_file + + mariadb: + image: mariadb:$MARIADB_TAG + ports: + - $MARIADB_PORT:3306 + <<: *env_file + volumes: + - m2_db:/var/lib/mysql + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] + interval: 1s + timeout: 3s + retries: 10 + + redis: + image: redis:$REDIS_TAG + ports: + - $REDIS_PORT:6379 + volumes: + - m2_redis:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 1s + timeout: 3s + retries: 10 + elasticsearch: - image: docker.io/bitnami/elasticsearch:7 + image: elasticsearch:$ELASTICSEARCH_TAG + ports: + - $ELASTICSEARCH_API_PORT:9200 + - $ELASTICSEARCH_INTERNODE_PORT:9300 + environment: + - discovery.type=single-node + - cluster.name=elasticsearch + - node.name=elasticsearch + - bootstrap.memory_lock=true + - xpack.security.enabled=false + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + ulimits: + memlock: + soft: -1 + hard: -1 + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:$ELASTICSEARCH_API_PORT"] + interval: 3s + timeout: 3s + retries: 15 volumes: - - "elasticsearch_data:/bitnami/elasticsearch/data" + - m2_elasticsearch:/usr/share/elasticsearch/data + volumes: - mariadb_data: - driver: local - magento_data: - driver: local - elasticsearch_data: - driver: local + m2_db: + m2_redis: + m2_elasticsearch: + m2_html: \ No newline at end of file diff --git a/setup.sh b/setup.sh index 114b0fc..62d2856 100755 --- a/setup.sh +++ b/setup.sh @@ -1,11 +1,4 @@ #!/bin/bash -DIR="$( cd "$( dirname "$0" )" && pwd )" # Get the current directory -# Colors for the output -GREEN=$(tput setaf 2) -RED=$(tput setaf 1) -YELLOW=$(tput setaf 3) -WHITE=$(tput setaf 7) -NC=$(tput sgr0) # No color unameOut="$(uname -s)" case "${unameOut}" in Linux*) open_cmd=xdg-open;; @@ -13,51 +6,111 @@ case "${unameOut}" in *) open_cmd=start esac -set -o allexport -# shellcheck source=.env.sample -source .env.sample -if [ -f .env ]; then - source .env -else - echo "⚠️ No .env file found, coping from .env.sample" +if [ ! -f .env ]; then cp .env.sample .env fi + +ngrok=0 +build=0 +open_browser=0 + +# Parse arguments: +# --build: Build of docker images +# --ngrok-token=YOUR_NGROK_TOKEN: Override the ngrok token in .env +# --ngrok: Use ngrok to expose the site +# --open-browser: Open the browser after the installation is complete +while [[ "$#" -gt 0 ]]; do + if [ "$1" == "--ngrok" ]; then + ngrok=1 + elif [ "$1" == "--build" ]; then + build=1 + elif [[ "$1" == --ngrok-token=* ]]; then + ngrok_token="${1#*=}" + sed -i.bak "s|NGROK_AUTHTOKEN=.*|NGROK_AUTHTOKEN=$ngrok_token|" .env + rm .env.bak + elif [ "$1" == "--open-browser" ]; then + open_browser=1 + fi + shift +done + +# Reset PUBLIC_URL inside .env +sed -i.bak "s|PUBLIC_URL=.*|PUBLIC_URL=|" .env +rm .env.bak + +set -o allexport +source .env set +o allexport -docker compose up -d --build --remove-orphans || { echo "❌ Failed to start docker compose" ; exit 1; } +if [ $ngrok -eq 1 ]; then + + if [ -z "$NGROK_AUTHTOKEN" ]; then + echo "❌ Please set NGROK_AUTHTOKEN with your ngrok auth token in your .env file (get it from https://dashboard.ngrok.com/)" + exit 1 + fi + + echo "🚀 Starting ngrok..." + + docker run -d -e NGROK_AUTHTOKEN=$NGROK_AUTHTOKEN \ + -p $NGROK_PORT:4040 \ + --name $NGROK_CONTAINER_NAME \ + --add-host=host:host-gateway \ + ngrok/ngrok:alpine \ + http host:$M2_HTTP_PORT + + M2_URL="" + retry=10 + timeout=1 + start=$(date +%s) + while [ -z "$M2_URL" ]; do + sleep $timeout + M2_URL=$(curl -s http://localhost:$NGROK_PORT/api/tunnels | grep -o '"public_url":"[^"]*"' | sed 's/"public_url":"\(.*\)"/\1/' | head -n 1) + if [ $(($(date +%s) - $start)) -gt $retry ]; then + docker rm -f $NGROK_CONTAINER_NAME || true + echo "❌ Error getting public url from ngrok after ${retry} seconds" + exit 1 + fi + done + + # Overwrite PUBLIC_URL inside .env + sed -i.bak "s|PUBLIC_URL=.*|PUBLIC_URL=$M2_URL|" .env + rm .env.bak + + echo "✅ Ngrok started. Public URL: $M2_URL" +fi + +if [ $build -eq 1 ]; then + docker compose up -d --build || exit 1 +else + docker compose up -d || exit 1 +fi echo "🚀 Waiting for installation to complete..." -retry=600 +retry=300 # 5 minutes timeout=1 start=$(date +%s) while [ $(($(date +%s) - $start)) -lt $retry ]; do - # Check if Magento is up and running against exposed http port just in case varnish or anything else is set in front. - response_code="$(curl -s -o /dev/null -w ''%{http_code}'' "http://localhost:${MAGENTO_HTTP_PORT}")" - if [[ $response_code == "000" ]] ; then - echo -ne "⏳ Waiting for Magento to be up and running... $(($(date +%s) - $start)) / $retry "\\r - sleep $timeout - docker compose ps --services | grep -q magento || { echo -ne \\r\\n"❌ Magento container failed"\\r\\n ; exit 1; } - continue - fi - if [[ $response_code == "500" ]] ; then - echo "❌ Something went wrong and Magento returned a 500 error" - exit 1; - fi + if docker compose exec web ls /var/www/html/.post-install-complete > /dev/null 2>&1; then + seconds=$(($(date +%s) - $start)) + echo "✅ Done in ${seconds} seconds." + echo "🔗 Browse products at ${M2_URL}" + echo "🔗 Access Admin at ${M2_URL}/admin" + echo "User: $M2_ADMIN_USER" + echo "Password: $M2_ADMIN_PASSWORD" + + if [ $open_browser -eq 1 ]; then + echo "🚀 Opening the browser..." + $open_cmd $M2_URL + fi - echo $GREEN - echo " ✅ Magento installed" - echo " Magento is up and running at http://${MAGENTO_HOST}:${MAGENTO_EXTERNAL_HTTP_PORT_NUMBER}/" - if [[ $MAGENTO_SAMPLEDATA == "true" || $MAGENTO_SAMPLEDATA == "yes" ]] ; then - $DIR/bin/install-sampledata || { echo "❌ Failed to install sample-data" ; exit 1; } + exit 0 + elif docker compose exec web ls /var/www/html/.post-install-failed > /dev/null 2>&1; then + seconds=$(($(date +%s) - $start)) + echo "❌ Installation failed after ${seconds} seconds." + exit 1 fi - $DIR/bin/magento setup:upgrade || { echo "❌ Failed to run setup:upgrade" ; exit 1; } - echo "🚀 Openning the browser..." - $open_cmd "http://${MAGENTO_HOST}:${MAGENTO_EXTERNAL_HTTP_PORT_NUMBER}/" - echo $NC - exit 0; + sleep $timeout done -echo $RED -echo "❌ Timeout after $retry seconds" -echo $NC +echo "❌ Timeout after ${retry} seconds" exit 1 \ No newline at end of file diff --git a/teardown.sh b/teardown.sh index 81e73db..cd60bb7 100755 --- a/teardown.sh +++ b/teardown.sh @@ -1,3 +1,7 @@ #!/bin/bash -docker-compose down --volumes --remove-orphans -docker system prune -a \ No newline at end of file +set -o allexport +source .env +set +o allexport + +docker compose down --volumes --remove-orphans +docker rm -f $NGROK_CONTAINER_NAME || true \ No newline at end of file