From 238777cb4159a064508f512e8fbea9427cab6ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 7 Jan 2017 01:53:57 -0300 Subject: [PATCH] [Nostromo] Improve Exception workflow --- src/Mage/Command/BuiltIn/DeployCommand.php | 84 ++++++----- .../Command/BuiltIn/Releases/ListCommand.php | 132 +++++++++--------- .../BuiltIn/Releases/RollbackCommand.php | 44 +++--- .../Runtime/Exception/DeploymentException.php | 20 --- .../Exception/InvalidEnvironmentException.php | 20 --- src/Mage/Runtime/Runtime.php | 9 +- src/Mage/Task/BuiltIn/Deploy/ReleaseTask.php | 5 + src/Mage/Task/BuiltIn/Deploy/RsyncTask.php | 2 +- .../Task/BuiltIn/Deploy/TarGz/CleanupTask.php | 2 +- .../Task/BuiltIn/Deploy/TarGz/CopyTask.php | 2 +- .../Task/BuiltIn/Deploy/TarGz/PrepareTask.php | 2 +- src/Mage/Task/BuiltIn/FS/AbstractFileTask.php | 4 +- src/Mage/Task/BuiltIn/FS/CopyTask.php | 8 +- src/Mage/Task/BuiltIn/FS/LinkTask.php | 8 +- src/Mage/Task/BuiltIn/FS/MoveTask.php | 8 +- src/Mage/Task/BuiltIn/FS/RemoveTask.php | 8 +- .../Task/BuiltIn/Git/ChangeBranchTask.php | 2 +- .../Task/{ => Exception}/ErrorException.php | 2 +- .../Task/{ => Exception}/SkipException.php | 2 +- .../BuiltIn/DeployCommandMiscTasksTest.php | 12 +- .../Command/BuiltIn/DeployCommandMiscTest.php | 1 - .../BuiltIn/Releases/ListCommandTest.php | 30 ++-- .../BuiltIn/Releases/RollbackCommandTest.php | 20 +-- src/Mage/Tests/Runtime/RuntimeTest.php | 6 +- src/Mage/Tests/Task/AbstractTaskTest.php | 2 +- .../Tests/Task/BuiltIn/FileSystemTaskTest.php | 14 +- src/Mage/Tests/Task/TestCaseFailTask.php | 2 +- src/Mage/Tests/UtilsTest.php | 6 +- src/Mage/Utils.php | 6 +- 29 files changed, 216 insertions(+), 247 deletions(-) delete mode 100644 src/Mage/Runtime/Exception/DeploymentException.php delete mode 100644 src/Mage/Runtime/Exception/InvalidEnvironmentException.php rename src/Mage/Task/{ => Exception}/ErrorException.php (95%) rename src/Mage/Task/{ => Exception}/SkipException.php (92%) diff --git a/src/Mage/Command/BuiltIn/DeployCommand.php b/src/Mage/Command/BuiltIn/DeployCommand.php index 42ba900..33dfe27 100644 --- a/src/Mage/Command/BuiltIn/DeployCommand.php +++ b/src/Mage/Command/BuiltIn/DeployCommand.php @@ -10,14 +10,12 @@ namespace Mage\Command\BuiltIn; -use Mage\Runtime\Exception\DeploymentException; -use Mage\Runtime\Exception\InvalidEnvironmentException; use Mage\Runtime\Exception\RuntimeException; use Mage\Runtime\Runtime; -use Mage\Task\ErrorException; use Mage\Task\ExecuteOnRollbackInterface; use Mage\Task\AbstractTask; -use Mage\Task\SkipException; +use Mage\Task\Exception\ErrorException; +use Mage\Task\Exception\SkipException; use Mage\Task\TaskFactory; use Mage\Utils; use Symfony\Component\Console\Input\InputInterface; @@ -70,37 +68,38 @@ class DeployCommand extends AbstractCommand try { $this->runtime->setEnvironment($input->getArgument('environment')); - } catch (InvalidEnvironmentException $exception) { - $output->writeln(sprintf('%s', $exception->getMessage())); - return $exception->getCode(); - } - - $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); - $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); - if ($this->runtime->getEnvironmentConfig('releases', false)) { - $this->runtime->generateReleaseId(); - $output->writeln(sprintf(' Release ID: %s', $this->runtime->getReleaseId())); - $this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId())); - } + $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); + $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); - if ($this->runtime->getConfigOptions('log_file', false)) { - $output->writeln(sprintf(' Logfile: %s', $this->runtime->getConfigOptions('log_file'))); - } + if ($this->runtime->getEnvironmentConfig('releases', false)) { + $this->runtime->generateReleaseId(); + $output->writeln(sprintf(' Release ID: %s', $this->runtime->getReleaseId())); + $this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId())); + } - $output->writeln(''); + if ($this->runtime->getConfigOptions('log_file', false)) { + $output->writeln(sprintf(' Logfile: %s', $this->runtime->getConfigOptions('log_file'))); + } - try { - // Check if Branch is forced if ($input->getOption('branch') !== false) { $this->runtime->setEnvironmentConfig('branch', $input->getOption('branch')); } + if ($this->runtime->getEnvironmentConfig('branch', false)) { + $output->writeln(sprintf(' Branch: %s', $this->runtime->getEnvironmentConfig('branch'))); + } + + $output->writeln(''); + $this->taskFactory = new TaskFactory($this->runtime); $this->runDeployment($output); - } catch (DeploymentException $exception) { + + } catch (RuntimeException $exception) { + $output->writeln(''); $output->writeln(sprintf('%s', $exception->getMessage())); - return $exception->getCode(); + $output->writeln(''); + $this->statusCode = 7; } $output->writeln('Finished Magallanes'); @@ -112,11 +111,11 @@ class DeployCommand extends AbstractCommand * Run the Deployment Process * * @param OutputInterface $output - * @throws DeploymentException + * @throws RuntimeException */ protected function runDeployment(OutputInterface $output) { - // Run Pre Deploy Tasks + // Run "Pre Deploy" Tasks $this->runtime->setStage(Runtime::PRE_DEPLOY); $preDeployTasks = $this->runtime->getTasks(); @@ -133,10 +132,10 @@ class DeployCommand extends AbstractCommand } if (!$this->runTasks($output, $preDeployTasks)) { - throw new DeploymentException(sprintf(' Tasks failed on %s stage, halting deployment', $this->getStageName()), 50); + throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50); } - // Run On Deploy Tasks + // Run "On Deploy" Tasks $hosts = $this->runtime->getEnvironmentConfig('hosts'); if (count($hosts) == 0) { $output->writeln(' No hosts defined, skipping On Deploy tasks'); @@ -165,13 +164,14 @@ class DeployCommand extends AbstractCommand foreach ($hosts as $host) { $this->runtime->setWorkingHost($host); if (!$this->runTasks($output, $onDeployTasks)) { - throw new DeploymentException(sprintf(' Tasks failed on %s stage, halting deployment', $this->getStageName()), 50); + $this->runtime->setWorkingHost(null); + throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50); } $this->runtime->setWorkingHost(null); } } - // Run On Release Tasks + // Run "On Release" Tasks $hosts = $this->runtime->getEnvironmentConfig('hosts'); if (count($hosts) == 0) { $output->writeln(' No hosts defined, skipping On Release tasks'); @@ -189,13 +189,14 @@ class DeployCommand extends AbstractCommand foreach ($hosts as $host) { $this->runtime->setWorkingHost($host); if (!$this->runTasks($output, $onReleaseTasks)) { - throw new DeploymentException(sprintf(' Tasks failed on %s stage, halting deployment', $this->getStageName()), 50); + $this->runtime->setWorkingHost(null); + throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50); } $this->runtime->setWorkingHost(null); } } - // Run Post Release Tasks + // Run "Post Release" Tasks $hosts = $this->runtime->getEnvironmentConfig('hosts'); if (count($hosts) == 0) { $output->writeln(' No hosts defined, skipping Post Release tasks'); @@ -213,13 +214,14 @@ class DeployCommand extends AbstractCommand foreach ($hosts as $host) { $this->runtime->setWorkingHost($host); if (!$this->runTasks($output, $postReleaseTasks)) { - throw new DeploymentException(sprintf(' Tasks failed on %s stage, halting deployment', $this->getStageName()), 50); + $this->runtime->setWorkingHost(null); + throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50); } $this->runtime->setWorkingHost(null); } } - // Run Post Deploy Tasks + // Run "Post Deploy" Tasks $this->runtime->setStage(Runtime::POST_DEPLOY); $postDeployTasks = $this->runtime->getTasks(); if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) { @@ -235,7 +237,7 @@ class DeployCommand extends AbstractCommand } if (!$this->runTasks($output, $postDeployTasks)) { - throw new DeploymentException(sprintf(' Tasks failed on %s stage, halting deployment', $this->getStageName()), 50); + throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50); } } @@ -282,19 +284,25 @@ class DeployCommand extends AbstractCommand $this->log(sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName())); } else { $output->writeln('FAIL'); - $this->statusCode = 500; + $this->statusCode = 180; $this->log(sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName())); } + } catch (SkipException $exception) { $succeededTasks++; $output->writeln('SKIPPED'); $this->log(sprintf('Task %s (%s) finished with SKIPPED, thrown SkipException', $task->getDescription(), $task->getName())); + } catch (ErrorException $exception) { $output->writeln(sprintf('ERROR [%s]', $exception->getTrimmedMessage())); $this->log(sprintf('Task %s (%s) finished with FAIL, with Error "%s"', $task->getDescription(), $task->getName(), $exception->getMessage())); - $this->statusCode = $exception->getCode(); + $this->statusCode = 190; } } + + if ($this->statusCode !== 0) { + break; + } } if ($succeededTasks != $totalTasks) { @@ -303,7 +311,7 @@ class DeployCommand extends AbstractCommand $alertColor = 'green'; } - $output->writeln(sprintf(' Finished %s tasks: %d/%d done.', $this->getStageName(), $alertColor, $succeededTasks, $totalTasks)); + $output->writeln(sprintf(' Finished %d/%d tasks for %s.', $alertColor, $succeededTasks, $totalTasks, $this->getStageName())); $output->writeln(''); return ($succeededTasks == $totalTasks); diff --git a/src/Mage/Command/BuiltIn/Releases/ListCommand.php b/src/Mage/Command/BuiltIn/Releases/ListCommand.php index 83d73cc..605b421 100644 --- a/src/Mage/Command/BuiltIn/Releases/ListCommand.php +++ b/src/Mage/Command/BuiltIn/Releases/ListCommand.php @@ -11,8 +11,6 @@ namespace Mage\Command\BuiltIn\Releases; use Mage\Utils; -use Mage\Runtime\Exception\InvalidEnvironmentException; -use Mage\Runtime\Exception\DeploymentException; use Mage\Runtime\Exception\RuntimeException; use Symfony\Component\Process\Process; use Symfony\Component\Console\Input\InputInterface; @@ -27,6 +25,11 @@ use Mage\Command\AbstractCommand; */ class ListCommand extends AbstractCommand { + /** + * @var int + */ + protected $statusCode = 0; + /** * Configure the Command */ @@ -45,8 +48,6 @@ class ListCommand extends AbstractCommand * @param InputInterface $input * @param OutputInterface $output * @return int|mixed - * @throws DeploymentException - * @throws RuntimeException */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -55,91 +56,92 @@ class ListCommand extends AbstractCommand try { $this->runtime->setEnvironment($input->getArgument('environment')); - } catch (InvalidEnvironmentException $exception) { - $output->writeln(sprintf('%s', $exception->getMessage())); - return $exception->getCode(); - } - - if (!$this->runtime->getEnvironmentConfig('releases', false)) { - throw new DeploymentException('Releases are not enabled', 70); - } - $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); - $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); + if (!$this->runtime->getEnvironmentConfig('releases', false)) { + throw new RuntimeException('Releases are not enabled', 70); + } - if ($this->runtime->getConfigOptions('log_file', false)) { - $output->writeln(sprintf(' Logfile: %s', $this->runtime->getConfigOptions('log_file'))); - } + $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); + $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); - $output->writeln(''); + if ($this->runtime->getConfigOptions('log_file', false)) { + $output->writeln(sprintf(' Logfile: %s', $this->runtime->getConfigOptions('log_file'))); + } - $hosts = $this->runtime->getEnvironmentConfig('hosts'); - if (count($hosts) == 0) { - $output->writeln('No hosts defined'); $output->writeln(''); - } else { - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); - - foreach ($hosts as $host) { - $this->runtime->setWorkingHost($host); - - // Get List of Releases - $cmdListReleases = sprintf('ls -1 %s/releases', $hostPath); - /** @var Process $process */ - $process = $this->runtime->runRemoteCommand($cmdListReleases, false); - if (!$process->isSuccessful()) { - throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80); - } + $hosts = $this->runtime->getEnvironmentConfig('hosts'); + if (count($hosts) == 0) { + $output->writeln('No hosts defined'); + $output->writeln(''); + } else { + $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); - if (trim($process->getOutput()) != '') { - $releases = explode(PHP_EOL, trim($process->getOutput())); - rsort($releases); - } else { - $releases = []; - } + foreach ($hosts as $host) { + $this->runtime->setWorkingHost($host); - if (count($releases) == 0) { - $output->writeln(sprintf(' No releases available on host %s:', $host)); - } else { - // Get Current Release - $cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath); + // Get List of Releases + $cmdListReleases = sprintf('ls -1 %s/releases', $hostPath); /** @var Process $process */ - $process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false); + $process = $this->runtime->runRemoteCommand($cmdListReleases, false); if (!$process->isSuccessful()) { - throw new RuntimeException(sprintf('Unable to retrieve current release from host "%s"', $host), 85); + throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80); } - $currentReleaseId = explode('/', trim($process->getOutput())); - $currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1]; + if (trim($process->getOutput()) != '') { + $releases = explode(PHP_EOL, trim($process->getOutput())); + rsort($releases); + } else { + $releases = []; + } + + if (count($releases) == 0) { + $output->writeln(sprintf(' No releases available on host %s:', $host)); + } else { + // Get Current Release + $cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath); + + /** @var Process $process */ + $process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false); + if (!$process->isSuccessful()) { + throw new RuntimeException(sprintf('Unable to retrieve current release from host "%s"', $host), 85); + } - $output->writeln(sprintf(' Releases on host %s:', $host)); + $currentReleaseId = explode('/', trim($process->getOutput())); + $currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1]; - foreach ($releases as $releaseId) { - $releaseDate = Utils::getReleaseDate($releaseId); + $output->writeln(sprintf(' Releases on host %s:', $host)); - $output->write(sprintf(' Release ID: %s - Date: %s [%s]', - $releaseId, - $releaseDate->format('Y-m-d H:i:s'), - Utils::getTimeDiff($releaseDate) - )); + foreach ($releases as $releaseId) { + $releaseDate = Utils::getReleaseDate($releaseId); - if ($releaseId == $currentReleaseId) { - $output->writeln(' [current]'); - } else { - $output->writeln(''); + $output->write(sprintf(' Release ID: %s - Date: %s [%s]', + $releaseId, + $releaseDate->format('Y-m-d H:i:s'), + Utils::getTimeDiff($releaseDate) + )); + + if ($releaseId == $currentReleaseId) { + $output->writeln(' [current]'); + } else { + $output->writeln(''); + } } } - } - $this->runtime->setWorkingHost(null); - $output->writeln(''); + $this->runtime->setWorkingHost(null); + $output->writeln(''); + } } + + } catch (RuntimeException $exception) { + $output->writeln(sprintf('%s', $exception->getMessage())); + $this->statusCode = $exception->getCode(); } $output->writeln('Finished Magallanes'); - return 0; + return $this->statusCode; } } diff --git a/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php b/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php index d92eac8..80768df 100644 --- a/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php +++ b/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php @@ -11,8 +11,7 @@ namespace Mage\Command\BuiltIn\Releases; use Mage\Task\TaskFactory; -use Mage\Runtime\Exception\InvalidEnvironmentException; -use Mage\Runtime\Exception\DeploymentException; +use Mage\Runtime\Exception\RuntimeException; use Symfony\Component\Process\Process; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; @@ -26,6 +25,11 @@ use Mage\Command\BuiltIn\DeployCommand; */ class RollbackCommand extends DeployCommand { + /** + * @var int + */ + protected $statusCode = 0; + /** * Configure the Command */ @@ -45,7 +49,6 @@ class RollbackCommand extends DeployCommand * @param InputInterface $input * @param OutputInterface $output * @return int|mixed - * @throws DeploymentException */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -54,24 +57,22 @@ class RollbackCommand extends DeployCommand try { $this->runtime->setEnvironment($input->getArgument('environment')); - } catch (InvalidEnvironmentException $exception) { - $output->writeln(sprintf('%s', $exception->getMessage())); - return $exception->getCode(); - } - if (!$this->runtime->getEnvironmentConfig('releases', false)) { - throw new DeploymentException('Releases are not enabled', 70); - } + if (!$this->runtime->getEnvironmentConfig('releases', false)) { + throw new RuntimeException('Releases are not enabled', 70); + } + + $releaseToRollback = $input->getArgument('release'); + if (($releaseId = $this->checkReleaseAvailability($releaseToRollback)) === false) { + throw new RuntimeException(sprintf('Release "%s" is not available on all hosts', $releaseToRollback), 72); + } - // Check if the Release exists in all hosts - $releaseToRollback = $input->getArgument('release'); - if ($releaseId = $this->checkReleaseAvailability($releaseToRollback)) { $this->runtime->setReleaseId($releaseId)->setRollback(true); $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); - $output->writeln(sprintf(' Rollback to Release ID: %s', $this->runtime->getReleaseId())); + $output->writeln(sprintf(' Rollback to Release Id: %s', $this->runtime->getReleaseId())); $this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId())); if ($this->runtime->getConfigOptions('log_file', false)) { @@ -80,22 +81,17 @@ class RollbackCommand extends DeployCommand $output->writeln(''); - // Get the Task Factory $this->taskFactory = new TaskFactory($this->runtime); + $this->runDeployment($output); - try { - $this->runDeployment($output); - } catch (DeploymentException $exception) { - $output->writeln(sprintf('%s', $exception->getMessage())); - return $exception->getCode(); - } - } else { - throw new DeploymentException(sprintf('Release "%s" is not available on all hosts', $releaseToRollback), 72); + } catch (RuntimeException $exception) { + $output->writeln(sprintf('%s', $exception->getMessage())); + $this->statusCode = $exception->getCode(); } $output->writeln('Finished Magallanes'); - return 0; + return $this->statusCode; } /** diff --git a/src/Mage/Runtime/Exception/DeploymentException.php b/src/Mage/Runtime/Exception/DeploymentException.php deleted file mode 100644 index 5b2d37f..0000000 --- a/src/Mage/Runtime/Exception/DeploymentException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Mage\Runtime\Exception; - -/** - * An Error occurred while Deploying - * - * @author Andrés Montañez - */ -class DeploymentException extends RuntimeException -{ -} diff --git a/src/Mage/Runtime/Exception/InvalidEnvironmentException.php b/src/Mage/Runtime/Exception/InvalidEnvironmentException.php deleted file mode 100644 index 10f4359..0000000 --- a/src/Mage/Runtime/Exception/InvalidEnvironmentException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Mage\Runtime\Exception; - -/** - * The provided Environment is invalid - * - * @author Andrés Montañez - */ -class InvalidEnvironmentException extends RuntimeException -{ -} diff --git a/src/Mage/Runtime/Runtime.php b/src/Mage/Runtime/Runtime.php index f93ffed..f9094da 100644 --- a/src/Mage/Runtime/Runtime.php +++ b/src/Mage/Runtime/Runtime.php @@ -13,7 +13,7 @@ namespace Mage\Runtime; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Symfony\Component\Process\Process; -use Mage\Runtime\Exception\InvalidEnvironmentException; +use Mage\Runtime\Exception\RuntimeException; /** * Runtime is a container of all run in time configuration, stages of progress, hosts being deployed, etc. @@ -210,7 +210,6 @@ class Runtime * @param mixed $key Section name * @param mixed $default Default value * @return mixed - * @throws InvalidEnvironmentException */ public function getEnvironmentConfig($key = null, $default = null) { @@ -257,7 +256,7 @@ class Runtime * * @param string $environment Environment name * @return Runtime - * @throws InvalidEnvironmentException + * @throws RuntimeException */ public function setEnvironment($environment) { @@ -266,7 +265,7 @@ class Runtime return $this; } - throw new InvalidEnvironmentException(sprintf('The environment "%s" does not exists.', $environment), 100); + throw new RuntimeException(sprintf('The environment "%s" does not exists.', $environment), 100); } /** @@ -305,7 +304,6 @@ class Runtime * Retrieve the defined Tasks for the current Environment and Stage * * @return array - * @throws InvalidEnvironmentException */ public function getTasks() { @@ -405,7 +403,6 @@ class Runtime * @param bool $jail Jail the command * @param int $timeout Seconds to wait * @return Process - * @throws InvalidEnvironmentException */ public function runRemoteCommand($cmd, $jail = true, $timeout = 120) { diff --git a/src/Mage/Task/BuiltIn/Deploy/ReleaseTask.php b/src/Mage/Task/BuiltIn/Deploy/ReleaseTask.php index 25779e2..8eecf3d 100644 --- a/src/Mage/Task/BuiltIn/Deploy/ReleaseTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/ReleaseTask.php @@ -10,6 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy; +use Mage\Task\Exception\ErrorException; use Mage\Task\ExecuteOnRollbackInterface; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; @@ -33,6 +34,10 @@ class ReleaseTask extends AbstractTask implements ExecuteOnRollbackInterface public function execute() { + if (!$this->runtime->getEnvironmentConfig('releases', false)) { + throw new ErrorException('This task is only available with releases enabled', 40); + } + $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); $releaseId = $this->runtime->getReleaseId(); diff --git a/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php b/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php index 5ae69ae..539981f 100644 --- a/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php index 19e64bd..10bd387 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy\TarGz; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php index 6ca3bff..a25d348 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy\TarGz; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php index 440a26b..dad36d3 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy\TarGz; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; diff --git a/src/Mage/Task/BuiltIn/FS/AbstractFileTask.php b/src/Mage/Task/BuiltIn/FS/AbstractFileTask.php index 1367106..4e799a6 100644 --- a/src/Mage/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Mage/Task/BuiltIn/FS/AbstractFileTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\FS; -use Mage\Runtime\Exception\RuntimeException; +use Mage\Task\Exception\ErrorException; use Mage\Task\AbstractTask; /** @@ -26,7 +26,7 @@ abstract class AbstractFileTask extends AbstractTask foreach ($mandatory as $parameter) { if (!array_key_exists($parameter, $this->options)) { - throw new RuntimeException(sprintf('Parameter "%s" is not defined', $parameter)); + throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter)); } } diff --git a/src/Mage/Task/BuiltIn/FS/CopyTask.php b/src/Mage/Task/BuiltIn/FS/CopyTask.php index 3238a33..4dc7bae 100644 --- a/src/Mage/Task/BuiltIn/FS/CopyTask.php +++ b/src/Mage/Task/BuiltIn/FS/CopyTask.php @@ -11,6 +11,7 @@ namespace Mage\Task\BuiltIn\FS; use Symfony\Component\Process\Process; +use Exception; /** * File System Task - Copy a File @@ -26,7 +27,12 @@ class CopyTask extends AbstractFileTask public function getDescription() { - return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + try { + return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + + } catch (Exception $exception) { + return '[FS] Copy [missing parameters]'; + } } public function execute() diff --git a/src/Mage/Task/BuiltIn/FS/LinkTask.php b/src/Mage/Task/BuiltIn/FS/LinkTask.php index 06b90fb..1ed5987 100644 --- a/src/Mage/Task/BuiltIn/FS/LinkTask.php +++ b/src/Mage/Task/BuiltIn/FS/LinkTask.php @@ -11,6 +11,7 @@ namespace Mage\Task\BuiltIn\FS; use Symfony\Component\Process\Process; +use Exception; /** * File System Task - Symlink a File @@ -26,7 +27,12 @@ class LinkTask extends AbstractFileTask public function getDescription() { - return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + try { + return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + + } catch (Exception $exception) { + return '[FS] Link [missing parameters]'; + } } public function execute() diff --git a/src/Mage/Task/BuiltIn/FS/MoveTask.php b/src/Mage/Task/BuiltIn/FS/MoveTask.php index 4da9b94..48b44f5 100644 --- a/src/Mage/Task/BuiltIn/FS/MoveTask.php +++ b/src/Mage/Task/BuiltIn/FS/MoveTask.php @@ -11,6 +11,7 @@ namespace Mage\Task\BuiltIn\FS; use Symfony\Component\Process\Process; +use Exception; /** * File System Task - Move a File @@ -26,7 +27,12 @@ class MoveTask extends AbstractFileTask public function getDescription() { - return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + try { + return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + + } catch (Exception $exception) { + return '[FS] Move [missing parameters]'; + } } public function execute() diff --git a/src/Mage/Task/BuiltIn/FS/RemoveTask.php b/src/Mage/Task/BuiltIn/FS/RemoveTask.php index 01726fc..443c536 100644 --- a/src/Mage/Task/BuiltIn/FS/RemoveTask.php +++ b/src/Mage/Task/BuiltIn/FS/RemoveTask.php @@ -11,6 +11,7 @@ namespace Mage\Task\BuiltIn\FS; use Symfony\Component\Process\Process; +use Exception; /** * File System Task - Remove a File @@ -26,7 +27,12 @@ class RemoveTask extends AbstractFileTask public function getDescription() { - return sprintf('[FS] Remove "%s"', $this->getFile('file')); + try { + return sprintf('[FS] Remove "%s"', $this->getFile('file')); + + } catch (Exception $exception) { + return '[FS] Remove [missing parameters]'; + } } public function execute() diff --git a/src/Mage/Task/BuiltIn/Git/ChangeBranchTask.php b/src/Mage/Task/BuiltIn/Git/ChangeBranchTask.php index 7ad9eaa..ae1d81e 100644 --- a/src/Mage/Task/BuiltIn/Git/ChangeBranchTask.php +++ b/src/Mage/Task/BuiltIn/Git/ChangeBranchTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Git; -use Mage\Task\SkipException; +use Mage\Task\Exception\SkipException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; diff --git a/src/Mage/Task/ErrorException.php b/src/Mage/Task/Exception/ErrorException.php similarity index 95% rename from src/Mage/Task/ErrorException.php rename to src/Mage/Task/Exception/ErrorException.php index ffd565f..56b4751 100644 --- a/src/Mage/Task/ErrorException.php +++ b/src/Mage/Task/Exception/ErrorException.php @@ -8,7 +8,7 @@ * file that was distributed with this source code. */ -namespace Mage\Task; +namespace Mage\Task\Exception; use Exception; diff --git a/src/Mage/Task/SkipException.php b/src/Mage/Task/Exception/SkipException.php similarity index 92% rename from src/Mage/Task/SkipException.php rename to src/Mage/Task/Exception/SkipException.php index c4f8eb8..4d23bb2 100644 --- a/src/Mage/Task/SkipException.php +++ b/src/Mage/Task/Exception/SkipException.php @@ -8,7 +8,7 @@ * file that was distributed with this source code. */ -namespace Mage\Task; +namespace Mage\Task\Exception; use Exception; diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php index af57eb4..a7fb063 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php @@ -13,8 +13,6 @@ namespace Mage\Tests\Command\BuiltIn; use Mage\Command\BuiltIn\DeployCommand; use Mage\Tests\MageApplicationMockup; use Mage\Command\AbstractCommand; -use Mage\Runtime\Exception\RuntimeException; -use Exception; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit_Framework_TestCase as TestCase; @@ -95,12 +93,8 @@ class DeployCommandMiscTasksTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); - $this->assertEquals('Invalid task name "invalid/task"', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertEquals(7, $tester->getStatusCode()); + $this->assertContains('Invalid task name "invalid/task"', $tester->getDisplay()); } } diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php index 940ca3d..d747d11 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTest.php @@ -192,7 +192,6 @@ class DeployCommandMiscTest extends TestCase 3 => 'composer install --optimize-autoloader', 4 => 'composer dump-autoload --optimize', 5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=./var/cache/* --exclude=./var/log/* --exclude=./web/app_dev.php ./ tester@testhost:/var/www/test', - 6 => 'git checkout master', ); // Check total of Executed Commands diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php index ae92c71..d5600a3 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -76,13 +76,9 @@ class ListCommandTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof DeploymentException); - $this->assertEquals('Releases are not enabled', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('Releases are not enabled', $tester->getDisplay()); } public function testFailToGetCurrentRelease() @@ -96,13 +92,9 @@ class ListCommandTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); - $this->assertEquals('Unable to retrieve current release from host "host1"', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('Unable to retrieve current release from host "host1"', $tester->getDisplay()); } public function testNoReleasesAvailable() @@ -131,13 +123,9 @@ class ListCommandTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); - $this->assertEquals('Unable to retrieve releases from host "host3"', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('Unable to retrieve releases from host "host3"', $tester->getDisplay()); } public function testNoHosts() diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php index 4bb61df..f198063 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php @@ -75,13 +75,9 @@ class RollbackCommandTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof DeploymentException); - $this->assertEquals('Releases are not enabled', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('Releases are not enabled', $tester->getDisplay()); } public function testRollbackReleaseNotAvailable() @@ -95,12 +91,8 @@ class RollbackCommandTest extends TestCase $tester = new CommandTester($command); - try { - $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); - $this->assertTrue(false, 'Command did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof DeploymentException); - $this->assertEquals('Release "20170101015115" is not available on all hosts', $exception->getMessage()); - } + $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); + $this->assertNotEquals(0, $tester->getStatusCode()); + $this->assertContains('Release "20170101015115" is not available on all hosts', $tester->getDisplay()); } } diff --git a/src/Mage/Tests/Runtime/RuntimeTest.php b/src/Mage/Tests/Runtime/RuntimeTest.php index 3dcc997..bdb0ca1 100644 --- a/src/Mage/Tests/Runtime/RuntimeTest.php +++ b/src/Mage/Tests/Runtime/RuntimeTest.php @@ -10,8 +10,8 @@ namespace Mage\Tests\Runtime; +use Mage\Runtime\Exception\RuntimeException; use Mage\Runtime\Runtime; -use Mage\Runtime\Exception\InvalidEnvironmentException; use Exception; use Monolog\Logger; use Monolog\Handler\TestHandler; @@ -66,7 +66,7 @@ class RuntimeTest extends TestCase $runtime = new Runtime(); $runtime->setEnvironment('invalid'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof InvalidEnvironmentException); + $this->assertTrue($exception instanceof RuntimeException); } try { @@ -74,7 +74,7 @@ class RuntimeTest extends TestCase $runtime->setConfiguration(['environments' => ['valid' => []]]); $runtime->setEnvironment('invalid'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof InvalidEnvironmentException); + $this->assertTrue($exception instanceof RuntimeException); } } diff --git a/src/Mage/Tests/Task/AbstractTaskTest.php b/src/Mage/Tests/Task/AbstractTaskTest.php index 215c760..e18da77 100644 --- a/src/Mage/Tests/Task/AbstractTaskTest.php +++ b/src/Mage/Tests/Task/AbstractTaskTest.php @@ -10,7 +10,7 @@ namespace Mage\Tests\Task; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Exception; use PHPUnit_Framework_TestCase as TestCase; diff --git a/src/Mage/Tests/Task/BuiltIn/FileSystemTaskTest.php b/src/Mage/Tests/Task/BuiltIn/FileSystemTaskTest.php index b3e561d..c28c3d5 100644 --- a/src/Mage/Tests/Task/BuiltIn/FileSystemTaskTest.php +++ b/src/Mage/Tests/Task/BuiltIn/FileSystemTaskTest.php @@ -10,7 +10,7 @@ namespace Mage\Tests\Task\BuiltIn; -use Mage\Runtime\Exception\RuntimeException; +use Mage\Task\Exception\ErrorException; use Mage\Task\BuiltIn\FS\CopyTask; use Mage\Task\BuiltIn\FS\LinkTask; use Mage\Task\BuiltIn\FS\MoveTask; @@ -121,10 +121,11 @@ class FileSystemTaskTest extends TestCase $task->setRuntime($runtime); try { + $this->assertContains('[missing parameters]', $task->getDescription()); $task->execute(); $this->assertTrue(false, 'Task did not failed'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); + $this->assertTrue($exception instanceof ErrorException); $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); } } @@ -198,10 +199,11 @@ class FileSystemTaskTest extends TestCase $task->setRuntime($runtime); try { + $this->assertContains('[missing parameters]', $task->getDescription()); $task->execute(); $this->assertTrue(false, 'Task did not failed'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); + $this->assertTrue($exception instanceof ErrorException); $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); } } @@ -273,10 +275,11 @@ class FileSystemTaskTest extends TestCase $task->setRuntime($runtime); try { + $this->assertContains('[missing parameters]', $task->getDescription()); $task->execute(); $this->assertTrue(false, 'Task did not failed'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); + $this->assertTrue($exception instanceof ErrorException); $this->assertEquals('Parameter "file" is not defined', $exception->getMessage()); } } @@ -350,10 +353,11 @@ class FileSystemTaskTest extends TestCase $task->setRuntime($runtime); try { + $this->assertContains('[missing parameters]', $task->getDescription()); $task->execute(); $this->assertTrue(false, 'Task did not failed'); } catch (Exception $exception) { - $this->assertTrue($exception instanceof RuntimeException); + $this->assertTrue($exception instanceof ErrorException); $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); } } diff --git a/src/Mage/Tests/Task/TestCaseFailTask.php b/src/Mage/Tests/Task/TestCaseFailTask.php index b206e3a..41101e5 100644 --- a/src/Mage/Tests/Task/TestCaseFailTask.php +++ b/src/Mage/Tests/Task/TestCaseFailTask.php @@ -10,7 +10,7 @@ namespace Mage\Tests\Task; -use Mage\Task\ErrorException; +use Mage\Task\Exception\ErrorException; use Mage\Task\AbstractTask; class TestCaseFailTask extends AbstractTask diff --git a/src/Mage/Tests/UtilsTest.php b/src/Mage/Tests/UtilsTest.php index f2c1207..307394c 100644 --- a/src/Mage/Tests/UtilsTest.php +++ b/src/Mage/Tests/UtilsTest.php @@ -19,9 +19,9 @@ class UtilsTest extends TestCase { public function testStageNames() { - $this->assertEquals('Pre Deployment', Utils::getStageName(Runtime::PRE_DEPLOY)); - $this->assertEquals('On Deployment', Utils::getStageName(Runtime::ON_DEPLOY)); - $this->assertEquals('Post Deployment', Utils::getStageName(Runtime::POST_DEPLOY)); + $this->assertEquals('Pre Deploy', Utils::getStageName(Runtime::PRE_DEPLOY)); + $this->assertEquals('On Deploy', Utils::getStageName(Runtime::ON_DEPLOY)); + $this->assertEquals('Post Deploy', Utils::getStageName(Runtime::POST_DEPLOY)); $this->assertEquals('On Release', Utils::getStageName(Runtime::ON_RELEASE)); $this->assertEquals('Post Release', Utils::getStageName(Runtime::POST_RELEASE)); $this->assertEquals('invalid-stage', Utils::getStageName('invalid-stage')); diff --git a/src/Mage/Utils.php b/src/Mage/Utils.php index eb6b866..ca5b649 100644 --- a/src/Mage/Utils.php +++ b/src/Mage/Utils.php @@ -30,15 +30,15 @@ class Utils { switch ($stage) { case Runtime::PRE_DEPLOY: - return 'Pre Deployment'; + return 'Pre Deploy'; break; case Runtime::ON_DEPLOY: - return 'On Deployment'; + return 'On Deploy'; break; case Runtime::POST_DEPLOY: - return 'Post Deployment'; + return 'Post Deploy'; break; case Runtime::ON_RELEASE: