[Nostromo] Improve Exception workflow

This commit is contained in:
Andrés Montañez
2017-01-07 01:53:57 -03:00
parent 6d5861ec82
commit 238777cb41
29 changed files with 216 additions and 247 deletions
@@ -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('<error>%s</error>', $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);
}
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getConfigOptions('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
}
if ($this->runtime->getConfigOptions('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
}
$output->writeln('');
$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);
$hosts = $this->runtime->getEnvironmentConfig('hosts');
if (count($hosts) == 0) {
$output->writeln('No hosts defined');
$output->writeln('');
} else {
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
// Get List of Releases
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
/** @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);
}
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 <fg=black;options=bold>%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 = [];
}
$output->writeln(sprintf(' Releases on host <fg=black;options=bold>%s</>:', $host));
if (count($releases) == 0) {
$output->writeln(sprintf(' No releases available on host <fg=black;options=bold>%s</>:', $host));
} else {
// Get Current Release
$cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath);
foreach ($releases as $releaseId) {
$releaseDate = Utils::getReleaseDate($releaseId);
/** @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->write(sprintf(' Release ID: <fg=magenta>%s</> - Date: <fg=black;options=bold>%s</> [%s]',
$releaseId,
$releaseDate->format('Y-m-d H:i:s'),
Utils::getTimeDiff($releaseDate)
));
$currentReleaseId = explode('/', trim($process->getOutput()));
$currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1];
if ($releaseId == $currentReleaseId) {
$output->writeln(' <fg=red;options=bold>[current]</>');
} else {
$output->writeln('');
$output->writeln(sprintf(' Releases on host <fg=black;options=bold>%s</>:', $host));
foreach ($releases as $releaseId) {
$releaseDate = Utils::getReleaseDate($releaseId);
$output->write(sprintf(' Release ID: <fg=magenta>%s</> - Date: <fg=black;options=bold>%s</> [%s]',
$releaseId,
$releaseDate->format('Y-m-d H:i:s'),
Utils::getTimeDiff($releaseDate)
));
if ($releaseId == $currentReleaseId) {
$output->writeln(' <fg=red;options=bold>[current]</>');
} else {
$output->writeln('');
}
}
}
}
$this->runtime->setWorkingHost(null);
$output->writeln('');
$this->runtime->setWorkingHost(null);
$output->writeln('');
}
}
} catch (RuntimeException $exception) {
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();
}
$output->writeln('Finished <fg=blue>Magallanes</>');
return 0;
return $this->statusCode;
}
}
@@ -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('<error>%s</error>', $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: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
$output->writeln(sprintf(' Rollback to Release ID: <fg=green>%s</>', $this->runtime->getReleaseId()));
$output->writeln(sprintf(' Rollback to Release Id: <fg=green>%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('<error>%s</error>', $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('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();
}
$output->writeln('Finished <fg=blue>Magallanes</>');
return 0;
return $this->statusCode;
}
/**