Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running commands on remote servers over SSH #2754

Closed
danielbachhuber opened this issue May 16, 2016 · 24 comments
Closed

Support running commands on remote servers over SSH #2754

danielbachhuber opened this issue May 16, 2016 · 24 comments
Assignees
Milestone

Comments

@danielbachhuber
Copy link
Member

danielbachhuber commented May 16, 2016

As a WP-CLI user, it would be convenient if I could use a global --ssh=<host> argument to run a command against a WordPress install on a remote server:

$ wp --ssh=v:/srv/www/wordpress-develop.dev/src option get home
http://wordpress-develop.dev

To modify the behavior of the command at runtime, it should be possible to hook in a custom command:

WP_CLI::add_hook( 'before_ssh', function() {
    switch( WP_CLI\Utils\parse_ssh_url( WP_CLI::get_runner()->config['ssh'], PHP_URL_HOST ) ) {
        case 'runcommand.io':
            putenv( 'WP_CLI_SSH_PRE_CMD=export PATH=$HOME/bin:$PATH' );
            break;
    }
});

https://github.com/xwp/wp-cli-ssh is good prior art from @westonruter and @jonathanbardo that we can crib from.

@danielbachhuber danielbachhuber self-assigned this May 16, 2016
@danielbachhuber danielbachhuber added this to the 0.24.0 milestone May 16, 2016
@danielbachhuber
Copy link
Member Author

@westonruter @jonathanbardo Do you recall why you originally used passthru() instead of ssh2_connect() and its corresponding library?

@westonruter
Copy link
Contributor

@danielbachhuber not exactly, but two reasons come to mind:

  1. I wasn't aware of ssh2_connect().
  2. And ssh2_connect() isn't a core PHP function but part of a PECL extension.

@jonathanbardo
Copy link
Contributor

I think at the time we also needed to be able to ssh into a jump server before reaching the final web server. This was going to be easier to do using passthru().

@danielbachhuber
Copy link
Member Author

Thanks guys. I think I'll end up going with passthru() because it will be easier to reuse the user's SSH config details.

@danielbachhuber
Copy link
Member Author

Debating whether we need to support port in some fashion. I'm inclined to have users define the port in their SSH config if it's non-standard.

@danielbachhuber
Copy link
Member Author

@westonruter @jonathanbardo Would you mind testing out the current implementation when you have a moment, and let me know what you think? Specifically, I'm looking for feedback on the implementation, and any additional features you think it needs.

@jonathanbardo
Copy link
Contributor

jonathanbardo commented May 20, 2016

@danielbachhuber I agree, SSH config for non standard port number makes sense.

@westonruter
Copy link
Contributor

Would you mind testing out the current implementation when you have a moment, and let me know what you think? Specifically, I'm looking for feedback on the implementation, and any additional features you think it needs.

@timneutkens actually, since you've been using WP-CLI SSH most frequently of late (AFAIK), would you give this PR a try and provide feedback?

@danielbachhuber
Copy link
Member Author

would you give this PR a try and provide feedback?

Just to clarify, the code has already landed on master. If you're using the Phar install, you can wp cli update --nightly to try it out.

@timneutkens
Copy link

@westonruter @danielbachhuber Will do. Bet I will have time to review this tomorrow.

@timneutkens
Copy link

Hey @danielbachhuber, looks good :)

Only thing I noticed is that wp is hardcoded here: https://github.com/wp-cli/wp-cli/blob/master/php/WP_CLI/Runner.php#L356.

Few weeks ago I created a pull request on wp-cli-ssh (xwp/wp-cli-ssh#27) to add support for a custom command to be ran. So you could do something like: wpcli_command: php ./vendor/bin/wp (https://github.com/xwp/wp-cli-ssh/blob/master/wp-cli.sample.yml#L22) when you install wp-cli using composer.

The hook you implemented probably solves the issue I had wanting to use a different php command for wp-cli. Just not the part where you would want to use a different path to the command.

@danielbachhuber
Copy link
Member Author

Only thing I noticed is that wp is hardcoded here

That was intentional, although I could be convinced otherwise.

The hook you implemented probably solves the issue I had wanting to use a different php command for wp-cli. Just not the part where you would want to use a different path to the command.

Well, my thinking is that you could create a just-in-time alias to a different path, if WP-CLI wasn't available as wp. Even though there is a workaround, it's somewhat intentional not to permit an arbitrary executable (e.g. it's a design decision that --ssh should only be used to execute WP-CLI).

What do you think about my explanation?

@timneutkens
Copy link

That's a fair point. You could alias wp using the hook. So that will do.

@timneutkens
Copy link

timneutkens commented May 25, 2016

I also noticed that you can provide WP_CLI_PHP to use another version php. So that is an even better solution to the issue I had 😄

@danielbachhuber
Copy link
Member Author

Debating whether we need to support port in some fashion. I'm inclined to have users define the port in their SSH config if it's non-standard.

I figured out a syntax for us to support port:

192.168.50.10:2222:/srv/www/wordpress-develop.dev/src

If the port isn't specified, then 22 is assumed:

192.168.50.10:/srv/www/wordpress-develop.dev/src

@timneutkens
Copy link

I agree, that's the best syntax for supporting ports 👍

@scribu
Copy link
Member

scribu commented Jun 15, 2016

Git uses a syntax without a colon between port and path:

ssh://[user@]host.xz[:port]/path/to/repo.git/

https://git-scm.com/docs/git-clone

@scribu
Copy link
Member

scribu commented Jun 15, 2016

Since it's a standard URL format, it should be parseable with PHP's parse_url().

@danielbachhuber
Copy link
Member Author

Since it's a standard URL format, it should be parseable with PHP's parse_url().

I agree, I'll update.

@danielbachhuber
Copy link
Member Author

Actually, parse_url() isn't quite compatible with using a SSH config like v. If we use regex instead, we could support a similar syntax to standard URL format:

@prod:
  ssh: rc~/webapps/production
@dev:
  ssh: v/srv/www/runcommand.dev
@wpdev:
  ssh: v:2222/srv/www/wordpress-develop.dev/src

@danielbachhuber
Copy link
Member Author

Feeling pretty good with this now as a v1. I'll handle website documentation as a part of #2767

@timneutkens
Copy link

Thanks Daniel, will have to make so changes to our internal setup 👍

@toleabivol
Copy link

How would I do it if I usually login with :
ssh -i ~/.ssh/pemfiles/yyyy.pem [email protected]

@danielbachhuber
Copy link
Member Author

@toleabivol You'll need to set a SSH config with the identity file reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants