Skip to content

Commit

Permalink
[init] zero downtime deployment module
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Calef committed May 18, 2020
0 parents commit b594812
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Zepgram - Benjamin Calef

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions Model/Plugin/ChangeDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of Zepgram\ZeroDowntimeDeployment\Model\Plugin
*
* @package Zepgram\ZeroDowntimeDeployment
* @file ChangeDetector.php
* @date 18 05 2020 17:25
*
* @author Benjamin Calef <[email protected]>
* @copyright 2020 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
*/

declare(strict_types=1);

namespace Zepgram\ZeroDowntimeDeployment\Model\Plugin;

use Magento\Deploy\Model\DeploymentConfig\ChangeDetector as MagentoChangeDetector;
use Magento\Deploy\Model\DeploymentConfig\DataCollector;
use Magento\Deploy\Model\DeploymentConfig\Hash;
use Magento\Deploy\Model\DeploymentConfig\Hash\Generator as HashGenerator;
use Magento\Framework\App\State;

/**
* Class ChangeDetector
* Disable change detection in configuration.
*/
class ChangeDetector extends MagentoChangeDetector
{
/** @var State */
private $state;

/**
* ChangeDetector constructor.
* @param Hash $configHash
* @param HashGenerator $hashGenerator
* @param DataCollector $dataConfigCollector
* @param State $state
*/
public function __construct(
Hash $configHash,
HashGenerator $hashGenerator,
DataCollector $dataConfigCollector,
State $state
) {
$this->state = $state;
parent::__construct($configHash, $hashGenerator, $dataConfigCollector);
}

/**
* @param null $sectionName
* @return bool
*/
public function hasChanges($sectionName = null)
{
if ($this->state->getMode() === State::MODE_PRODUCTION) {
return false;
}

return parent::hasChanges($sectionName);
}
}
63 changes: 63 additions & 0 deletions Plugin/DbStatusValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of Zepgram\ZeroDowntimeDeployment\Plugin
*
* @package Zepgram\ZeroDowntimeDeployment
* @file DbStatusvalidator.php
* @date 18 05 2020 17:28
*
* @author Benjamin Calef <[email protected]>
* @copyright 2020 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
*/

declare(strict_types=1);

namespace Zepgram\ZeroDowntimeDeployment\Plugin;

use Magento\Framework\App\FrontController;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\State;
use Magento\Framework\Cache\FrontendInterface as FrontendCacheInterface;
use Magento\Framework\Module\Plugin\DbStatusValidator as MagentoDbStatusValidator;

/**
* Class DbStatusValidator
* Disable change detection in module version.
*/
class DbStatusValidator
{
/** @var FrontendCacheInterface */
private $cache;

/** @var State */
private $state;

/**
* DbStatusValidator constructor.
* @param FrontendCacheInterface $cache
* @param State $state
*/
public function __construct(
FrontendCacheInterface $cache,
State $state
) {
$this->cache = $cache;
$this->state = $state;
}

/**
* @param MagentoDbStatusValidator $subject
* @param FrontController $frontController
* @param RequestInterface $request
*/
public function beforeBeforeDispatch(
MagentoDbStatusValidator $subject,
FrontController $frontController,
RequestInterface $request
) {
if ($this->state->getMode() === State::MODE_PRODUCTION) {
$this->cache->save('true', 'db_is_up_to_date');
}
}
}
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Zero Downtime Deployment #

Disable native change detection when Magento2 is on production mode to allow Zero Downtime Deployment (ZDD).

## Installation
```
composer require zepgram/module-zero-downtime-deployment
bin/magento module:enable Zepgram_ZeroDowntimeDeployment
bin/magento setup:upgrade
```

## Server

ZDD enables you to deploy your website without any downtime.
However, this module contains only necessary changes to make it possible on Magento2.

To be able to perform a complete ZDD you'll need a
<a href="https://www.google.com/search?q=blue+green+deployment+strategy&oq=blue+green+deployment+strategy">blue/green deployment strategy</a>.
Which depends on your hosting provider.

For example:
- AWS: https://aws.amazon.com/fr/quickstart/architecture/blue-green-deployment/
- Kubernetes: https://kubernetes.io/blog/2018/04/30/zero-downtime-deployment-kubernetes-jenkins/

The mainly steps to reach the ZDD with Magento2:
1. Start the deployment: green pods are the old one, for now they must stay active while creating blue pods.
1. Run `bin/magento setup:upgrade --keep-generated` to upgrade your database.
1. Upgrading is done: now green pods must be killed and replaced by blue pods based on health check statement.
1. Even if cache has been already cleared by `bin/magento setup:upgrade` you must clean it again with `bin/magento cache:flush` because old pods may corrupt your cache.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "zepgram/module-zero-downtime-deployment",
"description": "Disable native change detection when Magento2 is on production mode to allow Zero Downtime Deployment (ZDD)",
"type": "magento2-module",
"version": "0.0.1",
"require": {
"magento/framework": "^101.0.7|^102.0.0"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Zepgram\\ZeroDowntimeDeployment\\": ""
}
},
"license": "proprietary",
"repositories": {
"repo.magento.com": {
"type": "composer",
"url": "https://repo.magento.com/"
}
}
}
30 changes: 30 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!--
* This file is part of Zepgram\ZeroDowntimeDeployment\etc
*
* @package Zepgram\ZeroDowntimeDeployment
* @file di.xml
* @date 18 05 2020 17:20
*
* @author Benjamin Calef <[email protected]>
* @copyright 2020 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Deploy\Model\Plugin\ConfigChangeDetector">
<arguments>
<argument name="changeDetector" xsi:type="object">
Zepgram\ZeroDowntimeDeployment\Model\Plugin\ChangeDetector
</argument>
</arguments>
</type>
<type name="Magento\Framework\Module\Plugin\DbStatusValidator">
<plugin name="install_db_status_validator" type="Zepgram\ZeroDowntimeDeployment\Plugin\DbStatusValidator"/>
</type>
<type name="Zepgram\ZeroDowntimeDeployment\Plugin\DbStatusValidator">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
</arguments>
</type>
</config>
20 changes: 20 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<!--
* This file is part of Zepgram\ZeroDowntimeDeployment\etc
*
* @package Zepgram\ZeroDowntimeDeployment
* @file module.xml
* @date 18 05 2020 17:20
*
* @author Benjamin Calef <[email protected]>
* @copyright 2020 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Zepgram_ZeroDowntimeDeployment" setup_version="0.0.1">
<sequence>
<module name="Magento_Deploy"/>
</sequence>
</module>
</config>
7 changes: 7 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Zepgram_ZeroDowntimeDeployment',
__DIR__
);

0 comments on commit b594812

Please sign in to comment.