diff --git a/CHANGELOG.md b/CHANGELOG.md index c81798a..d061184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG for 3.X * 3.4.0 (2018-03-29) * [Issue#380] Throw exception if log_dir is defined but directory doesn't exists + * [Issue#405] Malformed ssh command when defining host:port notation * 3.3.0 (2017-07-22) diff --git a/src/Command/BuiltIn/DeployCommand.php b/src/Command/BuiltIn/DeployCommand.php index dd86e4d..339f275 100644 --- a/src/Command/BuiltIn/DeployCommand.php +++ b/src/Command/BuiltIn/DeployCommand.php @@ -175,8 +175,8 @@ class DeployCommand extends AbstractCommand return true; } - if ($this->runtime->getWorkingHost() !== null) { - $output->writeln(sprintf(' Starting %s tasks on host %s:', $this->getStageName(), $this->runtime->getWorkingHost())); + if ($this->runtime->getHostName() !== null) { + $output->writeln(sprintf(' Starting %s tasks on host %s:', $this->getStageName(), $this->runtime->getHostName())); } else { $output->writeln(sprintf(' Starting %s tasks:', $this->getStageName())); } diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index f28e03f..2a4eb07 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -429,7 +429,7 @@ class Runtime { $user = $this->getEnvOption('user', $this->getCurrentUser()); $sudo = $this->getEnvOption('sudo', false); - $host = $this->getWorkingHost(); + $host = $this->getHostName(); $sshConfig = $this->getSSHConfig(); $cmdDelegate = $cmd; @@ -485,6 +485,17 @@ class Runtime return isset($info[1]) ? $info[1] : null; } + /** + * Get the current Host Name + * + * @return string + */ + public function getHostName() + { + $info = explode(':', $this->getWorkingHost()); + return $info[0]; + } + /** * Gets a Temporal File name * diff --git a/src/Task/BuiltIn/Deploy/RsyncTask.php b/src/Task/BuiltIn/Deploy/RsyncTask.php index b1ac64b..b3ce7f9 100644 --- a/src/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Task/BuiltIn/Deploy/RsyncTask.php @@ -36,7 +36,7 @@ class RsyncTask extends AbstractTask $flags = $this->runtime->getEnvOption('rsync', '-avz'); $sshConfig = $this->runtime->getSSHConfig(); $user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser()); - $host = $this->runtime->getWorkingHost(); + $host = $this->runtime->getHostName(); $hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/'); $targetDir = rtrim($hostPath, '/'); diff --git a/src/Task/BuiltIn/Deploy/Tar/CopyTask.php b/src/Task/BuiltIn/Deploy/Tar/CopyTask.php index 294d2bc..997023d 100644 --- a/src/Task/BuiltIn/Deploy/Tar/CopyTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/CopyTask.php @@ -38,7 +38,7 @@ class CopyTask extends AbstractTask } $user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser()); - $host = $this->runtime->getWorkingHost(); + $host = $this->runtime->getHostName(); $sshConfig = $sshConfig = $this->runtime->getSSHConfig(); $hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/'); $currentReleaseId = $this->runtime->getReleaseId(); diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index eb782b2..6243946 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -61,8 +61,8 @@ abstract class AbstractFileTask extends AbstractTask '%environment%' => $this->runtime->getEnvironment(), ]; - if ($this->runtime->getWorkingHost() !== null) { - $mapping['%host%'] = $this->runtime->getWorkingHost(); + if ($this->runtime->getHostName() !== null) { + $mapping['%host%'] = $this->runtime->getHostName(); } if ($this->runtime->getReleaseId() !== null) { diff --git a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php index ead0053..b498f6b 100644 --- a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -68,6 +68,56 @@ class DeployCommandWithReleasesTest extends TestCase $this->assertEquals(0, $tester->getStatusCode()); } + public function testDeploymentWithReleasesWithPortCommands() + { + $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-port.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 cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./', + 6 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "mkdir -p /var/www/test/releases/1234567890"', + 7 => 'scp -P 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', + 8 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"', + 9 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"', + 10 => 'ssh -p 202 -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 202 -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 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assetic:dump --env=dev"', + 13 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"', + 14 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"', + 15 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"', + 16 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"', + 17 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"', + 18 => 'ssh -p 202 -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 testDeploymentWithReleasesWithFromCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-from.yml'); diff --git a/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php index 0ba490b..1e58531 100644 --- a/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithoutReleasesTest.php @@ -55,6 +55,43 @@ class DeployCommandWithoutReleasesTest extends TestCase $this->assertEquals(0, $tester->getStatusCode()); } + public function testDeploymentWithoutReleasesWithPortCommands() + { + $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases-with-port.yml'); + + /** @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 => 'rsync -e "ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=./var/cache/* --exclude=./var/log/* --exclude=./web/app_dev.php ./ tester@testhost:/var/www/test', + 6 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && bin/console cache:warmup --env=dev"', + 7 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && bin/console assets:install web --env=dev --symlink --relative"', + 8 => 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && bin/console assetic:dump --env=dev"', + 9 => '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 testDeploymentWithoutReleasesWithFromCommands() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases-with-from.yml'); diff --git a/tests/Resources/testhost-with-port.yml b/tests/Resources/testhost-with-port.yml new file mode 100644 index 0000000..2c8a58d --- /dev/null +++ b/tests/Resources/testhost-with-port.yml @@ -0,0 +1,27 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + - + - + hosts: + - testhost:202 + pre-deploy: + - git/update + - composer/install + - composer/dump-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/tests/Resources/testhost-without-releases-with-port.yml b/tests/Resources/testhost-without-releases-with-port.yml new file mode 100644 index 0000000..9f4f184 --- /dev/null +++ b/tests/Resources/testhost-without-releases-with-port.yml @@ -0,0 +1,24 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost:202 + pre-deploy: + - git/update + - composer/install + - composer/dump-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/assetic-dump: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/tests/Runtime/ProcessMockup.php b/tests/Runtime/ProcessMockup.php index 918a127..f5b862b 100644 --- a/tests/Runtime/ProcessMockup.php +++ b/tests/Runtime/ProcessMockup.php @@ -99,6 +99,14 @@ class ProcessMockup extends Process return '/var/www/test/releases/20170101015117'; } + if ($this->commandline == 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"') { + return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); + } + + if ($this->commandline == 'ssh -p 202 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "readlink -f /var/www/test/current"') { + return '/var/www/test/releases/20170101015117'; + } + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host1 "ls -1 /var/www/test/releases"') { return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); }