From ef4f09dd5e33d9ad8220c5716470f51538295e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Sat, 23 Jan 2021 20:01:13 +0100 Subject: [PATCH] Add Docker setup to simplify testing --- bin/bash | 2 + bin/cli | 3 ++ bin/clinotty | 3 ++ bin/composer | 2 + bin/dev-urn-catalog-generate | 6 +++ bin/devconsole | 2 + bin/download | 31 +++++++++++++++ bin/fixowns | 10 +++++ bin/fixperms | 11 ++++++ bin/grunt | 2 + bin/magento | 2 + bin/magerun2 | 7 ++++ bin/mysql | 9 +++++ bin/mysqldump | 2 + bin/node | 2 + bin/npm | 2 + bin/pwa-studio | 8 ++++ bin/redis | 2 + bin/remove | 2 + bin/removeall | 3 ++ bin/removevolumes | 8 ++++ bin/restart | 3 ++ bin/root | 3 ++ bin/rootnotty | 3 ++ bin/setup | 75 ++++++++++++++++++++++++++++++++++++ bin/setup-ssl | 17 ++++++++ bin/setup-ssl-ca | 15 ++++++++ bin/start | 57 +++++++++++++++++++++++++++ bin/status | 2 + bin/stop | 2 + bin/update | 8 ++++ bin/xdebug | 14 +++++++ docker-compose.dev.yml | 22 +++++++++++ docker-compose.yml | 54 ++++++++++++++++++++++++++ env/db.env | 5 +++ 35 files changed, 399 insertions(+) create mode 100755 bin/bash create mode 100755 bin/cli create mode 100755 bin/clinotty create mode 100755 bin/composer create mode 100755 bin/dev-urn-catalog-generate create mode 100755 bin/devconsole create mode 100755 bin/download create mode 100755 bin/fixowns create mode 100755 bin/fixperms create mode 100755 bin/grunt create mode 100755 bin/magento create mode 100755 bin/magerun2 create mode 100755 bin/mysql create mode 100755 bin/mysqldump create mode 100755 bin/node create mode 100755 bin/npm create mode 100755 bin/pwa-studio create mode 100755 bin/redis create mode 100755 bin/remove create mode 100755 bin/removeall create mode 100755 bin/removevolumes create mode 100755 bin/restart create mode 100755 bin/root create mode 100755 bin/rootnotty create mode 100755 bin/setup create mode 100755 bin/setup-ssl create mode 100755 bin/setup-ssl-ca create mode 100755 bin/start create mode 100755 bin/status create mode 100755 bin/stop create mode 100755 bin/update create mode 100755 bin/xdebug create mode 100644 docker-compose.dev.yml create mode 100644 docker-compose.yml create mode 100644 env/db.env diff --git a/bin/bash b/bin/bash new file mode 100755 index 0000000..55d7b05 --- /dev/null +++ b/bin/bash @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli bash diff --git a/bin/cli b/bin/cli new file mode 100755 index 0000000..3956059 --- /dev/null +++ b/bin/cli @@ -0,0 +1,3 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit +docker-compose exec phpfpm "$@" diff --git a/bin/clinotty b/bin/clinotty new file mode 100755 index 0000000..03f650d --- /dev/null +++ b/bin/clinotty @@ -0,0 +1,3 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit +docker-compose exec -T phpfpm "$@" diff --git a/bin/composer b/bin/composer new file mode 100755 index 0000000..1224439 --- /dev/null +++ b/bin/composer @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli composer "$@" diff --git a/bin/dev-urn-catalog-generate b/bin/dev-urn-catalog-generate new file mode 100755 index 0000000..f162b57 --- /dev/null +++ b/bin/dev-urn-catalog-generate @@ -0,0 +1,6 @@ +#!/bin/bash +bin/magento dev:urn-catalog:generate misc.xml +bin/copyfromcontainer misc.xml +mv src/misc.xml src/.idea/misc.xml + +echo "URN's have been generated, you may now restart PHPStorm" diff --git a/bin/devconsole b/bin/devconsole new file mode 100755 index 0000000..1c00dd7 --- /dev/null +++ b/bin/devconsole @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli bin/n98-magerun2.phar dev:console diff --git a/bin/download b/bin/download new file mode 100755 index 0000000..b9e3b26 --- /dev/null +++ b/bin/download @@ -0,0 +1,31 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify the version to download (ex. 2.0.0)" && exit + +edition=${2:-community} + +if [[ "$edition" == "enterprise" ]]; then + rm -rf src + composer create-project --repository=https://repo.magento.com/ --ignore-platform-reqs magento/project-enterprise-edition=$1 src + exit 0 +fi + +if [ ! -f ~/.docker-magento/magento2-$1.tar.gz ]; then + mkdir -p ~/.docker-magento + (cd ~/.docker-magento && curl -fOL http://pubfiles.nexcess.net/magento/ce-packages/magento2-$1.tar.gz) +fi + +# Fallback download to hypernode if archive doesn't exist on Nexcess (smaller than 1MB) +if [ $(find ~/.docker-magento/magento2-$1.tar.gz -size -1M) ]; then + (cd ~/.docker-magento && curl -o magento2-$1.tar.gz -fOL https://www.magento.mirror.hypernode.com/releases/magento-$1.tar.gz) +fi + +# Final fallback. If no archive exists, let's use Composer! +if [ ! -f ~/.docker-magento/magento2-$1.tar.gz ]; then + echo "Archive not found, or not yet available due to new version release." + echo "Attempting install with Composer..." + rm -rf src + composer create-project --repository=https://repo.magento.com/ --ignore-platform-reqs magento/project-community-edition=$1 src +else + echo "Extracting magento2-$1.tar.gz to ./src" + mkdir -p src && tar xzf ~/.docker-magento/magento2-$1.tar.gz -o -C src +fi diff --git a/bin/fixowns b/bin/fixowns new file mode 100755 index 0000000..78d7f3a --- /dev/null +++ b/bin/fixowns @@ -0,0 +1,10 @@ +#!/bin/bash +echo "Correcting filesystem ownerships..." + +if [ -z "$1" ]; then + bin/rootnotty chown -R app:app /var/www/ +else + bin/rootnotty chown -R app:app /var/www/html/$1 +fi + +echo "Filesystem ownerships corrected." diff --git a/bin/fixperms b/bin/fixperms new file mode 100755 index 0000000..c266e6f --- /dev/null +++ b/bin/fixperms @@ -0,0 +1,11 @@ +#!/bin/bash +echo "Correcting filesystem permissions..." + +if [ -z "$1" ]; then + bin/clinotty find var vendor pub/static pub/media app/etc \( -type f -or -type d \) -exec chmod u+w {} +; + bin/clinotty chmod u+x bin/magento +else + bin/clinotty find "$1" \( -type f -or -type d \) -exec chmod u+w {} +; +fi + +echo "Filesystem permissions corrected." diff --git a/bin/grunt b/bin/grunt new file mode 100755 index 0000000..49871a0 --- /dev/null +++ b/bin/grunt @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli grunt "$@" diff --git a/bin/magento b/bin/magento new file mode 100755 index 0000000..7e11cfa --- /dev/null +++ b/bin/magento @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli bin/magento "$@" diff --git a/bin/magerun2 b/bin/magerun2 new file mode 100755 index 0000000..76741bb --- /dev/null +++ b/bin/magerun2 @@ -0,0 +1,7 @@ +#!/bin/bash +if ! bin/clinotty ls bin/magerun2.phar 1> /dev/null 2>&1; then + bin/clinotty mkdir -p bin + bin/clinotty curl https://files.magerun.net/n98-magerun2.phar -o bin/magerun2.phar + bin/clinotty chmod +x bin/magerun2.phar +fi +bin/cli bin/magerun2.phar "$@" diff --git a/bin/mysql b/bin/mysql new file mode 100755 index 0000000..65ed699 --- /dev/null +++ b/bin/mysql @@ -0,0 +1,9 @@ +#!/bin/bash +source env/db.env +if [ -t 0 ]; then + # Need tty to run mysql shell + bin/cli mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} "$@" +else + # Read from stdin, ex: bin/mysql < dbdump.sql + bin/clinotty mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} "$@" +fi diff --git a/bin/mysqldump b/bin/mysqldump new file mode 100755 index 0000000..6696c7a --- /dev/null +++ b/bin/mysqldump @@ -0,0 +1,2 @@ +#!/bin/bash +bin/n98-magerun2 db:dump --stdout "$@" diff --git a/bin/node b/bin/node new file mode 100755 index 0000000..5614f8e --- /dev/null +++ b/bin/node @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli node "$@" diff --git a/bin/npm b/bin/npm new file mode 100755 index 0000000..3bcd6d6 --- /dev/null +++ b/bin/npm @@ -0,0 +1,2 @@ +#!/bin/bash +bin/cli npm "$@" diff --git a/bin/pwa-studio b/bin/pwa-studio new file mode 100755 index 0000000..19e677b --- /dev/null +++ b/bin/pwa-studio @@ -0,0 +1,8 @@ +#!/bin/bash +if [ ! -d pwa-studio ]; then + echo "PWA studio must first be installed by running bin/setup-pwa-studio" + exit +fi + +cd pwa-studio +NODE_TLS_REJECT_UNAUTHORIZED=0 yarn run watch:all diff --git a/bin/redis b/bin/redis new file mode 100755 index 0000000..5c678cd --- /dev/null +++ b/bin/redis @@ -0,0 +1,2 @@ +#!/bin/bash +docker-compose exec redis "$@" diff --git a/bin/remove b/bin/remove new file mode 100755 index 0000000..8ce3dc1 --- /dev/null +++ b/bin/remove @@ -0,0 +1,2 @@ +#!/bin/bash +docker-compose -f docker-compose.yml -f docker-compose.dev.yml rm --stop diff --git a/bin/removeall b/bin/removeall new file mode 100755 index 0000000..3a418f2 --- /dev/null +++ b/bin/removeall @@ -0,0 +1,3 @@ +#!/bin/bash +bin/remove +bin/removevolumes diff --git a/bin/removevolumes b/bin/removevolumes new file mode 100755 index 0000000..e3d256f --- /dev/null +++ b/bin/removevolumes @@ -0,0 +1,8 @@ +#!/bin/bash +current_folder=${PWD##*/} +volume_prefix=`echo $current_folder | awk '{print tolower($0)}' | sed 's/\.//g'` +docker volume rm ${volume_prefix}_appdata +docker volume rm ${volume_prefix}_dbdata +docker volume rm ${volume_prefix}_rabbitmqdata +docker volume rm ${volume_prefix}_sockdata +docker volume rm ${volume_prefix}_ssldata diff --git a/bin/restart b/bin/restart new file mode 100755 index 0000000..98c3134 --- /dev/null +++ b/bin/restart @@ -0,0 +1,3 @@ +#!/bin/bash +bin/stop "$@" +bin/start "$@" diff --git a/bin/root b/bin/root new file mode 100755 index 0000000..263e450 --- /dev/null +++ b/bin/root @@ -0,0 +1,3 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit +docker-compose exec -u root phpfpm "$@" diff --git a/bin/rootnotty b/bin/rootnotty new file mode 100755 index 0000000..5fb3e22 --- /dev/null +++ b/bin/rootnotty @@ -0,0 +1,3 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit +docker-compose exec -u root -T phpfpm "$@" diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..19338a8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,75 @@ +#!/bin/bash +BASE_URL=${1:-m2.localhost} +MAGE_VERSION=${2:-2.4.1} + +# load the DB env information +DIR="${BASH_SOURCE%/*}" +if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi +source "$DIR/../env/db.env" + +bin/restart +sleep 1 #Ensure containers are started... + +echo "Downloading Magento ${MAGE_VERSION} with Sample data..." +bin/root curl -o /var/www/html/magento.tar.gz https://pubfiles.nexcess.net/magento/ce-packages/magento2-with-samples-${MAGE_VERSION}.tar.gz +bin/rootnotty tar xvfz /var/www/html/magento.tar.gz +bin/rootnotty rm /var/www/html/magento.tar.gz +bin/rootnotty cp /var/www/html/nginx.conf.sample /var/www/html/nginx.conf +bin/rootnotty chown -R app.app /var/www/html +bin/rootnotty chmod +x bin/magento + +echo "Downloading magerun..." +bin/root curl -o /var/www/html/bin/magerun2.phar https://files.magerun.net/n98-magerun2-latest.phar +bin/rootnotty chmod +x /var/www/html/bin/magerun2.phar + +echo "Forcing reinstall of composer deps to ensure perms & reqs..." +bin/clinotty composer global require hirak/prestissimo +bin/clinotty composer update + +bin/clinotty bin/magento setup:install \ + --db-host=$MYSQL_HOST \ + --db-name=$MYSQL_DATABASE \ + --db-user=$MYSQL_USER \ + --db-password=$MYSQL_PASSWORD \ + --base-url=https://$BASE_URL/ \ + --base-url-secure=https://$BASE_URL/ \ + --backend-frontname=admin \ + --admin-firstname=Admin \ + --admin-lastname=User \ + --admin-email=admin@test.loc \ + --admin-user=magento \ + --admin-password=magento2\ + --language=en_US \ + --currency=EUR \ + --timezone=Europe/Berlin \ + --search-engine=elasticsearch7 \ + --elasticsearch-host=elasticsearch \ + --use-rewrites=1 + +echo "Turning on developer mode.." +bin/clinotty bin/magento deploy:mode:set developer +bin/clinotty bin/magento indexer:reindex + +echo "Forcing deploy of static content to speed up initial requests..." +bin/clinotty bin/magento setup:static-content:deploy -f + +echo "Re-indexing with Elasticsearch..." +bin/clinotty bin/magento indexer:reindex + +echo "Disable Magento 2FAuth module..." +bin/clinotty bin/magento module:disable Magento_TwoFactorAuth + +echo "Clearing the cache for good measure..." +bin/clinotty bin/magento cache:flush + +echo "Installing Sample data..." +bin/clinotty bin/magento sampledata:deploy + +echo "Generating SSL certificate..." +bin/setup-ssl $BASE_URL + +echo "Restarting containers with host bind mounts for dev..." +bin/restart + +echo "Docker development environment setup complete." +echo "You may now access your Magento instance at https://${BASE_URL}/" diff --git a/bin/setup-ssl b/bin/setup-ssl new file mode 100755 index 0000000..30bebb2 --- /dev/null +++ b/bin/setup-ssl @@ -0,0 +1,17 @@ +#!/bin/bash +[ -z "$1" ] && echo "Please specify a domain (ex. mydomain.test)" && exit + +# Generate certificate authority if not already setup +if ! docker-compose exec -T -u root app cat /root/.local/share/mkcert/rootCA.pem | grep -q 'BEGIN CERTIFICATE'; then + bin/setup-ssl-ca +fi + +# Generate the certificate for the specified domain +docker-compose exec -T -u root app mkcert -key-file nginx.key -cert-file nginx.crt "$@" +echo "Moving key and cert to /etc/nginx/certs/..." +docker-compose exec -T -u root app chown app:app nginx.key nginx.crt +docker-compose exec -T -u root app mv nginx.key nginx.crt /etc/nginx/certs/ + +# Restart nginx to apply the updates +echo "Restarting containers to apply updates..." +bin/restart diff --git a/bin/setup-ssl-ca b/bin/setup-ssl-ca new file mode 100755 index 0000000..221f2a4 --- /dev/null +++ b/bin/setup-ssl-ca @@ -0,0 +1,15 @@ +#!/bin/bash + +# Generate a new local CA "/root/.local/share/mkcert" +docker-compose exec -T -u root app mkcert -install + +docker cp $(docker-compose ps -q app|awk '{print $1}'):/root/.local/share/mkcert/rootCA.pem . +echo "System password requested to install certificate authority on host..." + +if [ "$(uname)" == "Darwin" ]; then + sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain rootCA.pem + rm rootCA.pem +else + sudo mv rootCA.pem /usr/local/share/ca-certificates/rootCA.pem + sudo update-ca-certificates +fi diff --git a/bin/start b/bin/start new file mode 100755 index 0000000..f955263 --- /dev/null +++ b/bin/start @@ -0,0 +1,57 @@ +#!/bin/bash + +## The following lines are commented oout per https://github.com/markshust/docker-magento/issues/351 + +<< 'COMMENTING-OUT' + +function parseYaml { + local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') + sed -ne "s|,$s\]$s\$|]|" \ + -e ":1;s|^\($s\)\($w\)$s:$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1\2: [\3]\n\1 - \4|;t1" \ + -e "s|^\($s\)\($w\)$s:$s\[$s\(.*\)$s\]|\1\2:\n\1 - \3|;p" $1 | \ + sed -ne "s|,$s}$s\$|}|" \ + -e ":1;s|^\($s\)-$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1- {\2}\n\1 \3: \4|;t1" \ + -e "s|^\($s\)-$s{$s\(.*\)$s}|\1-\n\1 \2|;p" | \ + sed -ne "s|^\($s\):|\1|" \ + -e "s|^\($s\)-$s[\"']\(.*\)[\"']$s\$|\1$fs$fs\2|p" \ + -e "s|^\($s\)-$s\(.*\)$s\$|\1$fs$fs\2|p" \ + -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ + -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" | \ + awk -F$fs '{ + indent = length($1)/2; + vname[indent] = $2; + for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}} + if (length($2) == 0) {vname[indent] = ++idx[indent] }; + if (length($3) > 0) { + vn=""; for (i=0; i