This commit is contained in:
Ramūnas Dronga 2016-03-01 12:36:55 +00:00
commit 3ca9c32f2e

View file

@ -39,6 +39,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
const DEPLOY_STRATEGY_GIT_REMOTE_CACHE = 'git-remote-cache'; const DEPLOY_STRATEGY_GIT_REMOTE_CACHE = 'git-remote-cache';
const DEPLOY_STRATEGY_GUESS = 'guess'; const DEPLOY_STRATEGY_GUESS = 'guess';
const DEFAULT_DEPLOY_STRATEGY = self::DEPLOY_STRATEGY_GUESS; const DEFAULT_DEPLOY_STRATEGY = self::DEPLOY_STRATEGY_GUESS;
const LOCK_FILE = '.mage/~working.lock';
/** /**
* Deploy has Failed * Deploy has Failed
@ -95,13 +96,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
* Current Status of the Deployment (in progress, succeded, failed) * Current Status of the Deployment (in progress, succeded, failed)
* @var string * @var string
*/ */
protected static $deployStatus = 'in_progress'; protected $deployStatus = self::IN_PROGRESS;
/** /**
* Total of Failed tasks * Total of Failed tasks
* @var integer * @var integer
*/ */
protected static $failedTasks = 0; protected $failedTasks = 0;
public function __construct() public function __construct()
{ {
@ -126,7 +127,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
*/ */
public static function getStatus() public static function getStatus()
{ {
return self::$deployStatus; return $this->deployStatus;
} }
/** /**
@ -145,12 +146,8 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
return 231; return 231;
} }
// Check for running instance and Lock if (!$this->lock()) {
if (file_exists(getcwd() . '/.mage/~working.lock')) {
Console::output('<red>There is already an instance of Magallanes running!</red>', 1, 2);
return 230; return 230;
} else {
touch(getcwd() . '/.mage/~working.lock');
} }
// Release ID // Release ID
@ -184,22 +181,22 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$this->runNonDeploymentTasks(AbstractTask::STAGE_PRE_DEPLOY, $this->getConfig(), 'Pre-Deployment'); $this->runNonDeploymentTasks(AbstractTask::STAGE_PRE_DEPLOY, $this->getConfig(), 'Pre-Deployment');
// Check Status // Check Status
if (self::$failedTasks > 0) { if ($this->failedTasks > 0) {
self::$deployStatus = self::FAILED; $this->deployStatus = self::FAILED;
Console::output('A total of <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2); Console::output('A total of <bold>' . $this->failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
} else { } else {
// Run Deployment Tasks // Run Deployment Tasks
$this->runDeploymentTasks(); $this->runDeploymentTasks();
// Check Status // Check Status
if (self::$failedTasks > 0) { if ($this->failedTasks > 0) {
self::$deployStatus = self::FAILED; $this->deployStatus = self::FAILED;
Console::output('A total of <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2); Console::output('A total of <bold>' . $this->failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
} } else {
// Run Post-Deployment Tasks // Run Post-Deployment Tasks
$this->runNonDeploymentTasks(AbstractTask::STAGE_POST_DEPLOY, $this->getConfig(), 'Post-Deployment'); $this->runNonDeploymentTasks(AbstractTask::STAGE_POST_DEPLOY, $this->getConfig(), 'Post-Deployment');
} }
}
// Time Information Hosts // Time Information Hosts
if ($this->hostsCount > 0) { if ($this->hostsCount > 0) {
@ -215,18 +212,15 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
Console::output('Total time: <bold>' . $timeText . '</bold>.', 1, 2); Console::output('Total time: <bold>' . $timeText . '</bold>.', 1, 2);
// Send Notifications // Send Notifications
$this->sendNotification(self::$failedTasks > 0 ? false : true); $this->sendNotification($this->failedTasks > 0 ? false : true);
// Unlock $this->unlock();
if (file_exists(getcwd() . '/.mage/~working.lock')) {
unlink(getcwd() . '/.mage/~working.lock');
}
if (self::$failedTasks === 0) { if ($this->failedTasks === 0) {
$exitCode = 0; $exitCode = 0;
} }
if (self::$deployStatus === self::FAILED) { if ($this->deployStatus === self::FAILED) {
$exitCode = 1; $exitCode = 1;
} }
@ -243,7 +237,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
protected function runNonDeploymentTasks($stage, Config $config, $title) protected function runNonDeploymentTasks($stage, Config $config, $title)
{ {
$tasksToRun = $config->getTasks($stage); $tasksToRun = $config->getTasks($stage);
self::$failedTasks = 0; $this->failedTasks = 0;
// PreDeployment Hook // PreDeployment Hook
if ($stage == AbstractTask::STAGE_PRE_DEPLOY) { if ($stage == AbstractTask::STAGE_PRE_DEPLOY) {
@ -260,11 +254,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// PostDeployment Hook // PostDeployment Hook
if ($stage == AbstractTask::STAGE_POST_DEPLOY) { if ($stage == AbstractTask::STAGE_POST_DEPLOY) {
// If Deploy failed, clear post deploy tasks
if (self::$deployStatus == self::FAILED) {
$tasksToRun = array();
}
// Change Branch Back // Change Branch Back
if ($config->deployment('scm', false)) { if ($config->deployment('scm', false)) {
array_push($tasksToRun, 'scm/change-branch'); array_push($tasksToRun, 'scm/change-branch');
@ -279,7 +268,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if (count($tasksToRun) == 0) { if (count($tasksToRun) == 0) {
Console::output('<bold>No </bold><light_cyan>' . $title . '</light_cyan> <bold>tasks defined.</bold>', 1, 3); Console::output('<bold>No </bold><light_cyan>' . $title . '</light_cyan> <bold>tasks defined.</bold>', 1, 3);
} else { return;
}
Console::output('Starting <bold>' . $title . '</bold> tasks:'); Console::output('Starting <bold>' . $title . '</bold> tasks:');
$tasks = 0; $tasks = 0;
@ -292,34 +283,31 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if ($this->runTask($task)) { if ($this->runTask($task)) {
$completedTasks++; $completedTasks++;
} else { } else {
self::$failedTasks++; $this->failedTasks++;
} }
} }
if ($completedTasks == $tasks) { $tasksColor = ($completedTasks == $tasks) ? 'green' : 'red';
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Console::output('Finished <bold>' . $title . '</bold> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3); Console::output('Finished <bold>' . $title . '</bold> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
} }
}
protected function runDeploymentTasks() protected function runDeploymentTasks()
{ {
if (self::$deployStatus == self::FAILED) { if ($this->deployStatus == self::FAILED) {
return; return;
} }
// Run Tasks for Deployment // Run Tasks for Deployment
$hosts = $this->getConfig()->getHosts(); $hosts = $this->getConfig()->getHosts();
$this->hostsCount = count($hosts); $this->hostsCount = count($hosts);
self::$failedTasks = 0; $this->failedTasks = 0;
if ($this->hostsCount == 0) { if ($this->hostsCount == 0) {
Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, skipping deployment tasks.</bold>', 1, 3); Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, skipping deployment tasks.</bold>', 1, 3);
} else { return;
}
$this->startTimeHosts = time(); $this->startTimeHosts = time();
foreach ($hosts as $hostKey => $host) { foreach ($hosts as $hostKey => $host) {
@ -349,7 +337,10 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if (count($tasksToRun) == 0) { if (count($tasksToRun) == 0) {
Console::output('<light_purple>Warning!</light_purple> <bold>No </bold><light_cyan>Deployment</light_cyan> <bold>tasks defined.</bold>', 2); Console::output('<light_purple>Warning!</light_purple> <bold>No </bold><light_cyan>Deployment</light_cyan> <bold>tasks defined.</bold>', 2);
Console::output('Deployment to <bold>' . $host . '</bold> skipped!', 1, 3); Console::output('Deployment to <bold>' . $host . '</bold> skipped!', 1, 3);
} else { $this->getConfig()->setHostConfig(null);
continue;
}
foreach ($tasksToRun as $taskData) { foreach ($tasksToRun as $taskData) {
$tasks++; $tasks++;
$task = Factory::get($taskData, $this->getConfig(), false, AbstractTask::STAGE_DEPLOY); $task = Factory::get($taskData, $this->getConfig(), false, AbstractTask::STAGE_DEPLOY);
@ -357,32 +348,27 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if ($this->runTask($task)) { if ($this->runTask($task)) {
$completedTasks++; $completedTasks++;
} else { } else {
self::$failedTasks++; $this->failedTasks++;
} }
} }
if ($completedTasks == $tasks) { $tasksColor = ($completedTasks == $tasks) ? 'green' : 'red';
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Console::output('Deployment to <bold>' . $this->getConfig()->getHost() . '</bold> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3); Console::output('Deployment to <bold>' . $this->getConfig()->getHost() . '</bold> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
// Reset Host Config // Reset Host Config
$this->getConfig()->setHostConfig(null); $this->getConfig()->setHostConfig(null);
} }
$this->endTimeHosts = time(); $this->endTimeHosts = time();
if (self::$failedTasks > 0) { if ($this->failedTasks > 0) {
self::$deployStatus = self::FAILED; $this->deployStatus = self::FAILED;
} else { } else {
self::$deployStatus = self::SUCCEDED; $this->deployStatus = self::SUCCEDED;
} }
// Releasing // Releasing
if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) === true) { if ($this->deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) === true) {
// Execute the Releases // Execute the Releases
Console::output('Starting the <bold>Releasing</bold>'); Console::output('Starting the <bold>Releasing</bold>');
$completedTasks = 0; $completedTasks = 0;
@ -452,7 +438,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
} }
} }
} }
}
protected function runRollbackTask(AbstractTask $task) protected function runRollbackTask(AbstractTask $task)
{ {
@ -466,9 +451,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$stagesString = implode(', ', $this->acceptedStagesToRollback); $stagesString = implode(', ', $this->acceptedStagesToRollback);
Console::output("<light_purple>Warning!</light_purple> <bold>Rollback during deployment can be called only at the stages: $stagesString <bold>", 1); Console::output("<light_purple>Warning!</light_purple> <bold>Rollback during deployment can be called only at the stages: $stagesString <bold>", 1);
Console::output("<bold>Rollback:<bold> <red>ABORTING</red>", 1, 3); Console::output("<bold>Rollback:<bold> <red>ABORTING</red>", 1, 3);
return false;
} elseif (count($hosts) == 0) { } elseif (count($hosts) == 0) {
Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, unable to get releases.</bold>', 1, 3); Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, unable to get releases.</bold>', 1, 3);
} else { return false;
}
// run rollback on all hosts
$result = true; $result = true;
foreach ($hosts as $hostKey => $host) { foreach ($hosts as $hostKey => $host) {
$hostConfig = null; $hostConfig = null;
@ -495,8 +484,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
} }
return $result; return $result;
} }
return false;
}
/** /**
* Runs a Task * Runs a Task
@ -512,44 +499,36 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if ($title === null) { if ($title === null) {
$title = 'Running <purple>' . $task->getName() . '</purple> ... '; $title = 'Running <purple>' . $task->getName() . '</purple> ... ';
} }
Console::output($title, 2, 0); Console::output($title, 2, 0);
$runTask = true;
if (($task instanceof SkipOnOverride) && $this->getConfig()->getParameter('overrideRelease', false)) { if (($task instanceof SkipOnOverride) && $this->getConfig()->getParameter('overrideRelease', false)) {
$runTask = false; Console::output('<yellow>SKIPPED</yellow>', 0);
return true;
} }
if ($runTask === true) {
try { try {
$result = $task->run(); $result = $task->run();
if ($result === true) { if ($result === true) {
Console::output('<green>OK</green>', 0); Console::output('<green>OK</green>', 0);
$result = true; return true;
} else { } else {
Console::output('<red>FAIL</red>', 0); Console::output('<red>FAIL</red>', 0);
$result = false;
} }
} catch (RollbackException $e) { } catch (RollbackException $e) {
Console::output('<red>FAIL, Rollback catched</red> [Message: ' . $e->getMessage() . ']', 0); Console::output('<red>FAIL, Rollback catched</red> [Message: ' . $e->getMessage() . ']', 0);
$this->runRollbackTask($task); $this->runRollbackTask($task);
$result = false;
} catch (ErrorWithMessageException $e) { } catch (ErrorWithMessageException $e) {
Console::output('<red>FAIL</red> [Message: ' . $e->getMessage() . ']', 0); Console::output('<red>FAIL</red> [Message: ' . $e->getMessage() . ']', 0);
$result = false;
} catch (SkipException $e) { } catch (SkipException $e) {
Console::output('<yellow>SKIPPED</yellow>', 0); Console::output('<yellow>SKIPPED</yellow>', 0);
$result = true; return true;
} catch (Exception $e) { } catch (Exception $e) {
Console::output('<red>FAIL</red>', 0); Console::output('<red>FAIL</red>', 0);
$result = false;
}
} else {
Console::output('<yellow>SKIPPED</yellow>', 0);
$result = true;
} }
return $result; return false;
} }
/** /**
@ -660,4 +639,34 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
return $strategy; return $strategy;
} }
/**
* Check for running instance and Lock
*
* @return boolean
*/
protected function lock()
{
// Check for running instance and Lock
$lockFile = getcwd() . '/' . self::LOCK_FILE;
if (file_exists($lockFile)) {
Console::output('<red>There is already an instance of Magallanes running!</red>', 1, 2);
return false;
}
touch($lockFile);
return true;
}
/**
* Unlock
*/
protected function unlock()
{
$lockFile = getcwd() . '/' . self::LOCK_FILE;
if (file_exists($lockFile)) {
unlink($lockFile);
}
}
} }