ssh.drush.inc

  1. 8.0.x commands/core/ssh.drush.inc
  2. 6.x commands/core/ssh.drush.inc
  3. 7.x commands/core/ssh.drush.inc
  4. 5.x commands/core/ssh.drush.inc
  5. master commands/core/ssh.drush.inc
  • The drush site-ssh command for connecting to a remote alias' server via
  • SSH, either for an interactive session or to run a shell command.

Functions

Namesort descending Description
drush_ssh_site_ssh Command callback.
ssh_drush_command

File

commands/core/ssh.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * The drush site-ssh command for connecting to a remote alias' server via
  5. * SSH, either for an interactive session or to run a shell command.
  6. */
  7. function ssh_drush_command() {
  8. $items['site-ssh'] = array(
  9. 'description' => 'Connect to a Drupal site\'s server via SSH for an interactive session or to run a shell command',
  10. 'arguments' => array(
  11. 'bash' => 'Bash to execute on target. Optional, except when site-alias is a list.',
  12. ),
  13. 'options' => array(
  14. 'cd' => "Directory to change to. Use a full path, TRUE for the site's Drupal root directory, or FALSE for the remote user's home directory. Defaults to the Drupal root.",
  15. ) + drush_shell_exec_proc_build_options(),
  16. 'handle-remote-commands' => TRUE,
  17. 'strict-option-handling' => TRUE,
  18. 'examples' => array(
  19. 'drush @mysite ssh' => 'Open an interactive shell on @mysite\'s server.',
  20. 'drush @prod ssh \'ls /tmp\'' => 'Run "ls /tmp" on @prod site. If @prod is a site list, then ls will be executed on each site.',
  21. 'drush @prod git pull' => 'Run "git pull" on the Drupal root directory on the @prod site.',
  22. ),
  23. 'aliases' => array('ssh'),
  24. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  25. 'topics' => array('docs-aliases'),
  26. );
  27. return $items;
  28. }
  29. /**
  30. * Command callback.
  31. */
  32. function drush_ssh_site_ssh($command = NULL) {
  33. // Get all of the args and options that appear after the command name.
  34. $args = drush_get_original_cli_args_and_options();
  35. // n.b. we do not escape the first (0th) arg to allow `drush ssh 'ls /path'`
  36. // to work in addition to the preferred form of `drush ssh ls /path`.
  37. // Supporting the legacy form means that we cannot give the full path to an
  38. // executable if it contains spaces.
  39. for ($x = 1; $x < count($args); $x++) {
  40. $args[$x] = drush_escapeshellarg($args[$x]);
  41. }
  42. $command = implode(' ', $args);
  43. if (!$alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
  44. return drush_set_error('DRUSH_MISSING_TARGET_ALIAS', 'A remote site alias is required. The way you call ssh command has changed to `drush @alias ssh`.');
  45. }
  46. $site = drush_sitealias_get_record($alias);
  47. // We only accept remote aliases.
  48. if (empty($site['remote-host'])) {
  49. drush_set_error('DRUSH_SITE_SSH_REMOTE_ALIAS_REQUIRED', dt('@alias does not specify a remote-host.', array('@alias' => $alias)));
  50. return;
  51. }
  52. // If we have multiple sites, run ourselves on each one. Set context back when done.
  53. if (isset($site['site-list'])) {
  54. if (empty($command)) {
  55. drush_set_error('DRUSH_SITE_SSH_COMMAND_REQUIRED', dt('A command is required when multiple site aliases are specified.'));
  56. return;
  57. }
  58. foreach ($site['site-list'] as $alias_single) {
  59. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $alias_single);
  60. drush_ssh_site_ssh($command);
  61. }
  62. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $alias);
  63. return;
  64. }
  65. $interactive = FALSE;
  66. $cd = drush_get_option('cd', TRUE);
  67. if (empty($command)) {
  68. $command = 'bash -l';
  69. $interactive = TRUE;
  70. }
  71. $cmd = drush_shell_proc_build($site, $command, $cd, $interactive);
  72. $status = drush_shell_proc_open($cmd);
  73. if ($status != 0) {
  74. return drush_set_error('DRUSH_SITE_SSH_ERROR', dt('An error @code occurred while running the command `@command`', array('@command' => $cmd, '@code' => $status)));
  75. }
  76. }