Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Feature/env init Magento cloud update #153

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 159 additions & 5 deletions environments/magento-cloud/init.env.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function getProjectAndEnvironment {
function :: {
echo
echo "==> [$(date +%H:%M:%S)] $@"
}
Comment on lines +1 to +4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently use this pattern anywhere, so I'm not sure we should start adopting it now

If we were, it should also be in the utility file - not in the individual command

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it makes sense.. let me remove it... I got the idea from here: https://github.com/davidalger/warden-env-magento2/blob/develop/.warden/commands/bootstrap.cmd


function getProjectAndEnvironment {
if [[ -f ".magento/local/project.yaml" ]] && [[ -f ".git/HEAD" ]]; then
PROJECT=$(cat .magento/local/project.yaml | grep id | sed "s/id: //")
ENVIRONMENT=$(cat .git/HEAD | sed "s/ref: refs\/heads\///")
Expand Down Expand Up @@ -84,7 +88,8 @@ sed "s/%ENVIRONMENT%/${ENVIRONMENT}/g" > .env


function getEnvironmentVariables {
ENV_DATA=$(magento-cloud var --columns=Name --level=environment --no-header --format=csv --project=$PROJECT --environment=$ENVIRONMENT)
ENV_DATA=$(magento-cloud var --columns=Name --level=environment \
--no-header --format=csv --project=$PROJECT --environment=$ENVIRONMENT)
ENV_DATA=$(echo "$ENV_DATA" | sort -u)
mkdir -p .warden
echo "version: '3.5'
Expand All @@ -96,21 +101,170 @@ services:
do
if [[ $LINE =~ ^env:([a-zA-Z0-9_]+) ]]
then
VAR=$(magento-cloud vget --property=value --level=environment --project=$PROJECT --environment=$ENVIRONMENT $LINE)
VAR=$(magento-cloud vget --property=value \
--level=environment --project=$PROJECT --environment=$ENVIRONMENT $LINE)
LINE=$(echo $LINE | sed 's/env://g')
echo " $LINE: '$VAR'" >> .warden/warden-env.yml
fi
done
}

while true; do
read -p $'\033[32mDo you want to import the Environment variables from '"$ENVIRONMENT"'? y/n\033[0m ' resp
read -p $'\033[32mDo you want to import the Environment variables from '"$ENVIRONMENT"'? y/n'$'\033[0m ' resp
case $resp in
[Yy]*)
echo "Saving enviroment variables in .warden/warden-env.yml";
:: Saving enviroment variables in .warden/warden-env.yml
getEnvironmentVariables
break;;
[Nn]*) exit;;
*) echo "Please answer (y)es or (n)o";;
esac
done

function backupEnvPhpFile {
if [[ -f "app/etc/env.php" ]]; then
cp app/etc/env.php app/etc/env.php.bak
:: "Backup of app/etc/env.php => app/etc/env.php.bak"
fi
}

function detectMultiStore {
config=$(cat ${WARDEN_DIR}/environments/magento-cloud/magento2-config.env)
config+="
system/default/web/unsecure/base_url=https://app."${WARDEN_ENV_NAME}".test/
system/default/web/secure/base_url=https://app."${WARDEN_ENV_NAME}".test/
system/default/web/secure/offloader_header=X-Forwarded-Proto
system/default/web/secure/use_in_frontend=1
system/default/web/secure/use_in_adminhtml=1
"
SQL=$(magento-cloud sql --project=$PROJECT --environment=$ENVIRONMENT --relationship=database \
-q "SELECT GROUP_CONCAT(code, '@') FROM store_website WHERE is_default = 0 ORDER BY website_id ASC;" \
| grep admin | sed 's/,//g' | sed 's/ //g' | sed 's/|//g' | sed 's/@/ /g' | sed 's/\r//g')

storesPhp=""

for website in $SQL; do
if [ "$website" != "admin" ]; then
config+="
system/websites/${website}/web/unsecure/base_url=https://${website/_/-}."${WARDEN_ENV_NAME}".test/
system/websites/${website}/web/secure/base_url=https://${website/_/-}."${WARDEN_ENV_NAME}".test/
"
storesPhp+=" case '${website/_/-}.${WARDEN_ENV_NAME}.test':
\$runCode = '${website}';
\$runType = 'website';
break;
"
fi
done
}

function createEnvPhpFile {
mkdir -p app/etc
ENV_PHP=$(magento-cloud ssh --project=$PROJECT --environment=$ENVIRONMENT php <<CODE
<?Php
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<?Php
<?php

\$config = <<<CONFIG
$config
CONFIG;
\$newConfig = require('app/etc/env.php');

\$lines = explode("\n",\$config);
ksort(\$lines);
foreach (\$lines as \$line) {
\$parts = explode('=', \$line, 2);
if(count(\$parts) != 2) continue;

\$path = explode('/', \$parts[0]);

\$current = &\$newConfig;
foreach (\$path as \$key) {
\$current = &\$current[\$key];
if (!is_array(\$current)) {
\$current = [];
}
}

switch (trim(trim(\$parts[1], "'"), '"')) {
case 'true':
\$current = true;
break;
case 'false':
\$current = false;
break;
case 'null':
\$current = null;
break;
default:
\$current = is_numeric(\$parts[1])?
(int)\$parts[1] : \$parts[1];
break;
}
}

\$export = var_export(\$newConfig, TRUE);
\$export = preg_replace("/^([ ]*)(.*)/m", '\$1\$1\$2', \$export);
\$array = preg_split("/\r\n|\n|\r/", \$export);
\$array = preg_replace(["/\s*array\s\(\$/", "/\)(,)?\$/", "/\s=>\s\$/"], [NULL, ']\$1', ' => ['], \$array);
echo join(PHP_EOL, array_filter(["["] + \$array));
?>
CODE
);
echo "<?php
return $ENV_PHP;" > app/etc/env.php

:: Created app/etc/env.php
}

while true; do
read -p $'\033[32mDo you want me to import the env.php from '"$ENVIRONMENT"' and update it for Den? y/n'$'\033[0m ' resp
case $resp in
[Yy]*)
backupEnvPhpFile
detectMultiStore
createEnvPhpFile
break;;
[Nn]*) exit;;
*) echo "Please answer (y)es or (n)o";;
esac
done

function createStoresPhpFile {
mkdir -p app/etc
if [[ -f "app/etc/stores.php" ]]; then
cp app/etc/stores.php app/etc/stores.php.bak
:: "Backup of app/etc/stores.php => app/etc/stores.php.bak"
fi

echo "<?php

use \Magento\Store\Model\StoreManager;
\$serverName = isset(\$_SERVER['HTTP_HOST']) ? \$_SERVER['HTTP_HOST'] : null;

switch (\$serverName) {
${storesPhp} default:
return;
}

if ((!isset(\$_SERVER[StoreManager::PARAM_RUN_TYPE])
|| !\$_SERVER[StoreManager::PARAM_RUN_TYPE])
&& (!isset(\$_SERVER[StoreManager::PARAM_RUN_CODE])
|| !\$_SERVER[StoreManager::PARAM_RUN_CODE])
) {
\$_SERVER[StoreManager::PARAM_RUN_CODE] = \$runCode;
\$_SERVER[StoreManager::PARAM_RUN_TYPE] = \$runType;
}" > app/etc/stores.php

:: Created app/etc/stores.php for multi-store. You still need to add it to your Composer.json [autoload], please read the documentation \(https://swiftotter.github.io/den/configuration/multipledomains.html\).
}

if [ storesPhp != "" ]; then
while true; do
read -p $'\033[32mYour environment is a multi-store, do you want me to create app/etc/stores.php settings? y/n'$'\033[0m ' resp
case $resp in
[Yy]*)
createStoresPhpFile
break;;
[Nn]*) exit;;
*) echo "Please answer (y)es or (n)o";;
esac
done
fi
97 changes: 97 additions & 0 deletions environments/magento-cloud/magento2-config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
MAGE_MODE=developer
backend/frontName=backend
cache/frontend/default/frontend_options/write_control=false
cache/frontend/page_cache/frontend_options/write_control=false
system/default/design/head/demonotice/enable=1
system/default/web/secure/offloader_header=X-Forwarded-Proto
system/default/web/secure/use_in_frontend=1
system/default/web/secure/use_in_adminhtml=1
system/default/web/seo/use_rewrites=1
system/default/web/url/use_store=0
system/default/web/url/redirect_to_base=1
system/default/web/cookie/cookie_path=null
system/default/web/cookie/cookie_httponly=1
system/default/web/cookie/cookie_restriction=0
system/default/web/session/use_remote_addr=0
system/default/web/session/use_http_via=0
system/default/web/session/use_http_x_forwarded_for=0
system/default/web/session/use_http_user_agent=0
system/default/web/session/use_frontend_sid=0
system/default/web/browser_capabilities/cookies=1
system/default/web/browser_capabilities/javascript=1
system/default/web/browser_capabilities/local_storage=1
system/default/catalog/frontend/list_allow_all=0
system/default/catalog/frontend/flat_catalog_category=0
system/default/catalog/frontend/flat_catalog_product=0
system/default/catalog/search/engine=elasticsearch7
system/default/catalog/search/enable_eav_indexer=1
system/default/catalog/search/elasticsearch7_server_hostname=elasticsearch
system/default/catalog/search/elasticsearch7_server_port=9200
system/default/catalog/search/elasticsearch7_index_prefix=magento2
system/default/catalog/search/elasticsearch7_enable_auth=0
system/default/catalog/search/elasticsearch7_server_timeout=15
system/default/system/full_page_cache/caching_application=2
system/default/system/full_page_cache/ttl=604800
system/default/dev/front_end_development_workflow/type=server_side_compilation
system/default/dev/template/allow_symlink=0
system/default/dev/template/minify_html=0
system/default/dev/js/merge_files=0
system/default/dev/js/enable_js_bundling=0
system/default/dev/js/minify_files=0
system/default/dev/js/translate_strategy=dictionary
system/default/dev/js/session_storage_logging=0
system/default/dev/js/session_storage_key=collected_errors
system/default/dev/css/merge_css_files=0
system/default/dev/css/minify_files=0
system/default/dev/static/sign=0
system/default/admin/url/use_custom=0
system/default/admin/url/use_custom_path=0
system/default/admin/security/use_form_key=1
system/default/admin/security/use_case_sensitive_login=0
system/default/admin/security/session_lifetime=7200
system/default/admin/security/lockout_failures=6
system/default/admin/security/lockout_threshold=30
system/default/admin/security/password_lifetime=90
system/default/admin/security/password_is_forced=1

db/connection/indexer/host=db
db/connection/indexer/dbname=magento
db/connection/indexer/username=magento
db/connection/indexer/password=magento
db/connection/indexer/model=mysql4
db/connection/indexer/engine=innodb
db/connection/indexer/initStatements=SET NAMES utf8;
db/connection/indexer/active=1
db/connection/indexer/persistent=null

db/connection/default/host=db
db/connection/default/dbname=magento
db/connection/default/username=magento
db/connection/default/password=magento
db/connection/default/model=mysql4
db/connection/default/engine=innodb
db/connection/default/initStatements=SET NAMES utf8;
db/connection/default/active=1
db/connection/default/driver_options/1014=false

db/slave_connection=null

session/save=redis
session/redis/host=redis
session/redis/port=6379
session/redis/password=

cache/frontend/default/backend=Magento\\Framework\\Cache\\Backend\\Redis
cache/frontend/default/backend_options/server=redis
cache/frontend/default/backend_options/port=6379
cache/frontend/default/backend_options/password=
cache/frontend/default/backend_options/remote_backend_options=null

cache/frontend/page_cache/backend=Magento\\Framework\\Cache\\Backend\\Redis
cache/frontend/page_cache/backend_options/server=redis
cache/frontend/page_cache/backend_options/port=6379
cache/frontend/page_cache/backend_options/password=

lock/provider=db
lock/config/path=null
lock/config/prefix=null