diff --git a/src/Commands/core/SshCommands.php b/src/Commands/core/SshCommands.php index 1aed1b846b..18681632ad 100644 --- a/src/Commands/core/SshCommands.php +++ b/src/Commands/core/SshCommands.php @@ -3,9 +3,13 @@ use Drush\Commands\DrushCommands; use Drush\Log\LogLevel; +use Drush\SiteAlias\SiteAliasManagerAwareInterface; +use Drush\SiteAlias\SiteAliasManagerAwareTrait; +use Drush\SiteAlias\SiteAliasName; -class SshCommands extends DrushCommands +class SshCommands extends DrushCommands implements SiteAliasManagerAwareInterface { + use SiteAliasManagerAwareTrait; /** * Connect to a Drupal site's server via SSH. @@ -35,6 +39,41 @@ public function ssh(array $args, $options = ['cd' => true]) } $command = implode(' ', $args); + // TODO: Remove when no longer needed. + if (!$this->hasSiteAliasManager()) { + return $this->legacySsh($command, $options); + } + + $alias = $this->siteAliasManager()->getSelf(); + if ($alias->isNone()) { + throw new \Exception('A site alias is required. The way you call ssh command has changed to `drush @alias ssh`.'); + } + + // Local sites run their bash without SSH. + if (!$alias->isRemote()) { + $return = drush_invoke_process('@self', 'core-execute', array($command), array('escape' => false)); + return $return['object']; + } + + // We have a remote site - build ssh command and run. + $interactive = false; + $cd = $options['cd']; + if (empty($command)) { + $command = 'bash -l'; + $interactive = true; + } + $config = $alias->exportConfig(); + $site = $config->get('options', []); + + $cmd = drush_shell_proc_build($site, $command, $cd, $interactive); + $status = drush_shell_proc_open($cmd); + if ($status != 0) { + throw new \Exception(dt('An error @code occurred while running the command `@command`', array('@command' => $cmd, '@code' => $status))); + } + } + + protected function legacySsh($command, $options = ['cd' => true]) + { if (!$alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) { throw new \Exception('A site alias is required. The way you call ssh command has changed to `drush @alias ssh`.'); } diff --git a/src/SiteAlias/AliasRecord.php b/src/SiteAlias/AliasRecord.php index 8a9a1b622f..4bce66fef9 100644 --- a/src/SiteAlias/AliasRecord.php +++ b/src/SiteAlias/AliasRecord.php @@ -101,6 +101,11 @@ public function isRemote() return $this->has('host'); } + public function isNone() + { + return empty($this->root()); + } + /** * Return the 'root' element of this alias if this alias record * is local.