Skip to content

Commit

Permalink
Merge pull request #3 from shochdoerfer/feature/optimize_docker
Browse files Browse the repository at this point in the history
Fix Docker setup issues for development
  • Loading branch information
shochdoerfer committed Nov 6, 2021
2 parents fd50b71 + c85d52a commit 8cfcc03
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 49 deletions.
23 changes: 22 additions & 1 deletion bin/cache-clean
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
#!/bin/bash
bin/cli /var/www/.composer-global/vendor/bin/cache-clean.js "$@"
COMPOSER_GLOBAL=/var/www/.composer-global
CACHE_CLEAN=${COMPOSER_GLOBAL}/vendor/bin/cache-clean.js

if ! bin/cliq ls $CACHE_CLEAN; then
echo "Installing devtools metapackage, just a moment..."
bin/cliq mkdir -p ${COMPOSER_GLOBAL}
bin/composer require --working-dir=${COMPOSER_GLOBAL} --quiet markshust/magento2-metapackage-devtools-cli:^1.0
echo "Devtools installed."
fi

if [ "$1" == "--watch" ]; then
# Kill duplicate watch process
WATCH_PID=$(bin/clinotty ps -eaf | grep "$CACHE_CLEAN --quiet --watch" | grep -v grep | awk '{print $2}')
if [[ "" != "$WATCH_PID" ]]; then
bin/cliq kill -9 "$WATCH_PID"
fi

# Run watch mode in the background
bin/cliq $CACHE_CLEAN --quiet --watch &
else
bin/cli $CACHE_CLEAN "$@"
fi
2 changes: 2 additions & 0 deletions bin/captainhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
bin/cli /var/www/.n98-magerun2/modules/magerun2-list-api-endpoints/vendor/bin/captainhook "$@"
1 change: 1 addition & 0 deletions bin/clinotty
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit
# -T: Disable pseudo-tty allocation
docker-compose exec -T phpfpm "$@"
3 changes: 2 additions & 1 deletion bin/cliq
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit
bin/clinotty "$@" >/dev/null
# Without stdout and stderr
bin/clinotty "$@" >/dev/null 2>&1
5 changes: 5 additions & 0 deletions bin/cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit
bin/root service cron "$@"
bin/root touch /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status
bin/root chown app:app /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status
4 changes: 3 additions & 1 deletion bin/download
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -o errexit

VERSION=${1:-2.4.2}
VERSION=${1:-2.4.3-p1}
EDITION=${2:-community}

bin/stop
Expand All @@ -11,3 +11,5 @@ docker-compose -f docker-compose.yml up -d
bin/setup-composer-auth

bin/clinotty composer create-project --repository=https://repo.magento.com/ magento/project-"${EDITION}"-edition="${VERSION}" .

bin/clinotty cp /var/www/html/nginx.conf.sample /var/www/html/nginx.conf
9 changes: 9 additions & 0 deletions bin/githook
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# This script will be called from the Git Hooks on the host and redirect the CaptainHookPHP commands into the phpfpm
# container. For some odd reason it was needed to change the working directory so that CaptainHookPHP can resolve paths
# properly
[ -z "$1" ] && echo "Please specify a CLI command (ex. ls)" && exit
# make sure the phpfpm container is running before trying to execute the git hook
docker-compose up --no-recreate -d phpfpm
docker-compose exec -w /var/www/.n98-magerun2/modules/magerun2-list-api-endpoints/ -T phpfpm "$@"

2 changes: 2 additions & 0 deletions bin/mftf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
bin/clinotty vendor/bin/mftf "$@"
23 changes: 18 additions & 5 deletions bin/n98-magerun2
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#!/bin/bash
if ! bin/clinotty ls bin/n98-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/n98-magerun2.phar
bin/clinotty chmod +x bin/n98-magerun2.phar
if ! bin/cliq ls bin/n98-magerun2.phar; then
echo "Downloading n98-magerun2.phar, just a moment..."
bin/clinotty curl -sS -O https://files.magerun.net/n98-magerun2.phar
bin/clinotty curl -sS -o n98-magerun2.phar.sha256 https://files.magerun.net/sha256.php?file=n98-magerun2.phar
bin/clinotty shasum -a 256 -c n98-magerun2.phar.sha256
[ $? != 0 ] && echo "sha256 checksum do not match!" && exit

bin/cliq chmod +x n98-magerun2.phar
bin/cliq mkdir -p bin
bin/cliq mv n98-magerun2.phar bin
fi

S=$(bin/cli cat /usr/local/etc/php/php.ini | grep -iGc 'xdebug.mode = off');

if [[ $S == 1 ]]; then
bin/cli bin/n98-magerun2.phar "$@"
else
bin/cli php -dxdebug.mode=debug -dxdebug.start_with_request=yes bin/n98-magerun2.phar "$@"
fi
bin/cli bin/n98-magerun2.phar "$@"
30 changes: 12 additions & 18 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ source env/db.env
# shellcheck source=../env/magento.env
source env/magento.env

BASE_URL=${1:-magento2.test}
DOMAIN=${1:-magento.test}
ES_HOST=elasticsearch
ES_PORT=9200

bin/download

bin/stop

docker-compose -f docker-compose.yml up -d
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
[ $? != 0 ] && echo "Failed to start Docker services" && exit

bin/clinotty chmod u+x bin/magento
mv .vscode src

echo "Waiting for connection to Elasticsearch..."
bin/clinotty timeout 100 bash -c "
Expand All @@ -26,13 +27,15 @@ bin/clinotty timeout 100 bash -c "
done"
[ $? != 0 ] && echo "Failed to connect to Elasticsearch" && exit

sleep 2

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"/ \
--base-url=https://"$DOMAIN"/ \
--base-url-secure=https://"$DOMAIN"/ \
--backend-frontname="$MAGENTO_ADMIN_FRONTNAME" \
--admin-firstname="$MAGENTO_ADMIN_FIRST_NAME" \
--admin-lastname="$MAGENTO_ADMIN_LAST_NAME" \
Expand Down Expand Up @@ -62,29 +65,20 @@ bin/clinotty bin/magento setup:install \
--elasticsearch-port=$ES_PORT \
--use-rewrites=1

echo "Copying files from container to host after install..."
bin/copyfromcontainer --all

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 "Setting basic URL and generating SSL certificate..."
bin/setup-domain "${DOMAIN}"

echo "Clearing the cache to apply updates..."
bin/clinotty bin/magento cache:flush

echo "Generating SSL certificate..."
bin/setup-ssl "$BASE_URL"

echo "Ensuring Composer auth.json is setup..."
bin/setup-composer-auth

echo "Installing cron (see docker-compose.yml to enable)..."
bin/magento cron:install

echo "Turning on developer mode.."
bin/clinotty bin/magento deploy:mode:set developer

echo "Docker development environment setup complete."
echo "You may now access your Magento instance at https://${BASE_URL}/"
echo "You may now access your Magento instance at https://${DOMAIN}/"
2 changes: 1 addition & 1 deletion bin/setup-composer-auth
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fi
echo "composer config --global http-basic.repo.magento.com ${PUBLIC_KEY} ${PRIVATE_KEY}" | bin/clinotty bash -

# Also make sure alternate auth.json is setup (Magento uses this internally)
bin/clinotty cp /var/www/.composer/auth.json ./var/composer_home/auth.json
bin/clinotty [ -d "./var/composer_home" ] && cp /var/www/.composer/auth.json ./var/composer_home/auth.json

echo "Composer auth has been setup."
13 changes: 13 additions & 0 deletions bin/setup-domain
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -o errexit

[ -z "$1" ] && echo "Please specify a domain name (ex. magento.test)" && exit

DOMAIN=$1

echo "Set https://${DOMAIN}/ to web/secure/base_url and web/secure/base_url"
bin/magento config:set web/secure/base_url https://"$DOMAIN"/
bin/magento config:set web/unsecure/base_url https://"$DOMAIN"/

echo "Generating SSL certificate..."
bin/setup-ssl "$DOMAIN"
3 changes: 2 additions & 1 deletion bin/setup-grunt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ DEFAULT_THEME_ID="select value from core_config_data where path = 'design/theme/
THEME_PATH="select theme_path from theme where theme_id in ($DEFAULT_THEME_ID);"
VENDOR_THEME=$(bin/n98-magerun2 db:query "$THEME_PATH" | sed -n 2p | cut -d$'\r' -f1)
THEME=$(echo "$VENDOR_THEME" | cut -d'/' -f2)
LOCALE_CODE=$(bin/magento config:show general/locale/code | cut -d$'\r' -f1 | sed 's/ *$//g')

# Generate local-theme.js for custom theme
read -r -d '' GEN_THEME_JS << EOM
Expand All @@ -13,7 +14,7 @@ var theme = require('./dev/tools/grunt/configs/themes');
theme['$THEME'] = {
area: 'frontend',
name: '$VENDOR_THEME',
locale: 'en_US',
locale: '$LOCALE_CODE',
files: [
'css/styles-m',
'css/styles-l'
Expand Down
59 changes: 57 additions & 2 deletions bin/setup-ssl-ca
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,63 @@ 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

### Check if Firefox is installed
FFoxBin="/Applications/Firefox.app/Contents/MacOS/firefox-bin"
if [ -f "$FFoxBin" ]; then
sudo echo "{\"policies\": {\"Certificates\": {\"ImportEnterpriseRoots\": true}}}" > policies.json

### Check if distribution directory exists
DistDirectory="/Applications/Firefox.app/Contents/Resources/distribution"
if [ ! -d "$DistDirectory" ]; then
sudo mkdir "$DistDirectory"
fi
### Move the newly created policies.json to the Certificates directory
sudo mv policies.json "$DistDirectory"/policies.json

### Check if Certificates directory exists
CertDirectory="/Library/Application Support/Mozilla/Certificates"
if [ ! -d "$CertDirectory" ]; then
sudo mkdir "$CertDirectory"
fi

### Move the newly created .pem to the Certificates directory
sudo mv rootCA.pem "$CertDirectory"/rootCA.pem
else
sudo rm rootCA.pem
fi

else
sudo mv rootCA.pem /usr/local/share/ca-certificates/rootCA.pem

### Requirement: apt install libnss3-tools
REQUIRED_PKG="libnss3-tools"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG found. Setting up $REQUIRED_PKG."
sudo apt-get --yes install $REQUIRED_PKG
fi

### CA file to install (CUSTOMIZE!)
certfile="rootCA.pem"
certname="Root CA"

### For cert8 (legacy - DBM)
for certDB in $(find ~/ -name "cert8.db")
do
certdir=$(dirname ${certDB});
certutil -D -n "${certname}" -i ${certfile} -d dbm:${certdir}
certutil -A -n "${certname}" -t "TCu,Cu,Tu" -i ${certfile} -d dbm:${certdir}
done

### For cert9 (SQL)
for certDB in $(find ~/ -name "cert9.db")
do
certdir=$(dirname ${certDB});
certutil -D -n "${certname}" -i ${certfile} -d sql:${certdir}
certutil -A -n "${certname}" -t "TCu,Cu,Tu" -i ${certfile} -d sql:${certdir}
done

sudo mv rootCA.pem /usr/local/share/ca-certificates/rootCA.crt
sudo update-ca-certificates
fi
3 changes: 1 addition & 2 deletions bin/start
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d --remove-or
## Then uncomment the below line (and leave uncommented) to start the agent automatically with bin/start:
#bin/root /etc/init.d/blackfire-agent start

bin/devtools-cli-check
bin/cliq /var/www/.composer-global/vendor/bin/cache-clean.js --quiet --watch &
bin/cache-clean --watch
4 changes: 2 additions & 2 deletions bin/xdebug
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ xdebug_toggle() {

xdebug_enable() {
if [[ $S == 1 ]]; then
bin/root sed -i -e 's/^xdebug.mode = off/xdebug.mode = debug/g' /usr/local/etc/php/php.ini
bin/root sed -i -e 's/^xdebug.mode = off/xdebug.mode = debug,coverage/g' /usr/local/etc/php/php.ini
sleep 1
bin/restart phpfpm
echo "Xdebug debug mode has been enabled."
Expand All @@ -31,7 +31,7 @@ xdebug_enable() {

xdebug_disable() {
if [[ $S == 0 ]]; then
bin/root sed -i -e 's/^xdebug.mode = debug/xdebug.mode = off/g' /usr/local/etc/php/php.ini
bin/root sed -i -e 's/^xdebug.mode = debug,coverage/xdebug.mode = off/g' /usr/local/etc/php/php.ini
sleep 1
bin/restart phpfpm
echo "Xdebug debug mode has been disabled."
Expand Down
6 changes: 6 additions & 0 deletions captainhook.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"config": {
"run-mode": "docker",
"run-exec": "./bin/githook",
"git-directory": "/var/www/.n98-magerun2/modules/magerun2-list-api-endpoints/.git/",
"run-path": "/var/www/.n98-magerun2/modules/magerun2-list-api-endpoints/vendor/bin/captainhook"
},
"commit-msg": {
"enabled": true,
"actions": [
Expand Down
16 changes: 1 addition & 15 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,7 @@ services:
app:
volumes: &appvolumes
## Host mounts with performance penalty, only put what is necessary here
- ./src/app/code:/var/www/html/app/code:cached
- ./src/app/design:/var/www/html/app/design:cached
- ./src/app/etc:/var/www/html/app/etc:cached
- ./src/composer.json:/var/www/html/composer.json:cached
- ./src/composer.lock:/var/www/html/composer.lock:cached
- ./src/nginx.conf.sample:/var/www/html/nginx.conf:cached
#- ./src/auth.json:/var/www/html/auth.json:cached
#- ./src/m2-hotfixes:/var/www/html/m2-hotfixes:cached
#- ./src/patches:/var/www/html/patches:cached
#- ./src/var/log:/var/www/html/var/log:cached
#- ./src/var/report:/var/www/html/var/report:cached
## To sync your SSH to the container, uncomment the following line:
#- ~/.ssh/id_rsa:/var/www/.ssh/id_rsa:cached
## Linux only: remove the above lines (except nginx.conf line) and mount the entire src directory with:
#- ./src:/var/www/html:cached
- ./:/var/www/.n98-magerun2/modules/magerun2-list-api-endpoints/:cached

phpfpm:
volumes: *appvolumes
Expand Down

0 comments on commit 8cfcc03

Please sign in to comment.