mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-06-16 20:45:02 +02:00
Merge 758fcf30da
into 47e4d7c9a1
This commit is contained in:
commit
3ca9c32f2e
|
@ -39,6 +39,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
const DEPLOY_STRATEGY_GIT_REMOTE_CACHE = 'git-remote-cache';
|
||||
const DEPLOY_STRATEGY_GUESS = 'guess';
|
||||
const DEFAULT_DEPLOY_STRATEGY = self::DEPLOY_STRATEGY_GUESS;
|
||||
const LOCK_FILE = '.mage/~working.lock';
|
||||
|
||||
/**
|
||||
* Deploy has Failed
|
||||
|
@ -95,13 +96,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
* Current Status of the Deployment (in progress, succeded, failed)
|
||||
* @var string
|
||||
*/
|
||||
protected static $deployStatus = 'in_progress';
|
||||
protected $deployStatus = self::IN_PROGRESS;
|
||||
|
||||
/**
|
||||
* Total of Failed tasks
|
||||
* @var integer
|
||||
*/
|
||||
protected static $failedTasks = 0;
|
||||
protected $failedTasks = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@ -126,7 +127,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
*/
|
||||
public static function getStatus()
|
||||
{
|
||||
return self::$deployStatus;
|
||||
return $this->deployStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,12 +146,8 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
return 231;
|
||||
}
|
||||
|
||||
// Check for running instance and Lock
|
||||
if (file_exists(getcwd() . '/.mage/~working.lock')) {
|
||||
Console::output('<red>There is already an instance of Magallanes running!</red>', 1, 2);
|
||||
if (!$this->lock()) {
|
||||
return 230;
|
||||
} else {
|
||||
touch(getcwd() . '/.mage/~working.lock');
|
||||
}
|
||||
|
||||
// Release ID
|
||||
|
@ -184,22 +181,22 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
$this->runNonDeploymentTasks(AbstractTask::STAGE_PRE_DEPLOY, $this->getConfig(), 'Pre-Deployment');
|
||||
|
||||
// Check Status
|
||||
if (self::$failedTasks > 0) {
|
||||
self::$deployStatus = self::FAILED;
|
||||
Console::output('A total of <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
|
||||
if ($this->failedTasks > 0) {
|
||||
$this->deployStatus = self::FAILED;
|
||||
Console::output('A total of <bold>' . $this->failedTasks . '</bold> 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 <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
|
||||
}
|
||||
|
||||
if ($this->failedTasks > 0) {
|
||||
$this->deployStatus = self::FAILED;
|
||||
Console::output('A total of <bold>' . $this->failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
|
||||
} else {
|
||||
// Run Post-Deployment Tasks
|
||||
$this->runNonDeploymentTasks(AbstractTask::STAGE_POST_DEPLOY, $this->getConfig(), 'Post-Deployment');
|
||||
}
|
||||
}
|
||||
|
||||
// Time Information Hosts
|
||||
if ($this->hostsCount > 0) {
|
||||
|
@ -215,18 +212,15 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
Console::output('Total time: <bold>' . $timeText . '</bold>.', 1, 2);
|
||||
|
||||
// Send Notifications
|
||||
$this->sendNotification(self::$failedTasks > 0 ? false : true);
|
||||
$this->sendNotification($this->failedTasks > 0 ? false : true);
|
||||
|
||||
// Unlock
|
||||
if (file_exists(getcwd() . '/.mage/~working.lock')) {
|
||||
unlink(getcwd() . '/.mage/~working.lock');
|
||||
}
|
||||
$this->unlock();
|
||||
|
||||
if (self::$failedTasks === 0) {
|
||||
if ($this->failedTasks === 0) {
|
||||
$exitCode = 0;
|
||||
}
|
||||
|
||||
if (self::$deployStatus === self::FAILED) {
|
||||
if ($this->deployStatus === self::FAILED) {
|
||||
$exitCode = 1;
|
||||
}
|
||||
|
||||
|
@ -243,7 +237,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
protected function runNonDeploymentTasks($stage, Config $config, $title)
|
||||
{
|
||||
$tasksToRun = $config->getTasks($stage);
|
||||
self::$failedTasks = 0;
|
||||
$this->failedTasks = 0;
|
||||
|
||||
// PreDeployment Hook
|
||||
if ($stage == AbstractTask::STAGE_PRE_DEPLOY) {
|
||||
|
@ -260,11 +254,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
|
||||
// PostDeployment Hook
|
||||
if ($stage == AbstractTask::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_push($tasksToRun, 'scm/change-branch');
|
||||
|
@ -279,7 +268,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
|
||||
if (count($tasksToRun) == 0) {
|
||||
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:');
|
||||
|
||||
$tasks = 0;
|
||||
|
@ -292,34 +283,31 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
if ($this->runTask($task)) {
|
||||
$completedTasks++;
|
||||
} else {
|
||||
self::$failedTasks++;
|
||||
$this->failedTasks++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($completedTasks == $tasks) {
|
||||
$tasksColor = 'green';
|
||||
} else {
|
||||
$tasksColor = 'red';
|
||||
}
|
||||
$tasksColor = ($completedTasks == $tasks) ? 'green' : 'red';
|
||||
|
||||
Console::output('Finished <bold>' . $title . '</bold> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
||||
}
|
||||
}
|
||||
|
||||
protected function runDeploymentTasks()
|
||||
{
|
||||
if (self::$deployStatus == self::FAILED) {
|
||||
if ($this->deployStatus == self::FAILED) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Run Tasks for Deployment
|
||||
$hosts = $this->getConfig()->getHosts();
|
||||
$this->hostsCount = count($hosts);
|
||||
self::$failedTasks = 0;
|
||||
$this->failedTasks = 0;
|
||||
|
||||
if ($this->hostsCount == 0) {
|
||||
Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, skipping deployment tasks.</bold>', 1, 3);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->startTimeHosts = time();
|
||||
foreach ($hosts as $hostKey => $host) {
|
||||
|
||||
|
@ -349,7 +337,10 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
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('Deployment to <bold>' . $host . '</bold> skipped!', 1, 3);
|
||||
} else {
|
||||
$this->getConfig()->setHostConfig(null);
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($tasksToRun as $taskData) {
|
||||
$tasks++;
|
||||
$task = Factory::get($taskData, $this->getConfig(), false, AbstractTask::STAGE_DEPLOY);
|
||||
|
@ -357,32 +348,27 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
if ($this->runTask($task)) {
|
||||
$completedTasks++;
|
||||
} else {
|
||||
self::$failedTasks++;
|
||||
$this->failedTasks++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($completedTasks == $tasks) {
|
||||
$tasksColor = 'green';
|
||||
} else {
|
||||
$tasksColor = 'red';
|
||||
}
|
||||
$tasksColor = ($completedTasks == $tasks) ? 'green' : 'red';
|
||||
|
||||
Console::output('Deployment to <bold>' . $this->getConfig()->getHost() . '</bold> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
||||
}
|
||||
|
||||
// Reset Host Config
|
||||
$this->getConfig()->setHostConfig(null);
|
||||
}
|
||||
$this->endTimeHosts = time();
|
||||
|
||||
if (self::$failedTasks > 0) {
|
||||
self::$deployStatus = self::FAILED;
|
||||
if ($this->failedTasks > 0) {
|
||||
$this->deployStatus = self::FAILED;
|
||||
} else {
|
||||
self::$deployStatus = self::SUCCEDED;
|
||||
$this->deployStatus = self::SUCCEDED;
|
||||
}
|
||||
|
||||
// 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
|
||||
Console::output('Starting the <bold>Releasing</bold>');
|
||||
$completedTasks = 0;
|
||||
|
@ -452,7 +438,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function runRollbackTask(AbstractTask $task)
|
||||
{
|
||||
|
@ -466,9 +451,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
$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("<bold>Rollback:<bold> <red>ABORTING</red>", 1, 3);
|
||||
return false;
|
||||
} elseif (count($hosts) == 0) {
|
||||
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;
|
||||
foreach ($hosts as $hostKey => $host) {
|
||||
$hostConfig = null;
|
||||
|
@ -495,8 +484,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
}
|
||||
return $result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a Task
|
||||
|
@ -512,44 +499,36 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
|
|||
if ($title === null) {
|
||||
$title = 'Running <purple>' . $task->getName() . '</purple> ... ';
|
||||
}
|
||||
|
||||
Console::output($title, 2, 0);
|
||||
|
||||
$runTask = true;
|
||||
if (($task instanceof SkipOnOverride) && $this->getConfig()->getParameter('overrideRelease', false)) {
|
||||
$runTask = false;
|
||||
Console::output('<yellow>SKIPPED</yellow>', 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($runTask === true) {
|
||||
try {
|
||||
$result = $task->run();
|
||||
|
||||
if ($result === true) {
|
||||
Console::output('<green>OK</green>', 0);
|
||||
$result = true;
|
||||
return true;
|
||||
} else {
|
||||
Console::output('<red>FAIL</red>', 0);
|
||||
$result = false;
|
||||
}
|
||||
} catch (RollbackException $e) {
|
||||
Console::output('<red>FAIL, Rollback catched</red> [Message: ' . $e->getMessage() . ']', 0);
|
||||
$this->runRollbackTask($task);
|
||||
$result = false;
|
||||
} catch (ErrorWithMessageException $e) {
|
||||
Console::output('<red>FAIL</red> [Message: ' . $e->getMessage() . ']', 0);
|
||||
$result = false;
|
||||
} catch (SkipException $e) {
|
||||
Console::output('<yellow>SKIPPED</yellow>', 0);
|
||||
$result = true;
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue