Problem/Motivation

I want to execute a command with 'drush ssh' inside the root of the site. But this is not allowed for the 'drush ssh' command. When looking at the core functions that run ssh it passes a 'cd = TRUE' to allow cd to root.

Proposed resolution

It was thinking of two solutions.

  1. One would be to add a --root options introducted for ssh. This does the same as when a command is run for drush.
  2. The other would be to allow the replacing of variables inside the command. For example: drush @dev ssh 'ls %root' would ls the root folder. Or drush @dev ssh 'ls %root/..' would list the parent folder of root. This last option is the most powerfull I think.

Remaining tasks

- Get agreement on the solution to choose or a implement both.

(reviews needed, tests to be written or run, documentation to be written, etc.)

API changes

The core API does not change but is extended. All other functionality keeps working the same I believe.

(A list of related issues.)

Comments

greg.1.anderson’s picture

Version: 7.x-5.x-dev » 8.x-6.x-dev

I think it would be fair game to have the ssh command always cd to the root before running the user-provided command. See examples.bashrc (specifically, in the 'gitd' alias) for an example.

Patches welcome.

matglas86’s picture

Having ssh alway cd into root would break existing implementations I think for people. If you have build your script around the fact that ssh alway logs in to the root of the remote user and do actions there it breaks your script when we would cd 'automagicly' into the drupal root.

greg.1.anderson’s picture

It is fair game to break backwards compatibility on major releases of Drush (e.g. Drush 6.x). I'd be fine with a flag to control the behavior, but I think that the default should be to cd to root. I'm not fond of doing replacements on the ssh command provided by the user.

moshe weitzman’s picture

I agree that the default should be to cd into drupal root.

matglas86’s picture

Might it be an option to implement this as default behavior but backport it with a option?

greg.1.anderson’s picture

By policy, default behavior in the 7.x-5.x branch must remain consistent, so yes.

greg.1.anderson’s picture

Working on this; it's pretty useful. I'll put up a patch soon; need to add -t to ssh options when just doing a 'normal' ssh (interactive shell). See related issue, #1991126: Default SSH to include -t so sql-cli works on remote aliases.

greg.1.anderson’s picture

Status: Active » Needs review
StatusFileSize
new2.1 KB

Here is a patch that adds the cd to the beginning of the commands. It also correctly handles interactive mode for simple ssh sessions with no command. Somehow, I like drush @site ssh git pull better than git @site pull. Even though the later is fewer keystrokes, the former just makes more intrinsic sense to me. Even though I wrote the bash function that makes the later possible, it still feels odd.

matglas86’s picture

Status: Needs review » Reviewed & tested by the community

This looks great. I was just about to post a patch but this look even better.

greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

Committed as 3e027bd, with the addition of a --cd option to specify which directory to cd to -- or use --cd=0 (or $options['cd'] = FALSE;) to disable. Docs included to show command-specific example in examples.drushrc.php.

greg.1.anderson’s picture

Fixed failing unit tests.

Automatically closed -- issue fixed for 2 weeks with no activity.