Skip to content

Commit

Permalink
Remove drush-aliases SSH calls to find the absolute app root (#869)
Browse files Browse the repository at this point in the history
This was done for dedicated (Enterprise) environments (whose absolute app root
was not /app). Drush now supports relative paths (since 9.6.2) so this is
unnecessary.

Users whose dedicated (Enterprise) sites are on old versions of Drush 9 (i.e.
between 9.0.0 and 9.6.2) are advised to upgrade Drush to at least 9.6.2. 9.7.1
is the latest version at the time of writing.

See the Drush fix in drush-ops/drush#4019
  • Loading branch information
pjcdawkins authored Nov 4, 2019
1 parent 40aa3f5 commit 73a09a9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 73 deletions.
43 changes: 3 additions & 40 deletions src/Command/Local/LocalDrushAliasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand Down Expand Up @@ -50,8 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
/** @var \Platformsh\Cli\Service\Drush $drush */
$drush = $this->getService('drush');

$apps = $drush->getDrupalApps($projectRoot);
if (empty($apps)) {
if (!$drush->getDrupalApps($projectRoot)) {
$this->stdErr->writeln('No Drupal applications found.');

return 1;
Expand Down Expand Up @@ -101,48 +99,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

$environments = $this->api()->getEnvironments($project, true, false);

// 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');
// Cache each environment's deployment information.
// This will at least be used for \Platformsh\Cli\Service\Drush::getSiteUrl().
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'),
'<comment>' . $app->getName() . '</comment>'
));
$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);
Expand Down
22 changes: 0 additions & 22 deletions src/Service/Drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Drush
/** @var string|null */
protected $executable;

/** @var string[] */
protected $cachedAppRoots = [];

/**
* @param Config|null $config
* @param Shell|null $shellHelper
Expand Down Expand Up @@ -70,25 +67,6 @@ 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.
*
Expand Down
12 changes: 1 addition & 11 deletions src/SiteAlias/DrushAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,9 @@ 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)) {
Expand Down

0 comments on commit 73a09a9

Please sign in to comment.