2016-12-31 07:52:25 +01:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
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;
|
2017-01-04 05:13:51 +01:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var TaskFactory
|
|
|
|
*/
|
|
|
|
protected $taskFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('deploy')
|
|
|
|
->setDescription('Deploy code to hosts')
|
|
|
|
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
|
2017-01-04 05:13:51 +01: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
|
|
|
|
*
|
|
|
|
* @param InputInterface $input
|
|
|
|
* @param OutputInterface $output
|
2017-01-08 06:25:40 +01:00
|
|
|
* @return integer
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$output->writeln('Starting <fg=blue>Magallanes</>');
|
|
|
|
$output->writeln('');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->runtime->setEnvironment($input->getArgument('environment'));
|
|
|
|
|
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-07 05:53:57 +01:00
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false)) {
|
|
|
|
$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-07 05:53:57 +01:00
|
|
|
if ($this->runtime->getConfigOptions('log_file', false)) {
|
|
|
|
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
|
|
|
|
}
|
2016-12-31 07:52:25 +01:00
|
|
|
|
2017-01-04 05:13:51 +01:00
|
|
|
if ($input->getOption('branch') !== false) {
|
|
|
|
$this->runtime->setEnvironmentConfig('branch', $input->getOption('branch'));
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
if ($this->runtime->getEnvironmentConfig('branch', false)) {
|
|
|
|
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvironmentConfig('branch')));
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln('');
|
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
$this->taskFactory = new TaskFactory($this->runtime);
|
|
|
|
$this->runDeployment($output);
|
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</>');
|
|
|
|
|
2017-01-01 07:55:09 +01:00
|
|
|
return $this->statusCode;
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the Deployment Process
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
2017-01-07 05:53:57 +01:00
|
|
|
* @throws RuntimeException
|
2016-12-31 07:52:25 +01:00
|
|
|
*/
|
|
|
|
protected function runDeployment(OutputInterface $output)
|
|
|
|
{
|
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);
|
2016-12-31 07:52:25 +01:00
|
|
|
$preDeployTasks = $this->runtime->getTasks();
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('branch', false) && !$this->runtime->inRollback()) {
|
|
|
|
if (!in_array('git/change-branch', $preDeployTasks)) {
|
|
|
|
array_unshift($preDeployTasks, 'git/change-branch');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
|
|
|
|
if (!in_array('deploy/targz/prepare', $preDeployTasks)) {
|
|
|
|
array_push($preDeployTasks, 'deploy/targz/prepare');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->runTasks($output, $preDeployTasks)) {
|
2017-01-07 05:53:57 +01:00
|
|
|
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
// Run "On Deploy" Tasks
|
2016-12-31 07:52:25 +01:00
|
|
|
$hosts = $this->runtime->getEnvironmentConfig('hosts');
|
|
|
|
if (count($hosts) == 0) {
|
|
|
|
$output->writeln(' No hosts defined, skipping On Deploy tasks');
|
|
|
|
$output->writeln('');
|
|
|
|
} else {
|
2017-01-01 05:33:34 +01:00
|
|
|
$this->runtime->setStage(Runtime::ON_DEPLOY);
|
2016-12-31 07:52:25 +01:00
|
|
|
$onDeployTasks = $this->runtime->getTasks();
|
|
|
|
|
2017-01-01 07:55:09 +01:00
|
|
|
if (!$this->runtime->inRollback()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false)) {
|
2017-01-05 00:21:12 +01:00
|
|
|
if (!in_array('deploy/targz/copy', $onDeployTasks) && !$this->runtime->inRollback()) {
|
2016-12-31 07:52:25 +01:00
|
|
|
array_unshift($onDeployTasks, 'deploy/targz/copy');
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:21:12 +01:00
|
|
|
if (!in_array('deploy/release/prepare', $onDeployTasks) && !$this->runtime->inRollback()) {
|
2016-12-31 07:52:25 +01:00
|
|
|
array_unshift($onDeployTasks, 'deploy/release/prepare');
|
|
|
|
}
|
2017-01-01 07:55:09 +01:00
|
|
|
} else {
|
|
|
|
if (!in_array('deploy/rsync', $onDeployTasks)) {
|
|
|
|
array_unshift($onDeployTasks, 'deploy/rsync');
|
|
|
|
}
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($hosts as $host) {
|
|
|
|
$this->runtime->setWorkingHost($host);
|
|
|
|
if (!$this->runTasks($output, $onDeployTasks)) {
|
2017-01-07 05:53:57 +01:00
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
// Run "On Release" Tasks
|
2016-12-31 07:52:25 +01:00
|
|
|
$hosts = $this->runtime->getEnvironmentConfig('hosts');
|
|
|
|
if (count($hosts) == 0) {
|
|
|
|
$output->writeln(' No hosts defined, skipping On Release tasks');
|
|
|
|
$output->writeln('');
|
|
|
|
} else {
|
2017-01-01 05:33:34 +01:00
|
|
|
$this->runtime->setStage(Runtime::ON_RELEASE);
|
2016-12-31 07:52:25 +01:00
|
|
|
$onReleaseTasks = $this->runtime->getTasks();
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false)) {
|
|
|
|
if (!in_array('deploy/release', $onReleaseTasks)) {
|
|
|
|
array_unshift($onReleaseTasks, 'deploy/release');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($hosts as $host) {
|
|
|
|
$this->runtime->setWorkingHost($host);
|
|
|
|
if (!$this->runTasks($output, $onReleaseTasks)) {
|
2017-01-07 05:53:57 +01:00
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
// Run "Post Release" Tasks
|
2016-12-31 07:52:25 +01:00
|
|
|
$hosts = $this->runtime->getEnvironmentConfig('hosts');
|
|
|
|
if (count($hosts) == 0) {
|
|
|
|
$output->writeln(' No hosts defined, skipping Post Release tasks');
|
|
|
|
$output->writeln('');
|
|
|
|
} else {
|
2017-01-01 05:33:34 +01:00
|
|
|
$this->runtime->setStage(Runtime::POST_RELEASE);
|
2016-12-31 07:52:25 +01:00
|
|
|
$postReleaseTasks = $this->runtime->getTasks();
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
|
|
|
|
if (!in_array('deploy/release/cleanup', $postReleaseTasks)) {
|
|
|
|
array_unshift($postReleaseTasks, 'deploy/release/cleanup');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($hosts as $host) {
|
|
|
|
$this->runtime->setWorkingHost($host);
|
|
|
|
if (!$this->runTasks($output, $postReleaseTasks)) {
|
2017-01-07 05:53:57 +01:00
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
$this->runtime->setWorkingHost(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
// Run "Post Deploy" Tasks
|
2017-01-01 05:33:34 +01:00
|
|
|
$this->runtime->setStage(Runtime::POST_DEPLOY);
|
2016-12-31 07:52:25 +01:00
|
|
|
$postDeployTasks = $this->runtime->getTasks();
|
|
|
|
if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
|
|
|
|
if (!in_array('deploy/targz/cleanup', $postDeployTasks)) {
|
|
|
|
array_unshift($postDeployTasks, 'deploy/targz/cleanup');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->runtime->getEnvironmentConfig('branch', false) && !$this->runtime->inRollback()) {
|
|
|
|
if (!in_array('git/change-branch', $postDeployTasks)) {
|
|
|
|
array_push($postDeployTasks, 'git/change-branch');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->runTasks($output, $postDeployTasks)) {
|
2017-01-07 05:53:57 +01:00
|
|
|
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs all the tasks
|
|
|
|
*
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param $tasks
|
|
|
|
* @return bool
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function runTasks(OutputInterface $output, $tasks)
|
|
|
|
{
|
|
|
|
if (count($tasks) == 0) {
|
2017-01-05 00:21:12 +01: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;
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:32:47 +01:00
|
|
|
if ($this->runtime->getWorkingHost()) {
|
2016-12-31 07:52:25 +01:00
|
|
|
$output->writeln(sprintf(' Starting <fg=black;options=bold>%s</> tasks on host <fg=black;options=bold>%s</>:', $this->getStageName(), $this->runtime->getWorkingHost()));
|
|
|
|
} else {
|
|
|
|
$output->writeln(sprintf(' Starting <fg=black;options=bold>%s</> tasks:', $this->getStageName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalTasks = count($tasks);
|
|
|
|
$succeededTasks = 0;
|
|
|
|
|
|
|
|
foreach ($tasks as $taskName) {
|
|
|
|
/** @var AbstractTask $task */
|
|
|
|
$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</>');
|
|
|
|
$this->log(sprintf('Task %s (%s) finished with SKIPPED, it was in a Rollback', $task->getDescription(), $task->getName()));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
if ($task->execute()) {
|
|
|
|
$succeededTasks++;
|
|
|
|
$output->writeln('<fg=green>OK</>');
|
|
|
|
$this->log(sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName()));
|
|
|
|
} else {
|
|
|
|
$output->writeln('<fg=red>FAIL</>');
|
2017-01-07 05:53:57 +01:00
|
|
|
$this->statusCode = 180;
|
2016-12-31 07:52:25 +01:00
|
|
|
$this->log(sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName()));
|
|
|
|
}
|
|
|
|
} catch (SkipException $exception) {
|
|
|
|
$succeededTasks++;
|
|
|
|
$output->writeln('<fg=yellow>SKIPPED</>');
|
|
|
|
$this->log(sprintf('Task %s (%s) finished with SKIPPED, thrown SkipException', $task->getDescription(), $task->getName()));
|
|
|
|
} catch (ErrorException $exception) {
|
2017-01-01 07:55:09 +01:00
|
|
|
$output->writeln(sprintf('<fg=red>ERROR</> [%s]', $exception->getTrimmedMessage()));
|
2016-12-31 07:52:25 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
if ($succeededTasks != $totalTasks) {
|
|
|
|
$alertColor = 'red';
|
|
|
|
} else {
|
|
|
|
$alertColor = 'green';
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:53:57 +01: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);
|
|
|
|
}
|
|
|
|
}
|