diff --git a/.docker/magento/Dockerfile.php74 b/.docker/magento/Dockerfile.php74 new file mode 100644 index 0000000..e21dccb --- /dev/null +++ b/.docker/magento/Dockerfile.php74 @@ -0,0 +1,87 @@ +# Dockerfile para PHP 7.4 con las extensiones necesarias para Magento Open Source +FROM php:7.4-apache + +SHELL ["/bin/bash", "-c"] + +RUN apt-get update && apt-get install -y --no-install-recommends \ + mariadb-client \ + libfreetype6-dev \ + libjpeg62-turbo-dev \ + libpng-dev \ + libicu-dev \ + libxslt1-dev \ + libzip-dev \ + libcurl4-openssl-dev \ + libonig-dev \ + libsodium-dev \ + git \ + curl \ + unzip \ + tar \ + libpcre3 \ + && pecl install xdebug-3.1.6 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 \ + bcmath \ + curl \ + opcache \ + gd \ + intl \ + mbstring \ + mysqli \ + pdo_mysql \ + simplexml \ + soap \ + sockets \ + sodium \ + tokenizer \ + xmlwriter \ + xsl \ + zip \ + && docker-php-ext-enable \ + redis \ + xdebug \ + && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.client_host=${M2_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.log_level=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && sed -i 's/^/;/' "/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini" \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install composer +COPY --from=composer:2 /usr/bin/composer /usr/bin/composer + +# Apache configuration +RUN a2enmod rewrite && echo "ServerName localhost" >> /etc/apache2/apache2.conf + +# PHP configuration +COPY ./docker-php-m2.ini /usr/local/etc/php/conf.d/docker-php-m2.ini + +# Custom scripts +COPY ./m2-entrypoint.php74.sh /usr/local/bin/m2-entrypoint +COPY ./toggle-xdebug.sh /usr/local/bin/toggle-xdebug +RUN chmod +x /usr/local/bin/m2-entrypoint && \ +chmod +x /usr/local/bin/toggle-xdebug + +ARG M2_VERSION + +RUN mkdir -p /Sequra/Core \ + && chown -R www-data:www-data /Sequra \ + && chmod -R 755 /Sequra \ + && mkdir -p /var/www/.composer/cache/files/ \ + && chown -R www-data:www-data /var/www/.composer + +WORKDIR /var/www/html + +RUN --mount=type=secret,id=M2_COMPOSER_REPO_KEY,env=M2_REPO_KEY \ + --mount=type=secret,id=M2_COMPOSER_REPO_SECRET,env=M2_REPO_SECRET \ + composer config -g --no-plugins allow-plugins true \ + && composer config -g http-basic.repo.magento.com $M2_REPO_KEY $M2_REPO_SECRET \ + && composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=$M2_VERSION . \ + && composer require n98/magerun2-dist 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/Dockerfile.php8 b/.docker/magento/Dockerfile.php8 new file mode 100644 index 0000000..9ef88d1 --- /dev/null +++ b/.docker/magento/Dockerfile.php8 @@ -0,0 +1,79 @@ +ARG PHP_VERSION=8.2 + +FROM php:${PHP_VERSION}-apache + +SHELL ["/bin/bash", "-c"] + +RUN apt-get update && apt-get install -y --no-install-recommends \ + mariadb-client \ + libfreetype6-dev \ + libjpeg62-turbo-dev \ + libpng-dev \ + libicu-dev \ + libxslt1-dev \ + libzip-dev \ + libssl-dev \ + git \ + curl \ + unzip \ + tar \ + libpcre3 \ + && 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 \ + bcmath \ + opcache \ + gd \ + intl \ + mysqli \ + pdo_mysql \ + soap \ + sockets \ + xsl \ + zip \ + && docker-php-ext-enable \ + redis \ + xdebug \ + && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.client_host=${M2_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.log_level=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && sed -i 's/^/;/' "/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini" \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install composer +COPY --from=composer:2 /usr/bin/composer /usr/bin/composer + +# Apache configuration +RUN a2enmod rewrite && echo "ServerName localhost" >> /etc/apache2/apache2.conf + +# PHP configuration +COPY ./docker-php-m2.ini /usr/local/etc/php/conf.d/docker-php-m2.ini + +# Custom scripts +COPY ./m2-entrypoint.sh /usr/local/bin/m2-entrypoint +COPY ./toggle-xdebug.sh /usr/local/bin/toggle-xdebug +RUN chmod +x /usr/local/bin/m2-entrypoint && \ +chmod +x /usr/local/bin/toggle-xdebug + +ARG M2_VERSION + +RUN mkdir -p /Sequra/Core \ + && chown -R www-data:www-data /Sequra \ + && chmod -R 755 /Sequra \ + && mkdir -p /var/www/.composer/cache/files/ \ + && chown -R www-data:www-data /var/www/.composer + +WORKDIR /var/www/html + +RUN --mount=type=secret,id=M2_COMPOSER_REPO_KEY,env=M2_REPO_KEY \ + --mount=type=secret,id=M2_COMPOSER_REPO_SECRET,env=M2_REPO_SECRET \ + composer config -g http-basic.repo.magento.com $M2_REPO_KEY $M2_REPO_SECRET \ + && composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=$M2_VERSION . \ + && composer require n98/magerun2-dist 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/HelperModule/Sequra/Helper/Console/Setup.php b/.docker/magento/HelperModule/Sequra/Helper/Console/Setup.php new file mode 100644 index 0000000..e74aaa0 --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/Console/Setup.php @@ -0,0 +1,68 @@ +setName(self::NAME) + ->setDescription('Setup data for quick testing'); + parent::configure(); + } + /** + * Execute command. + * + * @param InputInterface $input InputInterface + * @param OutputInterface $output OutputInterface + * + * @return int 0 if everything went fine, or an exit code + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $customerRepository = $objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class); + + $customer = $customerRepository->getById(1); + $customer->setLastname('Costello Costello'); + $addresses = $customer->getAddresses(); + if (!empty($addresses)) { + $address = reset($addresses); + $address->setStreet(['Nueva Calle', 'Piso 2']); + $address->setCity('Barcelona'); + $address->setPostcode('08010'); + $address->setCountryId('ES'); + $address->setTelephone('666666666'); + $regionFactory = $objectManager->create(\Magento\Directory\Model\RegionFactory::class); + $region = $regionFactory->create()->loadByName('Barcelona','ES'); + if ($region->getId()) { + $address->setRegionId($region->getId()); + } + } + + $customerRepository->save($customer); + $output->writeln("Dirección actualizada correctamente"); + return 0; + } +} \ No newline at end of file diff --git a/.docker/magento/HelperModule/Sequra/Helper/composer.json b/.docker/magento/HelperModule/Sequra/Helper/composer.json new file mode 100644 index 0000000..65aa57d --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/composer.json @@ -0,0 +1,15 @@ +{ + "name": "sequra/magento2-helper", + "type": "magento2-module", + "autoload": { + "psr-4": { + "Sequra\\Helper\\": "" + } + }, + "authors": [ + { + "name": "Sequra Engineering" + } + ], + "require": {} +} diff --git a/.docker/magento/HelperModule/Sequra/Helper/etc/config.xml b/.docker/magento/HelperModule/Sequra/Helper/etc/config.xml new file mode 100644 index 0000000..48c6d8f --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/etc/config.xml @@ -0,0 +1,3 @@ + + + diff --git a/.docker/magento/HelperModule/Sequra/Helper/etc/crontab.xml b/.docker/magento/HelperModule/Sequra/Helper/etc/crontab.xml new file mode 100644 index 0000000..0614ceb --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/etc/crontab.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.docker/magento/HelperModule/Sequra/Helper/etc/di.xml b/.docker/magento/HelperModule/Sequra/Helper/etc/di.xml new file mode 100644 index 0000000..e84bdf2 --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/etc/di.xml @@ -0,0 +1,11 @@ + + + + + + + Sequra\Helper\Console\Setup + + + + diff --git a/.docker/magento/HelperModule/Sequra/Helper/etc/events.xml b/.docker/magento/HelperModule/Sequra/Helper/etc/events.xml new file mode 100644 index 0000000..bfb5e96 --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/etc/events.xml @@ -0,0 +1,3 @@ + + + diff --git a/.docker/magento/HelperModule/Sequra/Helper/etc/module.xml b/.docker/magento/HelperModule/Sequra/Helper/etc/module.xml new file mode 100644 index 0000000..9df991e --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/etc/module.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.docker/magento/HelperModule/Sequra/Helper/registration.php b/.docker/magento/HelperModule/Sequra/Helper/registration.php new file mode 100644 index 0000000..d42ec11 --- /dev/null +++ b/.docker/magento/HelperModule/Sequra/Helper/registration.php @@ -0,0 +1,9 @@ +","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/.gitattributes b/.gitattributes index 07a9262..708212f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,6 +4,7 @@ .gitattributes export-ignore docker-compose.yml export-ignore docker-compose.override.sample.yml export-ignore +README.md export-ignore # Exclude specific directories .github export-ignore @@ -11,6 +12,8 @@ docs export-ignore docker export-ignore bin export-ignore docker-entrypoint-init.d export-ignore +.docker export-ignore +.github export-ignore # Exclude scripts *.sh export-ignore \ No newline at end of file diff --git a/README.md b/README.md index b57db26..c1c2d81 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The easiest, safest, and quickest way for your customers to pay on installments. +6.000 e-commerce and +1.5 million delight shoppers already use seQura. Are you still thinking about it? -This WooCommerce plugin allows you to make payments with [seQura](https://sequra.es). +This Magento 2 plugin allows you to make payments with [seQura](https://sequra.es). ### Benefits for merchants @@ -58,73 +58,75 @@ If you are not a seQura merchant yet, you can sign up [here](https://sqra.es/sig ## For developers -### How to try the module +### Starting the environment + The repository includes a docker-compose file to easily test the module. You can start the environment with the following command: ```bash ./setup.sh ``` +This will start a Magento 2 instance with the seQura module installed. You can access the admin panel at `http://localhost.sequrapi.com:8018/admin` with the credentials `admin`/`Admin123`. -This will start a Magento 2 instance with the seQura module installed. You can access the admin panel at `http://localhost:8018/admin` with the credentials `admin`/`password123`. +> [!IMPORTANT] +> Make sure you have the line `127.0.0.1 localhost.sequrapi.com` added in your hosts file. -Once installed you could start/stop the magento instance with `docker compose up` and `docker compose down` commands. +> [!NOTE] +> Once the setup is complete, the Magento root URL, back-office URL, and user credentials (including the password) will be displayed in your terminal. -### Customizing the environment -You could create you own .env file to customize the environment. You can copy the .env.example file and modify the values as needed. +Additionally, the setup script supports the following arguments: -In order to use magento sample data you MUST set your credentials for https://repo.magento.com/ in you .env file at +| Argument | Description | +| -------- | ------------------------------------------------------------------ | +| `--ngrok` | Starts an ngrok container to expose the site to internet using HTTPS. An ngrok Auth Token must be provided either as an argument or as a variable in the `.env` file for it to work | +| `--ngrok-token=YOUR_NGROK_TOKEN` | Define the ngrok Auth Token | +| `--build` | Force building Docker images | +| `--open-browser` | Open the browser and navigate to the Magento root URL once the installation is complete | -```bash -COMPOSER_AUTH='{"http-basic":{"repo.magento.com":{"username":"","password":""}}}' -``` +### Customization -Otherwise you will get a warning message: -> "warning": "You haven't provided your Magento authentication keys. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html" +When the setup script runs, it takes the configuration from the `.env` file in the root of the repository. If the file doesn't exists, it will create a new one, copying the `.env.sample` template. In order to customize your environment before the setup occurs, you might create your `.env` file. To avoid errors, is important that you make a duplicate of `.env.sample` and then rename it to `.env` -and the installation will fail. +You can read the `.env.sample` file to know what are the available configuration variables and understand the purpose of each one. -#### Other examples -* You can customize the Magento version by setting the `MAGENTO_VERSION` environment variable. -* You can customize the sequra/magento module version by setting the `SQ_M2_CORE_VERSION` environment variable. Leave it as local to use the local version of the module. -* You can customize the host and ports by setting the `MAGENTO_HOST` and `MAGENTO_HTTP_PORT` environment variable. +### Stopping the environment -### Loading sample data -You can load sample data with the following command: +To stop the containers and perform the cleanup operations run: ```bash -./bin/install-sampledata +./teardown.sh ``` -or setting the `MAGENTO_SAMPLEDATA` environment variable to `yes` when before running the ./setup.sh script. +## Utilities +This repo contains a group of utility scripts under `bin/` directory. The goal is to ease the execution of common tasks without installing additional software. -> After installing sample data you may get 404 errors for http://${MAGENTO_HOST}/%7B%7BMEDIA_URL%7D%7Dstyles.css. -> To fix this issue go to Content -> Design -> Configuration -> Edit your theme -> HTML Head -> Scripts and Style Sheets and change the line with `{{MEDIA_URL}}styles.css` to `media/styles.css` +| Utility | Description | +| -------- | ------------------------------------------------------------------ | +| `bin/composer ` | This is a wrapper to run composer commands. | +| `bin/magento ` | This is a wrapper to run Magento CLI commands. | +| `bin/n98-magerun2 ` | This is a wrapper to run n98 magerun CLI commands. | +| `bin/update-sequra` | Reinstall the seQura plugin in Magento's `vendor` directory using the project files as the repository. | +| `bin/xdebug` | Toggle XDebug on/off. By default XDebug comes disabled by default. | -### Other helper scripts -You can run commands in the Magento container with the following command: -```bash -./bin/magento -``` -To run magento commands in the container. +## Building Docker images -```bash -./bin/composer -``` -To run composer commands in the container. +The `docker-compose.yml` file uses a customized Magento 2 Docker image available at GitHub Packages Registry. Since the image is private, you need to authenticate to pull it. To do so, you need to create a GitHub Personal Access Token and store it in the `.env` file under the `GITHUB_TOKEN` variable. -```bash -./bin/mysql -``` -To open mysql terminal in the container. +Tools for building and pushing the Docker image are available in the `.docker/magento` directory. You can easily build and push the image by running the following commands: ```bash -./bin/shell +.docker/magento/build-image.sh ``` -To open a bash shell commands in the container. +The behavior of the script can be customized by setting the following arguments: -```bash -./bin/xdebug -``` -To toggle xdebug it will be enabled by default. +| Argument | Description | +| -------- | ------------------------------------------------------------------ | +| `--push` | Push the image to the GitHub Packages Registry. Authentication is required. | +| `--magento=` | The Magento version to use. Supported versions are 2.4.3-p3, 2.4.4-p11, 2.4.5-p10, 2.4.6-p8, 2.4.7-p3 | +| `--php=` | The PHP version to use. Supported versions are 7.4, 8.1, 8.2 | + +If arguments are not provided, the script will build the image using the values defined in the `.env` file. + +> [!NOTE] +> For pushing the image, you need a token with the `read:packages` and `write:packages` scopes. The token must be stored in the `.env` file under the `GITHUB_TOKEN` variable or as a global environment variable. \ No newline at end of file diff --git a/bin/composer b/bin/composer index 69ca5a4..d1469c3 100755 --- a/bin/composer +++ b/bin/composer @@ -1,2 +1,2 @@ #!/bin/bash -docker compose exec -u daemon -w /bitnami/magento magento php vendor/bin/composer $@ \ No newline at end of file +docker compose exec -u www-data magento composer $@ \ No newline at end of file diff --git a/bin/install-sampledata b/bin/install-sampledata deleted file mode 100755 index d27d2eb..0000000 --- a/bin/install-sampledata +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -echo " Installing sample data..." -docker compose exec -w /bitnami/magento magento bash -c "rm -rf var/di/* var/generation/*" -docker compose exec -u daemon -w /bitnami/magento magento bin/magento sampledata:deploy -docker compose exec -u daemon -w /bitnami/magento magento bin/magento setup:upgrade -docker compose exec -u daemon -w /bitnami/magento magento bin/magento cache:clean -docker compose exec -u daemon -w /bitnami/magento magento bin/magento cache:flush diff --git a/bin/magento b/bin/magento index 974390a..fea1e2b 100755 --- a/bin/magento +++ b/bin/magento @@ -1,2 +1,2 @@ #!/bin/bash -docker compose exec -u daemon -w /bitnami/magento magento bin/magento $@ \ No newline at end of file +docker compose exec -u www-data magento bin/magento $@ \ No newline at end of file diff --git a/bin/mysql b/bin/mysql deleted file mode 100755 index c33b08f..0000000 --- a/bin/mysql +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -docker compose exec -u daemon -w /bitnami/magento magento bash -c \ -"mysql -h \$MAGENTO_DATABASE_HOST -P \$MAGENTO_DATABASE_PORT_NUMBER -u \$MAGENTO_DATABASE_USER \$MAGENTO_DATABASE_NAME" diff --git a/bin/n98-magerun2 b/bin/n98-magerun2 index f3f8203..496b7e6 100755 --- a/bin/n98-magerun2 +++ b/bin/n98-magerun2 @@ -1,4 +1,2 @@ #!/bin/bash -docker compose exec -u daemon -w /bitnami/magento magento bash -c \ -"[ ! -f 'vendor/bin/n98-magerun2' ] && php vendor/bin/composer require n98/magerun2-dist" -docker compose exec -u daemon -w /bitnami/magento magento vendor/bin/n98-magerun2 $@ \ No newline at end of file +docker compose exec -u www-data magento vendor/bin/n98-magerun2 $@ diff --git a/bin/shell b/bin/shell deleted file mode 100755 index e5785a9..0000000 --- a/bin/shell +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -if [ -z "$1" ]; then - docker compose exec -u daemon -w /bitnami/magento magento bash -else - docker compose exec -u daemon -w /bitnami/magento magento bash -c "$@" -fi diff --git a/bin/update-sequra b/bin/update-sequra index 122976a..87d25c3 100755 --- a/bin/update-sequra +++ b/bin/update-sequra @@ -1,9 +1,3 @@ #!/bin/bash -echo "Mount Sequra Core module" -docker compose exec -u daemon -w /bitnami/magento magento bash -c " - php ./vendor/bin/composer config repositories.sequra/magento2-core path /Sequra/Core - COMPOSER_MIRROR_PATH_REPOS=1 php ./vendor/bin/composer reinstall sequra/magento2-core - bin/magento module:enable Sequra_Core - bin/magento setup:upgrade - /opt/bitnami/scripts/php/reload.sh -" \ No newline at end of file +echo "Updating Sequra_Core source code" +docker compose exec -u www-data magento /bin/bash -c "COMPOSER_MIRROR_PATH_REPOS=1 composer reinstall sequra/magento2-core && bin/magento module:enable Sequra_Core && bin/magento setup:upgrade" \ No newline at end of file diff --git a/bin/update-sources b/bin/update-sources deleted file mode 100755 index 159b177..0000000 --- a/bin/update-sources +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -docker cp ${COMPOSE_PROJECT_NAME:-magento2-core}-magento:/bitnami/magento .magento-src \ No newline at end of file diff --git a/bin/xdebug b/bin/xdebug index aa71b54..4091a3e 100755 --- a/bin/xdebug +++ b/bin/xdebug @@ -1,2 +1,2 @@ #!/bin/bash -docker compose exec magento /docker-entrypoint-init.d/xdebug.sh +docker compose exec magento toggle-xdebug \ No newline at end of file diff --git a/composer.json b/composer.json index 11bae1d..7841c3f 100644 --- a/composer.json +++ b/composer.json @@ -38,9 +38,20 @@ }, "archive": { "exclude": [ - "docs", - "scripts", - "Test" + ".docker", + ".github", + "bin", + "docker-entrypoint-init.d", + "Test", + ".env", + ".env.sample", + ".gitattributes", + ".gitignore", + "docker-compose.override.sample.yml", + "docker-compose.yml", + "README.md", + "setup.sh", + "teardown.sh" ] }, "require-dev": { @@ -51,4 +62,4 @@ "dealerdirect/phpcodesniffer-composer-installer": true } } -} +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 46030d2..a58fc24 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,49 +1,78 @@ +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: ghcr.io/sequra/magento2-core:$M2_VERSION-$PHP_VERSION + 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 + - ./.docker/magento/HelperModule/Sequra:/var/www/html/app/code/Sequra extra_hosts: - - "localhost.sequrapi.com:host-gateway" - depends_on: - - mariadb - - elasticsearch + - "${M2_HTTP_HOST}:host-gateway" + <<: *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/docker-entrypoint-init.d/00-set-developper-mode.sh b/docker-entrypoint-init.d/00-set-developper-mode.sh deleted file mode 100755 index 7af5482..0000000 --- a/docker-entrypoint-init.d/00-set-developper-mode.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -cd /bitnami/magento/ || exit 0 -bin/magento deploy:mode:set developer \ No newline at end of file diff --git a/docker-entrypoint-init.d/01-set-order_sequence.sh b/docker-entrypoint-init.d/01-set-order_sequence.sh deleted file mode 100755 index 026b3a8..0000000 --- a/docker-entrypoint-init.d/01-set-order_sequence.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -echo \ -"ALTER TABLE sequence_order_0 AUTO_INCREMENT = $(date +%s);" \ -"ALTER TABLE sequence_order_1 AUTO_INCREMENT = $(date +%s);" | \ -mysql -h $MAGENTO_DATABASE_HOST -P $MAGENTO_DATABASE_PORT_NUMBER -u $MAGENTO_DATABASE_USER $MAGENTO_DATABASE_NAME \ No newline at end of file diff --git a/docker-entrypoint-init.d/02-additional_setting.sh b/docker-entrypoint-init.d/02-additional_setting.sh deleted file mode 100755 index 0086a37..0000000 --- a/docker-entrypoint-init.d/02-additional_setting.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -cd /bitnami/magento/ || exit 0 -bin/magento config:set general/country/default $MAGENTO_COUNTRY \ No newline at end of file diff --git a/docker-entrypoint-init.d/install-sequra.sh b/docker-entrypoint-init.d/install-sequra.sh deleted file mode 100755 index e966dc4..0000000 --- a/docker-entrypoint-init.d/install-sequra.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash -echo "Mount Sequra Core module" -chown -R daemon /Sequra -cd /bitnami/magento/ || exit 0 -if [ "$SQ_M2_CORE_VERSION" = "local" ]; then - php ./vendor/bin/composer config repositories.sequra/magento2-core path /Sequra/Core - COMPOSER_MIRROR_PATH_REPOS=1 php ./vendor/bin/composer require sequra/magento2-core:^2.5 -else - composer require sequra/magento2-core:"$SQ_M2_CORE_VERSION" -fi -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; -bin/magento config:set dev/template/allow_symlink 1 -bin/magento module:enable Sequra_Core -bin/magento setup:upgrade -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" - -chown -R daemon ./* diff --git a/docker-entrypoint-init.d/xdebug.sh b/docker-entrypoint-init.d/xdebug.sh deleted file mode 100755 index e6e10ec..0000000 --- a/docker-entrypoint-init.d/xdebug.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash -source /opt/bitnami/scripts/apache/status.sh -if is_apache_running; then - if [ -e /opt/bitnami/php/etc/conf.d/xdebug.ini ]; then - echo "Disabling xdebug" - rm -f /opt/bitnami/php/etc/conf.d/xdebug.ini - else - echo "Enabling xdebug" - cat < /opt/bitnami/php/etc/conf.d/xdebug.ini -zend_extension = xdebug.so - -xdebug.mode = debug -xdebug.start_with_request = yes -xdebug.discover_client_host = 0 -xdebug.client_port = 9003 -xdebug.client_host=host.docker.internal -EOF - fi - /opt/bitnami/scripts/php/reload.sh -fi diff --git a/setup.sh b/setup.sh index 114b0fc..244534f 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,121 @@ 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 [ -z "$M2_COMPOSER_REPO_KEY" ]; then + echo "❌ Please set M2_COMPOSER_REPO_KEY with your Magento repo public key in your .env file" + exit 1 +fi + +if [ -z "$M2_COMPOSER_REPO_SECRET" ]; then + echo "❌ Please set M2_COMPOSER_REPO_SECRET with your Magento repo private key in your .env file" + exit 1 +fi + +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 magento 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