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