magallanes/src/Command/BuiltIn/DeployCommand.php

286 lines
9.7 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;
use Mage\Deploy\Strategy\StrategyInterface;
2016-12-31 07:52:25 +01:00
use Mage\Runtime\Exception\RuntimeException;
2017-01-01 05:33:34 +01:00
use Mage\Runtime\Runtime;
2016-12-31 07:52:25 +01:00
use Mage\Task\ExecuteOnRollbackInterface;
use Mage\Task\AbstractTask;
2017-01-07 05:53:57 +01:00
use Mage\Task\Exception\ErrorException;
use Mage\Task\Exception\SkipException;
2016-12-31 07:52:25 +01:00
use Mage\Task\TaskFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
2016-12-31 07:52:25 +01:00
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* The Deployment Command
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class DeployCommand extends AbstractCommand
{
2022-04-10 06:20:03 +02:00
protected TaskFactory $taskFactory;
2016-12-31 07:52:25 +01:00
/**
* 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('deploy')
->setDescription('Deploy code to hosts')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
2022-04-10 06:20:03 +02:00
->addOption(
'branch',
null,
InputOption::VALUE_REQUIRED,
'Force to switch to a branch other than the one defined',
false
);
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();
2016-12-31 07:52:25 +01:00
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
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->getEnvOption('releases', false)) {
2017-01-07 05:53:57 +01:00
$this->runtime->generateReleaseId();
$output->writeln(sprintf(' Release ID: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
}
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(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
if ($input->getOption('branch') !== false) {
2017-01-12 02:22:21 +01:00
$this->runtime->setEnvOption('branch', $input->getOption('branch'));
}
2017-01-12 02:22:21 +01:00
if ($this->runtime->getEnvOption('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvOption('branch')));
2017-01-07 05:53:57 +01:00
}
$output->writeln('');
$this->runDeployment($output, $strategy);
2017-01-07 05:53:57 +01:00
} catch (RuntimeException $exception) {
$output->writeln('');
2016-12-31 07:52:25 +01:00
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
2017-01-07 05:53:57 +01:00
$output->writeln('');
$this->statusCode = 7;
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
}
/**
* Run the Deployment Process
*
2017-01-07 05:53:57 +01:00
* @throws RuntimeException
2016-12-31 07:52:25 +01:00
*/
2022-04-10 06:20:03 +02:00
protected function runDeployment(OutputInterface $output, StrategyInterface $strategy): void
2016-12-31 07:52:25 +01:00
{
2017-01-07 05:53:57 +01:00
// Run "Pre Deploy" Tasks
2017-01-01 05:33:34 +01:00
$this->runtime->setStage(Runtime::PRE_DEPLOY);
if (!$this->runTasks($output, $strategy->getPreDeployTasks())) {
2017-01-11 01:48:38 +01:00
throw $this->getException();
2016-12-31 07:52:25 +01:00
}
2017-01-07 05:53:57 +01:00
// Run "On Deploy" Tasks
$this->runtime->setStage(Runtime::ON_DEPLOY);
$this->runOnHosts($output, $strategy->getOnDeployTasks());
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
// Run "On Release" Tasks
$this->runtime->setStage(Runtime::ON_RELEASE);
$this->runOnHosts($output, $strategy->getOnReleaseTasks());
2016-12-31 07:52:25 +01:00
// Run "Post Release" Tasks
$this->runtime->setStage(Runtime::POST_RELEASE);
$this->runOnHosts($output, $strategy->getPostReleaseTasks());
2016-12-31 07:52:25 +01:00
// Run "Post Deploy" Tasks
$this->runtime->setStage(Runtime::POST_DEPLOY);
if (!$this->runTasks($output, $strategy->getPostDeployTasks())) {
2017-01-11 01:48:38 +01:00
throw $this->getException();
2016-12-31 07:52:25 +01:00
}
}
2016-12-31 07:52:25 +01:00
2022-04-10 06:20:03 +02:00
/**
* @param string[] $tasks
*/
protected function runOnHosts(OutputInterface $output, array $tasks): void
{
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 = [];
}
2022-04-10 06:20:03 +02:00
if (count($hosts) === 0) {
$output->writeln(sprintf(' No hosts defined, skipping %s tasks', $this->getStageName()));
2016-12-31 07:52:25 +01:00
$output->writeln('');
2022-04-10 06:20:03 +02:00
return;
}
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
if (!$this->runTasks($output, $tasks)) {
2016-12-31 07:52:25 +01:00
$this->runtime->setWorkingHost(null);
throw $this->getException();
2016-12-31 07:52:25 +01:00
}
$this->runtime->setWorkingHost(null);
2016-12-31 07:52:25 +01:00
}
}
/**
* Runs all the tasks
*
2022-04-10 06:20:03 +02:00
* @param string[] $tasks
2016-12-31 07:52:25 +01:00
* @throws RuntimeException
*/
2022-04-10 06:20:03 +02:00
protected function runTasks(OutputInterface $output, array $tasks): bool
2016-12-31 07:52:25 +01:00
{
if (count($tasks) == 0) {
2022-04-10 06:20:03 +02:00
$output->writeln(
sprintf(' No tasks defined for <fg=black;options=bold>%s</> stage', $this->getStageName())
);
2016-12-31 07:52:25 +01:00
$output->writeln('');
return true;
}
if ($this->runtime->getHostName() !== null) {
2022-04-10 06:20:03 +02:00
$output->writeln(
sprintf(
' Starting <fg=black;options=bold>%s</> tasks on host <fg=black;options=bold>%s</>:',
$this->getStageName(),
$this->runtime->getHostName()
)
);
2016-12-31 07:52:25 +01:00
} else {
$output->writeln(sprintf(' Starting <fg=black;options=bold>%s</> tasks:', $this->getStageName()));
}
$totalTasks = count($tasks);
$succeededTasks = 0;
foreach ($tasks as $taskName) {
$task = $this->taskFactory->get($taskName);
$output->write(sprintf(' Running <fg=magenta>%s</> ... ', $task->getDescription()));
$this->log(sprintf('Running task %s (%s)', $task->getDescription(), $task->getName()));
if ($this->runtime->inRollback() && !$task instanceof ExecuteOnRollbackInterface) {
$succeededTasks++;
$output->writeln('<fg=yellow>SKIPPED</>');
2022-04-10 06:20:03 +02:00
$this->log(
sprintf(
'Task %s (%s) finished with SKIPPED, it was in a Rollback',
$task->getDescription(),
$task->getName()
)
);
2016-12-31 07:52:25 +01:00
} else {
try {
if ($task->execute()) {
$succeededTasks++;
$output->writeln('<fg=green>OK</>');
2022-04-10 06:20:03 +02:00
$this->log(
sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName())
);
2016-12-31 07:52:25 +01:00
} else {
$output->writeln('<fg=red>FAIL</>');
2017-01-07 05:53:57 +01:00
$this->statusCode = 180;
2022-04-10 06:20:03 +02:00
$this->log(
sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName())
);
2016-12-31 07:52:25 +01:00
}
} catch (SkipException $exception) {
$succeededTasks++;
$output->writeln('<fg=yellow>SKIPPED</>');
2022-04-10 06:20:03 +02:00
$this->log(
sprintf(
'Task %s (%s) finished with SKIPPED, thrown SkipException',
$task->getDescription(),
$task->getName()
)
);
2016-12-31 07:52:25 +01:00
} catch (ErrorException $exception) {
2017-01-01 07:55:09 +01:00
$output->writeln(sprintf('<fg=red>ERROR</> [%s]', $exception->getTrimmedMessage()));
2022-04-10 06:20:03 +02:00
$this->log(
sprintf(
'Task %s (%s) finished with FAIL, with Error "%s"',
$task->getDescription(),
$task->getName(),
$exception->getMessage()
)
);
2017-01-07 05:53:57 +01:00
$this->statusCode = 190;
2016-12-31 07:52:25 +01:00
}
}
2017-01-07 05:53:57 +01:00
if ($this->statusCode !== 0) {
break;
}
2016-12-31 07:52:25 +01:00
}
$alertColor = 'red';
if ($succeededTasks == $totalTasks) {
2016-12-31 07:52:25 +01:00
$alertColor = 'green';
}
2022-04-10 06:20:03 +02:00
$output->writeln(
sprintf(
' Finished <fg=%s>%d/%d</> tasks for <fg=black;options=bold>%s</>.',
$alertColor,
$succeededTasks,
$totalTasks,
$this->getStageName()
)
);
2016-12-31 07:52:25 +01:00
$output->writeln('');
return ($succeededTasks == $totalTasks);
}
/**
2017-01-11 01:48:38 +01:00
* Exception for halting the the current process
*/
2022-04-10 06:20:03 +02:00
protected function getException(): RuntimeException
{
2022-04-10 06:20:03 +02:00
return new RuntimeException(
sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()),
50
);
}
2016-12-31 07:52:25 +01:00
}