Skip to content
Open
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
10 changes: 5 additions & 5 deletions features/aliases.feature
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Feature: Create shortcuts to specific WordPress installs
When I try `wp @foo --debug --version`
Then STDERR should contain:
"""
Running SSH command: ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -T -vvv '' 'wp --debug --version'
Running SSH command: ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -T -vvv '' 'WP_CLI_SSH_RUN=1 wp --debug --version'
"""

Scenario: SSH alias expands tilde in path
Expand Down Expand Up @@ -287,7 +287,7 @@ Feature: Create shortcuts to specific WordPress installs
When I try `wp @foo plugin list --debug`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv 'user@host' 'cd '\''/path/to/wordpress'\''; wp plugin list --debug'
Running SSH command: ssh -T -vvv 'user@host' 'cd '\''/path/to/wordpress'\''; WP_CLI_SSH_RUN=1 wp plugin list --debug'
"""

Scenario: SSH commands correctly escape arguments with spaces
Expand All @@ -301,7 +301,7 @@ Feature: Create shortcuts to specific WordPress installs
When I try `wp @foo post create --post_title=My Title --debug`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv 'user@host' 'cd '\''/path/to/wordpress'\''; wp post create --post_title=My Title
Running SSH command: ssh -T -vvv 'user@host' 'cd '\''/path/to/wordpress'\''; WP_CLI_SSH_RUN=1 wp post create --post_title=My Title
"""

Scenario: Uses env command for runtime alias with separate path line
Expand All @@ -316,7 +316,7 @@ Feature: Create shortcuts to specific WordPress installs
When I try `wp @foo --debug --version`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv 'user@host' 'env WP_CLI_RUNTIME_ALIAS=
Running SSH command: ssh -T -vvv 'user@host' 'WP_CLI_SSH_RUN=1 env WP_CLI_RUNTIME_ALIAS=
"""
And STDERR should contain:
"""
Expand Down Expand Up @@ -367,7 +367,7 @@ Feature: Create shortcuts to specific WordPress installs
When I try `wp @foo --debug --version`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv 'user@host' 'env WP_CLI_RUNTIME_ALIAS=
Running SSH command: ssh -T -vvv 'user@host' 'WP_CLI_SSH_RUN=1 env WP_CLI_RUNTIME_ALIAS=
"""
And STDERR should contain:
"""
Expand Down
34 changes: 30 additions & 4 deletions features/flags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ Feature: Global flags
When I try `WP_CLI_STRICT_ARGS_MODE=1 wp --debug --ssh=/ --version`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv '' 'WP_CLI_STRICT_ARGS_MODE=1 wp
Running SSH command: ssh -T -vvv '' 'WP_CLI_STRICT_ARGS_MODE=1 WP_CLI_SSH_RUN=1 wp
"""

Scenario: SSH flag should support changing directories
When I try `wp --debug --ssh=wordpress:/my/path --version`
Then STDERR should contain:
"""
Running SSH command: ssh -T -vvv 'wordpress' 'cd '\''/my/path'\''; wp
Running SSH command: ssh -T -vvv 'wordpress' 'cd '\''/my/path'\''; WP_CLI_SSH_RUN=1 wp
"""

Scenario: SSH flag should support Docker
Expand All @@ -400,18 +400,44 @@ Feature: Global flags
Running SSH command: docker exec --user 'user' 'wordpress' sh -c
"""

Scenario: Root-level ssh config should support Docker scheme
Given an empty directory
And a wp-cli.yml file:
"""
ssh: docker:user@wordpress
"""
When I try `WP_CLI_DOCKER_NO_INTERACTIVE=1 wp --debug --version`
Then STDERR should contain:
"""
Running SSH command: docker exec --user 'user' 'wordpress' sh -c
"""

Scenario: Root-level ssh config should support Docker scheme with an alias
Given an empty directory
And a wp-cli.yml file:
"""
ssh: docker:user@wordpress
@local:
path: /var/www/html
"""
When I try `WP_CLI_DOCKER_NO_INTERACTIVE=1 wp @local --debug --version`
Then STDERR should contain:
"""
Running SSH command: docker exec --user 'user' 'wordpress' sh -c
"""

Scenario: SSH args should be passed to SSH command
When I try `wp --debug --ssh=wordpress --ssh-args="-o ConnectTimeout=5" --version`
Then STDERR should contain:
"""
Running SSH command: ssh '-o ConnectTimeout=5' -T -vvv 'wordpress' 'wp
Running SSH command: ssh '-o ConnectTimeout=5' -T -vvv 'wordpress' 'WP_CLI_SSH_RUN=1 wp
"""

Scenario: Multiple SSH args should be passed to SSH command
When I try `wp --debug --ssh=wordpress --ssh-args="-o ConnectTimeout=5" --ssh-args="-o ServerAliveInterval=10" --version`
Then STDERR should contain:
"""
Running SSH command: ssh '-o ConnectTimeout=5' '-o ServerAliveInterval=10' -T -vvv 'wordpress' 'wp
Running SSH command: ssh '-o ConnectTimeout=5' '-o ServerAliveInterval=10' -T -vvv 'wordpress' 'WP_CLI_SSH_RUN=1 wp
"""

Scenario: SSH args should be passed to Docker command
Expand Down
2 changes: 1 addition & 1 deletion php/WP_CLI/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function merge_yml( $path, $current_alias = null ) {
self::arrayify( $value );
$this->config[ $key ] = array_merge( $this->config[ $key ], $value );
} else {
if ( $current_alias && in_array( $key, self::$alias_spec, true ) ) {
if ( $current_alias && in_array( $key, self::$alias_spec, true ) && ! in_array( $key, array( 'ssh', 'ssh-args', 'proxyjump', 'key' ), true ) ) {
continue;
}
$this->config[ $key ] = $value;
Expand Down
21 changes: 17 additions & 4 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ private function run_ssh_command( string $connection_string ): void {
if ( getenv( 'WP_CLI_STRICT_ARGS_MODE' ) ) {
$env_vars .= 'WP_CLI_STRICT_ARGS_MODE=1 ';
}
$env_vars .= 'WP_CLI_SSH_RUN=1 ';

$wp_binary = Utils\get_env_or_config( 'WP_CLI_SSH_BINARY' ) ?: 'wp';
$wp_args = array_slice( (array) $GLOBALS['argv'], 1 );
Expand Down Expand Up @@ -831,7 +832,12 @@ private function generate_ssh_command( $bits, $wp_command ) {
}

if ( 'docker-compose-run' === $bits['scheme'] ) {
$command = '%s run %s%s%s%s%s%s %s';
$command = '%s run %s%s%s%s%s%s%s %s';

$env_flags = '-e WP_CLI_SSH_RUN=1 ';
if ( getenv( 'WP_CLI_STRICT_ARGS_MODE' ) ) {
$env_flags .= '-e WP_CLI_STRICT_ARGS_MODE=1 ';
Comment on lines +835 to +839
}
Comment on lines 834 to +840

$escaped_command = sprintf(
$command,
Expand All @@ -841,6 +847,7 @@ private function generate_ssh_command( $bits, $wp_command ) {
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
$is_stdout_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
$is_stdin_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
$env_flags,
escapeshellarg( $bits['host'] ),
$wp_command
);
Expand Down Expand Up @@ -1547,9 +1554,15 @@ function ( $alias ) {
}

if ( $this->config['ssh'] ) {
// @phpstan-ignore cast.string
$this->run_ssh_command( (string) $this->config['ssh'] );
return;
// Don't recurse if SSH came from config file and we're already in an SSH/container session.
// Still allow SSH if it was explicitly passed via --ssh= on the CLI (present in runtime_config).
$ssh_from_cli = isset( $this->runtime_config['ssh'] ) && '' !== $this->runtime_config['ssh'];
if ( getenv( 'WP_CLI_SSH_RUN' ) && ! $ssh_from_cli ) {
Comment on lines 1556 to +1560
WP_CLI::debug( 'Skipping SSH from config file: already running inside an SSH/container session. Use the `--ssh` flag to override.', 'bootstrap' );
} else {
// @phpstan-ignore cast.string
$this->run_ssh_command( (string) $this->config['ssh'] );
}
}

// Log WP-CLI HTTP requests
Expand Down
Loading