mirror of
https://github.com/hauke68/Magallanes.git
synced 2026-09-08 14:28:57 +02:00
[Nostromo] Initial Commit
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/*
|
||||
* 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\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;
|
||||
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
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('releases:list')
|
||||
->setDescription('List the releases on an environment')
|
||||
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the Command
|
||||
*
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int|mixed
|
||||
* @throws DeploymentException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->writeln('Starting <fg=blue>Magallanes</>');
|
||||
$output->writeln('');
|
||||
|
||||
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', 700);
|
||||
}
|
||||
|
||||
$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')));
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
// Get List of Releases
|
||||
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdListReleases, true);
|
||||
if (!$process->isSuccessful()) {
|
||||
throw new RuntimeException(sprintf('Unable to retrieve releases from host %s', $host), 800);
|
||||
}
|
||||
|
||||
$releases = explode(PHP_EOL, trim($process->getOutput()));
|
||||
rsort($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);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, true);
|
||||
if (!$process->isSuccessful()) {
|
||||
throw new RuntimeException(sprintf('Unable to retrieve current release from host %s', $host), 850);
|
||||
}
|
||||
|
||||
$currentReleaseId = explode('/', trim($process->getOutput()));
|
||||
$currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1];
|
||||
|
||||
$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('');
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('Finished <fg=blue>Magallanes</>');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/*
|
||||
* 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\Task\TaskFactory;
|
||||
use Mage\Runtime\Exception\InvalidEnvironmentException;
|
||||
use Mage\Runtime\Exception\DeploymentException;
|
||||
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\BuiltIn\DeployCommand;
|
||||
|
||||
/**
|
||||
* Command for Rolling Back a Releases
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class RollbackCommand extends DeployCommand
|
||||
{
|
||||
/**
|
||||
* Configure the Command
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('releases:rollback')
|
||||
->setDescription('Rollback to a release on an environment')
|
||||
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
|
||||
->addArgument('release', InputArgument::REQUIRED, 'The ID or the Index of the release to rollback to')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the Command
|
||||
*
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int|mixed
|
||||
* @throws DeploymentException
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->writeln('Starting <fg=blue>Magallanes</>');
|
||||
$output->writeln('');
|
||||
|
||||
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', 700);
|
||||
}
|
||||
|
||||
// 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()));
|
||||
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
|
||||
|
||||
if ($this->runtime->getConfigOptions('log_file', false)) {
|
||||
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
|
||||
}
|
||||
|
||||
$output->writeln('');
|
||||
|
||||
// Get the Task Factory
|
||||
$this->taskFactory = new TaskFactory($this->runtime);
|
||||
|
||||
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), 720);
|
||||
}
|
||||
|
||||
$output->writeln('Finished <fg=blue>Magallanes</>');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided Release ID is available in all hosts
|
||||
*
|
||||
* @param string $releaseToRollback Release ID
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkReleaseAvailability($releaseToRollback)
|
||||
{
|
||||
$releaseIdCandidate = false;
|
||||
$hosts = $this->runtime->getEnvironmentConfig('hosts');
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
|
||||
$releaseAvailableInAllHosts = true;
|
||||
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, true);
|
||||
if (!$process->isSuccessful()) {
|
||||
$releases = [];
|
||||
} else {
|
||||
$releases = explode(PHP_EOL, trim($process->getOutput()));
|
||||
rsort($releases);
|
||||
}
|
||||
|
||||
if (in_array($releaseToRollback, $releases)) {
|
||||
if ($releaseIdCandidate === false) {
|
||||
$releaseIdCandidate = $releaseToRollback;
|
||||
} else {
|
||||
if ($releaseIdCandidate != $releaseToRollback) {
|
||||
$releaseAvailableInAllHosts = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->runtime->setWorkingHost(null);
|
||||
}
|
||||
|
||||
if ($releaseAvailableInAllHosts) {
|
||||
return $releaseIdCandidate;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user