@ -3,6 +3,7 @@
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez < andres @ andresmontanez . com >
* (c) Alex V Kotelnikov < gudron @ gudron . me >
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@ -16,6 +17,7 @@ use Mage\Task\Factory;
use Mage\Task\AbstractTask;
use Mage\Task\Releases\SkipOnOverride;
use Mage\Task\ErrorWithMessageException;
use Mage\Task\RollbackException;
use Mage\Task\SkipException;
use Mage\Console;
use Mage\Config;
@ -109,20 +111,20 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
*/
public function run()
{
$exitCode = 100 0;
$exitCode = 24 0;
// Check if Environment is not Locked
$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Console::output('< red > This environment is locked!< / red > ', 1, 2);
echo file_get_contents($lockFile);
return 1010 ;
return 23 1;
}
// 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);
return 10 20;
return 23 0;
} else {
touch(getcwd() . '/.mage/~working.lock');
}
@ -131,21 +133,21 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$this->getConfig()->setReleaseId(date('YmdHis'));
// Deploy Summary
Console::output('< dark_gray > Deploy summary< / dark_gray > ', 1, 1);
Console::output('< bol d> Deploy summary< / bol d> ', 1, 1);
// Deploy Summary - Environment
Console::output('< dark_gray > Environment:< / dark_gray > < purple > ' . $this->getConfig()->getEnvironment() . '< / purple > ', 2, 1);
Console::output('< bol d> Environment:< / bol d> < purple > ' . $this->getConfig()->getEnvironment() . '< / purple > ', 2, 1);
// Deploy Summary - Releases
if ($this->getConfig()->release('enabled', false)) {
Console::output('< dark_gray > Release ID:< / dark_gray > < purple > ' . $this->getConfig()->getReleaseId() . '< / purple > ', 2, 1);
Console::output('< bol d> Release ID:< / bol d> < purple > ' . $this->getConfig()->getReleaseId() . '< / purple > ', 2, 1);
}
// Deploy Summary - SCM
if ($this->getConfig()->deployment('scm', false)) {
$scmConfig = $this->getConfig()->deployment('scm');
if (isset($scmConfig['branch'])) {
Console::output('< dark_gray > SCM Branch:< / dark_gray > < purple > ' . $scmConfig['branch'] . '< / purple > ', 2, 1);
Console::output('< bol d> SCM Branch:< / bol d> < purple > ' . $scmConfig['branch'] . '< / purple > ', 2, 1);
}
}
@ -160,7 +162,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// 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);
Console::output('A total of < bol d> ' . self::$failedTasks . '< / bol d> deployment tasks failed: < red > ABORTING< / red > ', 1, 2);
} else {
// Run Deployment Tasks
@ -169,7 +171,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// 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);
Console::output('A total of < bol d> ' . self::$failedTasks . '< / bol d> deployment tasks failed: < red > ABORTING< / red > ', 1, 2);
}
// Run Post-Deployment Tasks
@ -179,15 +181,15 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// Time Information Hosts
if ($this->hostsCount > 0) {
$timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
Console::output('Time for deployment: < dark_gray > ' . $timeTextHost . '< / dark_gray > .');
Console::output('Time for deployment: < bol d> ' . $timeTextHost . '< / bol d> .');
$timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
Console::output('Average time per host: < dark_gray > ' . $timeTextPerHost . '< / dark_gray > .');
Console::output('Average time per host: < bol d> ' . $timeTextPerHost . '< / bol d> .');
}
// Time Information General
$timeText = $this->transcurredTime(time() - $this->startTime);
Console::output('Total time: < dark_gray > ' . $timeText . '< / dark_gray > .', 1, 2);
Console::output('Total time: < bol d> ' . $timeText . '< / bol d> .', 1, 2);
// Send Notifications
$this->sendNotification(self::$failedTasks > 0 ? false : true);
@ -249,10 +251,10 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
}
if (count($tasksToRun) == 0) {
Console::output('< dark_gray > No < / dark_gray > < light_cyan > ' . $title . '< / light_cyan > < dark_gray > tasks defined.< / dark_gray > ', 1, 3);
Console::output('< bol d> No < / bol d> < light_cyan > ' . $title . '< / light_cyan > < bol d> tasks defined.< / bol d> ', 1, 3);
} else {
Console::output('Starting < dark_gray > ' . $title . '< / dark_gray > tasks:');
Console::output('Starting < bol d> ' . $title . '< / bol d> tasks:');
$tasks = 0;
$completedTasks = 0;
@ -274,7 +276,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$tasksColor = 'red';
}
Console::output('Finished < dark_gray > ' . $title . '< / dark_gray > tasks: < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Finished < bol d> ' . $title . '< / bol d> tasks: < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
}
}
@ -290,7 +292,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
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);
Console::output('< light_purple > Warning!< / light_purple > < bol d> No hosts defined, skipping deployment tasks.< / bol d> ', 1, 3);
} else {
$this->startTimeHosts = time();
@ -311,7 +313,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$tasks = 0;
$completedTasks = 0;
Console::output('Deploying to < dark_gray > ' . $this->getConfig()->getHost() . '< / dark_gray > ');
Console::output('Deploying to < bol d> ' . $this->getConfig()->getHost() . '< / bol d> ');
$tasksToRun = $this->getConfig()->getTasks();
@ -320,8 +322,8 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
array_unshift($tasksToRun, $deployStrategy);
if (count($tasksToRun) == 0) {
Console::output('< light_purple > Warning!< / light_purple > < dark_gray > No < / dark_gray > < light_cyan > Deployment< / light_cyan > < dark_gray > tasks defined.< / dark_gray > ', 2);
Console::output('Deployment to < dark_gray > ' . $host . '< / dark_gray > skipped!', 1, 3);
Console::output('< light_purple > Warning!< / light_purple > < bol d> No < / bol d> < light_cyan > Deployment< / light_cyan > < bol d> tasks defined.< / bol d> ', 2);
Console::output('Deployment to < bol d> ' . $host . '< / bol d> skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskData) {
@ -341,7 +343,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$tasksColor = 'red';
}
Console::output('Deployment to < dark_gray > ' . $this->getConfig()->getHost() . '< / dark_gray > completed: < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Deployment to < bol d> ' . $this->getConfig()->getHost() . '< / bol d> completed: < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
}
// Reset Host Config
@ -356,9 +358,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
}
// Releasing
if (self::$deployStatus == self::SUCCEDED & & $this->getConfig()->release('enabled', false) == true) {
if (self::$deployStatus == self::SUCCEDED & & $this->getConfig()->release('enabled', false) === true) {
// Execute the Releases
Console::output('Starting the < dark_gray > Releasing< / dark_gray > ');
Console::output('Starting the < bol d> Releasing< / bol d> ');
$completedTasks = 0;
foreach ($hosts as $hostKey => $host) {
@ -382,7 +384,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
// Reset Host Config
$this->getConfig()->setHostConfig(null);
}
Console::output('Finished the < dark_gray > Releasing< / dark_gray > ', 1, 3);
Console::output('Finished the < bol d> Releasing< / bol d> ', 1, 3);
// Execute the Post-Release Tasks
foreach ($hosts as $hostKey => $host) {
@ -403,7 +405,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$completedTasks = 0;
if (count($tasksToRun) > 0) {
Console::output('Starting < dark_gray > Post-Release< / dark_gray > tasks for < dark_gray > ' . $host . '< / dark_gray > :');
Console::output('Starting < bol d> Post-Release< / bol d> tasks for < bol d> ' . $host . '< / bol d> :');
foreach ($tasksToRun as $task) {
$task = Factory::get($task, $this->getConfig(), false, AbstractTask::STAGE_POST_RELEASE);
@ -418,7 +420,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
} else {
$tasksColor = 'red';
}
Console::output('Finished < dark_gray > Post-Release< / dark_gray > tasks for < dark_gray > ' . $host . '< / dark_gray > : < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Finished < bol d> Post-Release< / bol d> tasks for < bol d> ' . $host . '< / bol d> : < ' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '< /' . $tasksColor . '> tasks done.', 1, 3);
}
// Reset Host Config
@ -428,6 +430,28 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
}
}
protected function runRollbackTask(){
$this->getConfig()->reload();
$hosts = $this->getConfig()->getHosts();
if (count($hosts) == 0) {
Console::output('< light_purple > Warning!< / light_purple > < bold > No hosts defined, unable to get releases.< / bold > ', 1, 3);
} else {
$result = true;
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
$this->getConfig()->setReleaseId(-1);
$task = Factory::get('releases/rollback', $this->getConfig());
$task->init();
$result = $task->run() & & $result;
}
return $result;
}
return false;
}
/**
* Runs a Task
*
@ -439,7 +463,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
{
$task->init();
if ($title == null) {
if ($title === null) {
$title = 'Running < purple > ' . $task->getName() . '< / purple > ... ';
}
Console::output($title, 2, 0);
@ -449,11 +473,11 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
$runTask = false;
}
if ($runTask == true) {
if ($runTask === true) {
try {
$result = $task->run();
if ($result == true) {
if ($result === true) {
Console::output('< green > OK< / green > ', 0);
$result = true;
@ -461,6 +485,11 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
Console::output('< red > FAIL< / red > ', 0);
$result = false;
}
} catch (RollbackException $e) {
Console::output('< red > FAIL, Rollback started< / red > [Message: ' . $e->getMessage() . ']', 0);
$this->runRollbackTask();
$result = false;
} catch (ErrorWithMessageException $e) {
Console::output('< red > FAIL< / red > [Message: ' . $e->getMessage() . ']', 0);
$result = false;
@ -564,7 +593,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
case self::DEPLOY_STRATEGY_GUESS:
default:
if ($this->getConfig()->release('enabled', false) == true) {
if ($this->getConfig()->release('enabled', false) === true) {
$deployStrategy = 'deployment/strategy/tar-gz';
} else {
$deployStrategy = 'deployment/strategy/rsync';