Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,52 @@ private function _run_command() {
$this->run_command( $this->arguments, $this->assoc_args );
}

/**
* Perform a command against a remote server over SSH
*/
private function run_ssh_command( $ssh ) {

$host = $ssh;
$path = '';
if ( false !== ( $key = stripos( $host, ':' ) ) ) {
$path = substr( $host, $key + 1 );
$host = substr( $host, 0, $key );
}

WP_CLI::do_hook( 'before_ssh' );

WP_CLI::debug( 'SSH host: ' . $host, 'bootstrap' );
WP_CLI::debug( 'SSH path: ' . $path, 'bootstrap' );

$is_tty = function_exists( 'posix_isatty' ) && posix_isatty( STDOUT );

$pre_cmd = getenv( 'WP_CLI_SSH_PRE_CMD' );
if ( $pre_cmd ) {
$pre_cmd = rtrim( $pre_cmd, ';' ) . '; ';
}
$wp_binary = 'wp';
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter @jonathanbardo In situations where wp is defined by an alias or addition to PATH in a .bash_profile, have you found a way to load this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/usr/bin/env wp ?
I haven't tested yet. 😄

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber That's a valid scenario and I don't think we ever faced that problematic while we developed wp-cli-ssh.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not quite:

local ➜  wp-cli git:(2754-ssh) ✗ wp --ssh=hb
/usr/bin/env: wp: No such file or directory

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid scenario and I don't think we ever faced that problematic while we developed wp-cli-ssh.

Unfortunately, it's a problem with the first server I tested with :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it looks difficult.

  • Place the ~/.ssh/environment into local machine.
  • Add PermitUserEnvironment yes in /etc/ssh/sshd_config on remote server.

http://stackoverflow.com/questions/216202/why-does-an-ssh-remote-command-get-fewer-environment-variables-then-when-run-man

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber This is why there was recent support for a wpcli_command config to handle situations where wp isn't on the PATH: xwp/wp-cli-ssh#27

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why there was recent support for a wpcli_command config to handle situations where wp isn't on the PATH: xwp/wp-cli-ssh#27

@westonruter Did you explore trying to get the data out of .bash_profile ? I'd prefer to not have to institute a workaround...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't. Also, perhaps the PHAR isn't even aliased as wp on the target environment. I think that there will ultimately need to be a way to provide an alternate path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't. Also, perhaps the PHAR isn't even aliased as wp on the target environment. I think that there will ultimately need to be a way to provide an alternate path.

I ended up creating a WP_CLI_PRE_SSH_CMD env variable, to offer a bit more flexibility:

WP_CLI::add_hook( 'before_ssh', function() {
    if ( $ssh = WP_CLI::get_runner()->config['ssh'] ) {
        list( $host, $path ) = explode( ':', $ssh );
        switch( $host ) {
            case 'hb':
                putenv( 'WP_CLI_SSH_PRE_CMD=export PATH=$HOME/bin:$PATH' );
                break;
        }
    }
});

$wp_args = array_slice( $GLOBALS['argv'], 1 );
$wp_path = $path ? sprintf( '--path=%s', str_replace( '~', '$HOME', $path ) ) : '';
foreach( $wp_args as $k => $v ) {
if ( preg_match( '#--ssh=#', $v ) ) {
unset( $wp_args[ $k ] );
}
}
$command = sprintf(
'ssh -q %s %s %s',
escapeshellarg( $host ),
$is_tty ? '-t' : '-T',
escapeshellarg( $pre_cmd . $wp_binary . ' ' . $wp_path . ' ' . implode( ' ', $wp_args ) )
);

WP_CLI::debug( 'Running SSH command: ' . $command, 'bootstrap' );

passthru( $command, $exit_code );
if ( 0 !== $exit_code ) {
exit( $exit_code );
}
}

/**
* Check whether a given command is disabled by the config
*
Expand Down Expand Up @@ -668,6 +714,11 @@ public function start() {
}
}

if ( $this->config['ssh'] ) {
$this->run_ssh_command( $this->config['ssh'] );
return;
}

// Show synopsis if it's a composite command.
$r = $this->find_command_to_run( $this->arguments );
if ( is_array( $r ) ) {
Expand Down
6 changes: 6 additions & 0 deletions php/config-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
'desc' => 'Path to the WordPress files.',
),

'ssh' => array(
'runtime' => '=<ssh>',
'file' => '<ssh>',
'desc' => 'Perform operation against a remote server over SSH.',
),

'url' => array(
'runtime' => '=<url>',
'file' => '<url>',
Expand Down