Skip to content

Commit

Permalink
Fixes for v4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-montanez committed Feb 20, 2021
1 parent b7ade4d commit 373ce5d
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG for 4.X
=================

* 4.1.1 (2021-02-20)
* Add `copyDirectory` option
* Bug fixes
* Improve testing and coverage


* 4.1.0 (2021-02-19)
* PHP 8 and Symfony 5 compatibility [PR#448]
* Timeout option for SSH [PR#436]
Expand Down
2 changes: 1 addition & 1 deletion src/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
*/
class Mage
{
const VERSION = '4.1.0';
const VERSION = '4.1.1';
const CODENAME = 'Discovery One';
}
5 changes: 5 additions & 0 deletions src/Runtime/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class Runtime
*/
protected $rollback = false;

public function isWindows()
{
return stripos(PHP_OS, 'WIN') === 0;
}

/**
* Generate the Release ID
*
Expand Down
8 changes: 4 additions & 4 deletions src/Task/BuiltIn/Deploy/Tar/PrepareTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function execute()

$excludes = $this->getExcludes();
$tarPath = $this->runtime->getEnvOption('tar_create_path', 'tar');
$flags = $this->runtime->getEnvOption('tar_create', stripos(PHP_OS, 'WIN') === 0 ? '--force-local -c -z -p -f' : 'cfzp');
$from = './';
$flags = $this->runtime->getEnvOption('tar_create', $this->runtime->isWindows() ? '--force-local -c -z -p -f' : 'cfzp');
$from = $this->runtime->getEnvOption('from', './');

if ($fromPath = $this->runtime->getEnvOption('from', false)) {
$from = sprintf('-C %s %s', $fromPath, $from);
if ($this->runtime->getEnvOption('copyDirectory', false)) {
$from = sprintf('-C %s ./', $from);
}

$cmdTar = sprintf('%s %s %s %s %s', $tarPath, $flags, $tarLocal, $excludes, $from);
Expand Down
103 changes: 102 additions & 1 deletion tests/Command/BuiltIn/DeployCommandWithReleasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Mage\Command\BuiltIn\DeployCommand;
use Mage\Command\AbstractCommand;
use Mage\Tests\MageApplicationMockup;
use Mage\Tests\MageApplicationWindowsMockup;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -133,6 +134,106 @@ public function testDeploymentWithReleasesWithFromCommands()

$ranCommands = $application->getRuntime()->getRanCommands();

$testCase = array(
0 => 'git branch | grep "*"',
1 => 'git checkout test',
2 => 'git pull',
3 => 'composer install --optimize-autoloader',
4 => 'composer dump-autoload --optimize',
5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./dist',
6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"',
9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"',
10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:warmup --env=dev"',
11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assets:install web --env=dev --symlink --relative"',
12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:pool:prune --env=dev"',
13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"',
14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"',
15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"',
16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"',
17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"',
18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"',
19 => 'rm /tmp/mageXYZ',
20 => 'git checkout master',
);

// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));

// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}

$this->assertEquals(0, $tester->getStatusCode());
}

public function testDeploymentWithReleasesWithFromCommandsOnWindows()
{
$application = new MageApplicationWindowsMockup(__DIR__ . '/../../Resources/testhost-with-from.yml');

$application->getRuntime()->setReleaseId('20170101015120');

/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);

$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);

$ranCommands = $application->getRuntime()->getRanCommands();

$testCase = array(
0 => 'git branch | grep "*"',
1 => 'git checkout test',
2 => 'git pull',
3 => 'composer install --optimize-autoloader',
4 => 'composer dump-autoload --optimize',
5 => 'tar --force-local -c -z -p -f /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./dist',
6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"',
9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"',
10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:warmup --env=dev"',
11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assets:install web --env=dev --symlink --relative"',
12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:pool:prune --env=dev"',
13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"',
14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"',
15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"',
16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"',
17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"',
18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"',
19 => 'rm /tmp/mageXYZ',
20 => 'git checkout master',
);

// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));

// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}

$this->assertEquals(0, $tester->getStatusCode());
}

public function testDeploymentWithReleasesWithFromCommandsWithDirectoryCopy()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-from-copy-directory.yml');

$application->getRuntime()->setReleaseId('20170101015120');

/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);

$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);

$ranCommands = $application->getRuntime()->getRanCommands();

$testCase = array(
0 => 'git branch | grep "*"',
1 => 'git checkout test',
Expand Down Expand Up @@ -189,7 +290,7 @@ public function testDeploymentWithReleasesWithTimeout()
2 => 'git pull',
3 => 'composer install --optimize-autoloader',
4 => 'composer dump-autoload --optimize',
5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" -C ./ ./',
5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./',
6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"',
Expand Down
27 changes: 27 additions & 0 deletions tests/MageApplicationWindowsMockup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mage\Tests;

use Mage\Tests\Runtime\RuntimeWindowsMockup;
use Mage\MageApplication;

class MageApplicationWindowsMockup extends MageApplication
{
/**
* Gets the Runtime instance to use
*
* @return RuntimeWindowsMockup
*/
protected function instantiateRuntime()
{
return new RuntimeWindowsMockup();
}
}
29 changes: 29 additions & 0 deletions tests/Resources/testhost-with-from-copy-directory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
magephp:
log_dir: /tmp
environments:
test:
user: tester
branch: test
from: ./dist
copyDirectory: true
host_path: /var/www/test
releases: 4
exclude:
- ./var/cache/*
- ./var/log/*
- ./web/app_dev.php
-
-
hosts:
- testhost
pre-deploy:
- git/update
- composer/install
- composer/dump-autoload
on-deploy:
- symfony/cache-warmup: { env: 'dev' }
- symfony/assets-install: { env: 'dev' }
- symfony/cache-pool-prune: { env: 'dev' }
on-release:
post-release:
post-deploy:
19 changes: 19 additions & 0 deletions tests/Runtime/RuntimeWindowsMockup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mage\Tests\Runtime;

class RuntimeWindowsMockup extends RuntimeMockup
{
public function isWindows()
{
return true;
}
}

0 comments on commit 373ce5d

Please sign in to comment.