* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Mage\Command\BuiltIn; use Mage\Command\AbstractCommand; use Mage\Command\RequiresEnvironment; use Mage\Task\Factory; use Mage\Console; /** * This is an Alias of "release rollback" * * @author Andrés Montañez */ class RollbackCommand extends AbstractCommand implements RequiresEnvironment { public function __construct() { $this->setName('Rollback command') ->setHelpMessage('Rollbacks the release by given release id or index') ->setSyntaxMessage('mage rollback [releaseId] to:[environment_name]') ->addUsageExample( 'mage rollback 20120101172148 to:production', 'Rollbacks the 20120101172148 release on production environment' ) ->addUsageExample( 'mage rollback -1 to:production', 'Rollbacks the last release -1 release' ) ->addUsageExample( 'mage rollback -1 to:production --deleteCurrent', 'Rollbacks the last release -1 release and removes current release' ); } /** * Rollback a release * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 105; $releaseId = $this->getConfig()->getArgument(1); if (!is_numeric($releaseId)) { Console::output('This release is mandatory.', 1, 2); return 104; } $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 106; } // Run Tasks for Deployment $hosts = $this->getConfig()->getHosts(); if (count($hosts) == 0) { Console::output('Warning! No hosts defined, unable to get releases.', 1, 3); } else { $result = true; foreach ($hosts as $hostKey => $host) { // Check if Host has specific configuration $hostConfig = null; if (is_array($host)) { $hostConfig = $host; $host = $hostKey; } // Set Host and Host Specific Config $this->getConfig()->setHost($host); $this->getConfig()->setHostConfig($hostConfig); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); $task->init(); $result = $task->run() && $result; } if ($result) { $exitCode = 0; } } return $exitCode; } }