Allow overriding a binary

This commit is contained in:
Yanick Witschi 2017-06-07 15:49:18 +02:00
parent 280f48cd18
commit e1c20a6fdd
6 changed files with 97 additions and 4 deletions

View file

@ -155,6 +155,26 @@ class Runtime
return $default;
}
/**
* Retrieve a binary and check if it was changed in the "binary-map" configuration.
* This allows e.g. to override "tar" by "gnutar" etc.
*
* @param string $binary
*
* @return string
*/
public function getBinary($binary)
{
$map = $this->getConfigOption('binary-map', []);
if (!array_key_exists($binary, $map)) {
return $binary;
}
return $map[$binary];
}
/**
* Sets the Logger instance
*

View file

@ -46,7 +46,18 @@ class RsyncTask extends AbstractTask
$excludes = $this->getExcludes();
$from = $this->runtime->getEnvOption('from', './');
$cmdRsync = sprintf('rsync -e "ssh -p %d %s" %s %s %s %s@%s:%s', $sshConfig['port'], $sshConfig['flags'], $flags, $excludes, $from, $user, $host, $targetDir);
$cmdRsync = sprintf('%s -e "%s -p %d %s" %s %s %s %s@%s:%s',
$this->runtime->getBinary('rsync'),
$this->runtime->getBinary('ssh'),
$sshConfig['port'],
$sshConfig['flags'],
$flags,
$excludes,
$from,
$user,
$host,
$targetDir
);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdRsync, 600);

View file

@ -49,12 +49,21 @@ class CopyTask extends AbstractTask
$tarLocal = $this->runtime->getVar('tar_local');
$tarRemote = basename($tarLocal);
$cmdCopy = sprintf('scp -P %d %s %s %s@%s:%s/%s', $sshConfig['port'], $sshConfig['flags'], $tarLocal, $user, $host, $targetDir, $tarRemote);
$cmdCopy = sprintf('%s -P %d %s %s %s@%s:%s/%s',
$this->runtime->getBinary('scp'),
$sshConfig['port'],
$sshConfig['flags'],
$tarLocal,
$user,
$host,
$targetDir,
$tarRemote
);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdCopy, 300);
if ($process->isSuccessful()) {
$cmdUnTar = sprintf('cd %s && tar %s %s', $targetDir, $flags, $tarRemote);
$cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $this->runtime->getBinary('tar'), $flags, $tarRemote);
$process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
if ($process->isSuccessful()) {
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote);

View file

@ -43,7 +43,7 @@ class PrepareTask extends AbstractTask
$excludes = $this->getExcludes();
$flags = $this->runtime->getEnvOption('tar_create', 'cfzp');
$from = $this->runtime->getEnvOption('from', './');
$cmdTar = sprintf('tar %s %s %s %s', $flags, $tarLocal, $excludes, $from);
$cmdTar = sprintf('%s %s %s %s %s', $this->runtime->getBinary('tar'), $flags, $tarLocal, $excludes, $from);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdTar, 300);

View file

@ -148,6 +148,41 @@ class DeployCommandWithReleasesTest extends TestCase
$this->assertNotEquals(0, $tester->getStatusCode());
}
public function testDeploymentWithDifferentBinary()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/binary-map.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 => 'gnutar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./dist',
1 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "mkdir -p /var/www/test/releases/1234567890"',
2 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ',
3 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && gnutar xfzop mageXYZ"',
4 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"',
5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"',
6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"',
7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"',
8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"',
9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"',
10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"',
11 => 'rm /tmp/mageXYZ',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
}
public function testDeploymentWithoutReleasesTarCleanup()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar3.yml');

View file

@ -0,0 +1,18 @@
magephp:
binary-map:
tar: gnutar
environments:
test:
user: tester
from: ./dist
host_path: /var/www/test
releases: 4
exclude:
- ./var/cache/*
- ./var/log/*
- ./web/app_dev.php
-
-
hosts:
- testhost