From fd7c93f115fe0bbc0867974a513a7f61d7b4f255 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Tue, 3 Dec 2019 14:41:54 +0000 Subject: [PATCH] Revert "Remove drush-aliases SSH calls to find the absolute app root (#869)" This reverts commit 73a09a957dea415df8a05681dfef55e84665dd05. --- .../Local/LocalDrushAliasesCommand.php | 43 +++++++++++++++++-- src/Service/Drush.php | 22 ++++++++++ src/SiteAlias/DrushAlias.php | 12 +++++- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/Command/Local/LocalDrushAliasesCommand.php b/src/Command/Local/LocalDrushAliasesCommand.php index 67572eda4d..b3ddb5325c 100644 --- a/src/Command/Local/LocalDrushAliasesCommand.php +++ b/src/Command/Local/LocalDrushAliasesCommand.php @@ -5,6 +5,7 @@ use Platformsh\Cli\Command\CommandBase; use Platformsh\Cli\Exception\RootNotFoundException; use Platformsh\Cli\Local\BuildFlavor\Drupal; +use Platformsh\Cli\Model\Host\RemoteHost; use Platformsh\Cli\Service\Drush; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -49,7 +50,8 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var \Platformsh\Cli\Service\Drush $drush */ $drush = $this->getService('drush'); - if (!$drush->getDrupalApps($projectRoot)) { + $apps = $drush->getDrupalApps($projectRoot); + if (empty($apps)) { $this->stdErr->writeln('No Drupal applications found.'); return 1; @@ -99,13 +101,48 @@ protected function execute(InputInterface $input, OutputInterface $output) $environments = $this->api()->getEnvironments($project, true, false); - // Cache each environment's deployment information. - // This will at least be used for \Platformsh\Cli\Service\Drush::getSiteUrl(). + // Attempt to find the absolute application root directory for + // each Enterprise environment. This will be cached by the Drush + // service ($drush), for use while generating aliases. + /** @var \Platformsh\Cli\Service\RemoteEnvVars $envVarsService */ + $envVarsService = $this->getService('remote_env_vars'); + /** @var \Platformsh\Cli\Service\Ssh $ssh */ + $ssh = $this->getService('ssh'); + /** @var \Platformsh\Cli\Service\Shell $shell */ + $shell = $this->getService('shell'); foreach ($environments as $environment) { + + // Cache the environment's deployment information. + // This will at least be used for \Platformsh\Cli\Service\Drush::getSiteUrl(). if (!$this->api()->hasCachedCurrentDeployment($environment) && $environment->isActive()) { $this->debug('Fetching deployment information for environment: ' . $environment->id); $this->api()->getCurrentDeployment($environment); } + + if ($environment->deployment_target === 'local') { + continue; + } + foreach ($apps as $app) { + $sshUrl = $environment->getSshUrl($app->getName()); + if (empty($sshUrl)) { + continue; + } + try { + $appRoot = $envVarsService->getEnvVar('APP_DIR', new RemoteHost($sshUrl, $ssh, $shell)); + } catch (\Symfony\Component\Process\Exception\RuntimeException $e) { + $this->stdErr->writeln(sprintf( + 'Unable to find app root for environment %s, app %s', + $this->api()->getEnvironmentLabel($environment, 'comment'), + '' . $app->getName() . '' + )); + $this->stdErr->writeln($e->getMessage()); + continue; + } + if (!empty($appRoot)) { + $this->debug(sprintf('App root for %s: %s', $sshUrl, $appRoot)); + $drush->setCachedAppRoot($sshUrl, $appRoot); + } + } } $drush->createAliases($project, $projectRoot, $environments, $current_group); diff --git a/src/Service/Drush.php b/src/Service/Drush.php index 7d47d314e7..d5da540ee7 100644 --- a/src/Service/Drush.php +++ b/src/Service/Drush.php @@ -39,6 +39,9 @@ class Drush /** @var string|null */ protected $executable; + /** @var string[] */ + protected $cachedAppRoots = []; + /** * @param Config|null $config * @param Shell|null $shellHelper @@ -67,6 +70,25 @@ public function getHomeDir() return $this->homeDir ?: Filesystem::getHomeDirectory(); } + /** + * @param string $sshUrl + * @param string $enterpriseAppRoot + */ + public function setCachedAppRoot($sshUrl, $enterpriseAppRoot) + { + $this->cachedAppRoots[$sshUrl] = $enterpriseAppRoot; + } + + /** + * @param string $sshUrl + * + * @return string + */ + public function getCachedAppRoot($sshUrl) + { + return isset($this->cachedAppRoots[$sshUrl]) ? $this->cachedAppRoots[$sshUrl] : false; + } + /** * Find the global Drush configuration directory. * diff --git a/src/SiteAlias/DrushAlias.php b/src/SiteAlias/DrushAlias.php index fa88e28316..8898fc4064 100644 --- a/src/SiteAlias/DrushAlias.php +++ b/src/SiteAlias/DrushAlias.php @@ -246,9 +246,19 @@ protected function generateRemoteAlias(Environment $environment, LocalApplicatio 'options' => [ $this->getAutoRemoveKey() => true, ], - 'root' => $app->getDocumentRoot(), ]; + // For most environments, we know the application root directory is + // '/app'. It's different in Enterprise environments. + if ($environment->deployment_target !== 'local') { + $appRoot = $this->drush->getCachedAppRoot($sshUrl); + if ($appRoot) { + $alias['root'] = rtrim($appRoot, '/') . '/' . $app->getDocumentRoot(); + } + } else { + $alias['root'] = '/app/' . $app->getDocumentRoot(); + } + list($alias['user'], $alias['host']) = explode('@', $sshUrl, 2); if ($url = $this->drush->getSiteUrl($environment, $app)) {