/** * Rollback a release * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 450; $releaseId = $this->getConfig()->getArgument(1); if (!is_numeric($releaseId)) { Console::output('<red>This release is mandatory.</red>', 1, 2); return 451; } $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 20; } // Run Tasks for Deployment $hosts = $this->getConfig()->getHosts(); if (count($hosts) == 0) { Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, unable to get releases.</dark_gray>', 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; } if ($result) { $exitCode = 0; } } return $exitCode; }
/** * List the Releases, Rollback to a Release * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 100; $subCommand = $this->getConfig()->getArgument(1); // Run Tasks for Deployment $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); return 101; } $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); switch ($subCommand) { case 'list': $task = Factory::get('releases/list', $this->getConfig()); $task->init(); $result = $task->run() && $result; break; case 'rollback': if (!is_numeric($this->getConfig()->getParameter('release', ''))) { Console::output('<red>Missing required releaseid.</red>', 1, 2); return 102; } $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 103; } $releaseId = $this->getConfig()->getParameter('release', ''); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); $task->init(); $result = $task->run() && $result; break; } } if ($result) { $exitCode = 0; } return $exitCode; }
/** * Updates the SCM Base Code * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 200; $task = Factory::get('scm/update', $this->getConfig()); $task->init(); Console::output('Updating application via ' . $task->getName() . ' ... ', 1, 0); $result = $task->run(); if ($result === true) { Console::output('<green>OK</green>' . PHP_EOL, 0); $exitCode = 0; } else { Console::output('<red>FAIL</red>' . PHP_EOL, 0); } return $exitCode; }
/** * List the Releases, Rollback to a Release * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 400; $subCommand = $this->getConfig()->getArgument(1); // Run Tasks for Deployment $hosts = $this->getConfig()->getHosts(); if (count($hosts) == 0) { Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, unable to get releases.</dark_gray>', 1, 3); return 401; } $result = true; foreach ($hosts as $host) { $this->getConfig()->setHost($host); switch ($subCommand) { case 'list': $task = Factory::get('releases/list', $this->getConfig()); $task->init(); $result = $task->run() && $result; break; case 'rollback': if (!is_numeric($this->getConfig()->getParameter('release', ''))) { Console::output('<red>Missing required releaseid.</red>', 1, 2); return 410; } $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 420; } $releaseId = $this->getConfig()->getParameter('release', ''); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); $task->init(); $result = $task->run() && $result; break; } } if ($result) { $exitCode = 0; } return $exitCode; }
/** * Rollback a release * @see \Mage\Command\AbstractCommand::run() */ public function run() { $exitCode = 105; $releaseId = $this->getConfig()->getArgument(1); if (!is_numeric($releaseId)) { Console::output('<red>This release is mandatory.</red>', 1, 2); return 104; } $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 106; } // Run Tasks for Deployment $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 $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; }
protected function runDeploymentTasks() { if (self::$deployStatus == self::FAILED) { return; } // Run Tasks for Deployment $hosts = $this->getConfig()->getHosts(); $this->hostsCount = count($hosts); 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); } else { $this->startTimeHosts = time(); 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); // Prepare Tasks $tasks = 0; $completedTasks = 0; Console::output('Deploying to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>'); $tasksToRun = $this->getConfig()->getTasks(); $deployStrategy = $this->chooseDeployStrategy(); 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); } else { foreach ($tasksToRun as $taskData) { $tasks++; $task = Factory::get($taskData, $this->getConfig(), false, AbstractTask::STAGE_DEPLOY); if ($this->runTask($task)) { $completedTasks++; } else { self::$failedTasks++; } } if ($completedTasks == $tasks) { $tasksColor = 'green'; } else { $tasksColor = 'red'; } Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> 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; } else { self::$deployStatus = self::SUCCEDED; } // Releasing if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) == true) { // Execute the Releases Console::output('Starting the <dark_gray>Releasing</dark_gray>'); $completedTasks = 0; foreach ($hosts as $hostKey => $host) { // Check if Host has specific configuration $hostConfig = null; if (is_array($host)) { $hostConfig = $host; $host = $hostKey; } // Set Host $this->getConfig()->setHost($host); $this->getConfig()->setHostConfig($hostConfig); $task = Factory::get($this->chooseReleaseStrategy(), $this->getConfig(), false, AbstractTask::STAGE_DEPLOY); if ($this->runTask($task, 'Releasing on host <purple>' . $host . '</purple> ... ')) { $completedTasks++; } // Reset Host Config $this->getConfig()->setHostConfig(null); } Console::output('Finished the <dark_gray>Releasing</dark_gray>', 1, 3); // Execute the Post-Release Tasks foreach ($hosts as $hostKey => $host) { // Check if Host has specific configuration $hostConfig = null; if (is_array($host)) { $hostConfig = $host; $host = $hostKey; } // Set Host $this->getConfig()->setHost($host); $this->getConfig()->setHostConfig($hostConfig); $tasksToRun = $this->getConfig()->getTasks(AbstractTask::STAGE_POST_RELEASE); $tasks = count($tasksToRun); $completedTasks = 0; if (count($tasksToRun) > 0) { Console::output('Starting <dark_gray>Post-Release</dark_gray> tasks for <dark_gray>' . $host . '</dark_gray>:'); foreach ($tasksToRun as $task) { $task = Factory::get($task, $this->getConfig(), false, AbstractTask::STAGE_POST_RELEASE); if ($this->runTask($task)) { $completedTasks++; } } if ($completedTasks == $tasks) { $tasksColor = 'green'; } 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); } // Reset Host Config $this->getConfig()->setHostConfig(null); } } } }
/** * Performs a Rollback Operation * @see \Mage\Task\AbstractTask::run() */ public function run() { if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); if (substr($symlink, 0, 1) == '/') { $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; } $output = ''; $result = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $output); $releases = $output == '' ? array() : explode(PHP_EOL, $output); if (count($releases) == 0) { Console::output('Release are not available for <bold>' . $this->getConfig()->getHost() . '</bold> ... <red>FAIL</red>'); } else { rsort($releases); $deleteCurrent = $this->getConfig()->getParameter('deleteCurrent', $this->getConfig()->deployment('delete-on-rollback', $this->getConfig()->general('delete-on-rollback', false))); $releaseIsAvailable = false; if ($this->getReleaseId() == '') { $releaseId = $releases[0]; $releaseIsAvailable = true; } elseif ($this->getReleaseId() <= 0) { $index = $this->getReleaseId() * -1; if (isset($releases[$index])) { $releaseId = $releases[$index]; $releaseIsAvailable = true; } } else { if (in_array($this->getReleaseId(), $releases)) { $releaseId = $this->getReleaseId(); $releaseIsAvailable = true; } } $currentCopy = rtrim($releasesDirectory, '/') . '/' . $releaseId; if (!$releaseIsAvailable) { Console::output('Release <bold>' . $this->getReleaseId() . '</bold> is invalid or unavailable for <bold>' . $this->getConfig()->getHost() . '</bold> ... <red>FAIL</red>'); } else { Console::output('Rollback release on <bold>' . $this->getConfig()->getHost() . '</bold>'); $rollbackTo = $releasesDirectory . '/' . $releaseId; // Get Current Release if ($deleteCurrent) { $result = $this->runCommandRemote('ls -l ' . $symlink, $output) && $result; $currentRelease = explode('/', $output); $currentRelease = trim(array_pop($currentRelease)); } // Tasks $tasks = 1; $completedTasks = 0; $tasksToRun = $this->getConfig()->getTasks(); $this->getConfig()->setReleaseId($releaseId); // Run Deploy Tasks foreach ($tasksToRun as $taskData) { $task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_DEPLOY); $task->init(); Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false); if ($task instanceof RollbackAware) { /* @var $task AbstractTask */ $tasks++; $result = $task->run(); if ($result === true) { Console::output('<green>OK</green>', 0); $completedTasks++; } else { Console::output('<red>FAIL</red>', 0); } } else { Console::output('<yellow>SKIPPED</yellow>', 0); } } // Changing Release Console::output('Running <purple>Rollback Release [id=' . $releaseId . ']</purple> ... ', 2, false); $userGroup = ''; $resultFetch = $this->runCommandRemote('ls -ld ' . $rollbackTo . ' | awk \'{print \\$3":"\\$4}\'', $userGroup); $tmplink = $symlink . '.tmp'; $command = "ln -sfn {$currentCopy} {$tmplink}"; if ($resultFetch && $userGroup) { $command .= " && chown -h {$userGroup} {$tmplink}"; } $command .= " && mv -f {$tmplink} {$symlink}"; $result = $this->runCommandRemote($command); if ($result) { Console::output('<green>OK</green>', 0); $completedTasks++; // Delete Old Current Release if ($deleteCurrent && $currentRelease) { $this->runCommandRemote('rm -rf ' . $releasesDirectory . '/' . $currentRelease, $output); } } else { Console::output('<red>FAIL</red>', 0); } // Run Post Release Tasks $tasksToRun = $this->getConfig()->getTasks(AbstractTask::STAGE_POST_DEPLOY); foreach ($tasksToRun as $taskData) { $task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_POST_DEPLOY); $task->init(); Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false); if ($task instanceof RollbackAware) { /* @var $task AbstractTask */ $tasks++; $result = $task->run(); if ($result === true) { Console::output('<green>OK</green>', 0); $completedTasks++; } else { Console::output('<red>FAIL</red>', 0); } } else { Console::output('<yellow>SKIPPED</yellow>', 0); } } if ($completedTasks == $tasks) { $tasksColor = 'green'; } else { $tasksColor = 'red'; } Console::output('Release rollback on <bold>' . $this->getConfig()->getHost() . '</bold> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3); } } return $result; } else { return false; } }
/** * @expectedException \Exception * @expectedExceptionMessage Task "Unknowntask" not found. * @covers Mage\Task\Factory::get */ public function testGetClassDoesNotExist() { Factory::get('unknowntask', $this->config); }
protected function runRollbackTask(AbstractTask $task) { $this->getConfig()->reload(); $hosts = $this->getConfig()->getHosts(); Console::output("", 1, 2); Console::output("Starting the <bold>rollback</bold>", 1, 1); if (!in_array($task->getStage(), $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("<bold>Rollback:<bold> <red>ABORTING</red>", 1, 3); } elseif (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 $hostKey => $host) { $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(-1); $task = Factory::get(array('name' => 'releases/rollback', 'parameters' => array('inDeploy' => true)), $this->getConfig(), false, $task->getStage()); $task->init(); $result = $task->run() && $result; } return $result; } return false; }