mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	[Nostromo] Fixes and Tests
This commit is contained in:
		
							parent
							
								
									4b7cc618b4
								
							
						
					
					
						commit
						677bf1ebd4
					
				@ -32,6 +32,11 @@ use Mage\Command\AbstractCommand;
 | 
			
		||||
 */
 | 
			
		||||
class DeployCommand extends AbstractCommand
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var int
 | 
			
		||||
     */
 | 
			
		||||
    protected $statusCode = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var TaskFactory
 | 
			
		||||
     */
 | 
			
		||||
@ -93,7 +98,7 @@ class DeployCommand extends AbstractCommand
 | 
			
		||||
 | 
			
		||||
        $output->writeln('Finished <fg=blue>Magallanes</>');
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
        return $this->statusCode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@ -133,20 +138,21 @@ class DeployCommand extends AbstractCommand
 | 
			
		||||
            $this->runtime->setStage(Runtime::ON_DEPLOY);
 | 
			
		||||
            $onDeployTasks = $this->runtime->getTasks();
 | 
			
		||||
 | 
			
		||||
            if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
 | 
			
		||||
            if (!$this->runtime->inRollback()) {
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($this->runtime->getEnvironmentConfig('releases', false)) {
 | 
			
		||||
                if (!in_array('deploy/targz/copy', $onDeployTasks)) {
 | 
			
		||||
                    array_unshift($onDeployTasks, 'deploy/targz/copy');
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                if (!in_array('deploy/rsync', $onDeployTasks) && !$this->runtime->inRollback()) {
 | 
			
		||||
                    array_unshift($onDeployTasks, 'deploy/rsync');
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) {
 | 
			
		||||
                if (!in_array('deploy/release/prepare', $onDeployTasks)) {
 | 
			
		||||
                    array_unshift($onDeployTasks, 'deploy/release/prepare');
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                if (!in_array('deploy/rsync', $onDeployTasks)) {
 | 
			
		||||
                    array_unshift($onDeployTasks, 'deploy/rsync');
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            foreach ($hosts as $host) {
 | 
			
		||||
@ -269,6 +275,7 @@ class DeployCommand extends AbstractCommand
 | 
			
		||||
                        $this->log(sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName()));
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $output->writeln('<fg=red>FAIL</>');
 | 
			
		||||
                        $this->statusCode = 500;
 | 
			
		||||
                        $this->log(sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName()));
 | 
			
		||||
                    }
 | 
			
		||||
                } catch (SkipException $exception) {
 | 
			
		||||
@ -276,8 +283,9 @@ class DeployCommand extends AbstractCommand
 | 
			
		||||
                    $output->writeln('<fg=yellow>SKIPPED</>');
 | 
			
		||||
                    $this->log(sprintf('Task %s (%s) finished with SKIPPED, thrown SkipException', $task->getDescription(), $task->getName()));
 | 
			
		||||
                } catch (ErrorException $exception) {
 | 
			
		||||
                    $output->writeln(sprintf('<fg=red>FAIL</> [%s]', $exception->getTrimmedMessage()));
 | 
			
		||||
                    $output->writeln(sprintf('<fg=red>ERROR</> [%s]', $exception->getTrimmedMessage()));
 | 
			
		||||
                    $this->log(sprintf('Task %s (%s) finished with FAIL, with Error "%s"', $task->getDescription(), $task->getName(), $exception->getMessage()));
 | 
			
		||||
                    $this->statusCode = $exception->getCode();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -48,17 +48,18 @@ class MageApplication extends Application
 | 
			
		||||
        if (array_key_exists('magephp', $config)) {
 | 
			
		||||
            $config = $config['magephp'];
 | 
			
		||||
 | 
			
		||||
            $logger = null;
 | 
			
		||||
            if (array_key_exists('log_dir', $config)) {
 | 
			
		||||
                $logfile = sprintf('%s/%s.log', $config['log_dir'], date('Ymd_His'));
 | 
			
		||||
                $config['log_file'] = $logfile;
 | 
			
		||||
 | 
			
		||||
                $logger = new Logger('magephp');
 | 
			
		||||
                $logger->pushHandler(new StreamHandler($logfile));
 | 
			
		||||
 | 
			
		||||
                $this->runtime = new Runtime();
 | 
			
		||||
                $this->runtime->setConfiguration($config);
 | 
			
		||||
                $this->runtime->setLogger($logger);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $this->runtime = new Runtime();
 | 
			
		||||
            $this->runtime->setConfiguration($config);
 | 
			
		||||
            $this->runtime->setLogger($logger);
 | 
			
		||||
        } else {
 | 
			
		||||
            throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $file));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -10,6 +10,7 @@
 | 
			
		||||
 | 
			
		||||
namespace Mage\Task\BuiltIn\Deploy;
 | 
			
		||||
 | 
			
		||||
use Mage\Task\ErrorException;
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
use Mage\Task\AbstractTask;
 | 
			
		||||
 | 
			
		||||
@ -38,7 +39,7 @@ class RsyncTask extends AbstractTask
 | 
			
		||||
        $targetDir = rtrim($hostPath, '/');
 | 
			
		||||
 | 
			
		||||
        if ($this->runtime->getEnvironmentConfig('releases', false)) {
 | 
			
		||||
            $targetDir = sprintf('%s/releases/%s', $hostPath, $this->runtime->getReleaseId());
 | 
			
		||||
            throw new ErrorException('Can\'t be used with Releases, use "deploy/targz/copy"');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $excludes = $this->getExcludes();
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@
 | 
			
		||||
 | 
			
		||||
namespace Mage\Task\BuiltIn\Deploy\TarGz;
 | 
			
		||||
 | 
			
		||||
use Mage\Runtime\Exception\DeploymentException;
 | 
			
		||||
use Mage\Task\ErrorException;
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
use Mage\Task\AbstractTask;
 | 
			
		||||
 | 
			
		||||
@ -34,7 +34,7 @@ class CleanupTask extends AbstractTask
 | 
			
		||||
    public function execute()
 | 
			
		||||
    {
 | 
			
		||||
        if (!$this->runtime->getEnvironmentConfig('releases', false)) {
 | 
			
		||||
            throw new DeploymentException('This task is only available with releases enabled', 400);
 | 
			
		||||
            throw new ErrorException('This task is only available with releases enabled', 400);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $tarGzLocal = $this->runtime->getVar('targz_local');
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ use Exception;
 | 
			
		||||
 */
 | 
			
		||||
class ErrorException extends Exception
 | 
			
		||||
{
 | 
			
		||||
    public function getTrimmedMessage($maxLength = 20)
 | 
			
		||||
    public function getTrimmedMessage($maxLength = 80)
 | 
			
		||||
    {
 | 
			
		||||
        $message = $this->getMessage();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -124,6 +124,169 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithErrorTaskCommands()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DeployCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'branch' => 'test',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                'releases' => 4,
 | 
			
		||||
                                'exclude' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'vendor',
 | 
			
		||||
                                        1 => 'app/cache',
 | 
			
		||||
                                        2 => 'app/log',
 | 
			
		||||
                                        3 => 'web/app_dev.php',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'hosts' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'testhost',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'pre-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'git/update',
 | 
			
		||||
                                        1 => 'composer/install',
 | 
			
		||||
                                        2 => 'composer/generate-autoload',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'deploy/rsync',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-release' => null,
 | 
			
		||||
                                'post-release' => null,
 | 
			
		||||
                                'post-deploy' => null,
 | 
			
		||||
                            ),
 | 
			
		||||
                    ),
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('20170101015120');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName(), 'environment' => 'test']);
 | 
			
		||||
 | 
			
		||||
        $ranCommands = $runtime->getRanCommands();
 | 
			
		||||
 | 
			
		||||
        $testCase = array(
 | 
			
		||||
            0 => 'git branch | grep "*"',
 | 
			
		||||
            1 => 'git checkout test',
 | 
			
		||||
            2 => 'git pull',
 | 
			
		||||
            3 => 'composer install --dev',
 | 
			
		||||
            4 => 'composer dumpautoload --optimize',
 | 
			
		||||
            5 => 'tar cfz /tmp/mageXYZ --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./',
 | 
			
		||||
            6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"',
 | 
			
		||||
            7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ',
 | 
			
		||||
            8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"',
 | 
			
		||||
            9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertNotEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithFailingPostDeployTaskCommands()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DeployCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'branch' => 'test',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                'exclude' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'vendor',
 | 
			
		||||
                                        1 => 'app/cache',
 | 
			
		||||
                                        2 => 'app/log',
 | 
			
		||||
                                        3 => 'web/app_dev.php',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'hosts' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'testhost',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'pre-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'git/update',
 | 
			
		||||
                                        1 => 'composer/install',
 | 
			
		||||
                                        2 => 'composer/generate-autoload',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'deploy/rsync',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-release' => null,
 | 
			
		||||
                                'post-release' => null,
 | 
			
		||||
                                'post-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'deploy/targz/cleanup',
 | 
			
		||||
                                    ),
 | 
			
		||||
                            ),
 | 
			
		||||
                    ),
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('20170101015120');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName(), 'environment' => 'test']);
 | 
			
		||||
 | 
			
		||||
        $ranCommands = $runtime->getRanCommands();
 | 
			
		||||
 | 
			
		||||
        $testCase = array(
 | 
			
		||||
            0 => 'git branch | grep "*"',
 | 
			
		||||
            1 => 'git checkout test',
 | 
			
		||||
            2 => 'git pull',
 | 
			
		||||
            3 => 'composer install --dev',
 | 
			
		||||
            4 => 'composer dumpautoload --optimize',
 | 
			
		||||
            5 => 'rsync -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test',
 | 
			
		||||
            6 => 'git checkout master',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertNotEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithoutReleasesCommands()
 | 
			
		||||
@ -228,6 +391,8 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithSkippingTask()
 | 
			
		||||
@ -333,5 +498,7 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user