diff --git a/Mage/Command/AbstractCommand.php b/Mage/Command/AbstractCommand.php index de92a8b..b513517 100644 --- a/Mage/Command/AbstractCommand.php +++ b/Mage/Command/AbstractCommand.php @@ -28,6 +28,7 @@ abstract class AbstractCommand /** * Runs the Command + * @return integer exit code * @throws \Exception */ public abstract function run(); diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index 4f17543..f67e875 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -28,12 +28,14 @@ class CompileCommand extends AbstractCommand { if (ini_get('phar.readonly')) { Console::output('The php.ini variable phar.readonly must be Off.', 1, 2); - return; + return 300; } $compiler = new Compiler; $compiler->compile(); Console::output('mage.phar compiled successfully', 0, 2); + + return 0; } } diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index e0f45d8..829a329 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -108,18 +108,20 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { + $exitCode = 1000; + // Check if Environment is not Locked $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; if (file_exists($lockFile)) { Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return; + return 1010; } // Check for running instance and Lock if (file_exists(getcwd() . '/.mage/~working.lock')) { Console::output('There is already an instance of Magallanes running!', 1, 2); - return; + return 1020; } else { touch(getcwd() . '/.mage/~working.lock'); } @@ -193,6 +195,12 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment if (file_exists(getcwd() . '/.mage/~working.lock')) { unlink(getcwd() . '/.mage/~working.lock'); } + + if (self::$failedTasks === 0) { + $exitCode = 0; + } + + return $exitCode; } /** diff --git a/Mage/Command/BuiltIn/InitCommand.php b/Mage/Command/BuiltIn/InitCommand.php index 6f478c0..f996335 100644 --- a/Mage/Command/BuiltIn/InitCommand.php +++ b/Mage/Command/BuiltIn/InitCommand.php @@ -27,6 +27,7 @@ class InitCommand extends AbstractCommand */ public function run() { + $exitCode = 50; $configDir = getcwd() . '/.mage'; Console::output('Initiating managing process for application with Magallanes'); @@ -49,9 +50,13 @@ class InitCommand extends AbstractCommand if (!in_array(false, $results)) { Console::output('Success!! The configuration for Magallanes has been generated at .mage directory.'); Console::output('Please!! Review and adjust the configuration.', 2, 2); + $exitCode = 0; + } else { Console::output('Error!! Unable to generate the configuration.', 1, 2); } + + return $exitCode; } } diff --git a/Mage/Command/BuiltIn/InstallCommand.php b/Mage/Command/BuiltIn/InstallCommand.php index aabc2a9..47a8411 100644 --- a/Mage/Command/BuiltIn/InstallCommand.php +++ b/Mage/Command/BuiltIn/InstallCommand.php @@ -26,6 +26,7 @@ class InstallCommand extends AbstractCommand */ public function run() { + $exitCode = 88; Console::output('Installing Magallanes... ', 1, 0); // Vars @@ -74,7 +75,10 @@ class InstallCommand extends AbstractCommand } Console::output('Success!', 0, 2); + $exitCode = 0; } + + return $exitCode; } /** diff --git a/Mage/Command/BuiltIn/ListCommand.php b/Mage/Command/BuiltIn/ListCommand.php index 0e67a0f..48e89bf 100644 --- a/Mage/Command/BuiltIn/ListCommand.php +++ b/Mage/Command/BuiltIn/ListCommand.php @@ -31,12 +31,13 @@ class ListCommand extends AbstractCommand */ public function run() { + $exitCode = 600; $subCommand = $this->getConfig()->getArgument(1); try { switch ($subCommand) { case 'environments': - $this->listEnvironments(); + $exitCode = $this->listEnvironments(); break; default; @@ -46,6 +47,8 @@ class ListCommand extends AbstractCommand } catch (Exception $e) { Console::output('' . $e->getMessage() . '', 1, 2); } + + return $exitCode; } /** @@ -53,6 +56,7 @@ class ListCommand extends AbstractCommand */ protected function listEnvironments() { + $exitCode = 600; $environments = array(); $content = scandir(getcwd() . '/.mage/config/environment/'); foreach ($content as $file) { @@ -68,9 +72,12 @@ class ListCommand extends AbstractCommand Console::output('* ' . $environment . '', 2, 1); } Console::output('', 1, 1); + $exitCode = 0; } else { Console::output('You don\'t have any environment configured.', 1, 2); } + + return $exitCode; } } diff --git a/Mage/Command/BuiltIn/LockCommand.php b/Mage/Command/BuiltIn/LockCommand.php index b34e5e9..6c2a59e 100644 --- a/Mage/Command/BuiltIn/LockCommand.php +++ b/Mage/Command/BuiltIn/LockCommand.php @@ -43,6 +43,8 @@ class LockCommand extends AbstractCommand implements RequiresEnvironment file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s') . $lockmsg); Console::output('Locked deployment to ' . $this->getConfig()->getEnvironment() . ' environment', 1, 2); + + return 0; } } diff --git a/Mage/Command/BuiltIn/ReleasesCommand.php b/Mage/Command/BuiltIn/ReleasesCommand.php index cd4f761..2837903 100644 --- a/Mage/Command/BuiltIn/ReleasesCommand.php +++ b/Mage/Command/BuiltIn/ReleasesCommand.php @@ -28,7 +28,7 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { - $result = false; + $exitCode = 400; $subCommand = $this->getConfig()->getArgument(1); // Run Tasks for Deployment @@ -40,9 +40,10 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment 1, 3 ); - return false; + return 401; } + $result = true; foreach ($hosts as $host) { $this->getConfig()->setHost($host); @@ -50,14 +51,14 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment case 'list': $task = Factory::get('releases/list', $this->getConfig()); $task->init(); - $result = $task->run(); + $result = $task->run() && $result; break; case 'rollback': if (!is_numeric($this->getConfig()->getParameter('release', ''))) { Console::output('Missing required releaseid.', 1, 2); - return false; + return 410; } $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; @@ -65,18 +66,22 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return false; + return 420; } $releaseId = $this->getConfig()->getParameter('release', ''); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); $task->init(); - $result = $task->run(); + $result = $task->run() && $result; break; } } - return $result; + if ($result) { + $exitCode = 0; + } + + return $exitCode; } } diff --git a/Mage/Command/BuiltIn/RollbackCommand.php b/Mage/Command/BuiltIn/RollbackCommand.php index b6bda5c..7a5b85b 100644 --- a/Mage/Command/BuiltIn/RollbackCommand.php +++ b/Mage/Command/BuiltIn/RollbackCommand.php @@ -28,19 +28,19 @@ class RollbackCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { - $result = false; + $exitCode = 450; $releaseId = $this->getConfig()->getArgument(1); if (!is_numeric($releaseId)) { Console::output('This release is mandatory.', 1, 2); - return false; + return 451; } $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; if (file_exists($lockFile)) { Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return null; + return 20; } // Run Tasks for Deployment @@ -50,16 +50,21 @@ class RollbackCommand extends AbstractCommand implements RequiresEnvironment Console::output('Warning! No hosts defined, unable to get releases.', 1, 3); } else { + $result = true; foreach ($hosts as $host) { $this->getConfig()->setHost($host); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); $task->init(); - $result = $task->run(); + $result = $task->run() && $result; + } + + if ($result) { + $exitCode = 0; } } - return $result; + return $exitCode; } } diff --git a/Mage/Command/BuiltIn/UnlockCommand.php b/Mage/Command/BuiltIn/UnlockCommand.php index b582d1a..462349c 100644 --- a/Mage/Command/BuiltIn/UnlockCommand.php +++ b/Mage/Command/BuiltIn/UnlockCommand.php @@ -34,6 +34,8 @@ class UnlockCommand } Console::output('Unlocked deployment to ' . $this->getConfig()->getEnvironment() . ' environment', 1, 2); + + return 0; } } diff --git a/Mage/Command/BuiltIn/UpdateCommand.php b/Mage/Command/BuiltIn/UpdateCommand.php index 2e1fb4f..9cba1cb 100644 --- a/Mage/Command/BuiltIn/UpdateCommand.php +++ b/Mage/Command/BuiltIn/UpdateCommand.php @@ -27,6 +27,8 @@ class UpdateCommand extends AbstractCommand */ public function run() { + $exitCode = 200; + $task = Factory::get('scm/update', $this->getConfig()); $task->init(); @@ -35,9 +37,13 @@ class UpdateCommand extends AbstractCommand if ($result == true) { Console::output('OK' . PHP_EOL, 0); + $exitCode = 0; + } else { Console::output('FAIL' . PHP_EOL, 0); } + + return $exitCode; } } \ No newline at end of file diff --git a/Mage/Command/BuiltIn/UpgradeCommand.php b/Mage/Command/BuiltIn/UpgradeCommand.php index 86bb0aa..5de6a2d 100644 --- a/Mage/Command/BuiltIn/UpgradeCommand.php +++ b/Mage/Command/BuiltIn/UpgradeCommand.php @@ -38,6 +38,7 @@ class UpgradeCommand extends AbstractCommand */ public function run() { + $exitCode = 100; Console::output('Upgrading Magallanes ... ', 1, 0); $user = ''; @@ -59,10 +60,12 @@ class UpgradeCommand extends AbstractCommand if ($versionCompare == 0) { Console::output('SKIP', 0, 1); Console::output('Your current version is up to date.', 2); + $exitCode = 0; } else if ($versionCompare == 1) { Console::output('SKIP', 0, 1); Console::output('Your current version is newer.', 2); + $exitCode = 0; } else if ($versionCompare == -1) { // Download Package @@ -82,6 +85,7 @@ class UpgradeCommand extends AbstractCommand Console::executeCommand('mv ' . dirname($tarballFile) . '/magallanes ' . MAGALLANES_DIRECTORY); Console::output('OK', 0, 1); + $exitCode = 0; } } else { @@ -93,5 +97,7 @@ class UpgradeCommand extends AbstractCommand Console::output('Invalid version.', 2); } } + + return $exitCode; } } diff --git a/Mage/Command/BuiltIn/VersionCommand.php b/Mage/Command/BuiltIn/VersionCommand.php index d2f8827..ca591d7 100644 --- a/Mage/Command/BuiltIn/VersionCommand.php +++ b/Mage/Command/BuiltIn/VersionCommand.php @@ -27,6 +27,8 @@ class VersionCommand extends AbstractCommand public function run() { Console::output('Running Magallanes version ' . MAGALLANES_VERSION . '', 0, 2); + + return 0; } } \ No newline at end of file diff --git a/Mage/Console.php b/Mage/Console.php index bb48cf6..453e2a9 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -135,8 +135,7 @@ class Console throw new Exception('You must specify an environment for this command.'); } } - $command->run(); - $exitCode = 0; + $exitCode = $command->run(); } catch (Exception $exception) { self::output('' . $exception->getMessage() . '', 1, 2);