mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	[Nostromo] Refactor Deployment, add Strategies.
This commit is contained in:
		
							parent
							
								
									bd5c2de798
								
							
						
					
					
						commit
						cec0bc1255
					
				@ -10,6 +10,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Mage\Command\BuiltIn;
 | 
					namespace Mage\Command\BuiltIn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Mage\Deploy\Strategy\StrategyInterface;
 | 
				
			||||||
use Mage\Runtime\Exception\RuntimeException;
 | 
					use Mage\Runtime\Exception\RuntimeException;
 | 
				
			||||||
use Mage\Runtime\Runtime;
 | 
					use Mage\Runtime\Runtime;
 | 
				
			||||||
use Mage\Task\ExecuteOnRollbackInterface;
 | 
					use Mage\Task\ExecuteOnRollbackInterface;
 | 
				
			||||||
@ -63,6 +64,9 @@ class DeployCommand extends AbstractCommand
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            $this->runtime->setEnvironment($input->getArgument('environment'));
 | 
					            $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()));
 | 
					            $output->writeln(sprintf('    Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
 | 
				
			||||||
            $this->log(sprintf('Environment: %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('    Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $output->writeln(sprintf('    Strategy: <fg=green>%s</>', $strategy->getName()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($input->getOption('branch') !== false) {
 | 
					            if ($input->getOption('branch') !== false) {
 | 
				
			||||||
                $this->runtime->setEnvironmentConfig('branch', $input->getOption('branch'));
 | 
					                $this->runtime->setEnvironmentConfig('branch', $input->getOption('branch'));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -85,9 +91,7 @@ class DeployCommand extends AbstractCommand
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $output->writeln('');
 | 
					            $output->writeln('');
 | 
				
			||||||
 | 
					            $this->runDeployment($output, $strategy);
 | 
				
			||||||
            $this->taskFactory = new TaskFactory($this->runtime);
 | 
					 | 
				
			||||||
            $this->runDeployment($output);
 | 
					 | 
				
			||||||
        } catch (RuntimeException $exception) {
 | 
					        } catch (RuntimeException $exception) {
 | 
				
			||||||
            $output->writeln('');
 | 
					            $output->writeln('');
 | 
				
			||||||
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
 | 
					            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
 | 
				
			||||||
@ -104,131 +108,52 @@ class DeployCommand extends AbstractCommand
 | 
				
			|||||||
     * Run the Deployment Process
 | 
					     * Run the Deployment Process
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param OutputInterface $output
 | 
					     * @param OutputInterface $output
 | 
				
			||||||
 | 
					     * @param StrategyInterface $strategy
 | 
				
			||||||
     * @throws RuntimeException
 | 
					     * @throws RuntimeException
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    protected function runDeployment(OutputInterface $output)
 | 
					    protected function runDeployment(OutputInterface $output, StrategyInterface $strategy)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // Run "Pre Deploy" Tasks
 | 
					        // Run "Pre Deploy" Tasks
 | 
				
			||||||
        $this->runtime->setStage(Runtime::PRE_DEPLOY);
 | 
					        $this->runtime->setStage(Runtime::PRE_DEPLOY);
 | 
				
			||||||
        $preDeployTasks = $this->runtime->getTasks();
 | 
					        if (!$this->runTasks($output, $strategy->getPreDeployTasks())) {
 | 
				
			||||||
 | 
					            $this->halt();
 | 
				
			||||||
        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);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Run "On Deploy" Tasks
 | 
					        // Run "On Deploy" Tasks
 | 
				
			||||||
        $hosts = $this->runtime->getEnvironmentConfig('hosts');
 | 
					        $this->runtime->setStage(Runtime::ON_DEPLOY);
 | 
				
			||||||
        if (count($hosts) == 0) {
 | 
					        $this->runOnHosts($output, $strategy->getOnDeployTasks());
 | 
				
			||||||
            $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);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Run "On Release" Tasks
 | 
					        // Run "On Release" Tasks
 | 
				
			||||||
        $hosts = $this->runtime->getEnvironmentConfig('hosts');
 | 
					        $this->runtime->setStage(Runtime::ON_RELEASE);
 | 
				
			||||||
        if (count($hosts) == 0) {
 | 
					        $this->runOnHosts($output, $strategy->getOnReleaseTasks());
 | 
				
			||||||
            $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);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Run "Post Release" Tasks
 | 
					        // Run "Post Release" Tasks
 | 
				
			||||||
        $hosts = $this->runtime->getEnvironmentConfig('hosts');
 | 
					        $this->runtime->setStage(Runtime::POST_RELEASE);
 | 
				
			||||||
        if (count($hosts) == 0) {
 | 
					        $this->runOnHosts($output, $strategy->getPostReleaseTasks());
 | 
				
			||||||
            $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);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Run "Post Deploy" Tasks
 | 
					        // Run "Post Deploy" Tasks
 | 
				
			||||||
        $this->runtime->setStage(Runtime::POST_DEPLOY);
 | 
					        $this->runtime->setStage(Runtime::POST_DEPLOY);
 | 
				
			||||||
        $postDeployTasks = $this->runtime->getTasks();
 | 
					        if (!$this->runTasks($output, $strategy->getPostDeployTasks())) {
 | 
				
			||||||
        if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
 | 
					            $this->halt();
 | 
				
			||||||
            if (!in_array('deploy/targz/cleanup', $postDeployTasks)) {
 | 
					 | 
				
			||||||
                array_unshift($postDeployTasks, 'deploy/targz/cleanup');
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($this->runtime->getEnvironmentConfig('branch', false) && !$this->runtime->inRollback()) {
 | 
					    protected function runOnHosts(OutputInterface $output, $tasks)
 | 
				
			||||||
            if (!in_array('git/change-branch', $postDeployTasks)) {
 | 
					    {
 | 
				
			||||||
                array_push($postDeployTasks, 'git/change-branch');
 | 
					        $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);
 | 
					        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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -58,6 +58,9 @@ class RollbackCommand extends DeployCommand
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            $this->runtime->setEnvironment($input->getArgument('environment'));
 | 
					            $this->runtime->setEnvironment($input->getArgument('environment'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $strategy = $this->runtime->guessStrategy();
 | 
				
			||||||
 | 
					            $this->taskFactory = new TaskFactory($this->runtime);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!$this->runtime->getEnvironmentConfig('releases', false)) {
 | 
					            if (!$this->runtime->getEnvironmentConfig('releases', false)) {
 | 
				
			||||||
                throw new RuntimeException('Releases are not enabled', 70);
 | 
					                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(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);
 | 
					            $output->writeln('');
 | 
				
			||||||
            $this->runDeployment($output);
 | 
					            $this->runDeployment($output, $strategy);
 | 
				
			||||||
        } catch (RuntimeException $exception) {
 | 
					        } catch (RuntimeException $exception) {
 | 
				
			||||||
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
 | 
					            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
 | 
				
			||||||
            $this->statusCode = $exception->getCode();
 | 
					            $this->statusCode = $exception->getCode();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										122
									
								
								src/Deploy/Strategy/ReleasesStrategy.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								src/Deploy/Strategy/ReleasesStrategy.php
									
									
									
									
									
										Normal 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));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										96
									
								
								src/Deploy/Strategy/RsyncStrategy.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								src/Deploy/Strategy/RsyncStrategy.php
									
									
									
									
									
										Normal 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));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										35
									
								
								src/Deploy/Strategy/StrategyInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/Deploy/Strategy/StrategyInterface.php
									
									
									
									
									
										Normal 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();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -10,6 +10,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Mage\Runtime;
 | 
					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\LoggerInterface;
 | 
				
			||||||
use Psr\Log\LogLevel;
 | 
					use Psr\Log\LogLevel;
 | 
				
			||||||
use Symfony\Component\Process\Process;
 | 
					use Symfony\Component\Process\Process;
 | 
				
			||||||
@ -472,4 +475,31 @@ class Runtime
 | 
				
			|||||||
        $userData = posix_getpwuid(posix_geteuid());
 | 
					        $userData = posix_getpwuid(posix_geteuid());
 | 
				
			||||||
        return $userData['name'];
 | 
					        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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,5 +11,5 @@ magephp:
 | 
				
			|||||||
                - ./web/app_dev.php
 | 
					                - ./web/app_dev.php
 | 
				
			||||||
            hosts:
 | 
					            hosts:
 | 
				
			||||||
                - host2
 | 
					                - host2
 | 
				
			||||||
            on-release:
 | 
					            on-deploy:
 | 
				
			||||||
                - deploy/release
 | 
					                - deploy/release
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user