diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index b42826d..c3478a0 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -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 * diff --git a/src/Task/BuiltIn/Deploy/RsyncTask.php b/src/Task/BuiltIn/Deploy/RsyncTask.php index b1ac64b..783d41d 100644 --- a/src/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Task/BuiltIn/Deploy/RsyncTask.php @@ -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); diff --git a/src/Task/BuiltIn/Deploy/Tar/CopyTask.php b/src/Task/BuiltIn/Deploy/Tar/CopyTask.php index 0609452..8a8ca0e 100644 --- a/src/Task/BuiltIn/Deploy/Tar/CopyTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/CopyTask.php @@ -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); diff --git a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php index fb0c5fe..7ad1ff3 100644 --- a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php @@ -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); diff --git a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php index ead0053..3ddcbf1 100644 --- a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -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'); diff --git a/tests/Resources/binary-map.yml b/tests/Resources/binary-map.yml new file mode 100644 index 0000000..46bd0ab --- /dev/null +++ b/tests/Resources/binary-map.yml @@ -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 +