Abort Deployment when pre-deploy task fails. Addresses issue #29

This commit is contained in:
Andrés Montañez 2013-11-08 13:42:34 -02:00
parent d8961674d3
commit 03b158c0a5
3 changed files with 212 additions and 159 deletions

View file

@ -70,8 +70,18 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
*/
protected $hostsCount = 0;
/**
* Current Status of the Deployment (in progress, succeded, failed)
* @var string
*/
protected static $deployStatus = 'in_progress';
/**
* Total of Failed tasks
* @var integer
*/
protected static $failedTasks = 0;
/**
* Returns the Status of the Deployment
*
@ -105,7 +115,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// Release ID
$this->getConfig()->setReleaseId(date('YmdHis'));
$failedTasks = 0;
// Deploy Summary
Console::output('<dark_gray>Deploy summary</dark_gray>', 1, 1);
@ -134,9 +143,131 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// Run Pre-Deployment Tasks
$this->runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
// Check Status
if (self::$failedTasks > 0) {
self::$deployStatus = self::FAILED;
Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
} else {
// Run Deployment Tasks
$this->runDeploymentTasks();
// Check Status
if (self::$failedTasks > 0) {
self::$deployStatus = self::FAILED;
Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
}
// Run Post-Deployment Tasks
$this->runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
}
// Time Information Hosts
if ($this->hostsCount > 0) {
$timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
$timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
}
// Time Information General
$timeText = $this->transcurredTime(time() - $this->startTime);
Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
// Send Notifications
$this->sendNotification();
// Unlock
if (file_exists('.mage/~working.lock')) {
unlink('.mage/~working.lock');
}
}
/**
* Execute Pre and Post Deployment Tasks
*
* @param string $stage
* @param Config $config
* @param string $title
*/
protected function runNonDeploymentTasks($stage, Config $config, $title)
{
$tasksToRun = $config->getTasks($stage);
self::$failedTasks = 0;
// PreDeployment Hook
if ($stage == 'pre-deploy') {
// Look for Remote Source
if (is_array($config->deployment('source', null))) {
array_unshift($tasksToRun, 'scm/clone');
}
// Change Branch
if ($config->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
}
}
// PostDeployment Hook
if ($stage == 'post-deploy') {
// If Deploy failed, clear post deploy tasks
if (self::$deployStatus == self::FAILED) {
$tasksToRun = array();
}
// Change Branch Back
if ($config->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
$config->addParameter('_changeBranchRevert');
}
// Remove Remote Source
if (is_array($config->deployment('source', null))) {
array_push($tasksToRun, 'scm/remove-clone');
}
}
if (count($tasksToRun) == 0) {
Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
} else {
Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
$tasks = 0;
$completedTasks = 0;
foreach ($tasksToRun as $taskData) {
$tasks++;
$task = Factory::get($taskData, $config, false, $stage);
if ($this->runTask($task)) {
$completedTasks++;
} else {
self::$failedTasks++;
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
protected function runDeploymentTasks()
{
if (self::$deployStatus == self::FAILED) {
return;
}
// Run Tasks for Deployment
$hosts = $this->getConfig()->getHosts();
$this->hostsCount = count($hosts);
self::$failedTasks = 0;
if ($this->hostsCount == 0) {
Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
@ -197,7 +328,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if ($this->runTask($task)) {
$completedTasks++;
} else {
$failedTasks++;
self::$failedTasks++;
}
}
@ -215,9 +346,8 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
}
$this->endTimeHosts = time();
if ($failedTasks > 0) {
if (self::$failedTasks > 0) {
self::$deployStatus = self::FAILED;
Console::output('A total of <dark_gray>' . $failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
} else {
self::$deployStatus = self::SUCCEDED;
}
@ -292,101 +422,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
}
}
}
// Run Post-Deployment Tasks
$this->runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
// Time Information Hosts
if ($this->hostsCount > 0) {
$timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
$timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
}
// Time Information General
$timeText = $this->transcurredTime(time() - $this->startTime);
Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
// Send Notifications
$this->sendNotification();
// Unlock
if (file_exists('.mage/~working.lock')) {
unlink('.mage/~working.lock');
}
}
/**
* Execute Pre and Post Deployment Tasks
*
* @param string $stage
* @param Config $config
* @param string $title
*/
protected function runNonDeploymentTasks($stage, Config $config, $title)
{
$tasksToRun = $config->getTasks($stage);
// PreDeployment Hook
if ($stage == 'pre-deploy') {
// Look for Remote Source
if (is_array($config->deployment('source', null))) {
array_unshift($tasksToRun, 'scm/clone');
}
// Change Branch
if ($config->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
}
}
// PostDeployment Hook
if ($stage == 'post-deploy') {
// If Deploy failed, clear post deploy tasks
if (self::$deployStatus == self::FAILED) {
$tasksToRun = array();
}
// Change Branch Back
if ($config->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
$config->addParameter('_changeBranchRevert');
}
// Remove Remote Source
if (is_array($config->deployment('source', null))) {
array_push($tasksToRun, 'scm/remove-clone');
}
}
if (count($tasksToRun) == 0) {
Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
} else {
Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
$tasks = 0;
$completedTasks = 0;
foreach ($tasksToRun as $taskData) {
$tasks++;
$task = Factory::get($taskData, $config, false, $stage);
if ($this->runTask($task)) {
$completedTasks++;
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
/**

View file

@ -4,18 +4,19 @@ deployment:
from: ./
to: /var/www/
scm:
branch: master2
branch: master
releases:
enabled: true
max: 5
symlink: current
directory: releases
hosts:
# - localhost
- localhost
# - dbserver
tasks:
pre-deploy:
- sampleTask
# - failTask
- scm/update
on-deploy:
- privileges

View file

@ -0,0 +1,17 @@
<?php
namespace Task;
use Mage\Task\AbstractTask;
class FailTask extends AbstractTask
{
public function getName()
{
return 'A Failing Task';
}
public function run()
{
return false;
}
}