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

Speed up vagrant scheme calls by caching vagrant ssh-config #5235

Merged
merged 6 commits into from
Jul 23, 2019
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
4 changes: 2 additions & 2 deletions features/flags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,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 -q '' -T 'WP_CLI_STRICT_ARGS_MODE=1 wp
Running SSH command: ssh -q -T '' 'WP_CLI_STRICT_ARGS_MODE=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 -q 'wordpress' -T 'cd '\''/my/path'\''; wp
Running SSH command: ssh -q -T 'wordpress' 'cd '\''/my/path'\''; wp
"""

Scenario: SSH flag should support Docker
Expand Down
49 changes: 39 additions & 10 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private function generate_ssh_command( $bits, $wp_command ) {
$escaped_command = '';

// Set default values.
foreach ( array( 'scheme', 'user', 'host', 'port', 'path' ) as $bit ) {
foreach ( array( 'scheme', 'user', 'host', 'port', 'path', 'key' ) as $bit ) {
if ( ! isset( $bits[ $bit ] ) ) {
$bits[ $bit ] = null;
}
Expand Down Expand Up @@ -523,28 +523,57 @@ private function generate_ssh_command( $bits, $wp_command ) {

// Vagrant ssh-config.
if ( 'vagrant' === $bits['scheme'] ) {
$command = 'vagrant ssh -c %s %s';
$cache = WP_CLI::get_cache();
$cache_key = 'vagrant:' . $this->project_config_path;
if ( $cache->has( $cache_key ) ) {
$cached = $cache->read( $cache_key );
$values = json_decode( $cached, true );
} else {
$ssh_config = shell_exec( 'vagrant ssh-config 2>/dev/null' );
if ( preg_match_all( '#\s*(?<NAME>[a-zA-Z]+)\s(?<VALUE>.+)\s*#', $ssh_config, $matches ) ) {
$values = array_combine( $matches['NAME'], $matches['VALUE'] );
$cache->write( $cache_key, json_encode( $values ) );
}
}

$escaped_command = sprintf(
$command,
escapeshellarg( $wp_command ),
escapeshellarg( $bits['host'] )
);
if ( empty( $bits['host'] ) || $bits['host'] === $values['Host'] ) {
$bits['scheme'] = 'ssh';
$bits['host'] = $values['HostName'];
$bits['port'] = $values['Port'];
$bits['user'] = $values['User'];
$bits['key'] = $values['IdentityFile'];
}

// If we could not resolve the bits still, fallback to just `vagrant ssh`
if ( 'vagrant' === $bits['scheme'] ) {
$command = 'vagrant ssh -c %s %s';

$escaped_command = sprintf(
$command,
escapeshellarg( $wp_command ),
escapeshellarg( $bits['host'] )
);
}
}

// Default scheme is SSH.
if ( 'ssh' === $bits['scheme'] || null === $bits['scheme'] ) {
$command = 'ssh -q %s%s %s %s';
$command = 'ssh -q %s %s %s';

if ( $bits['user'] ) {
$bits['host'] = $bits['user'] . '@' . $bits['host'];
}

$command_args = [
$bits['port'] ? '-p ' . (int) $bits['port'] . ' ' : '',
$bits['key'] ? sprintf( '-i %s', $bits['key'] ) : '',
$is_tty ? '-t' : '-T',
];

$escaped_command = sprintf(
$command,
$bits['port'] ? '-p ' . (int) $bits['port'] . ' ' : '',
implode( ' ', array_filter( $command_args ) ),
escapeshellarg( $bits['host'] ),
$is_tty ? '-t' : '-T',
escapeshellarg( $wp_command )
);
}
Expand Down
7 changes: 2 additions & 5 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,8 @@ function parse_ssh_url( $url, $component = -1 ) {
// Find the hostname from `vagrant ssh-config` automatically.
if ( preg_match( '/^vagrant:?/', $url ) ) {
if ( 'vagrant' === $bits['host'] && empty( $bits['scheme'] ) ) {
$ssh_config = shell_exec( 'vagrant ssh-config 2>/dev/null' );
if ( preg_match( '/Host\s(.+)/', $ssh_config, $matches ) ) {
$bits['scheme'] = 'vagrant';
$bits['host'] = $matches[1];
}
$bits['scheme'] = 'vagrant';
$bits['host'] = '';
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/test-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,14 @@ public function testParseSSHUrl() {
// vagrant scheme
$testcase = 'vagrant:/var/www/html';
$expected = array(
'host' => 'vagrant',
'path' => '/var/www/html',
'scheme' => 'vagrant',
'host' => '',
'path' => '/var/www/html',
);
$this->assertEquals( $expected, Utils\parse_ssh_url( $testcase ) );
$this->assertEquals( null, Utils\parse_ssh_url( $testcase, PHP_URL_SCHEME ) );
$this->assertEquals( 'vagrant', Utils\parse_ssh_url( $testcase, PHP_URL_SCHEME ) );
$this->assertEquals( null, Utils\parse_ssh_url( $testcase, PHP_URL_USER ) );
$this->assertEquals( 'vagrant', Utils\parse_ssh_url( $testcase, PHP_URL_HOST ) );
$this->assertEquals( '', Utils\parse_ssh_url( $testcase, PHP_URL_HOST ) );
$this->assertEquals( null, Utils\parse_ssh_url( $testcase, PHP_URL_PORT ) );
$this->assertEquals( '/var/www/html', Utils\parse_ssh_url( $testcase, PHP_URL_PATH ) );

Expand Down