Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #74 from niels-nijens/deployment-event-flow
Browse files Browse the repository at this point in the history
Implemented release deployment event flow
  • Loading branch information
niels-nijens committed Nov 30, 2015
2 parents 40da335 + 976aa6c commit 2c0aecf
Show file tree
Hide file tree
Showing 17 changed files with 758 additions and 132 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"require": {
"php": ">=5.4.0",
"accompli/chrono": "^0.1",
"composer/semver": "^1.2",
"justinrainbow/json-schema": "^1.3",
"niels-nijens/utilities": "^3.2",
"phpseclib/phpseclib": "^2.0",
Expand Down
65 changes: 64 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 33 additions & 22 deletions src/AccompliEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/**
* AccompliEvents.
*
* @author Niels Nijens <nijens.niels@gmail.com>
**/
* @author Niels Nijens <nijens.niels@gmail.com>
*/
final class AccompliEvents
{
/**
Expand All @@ -26,27 +26,27 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\DeployReleaseEvent instance.
*
* @var string
**/
*/
const DEPLOY_RELEASE = 'accompli.deploy_release';

/**
* The DEPLOY_RELEASE_COMPLETE event is dispatched when a Release succesfully deployed.
* The DEPLOY_RELEASE_COMPLETE event is dispatched when a Release is succesfully deployed.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\DeployReleaseCompleteEvent instance.
* Accompli\EventDispatcher\Event\DeployReleaseEvent instance.
*
* @var string
**/
*/
const DEPLOY_RELEASE_COMPLETE = 'accompli.deploy_release_complete';

/**
* The DEPLOY_RELEASE_FAILED event is dispatched when deployment of a Release has failed.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\DeployReleaseFailedEvent instance.
* Accompli\EventDispatcher\Event\FailedEvent instance.
*
* @var string
**/
*/
const DEPLOY_RELEASE_FAILED = 'accompli.deploy_release_failed';

/**
Expand All @@ -56,17 +56,27 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\GatherFactsEvent instance.
*
* @var string
**/
*/
const GATHER_FACTS = 'accompli.gather_facts';

/**
* The GET_WORKSPACE event is dispatched when the Workspace instance from a host is required.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\WorkspaceEvent instance.
*
* @var string
*/
const GET_WORKSPACE = 'accompli.get_workspace';

/**
* The INSTALL_RELEASE event is dispatched when a Release requires installation.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\InstallReleaseEvent instance.
*
* @var string
**/
*/
const INSTALL_RELEASE = 'accompli.install_release';

/**
Expand All @@ -76,7 +86,7 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\InstallReleaseEvent instance.
*
* @var string
**/
*/
const INSTALL_RELEASE_COMPLETE = 'accompli.install_release_complete';

/**
Expand All @@ -86,7 +96,7 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\FailedEvent instance.
*
* @var string
**/
*/
const INSTALL_RELEASE_FAILED = 'accompli.install_release_failed';

/**
Expand All @@ -106,7 +116,7 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\PrepareDeployReleaseEvent instance.
*
* @var string
**/
*/
const PREPARE_DEPLOY_RELEASE = 'accompli.prepare_deploy_release';

/**
Expand All @@ -116,7 +126,7 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\PrepareReleaseEvent instance.
*
* @var string
**/
*/
const PREPARE_RELEASE = 'accompli.prepare_release';

/**
Expand All @@ -126,44 +136,44 @@ final class AccompliEvents
* Accompli\EventDispatcher\Event\PrepareWorkspaceEvent instance.
*
* @var string
**/
*/
const PREPARE_WORKSPACE = 'accompli.prepare_workspace';

/**
* The ROLLBACK_RELEASE event is dispatched when a previous Release is being deployed.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\RollbackReleaseEvent instance.
* Accompli\EventDispatcher\Event\DeployReleaseEvent instance.
*
* @var string
**/
*/
const ROLLBACK_RELEASE = 'accompli.rollback_release';

/**
* The ROLLBACK_RELEASE_COMPLETE event is dispatched when a previous Release is successfully deployed.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\RollbackReleaseCompleteEvent instance.
* Accompli\EventDispatcher\Event\DeployReleaseEvent instance.
*
* @var string
**/
*/
const ROLLBACK_RELEASE_COMPLETE = 'accompli.rollback_release_complete';

/**
* The ROLLBACK_RELEASE_FAILED event is dispatched when deployment of a previous Release has failed.
*
* The event listener receives an
* Accompli\EventDispatcher\Event\RollbackReleaseFailedEvent instance.
* Accompli\EventDispatcher\Event\FailedEvent instance.
*
* @var string
**/
*/
const ROLLBACK_RELEASE_FAILED = 'accompli.rollback_release_failed';

/**
* Returns an array with all the event names (constants).
*
* @return array
**/
*/
public static function getEventNames()
{
return array(
Expand All @@ -172,6 +182,7 @@ public static function getEventNames()
self::DEPLOY_RELEASE_COMPLETE,
self::DEPLOY_RELEASE_FAILED,
self::GATHER_FACTS,
self::GET_WORKSPACE,
self::INSTALL_RELEASE,
self::INSTALL_RELEASE_COMPLETE,
self::INSTALL_RELEASE_FAILED,
Expand Down
54 changes: 53 additions & 1 deletion src/Deployment/Strategy/AbstractDeploymentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace Accompli\Deployment\Strategy;

use Accompli\AccompliEvents;
use Accompli\Configuration\ConfigurationInterface;
use Accompli\DependencyInjection\ConfigurationAwareInterface;
use Accompli\DependencyInjection\EventDispatcherAwareInterface;
use Accompli\Deployment\Release;
use Accompli\Deployment\Workspace;
use Accompli\EventDispatcher\Event\DeployReleaseEvent;
use Accompli\EventDispatcher\Event\FailedEvent;
use Accompli\EventDispatcher\Event\HostEvent;
use Accompli\EventDispatcher\Event\PrepareDeployReleaseEvent;
use Accompli\EventDispatcher\Event\WorkspaceEvent;
use Accompli\EventDispatcher\EventDispatcherInterface;
use Composer\Semver\Comparator;
use Exception;

/**
* AbstractDeploymentStrategy.
Expand Down Expand Up @@ -47,7 +57,49 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
/**
* {@inheritdoc}
*/
public function deploy($version, $stage = null)
public function deploy($version, $stage)
{
$hosts = $this->configuration->getHostsByStage($stage);
foreach ($hosts as $host) {
$exception = null;

$deployEventName = AccompliEvents::DEPLOY_RELEASE;
$deployCompleteEventName = AccompliEvents::DEPLOY_RELEASE_COMPLETE;
$deployFailedEventName = AccompliEvents::DEPLOY_RELEASE_FAILED;

try {
$this->eventDispatcher->dispatch(AccompliEvents::CREATE_CONNECTION, new HostEvent($host));

$workspaceEvent = new WorkspaceEvent($host);
$this->eventDispatcher->dispatch(AccompliEvents::GET_WORKSPACE, $workspaceEvent);

$workspace = $workspaceEvent->getWorkspace();
if ($workspace instanceof Workspace) {
$prepareDeployReleaseEvent = new PrepareDeployReleaseEvent($workspace, $version);
$this->eventDispatcher->dispatch(AccompliEvents::PREPARE_DEPLOY_RELEASE, $prepareDeployReleaseEvent);

$release = $prepareDeployReleaseEvent->getRelease();
if ($release instanceof Release) {
$currentRelease = $prepareDeployReleaseEvent->getCurrentRelease();
if ($currentRelease instanceof Release && Comparator::lessThan($release->getVersion(), $currentRelease->getVersion())) {
$deployEventName = AccompliEvents::ROLLBACK_RELEASE;
$deployCompleteEventName = AccompliEvents::ROLLBACK_RELEASE_COMPLETE;
$deployFailedEventName = AccompliEvents::ROLLBACK_RELEASE_FAILED;
}

$deployReleaseEvent = new DeployReleaseEvent($release, $currentRelease);
$this->eventDispatcher->dispatch($deployEventName, $deployReleaseEvent);

$this->eventDispatcher->dispatch($deployCompleteEventName, $deployReleaseEvent);

continue;
}
}
} catch (Exception $exception) {
}

$failedEvent = new FailedEvent($this->eventDispatcher->getLastDispatchedEvent(), $exception);
$this->eventDispatcher->dispatch($deployFailedEventName, $failedEvent);
}
}
}
6 changes: 3 additions & 3 deletions src/Deployment/Strategy/DeploymentStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function install($version, $stage = null);
/**
* Deploys a release according to the deployment strategy.
*
* @param string $version
* @param string|null $stage
* @param string $version
* @param string $stage
*/
public function deploy($version, $stage = null);
public function deploy($version, $stage);
}
41 changes: 0 additions & 41 deletions src/EventDispatcher/Event/AbstractDeploymentEvent.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/EventDispatcher/Event/DeployReleaseCompleteEvent.php

This file was deleted.

Loading

0 comments on commit 2c0aecf

Please sign in to comment.