[Nostromo] Refactor Deployment, add Strategies.

This commit is contained in:
Andrés Montañez 2017-01-10 21:28:32 -03:00
parent bd5c2de798
commit cec0bc1255
7 changed files with 334 additions and 113 deletions

View file

@ -10,6 +10,7 @@
namespace Mage\Command\BuiltIn;
use Mage\Deploy\Strategy\StrategyInterface;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Mage\Task\ExecuteOnRollbackInterface;
@ -63,6 +64,9 @@ class DeployCommand extends AbstractCommand
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
@ -76,6 +80,8 @@ class DeployCommand extends AbstractCommand
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
}
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
if ($input->getOption('branch') !== false) {
$this->runtime->setEnvironmentConfig('branch', $input->getOption('branch'));
}
@ -85,9 +91,7 @@ class DeployCommand extends AbstractCommand
}
$output->writeln('');
$this->taskFactory = new TaskFactory($this->runtime);
$this->runDeployment($output);
$this->runDeployment($output, $strategy);
} catch (RuntimeException $exception) {
$output->writeln('');
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
@ -104,131 +108,52 @@ class DeployCommand extends AbstractCommand
* Run the Deployment Process
*
* @param OutputInterface $output
* @param StrategyInterface $strategy
* @throws RuntimeException
*/
protected function runDeployment(OutputInterface $output)
protected function runDeployment(OutputInterface $output, StrategyInterface $strategy)
{
// Run "Pre Deploy" Tasks
$this->runtime->setStage(Runtime::PRE_DEPLOY);
$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)) {
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
if (!$this->runTasks($output, $strategy->getPreDeployTasks())) {
$this->halt();
}
// Run "On Deploy" Tasks
$hosts = $this->runtime->getEnvironmentConfig('hosts');
if (count($hosts) == 0) {
$output->writeln(' No hosts defined, skipping On Deploy tasks');
$output->writeln('');
} else {
$this->runtime->setStage(Runtime::ON_DEPLOY);
$onDeployTasks = $this->runtime->getTasks();
if ($this->runtime->getEnvironmentConfig('releases', false)) {
if (!in_array('deploy/targz/copy', $onDeployTasks) && !$this->runtime->inRollback()) {
array_unshift($onDeployTasks, 'deploy/targz/copy');
}
if (!in_array('deploy/release/prepare', $onDeployTasks) && !$this->runtime->inRollback()) {
array_unshift($onDeployTasks, 'deploy/release/prepare');
}
} else {
if (!in_array('deploy/rsync', $onDeployTasks) && !$this->runtime->inRollback()) {
array_unshift($onDeployTasks, 'deploy/rsync');
}
}
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
if (!$this->runTasks($output, $onDeployTasks)) {
$this->runtime->setWorkingHost(null);
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
$this->runtime->setWorkingHost(null);
}
}
$this->runtime->setStage(Runtime::ON_DEPLOY);
$this->runOnHosts($output, $strategy->getOnDeployTasks());
// Run "On Release" Tasks
$hosts = $this->runtime->getEnvironmentConfig('hosts');
if (count($hosts) == 0) {
$output->writeln(' No hosts defined, skipping On Release tasks');
$output->writeln('');
} else {
$this->runtime->setStage(Runtime::ON_RELEASE);
$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)) {
$this->runtime->setWorkingHost(null);
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
$this->runtime->setWorkingHost(null);
}
}
$this->runtime->setStage(Runtime::ON_RELEASE);
$this->runOnHosts($output, $strategy->getOnReleaseTasks());
// Run "Post Release" Tasks
$hosts = $this->runtime->getEnvironmentConfig('hosts');
if (count($hosts) == 0) {
$output->writeln(' No hosts defined, skipping Post Release tasks');
$output->writeln('');
} else {
$this->runtime->setStage(Runtime::POST_RELEASE);
$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)) {
$this->runtime->setWorkingHost(null);
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
$this->runtime->setWorkingHost(null);
}
}
$this->runtime->setStage(Runtime::POST_RELEASE);
$this->runOnHosts($output, $strategy->getPostReleaseTasks());
// Run "Post Deploy" Tasks
$this->runtime->setStage(Runtime::POST_DEPLOY);
$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->runTasks($output, $strategy->getPostDeployTasks())) {
$this->halt();
}
}
if ($this->runtime->getEnvironmentConfig('branch', false) && !$this->runtime->inRollback()) {
if (!in_array('git/change-branch', $postDeployTasks)) {
array_push($postDeployTasks, 'git/change-branch');
protected function runOnHosts(OutputInterface $output, $tasks)
{
$hosts = $this->runtime->getEnvironmentConfig('hosts');
if (count($hosts) == 0) {
$output->writeln(sprintf(' No hosts defined, skipping %s tasks', $this->getStageName()));
$output->writeln('');
} else {
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
if (!$this->runTasks($output, $tasks)) {
$this->runtime->setWorkingHost(null);
$this->halt();
}
$this->runtime->setWorkingHost(null);
}
}
if (!$this->runTasks($output, $postDeployTasks)) {
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
}
/**
@ -304,4 +229,14 @@ class DeployCommand extends AbstractCommand
return ($succeededTasks == $totalTasks);
}
/**
* Halts the current process
*
* @throws RuntimeException
*/
protected function halt()
{
throw new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
}

View file

@ -58,6 +58,9 @@ class RollbackCommand extends DeployCommand
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
throw new RuntimeException('Releases are not enabled', 70);
}
@ -79,10 +82,10 @@ class RollbackCommand extends DeployCommand
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
}
$output->writeln('');
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
$this->taskFactory = new TaskFactory($this->runtime);
$this->runDeployment($output);
$output->writeln('');
$this->runDeployment($output, $strategy);
} catch (RuntimeException $exception) {
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();

View file

@ -0,0 +1,122 @@
<?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\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
/**
* Strategy for Deployment with Releases, using TarGz and SCP
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ReleasesStrategy implements StrategyInterface
{
/**
* @var Runtime
*/
protected $runtime;
public function getName()
{
return 'Releases';
}
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
}
public function getPreDeployTasks()
{
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_unshift($tasks, 'git/change-branch');
}
if (!$this->runtime->inRollback() && !in_array('deploy/targz/prepare', $tasks)) {
array_push($tasks, 'deploy/targz/prepare');
}
return $tasks;
}
public function getOnDeployTasks()
{
$this->checkStage(Runtime::ON_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/targz/copy', $tasks)) {
array_unshift($tasks, 'deploy/targz/copy');
}
if (!$this->runtime->inRollback() && !in_array('deploy/release/prepare', $tasks)) {
array_unshift($tasks, 'deploy/release/prepare');
}
return $tasks;
}
public function getOnReleaseTasks()
{
$this->checkStage(Runtime::ON_RELEASE);
$tasks = $this->runtime->getTasks();
if (!in_array('deploy/release', $tasks)) {
array_unshift($tasks, 'deploy/release');
}
return $tasks;
}
public function getPostReleaseTasks()
{
$this->checkStage(Runtime::POST_RELEASE);
$tasks = $this->runtime->getTasks();
if (!in_array('deploy/release/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/release/cleanup');
}
return $tasks;
}
public function getPostDeployTasks()
{
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/targz/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/targz/cleanup');
}
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
}
return $tasks;
}
/**
* Check the runtime stage is correct
*
* @param $stage
* @throws RuntimeException
*/
private function checkStage($stage)
{
if ($this->runtime->getStage() !== $stage) {
throw new RuntimeException(sprintf('Invalid stage, got "%s" but expected "%"', $this->runtime->getStage(), $stage));
}
}
}

View file

@ -0,0 +1,96 @@
<?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\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
/**
* Strategy for Deployment with Rsync
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RsyncStrategy implements StrategyInterface
{
/**
* @var Runtime
*/
protected $runtime;
public function getName()
{
return 'Rsync';
}
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
}
public function getPreDeployTasks()
{
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_unshift($tasks, 'git/change-branch');
}
return $tasks;
}
public function getOnDeployTasks()
{
$this->checkStage(Runtime::ON_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/rsync', $tasks)) {
array_unshift($tasks, 'deploy/rsync');
}
return $tasks;
}
public function getOnReleaseTasks()
{
return [];
}
public function getPostReleaseTasks()
{
return [];
}
public function getPostDeployTasks()
{
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
}
return $tasks;
}
/**
* Check the runtime stage is correct
*
* @param $stage
* @throws RuntimeException
*/
private function checkStage($stage)
{
if ($this->runtime->getStage() !== $stage) {
throw new RuntimeException(sprintf('Invalid stage, got "%s" but expected "%"', $this->runtime->getStage(), $stage));
}
}
}

View file

@ -0,0 +1,35 @@
<?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\Deploy\Strategy;
use Mage\Runtime\Runtime;
/**
* Interface for Deploy Strategies
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
interface StrategyInterface
{
public function getName();
public function setRuntime(Runtime $runtime);
public function getPreDeployTasks();
public function getOnDeployTasks();
public function getOnReleaseTasks();
public function getPostReleaseTasks();
public function getPostDeployTasks();
}

View file

@ -10,6 +10,9 @@
namespace Mage\Runtime;
use Mage\Deploy\Strategy\ReleasesStrategy;
use Mage\Deploy\Strategy\RsyncStrategy;
use Mage\Deploy\Strategy\StrategyInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Process\Process;
@ -472,4 +475,31 @@ class Runtime
$userData = posix_getpwuid(posix_geteuid());
return $userData['name'];
}
/**
* Shortcut for getting Branch information
*
* @return boolean|string
*/
public function getBranch()
{
return $this->getEnvironmentConfig('branch', false);
}
/**
* Guesses the Deploy Strategy to use
*
* @return StrategyInterface
*/
public function guessStrategy()
{
$strategy = new RsyncStrategy();
if ($this->getEnvironmentConfig('releases', false)) {
$strategy = new ReleasesStrategy();
}
$strategy->setRuntime($this);
return $strategy;
}
}

View file

@ -11,5 +11,5 @@ magephp:
- ./web/app_dev.php
hosts:
- host2
on-release:
on-deploy:
- deploy/release