Skip to content

Instantly share code, notes, and snippets.

@aboritskiy
Last active July 1, 2021 15:15
Show Gist options
  • Save aboritskiy/b527f7bff7df24d2e62b1527505255bd to your computer and use it in GitHub Desktop.
Save aboritskiy/b527f7bff7df24d2e62b1527505255bd to your computer and use it in GitHub Desktop.
Magento2 EE Cloud tips and tricks

Magento2 EE Cloud tips and tricks

  1. Before you start - go through http://devdocs.magento.com/guides/v2.0/cloud/env/environments.html the information below is supposed to be an additional knowledge to the above.
  2. Production and staging environments have no GUI, don't confuse them with developer environment, which you can access via https://eu.magento.cloud/projects
  3. first time you push the code to the production/staging environment, make sure you have created magento database, but left it empty. I.e. no tables inside. Make sure the first deploy went well, i.e. the website runs and there are no error in the build log. Search for logs under /var/log/platform/<project-key>/post_deploy.log in production envronment and under /var/log/deploy.log in the dev environment.
  4. The fact that your SSH-key is added to developer environment doesn't mean you have access to the production environmnet - make sure to ask magento-cloud support people to add your key.
  5. Magento Cloud Support has 3 branches/departments: cloud support, application support and cdn support. Make sure you choose the right department when creating the ticket - this will help receiving the answer faster.
  6. There are 2 interfaces to create support tickets, magento one and zendesk based one. Use the the latter, is it the only which allows you to fullfil previous point.
  7. Multiwebsite setup. In case you need to have multi-website setup you are in trouble, since there is no out of the box solution from magento. See magento-vars.php and the console command. Check build log for the actual URLs. While debugging the setup - consider using app/etc/env.php to put some debugging statements, doing push and commit everytime takes way too much time because single deploy takes about 10-15 minutes. Be aware that magento-vars.php is not loaded in the production environment by default - ask support people.
  8. If after you made a push something went wrong and magetno is not functional - start checking logs. Build log, web-server logs, magento logs. Search for logs under ls -la /var/log/platform/<project-key>/post_deploy.log in production envronment and under /var/log/deploy.log in the dev environment.
  9. In case you need to limit acess to the production system, you have two options: custom fastly VCL or asking magento support team for setting up basic HTTP Auth.
  10. Having a magento-cloud project you receive a packet of systems running together, including Fastly, Blackfire, Newrelic. To get access to them contact project admin (the person who received initial credentials) or magento support.
  11. One should install magento fastly extensinon in order to use Fastly. Ask magento support for the credentials.
  12. In case you need to flush Fastly cache - use the API call to do that. For credentials - ask magento support. For executing the call, I'm using Postman.
  13. In case you use custom Fastly VCL, use this tool from magento-cloud/platform.sh guys to update the VCL.
  14. It might happen that after doing deploy you still see outdated styling or some php functionalities are missing. Try executing the following on the cloud instance:
    php bin/magento maintenance:enable; rm -rf var/* pub/static/*; php bin/magento module:disable Magento_Weee; php bin/magento module:enable Magento_Weee; php bin/magento setup:di:compile; php bin/magento setup:static-content:deploy; php bin/magento cache:flush; php bin/magento maintenance:disable
    
  15. Magento cloud support team has slack chat - ask support to get access there. unfortunately it was discontinued.
<?php
// enable, adjust and copy this code for each store you run
// Store #0, default one
//if (isHttpHost("example.com")) {
// $_SERVER["MAGE_RUN_CODE"] = "default";
// $_SERVER["MAGE_RUN_TYPE"] = "store";
//}
function isHttpHost($host)
{
if (!isset($_SERVER['HTTP_HOST'])) {
return false;
}
return strpos(str_replace('---', '.', $_SERVER['HTTP_HOST']), $host) === 0;
}
/**
* this is a modification to assign proper env variables
* to let magento initialize the right website in the cloud environment.
* it seems to be no better way than that
*
* according to the RFC-1035, the max label length is 63-ascii char
* and the max domain is length 255-ascii char. So we should use shorter domains to fit
* inside those limits.
*
* @see .magento/routes.yaml
* @see https://tools.ietf.org/html/rfc1035
*/
if (isHttpHost('corp.')) {
$_SERVER['MAGE_RUN_CODE'] = 'corporate';
$_SERVER['MAGE_RUN_TYPE'] = 'website';
} else if (isHttpHost('shop.')) {
$_SERVER['MAGE_RUN_CODE'] = 'shop';
$_SERVER['MAGE_RUN_TYPE'] = 'website';
} else {
$_SERVER['MAGE_RUN_CODE'] = 'base';
$_SERVER['MAGE_RUN_TYPE'] = 'website';
}
#.magento/routes.yaml
http://corp.{default}/:
cache:
cookies:
- '*'
default_ttl: 0
enabled: true
headers:
- Accept
- Accept-Language
redirects:
expires: -1s
paths: {}
ssi:
enabled: false
type: upstream
upstream: mymagento:php
http://shop.{default}/:
cache:
cookies:
- '*'
default_ttl: 0
enabled: true
headers:
- Accept
- Accept-Language
redirects:
expires: -1s
paths: {}
ssi:
enabled: false
type: upstream
upstream: mymagento:php
http://{default}/:
type: upstream
upstream: mymagento:php
<?php
namespace Sitewards\SyncURLs\Console\Command;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
use \Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use \Magento\Store\Model\StoreManagerInterface;
class SyncURLsCommand extends Command
{
const ERROR_CODE_NO_ERROR = 0;
const ERROR_CODE_GENERAL_ERROR = 1;
const ERROR_CODE_MAGENTO_CLOUD_ENV_IS_NOT_AVAILABLE = 2;
const ERROR_CODE_NO_WEBSITES_TO_UPDATE = 3;
const ERROR_CODE_MAGENTO_CLOUD_ENV_EMPTY_OR_CORRUPT = 4;
/** @var ConfigInterface */
protected $resourceConfig;
/**
* @see .magento/routes.yaml
* @see magento-vars.php
* this duplication is somehow ugly, need to find a way to read configs.
*
* @var array
*/
protected $labelToWebsiteCode = array(
'corp' => array(
'MAGE_RUN_CODE' => 'corporate',
'MAGE_RUN_TYPE' => 'website',
'scopeType' => 'websites'
),
'shop' => array(
'MAGE_RUN_CODE' => 'shop',
'MAGE_RUN_TYPE' => 'website',
'scopeType' => 'websites'
)
);
/** @var StoreManagerInterface */
protected $storeManager;
/**
* SyncURLsCommand constructor.
*
* @param ConfigInterface $resourceConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(ConfigInterface $resourceConfig, StoreManagerInterface $storeManager)
{
parent::__construct(null);
$this->resourceConfig = $resourceConfig;
$this->storeManager = $storeManager;
}
protected function configure()
{
$this->setName('sitewards:sync-urls')->setDescription('Reads base URL configuration from the environment and applies the config setting.');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (empty($_ENV['MAGENTO_CLOUD_ROUTES'])) {
$output->writeln('ERROR MAGENTO_CLOUD_ROUTES environment variable is not available');
return self::ERROR_CODE_MAGENTO_CLOUD_ENV_IS_NOT_AVAILABLE;
}
$websites = $this->storeManager->getWebsites(false, true);
if (empty($websites)) {
$output->writeln('ERROR This magento instance seem to have no websites to update');
return self::ERROR_CODE_NO_WEBSITES_TO_UPDATE;
}
$decodedRoutes = json_decode(base64_decode($_ENV['MAGENTO_CLOUD_ROUTES']));
if (empty($decodedRoutes)) {
$output->writeln('ERROR MAGENTO_CLOUD_ROUTES environment variable contains empty or mis-formatted data');
return self::ERROR_CODE_MAGENTO_CLOUD_ENV_EMPTY_OR_CORRUPT;
}
$hasErrors = false;
foreach ($decodedRoutes as $url => $value) {
$filteredUrl = $this->filterHostName($url);
$hostname = parse_url($filteredUrl, PHP_URL_HOST);
$protocol = parse_url($filteredUrl, PHP_URL_SCHEME);
$firstLabel = $this->getFirstHostNameLabel($hostname);
if (!empty($this->labelToWebsiteCode[$firstLabel])) {
$storeCode = $this->labelToWebsiteCode[$firstLabel]['MAGE_RUN_CODE'];
$scopeType = $this->labelToWebsiteCode[$firstLabel]['scopeType'];
$output->writeln('');
$output->writeln(sprintf('Assigning "%s" to "%s" as base URL... ', $url, $storeCode));
$output->write(' --> ');
if (empty($websites[$storeCode])) {
$output->writeln(sprintf('ERROR website "%s" does not exist, skipping.', $storeCode));
$hasErrors = true;
continue;
}
if ($protocol === 'http') {
$this->resourceConfig->saveConfig('web/unsecure/base_url', $url, $scopeType, $websites[$storeCode]->getId());
} else if ($protocol === 'https') {
$this->resourceConfig->saveConfig('web/secure/base_url', $url, $scopeType, $websites[$storeCode]->getId());
} else {
$output->writeln(sprintf('ERROR unknown protocol "%s", skipping.', $protocol));
$hasErrors = true;
continue;
}
$output->writeln('OK');
}
}
return $hasErrors ? self::ERROR_CODE_GENERAL_ERROR : self::ERROR_CODE_NO_ERROR;
}
/**
* @param string $hostName
*
* @return mixed
*/
protected function filterHostName($hostName)
{
return str_replace('---', '.', $hostName);
}
/**
* @param string $hostName
*
* @return string
*/
protected function getFirstHostNameLabel($hostName)
{
$labels = explode('.', $hostName);
if (empty($labels[0])) {
return '';
} else {
return $labels[0];
}
}
}
@diazwatson
Copy link

magetno -> Magento

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment