magallanes/src/Command/BuiltIn/Releases/ListCommand.php

149 lines
5.5 KiB
PHP
Raw Normal View History

2016-12-31 07:52:25 +01:00
<?php
2022-04-10 06:20:03 +02:00
2016-12-31 07:52:25 +01:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn\Releases;
use Mage\Utils;
use Mage\Runtime\Exception\RuntimeException;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* Command for Listing all Releases
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ListCommand extends AbstractCommand
{
/**
* Configure the Command
*/
2022-04-10 06:20:03 +02:00
protected function configure(): void
2016-12-31 07:52:25 +01:00
{
$this
->setName('releases:list')
->setDescription('List the releases on an environment')
2022-04-10 06:20:03 +02:00
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to');
2016-12-31 07:52:25 +01:00
}
/**
* Execute the Command
*/
2022-04-10 06:20:03 +02:00
protected function execute(InputInterface $input, OutputInterface $output): int
2016-12-31 07:52:25 +01:00
{
2017-01-29 23:25:03 +01:00
$this->requireConfig();
$utils = new Utils();
2016-12-31 07:52:25 +01:00
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
2017-01-12 02:22:21 +01:00
if (!$this->runtime->getEnvOption('releases', false)) {
2017-01-07 05:53:57 +01:00
throw new RuntimeException('Releases are not enabled', 70);
}
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
2016-12-31 07:52:25 +01:00
2017-01-12 02:22:21 +01:00
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
2017-01-07 05:53:57 +01:00
}
2016-12-31 07:52:25 +01:00
$output->writeln('');
2017-01-12 02:22:21 +01:00
$hosts = $this->runtime->getEnvOption('hosts');
2018-03-29 23:09:11 +02:00
if (!is_array($hosts) && !$hosts instanceof \Countable) {
$hosts = [];
}
2017-01-07 05:53:57 +01:00
if (count($hosts) == 0) {
$output->writeln('No hosts defined');
$output->writeln('');
} else {
2017-01-12 02:22:21 +01:00
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
// Get List of Releases
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
2016-12-31 07:52:25 +01:00
/** @var Process $process */
2017-01-07 05:53:57 +01:00
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
2016-12-31 07:52:25 +01:00
if (!$process->isSuccessful()) {
2017-01-07 05:53:57 +01:00
throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80);
2016-12-31 07:52:25 +01:00
}
$releases = [];
2017-01-07 05:53:57 +01:00
if (trim($process->getOutput()) != '') {
2018-11-20 14:06:12 +01:00
$releases = explode("\n", trim($process->getOutput()));
2017-01-07 05:53:57 +01:00
rsort($releases);
}
if (count($releases) == 0) {
2022-04-10 06:20:03 +02:00
$output->writeln(
sprintf(' No releases available on host <fg=black;options=bold>%s</>:', $host)
);
2017-01-07 05:53:57 +01:00
} else {
// Get Current Release
$cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false);
if (!$process->isSuccessful()) {
2022-04-10 06:20:03 +02:00
throw new RuntimeException(
sprintf('Unable to retrieve current release from host "%s"', $host),
85
);
2017-01-07 05:53:57 +01:00
}
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
$currentReleaseId = explode('/', trim($process->getOutput()));
$currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1];
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
$output->writeln(sprintf(' Releases on host <fg=black;options=bold>%s</>:', $host));
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
foreach ($releases as $releaseId) {
$releaseDate = $utils->getReleaseDate($releaseId);
2016-12-31 07:52:25 +01:00
2022-04-10 06:20:03 +02:00
$output->write(sprintf(
' Release ID: <fg=magenta>%s</> - Date: <fg=black;options=bold>%s</> [%s]',
2017-01-07 05:53:57 +01:00
$releaseId,
$releaseDate->format('Y-m-d H:i:s'),
$utils->getTimeDiff($releaseDate)
2017-01-07 05:53:57 +01:00
));
if ($releaseId == $currentReleaseId) {
$output->writeln(' <fg=red;options=bold>[current]</>');
} else {
$output->writeln('');
}
2016-12-31 07:52:25 +01:00
}
}
2017-01-07 05:53:57 +01:00
$this->runtime->setWorkingHost(null);
$output->writeln('');
}
2016-12-31 07:52:25 +01:00
}
2017-01-07 05:53:57 +01:00
} catch (RuntimeException $exception) {
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();
2016-12-31 07:52:25 +01:00
}
$output->writeln('Finished <fg=blue>Magallanes</>');
2022-04-10 06:20:03 +02:00
return intval($this->statusCode);
2016-12-31 07:52:25 +01:00
}
}