Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

Commit 5fd7351

Browse files
Fixed drush-ops#1618: drush_is_local_host() only matches unambiguously local hostnames.
1 parent 441fe96 commit 5fd7351

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

drush.api.php

+16
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ function hook_drush_command_alter(&$command) {
202202

203203
}
204204

205+
/**
206+
* Adjust the contents of a site alias.
207+
*/
208+
function hook_drush_sitealias_alter(&$alias_record) {
209+
// If the alias is "remote", but the remote site is
210+
// the system this command is running on, convert the
211+
// alias record to a local alias.
212+
if (isset($alias_record['remote-host'])) {
213+
$uname = php_uname('n');
214+
if ($alias_record['remote-host'] == $uname) {
215+
unset($alias_record['remote-host']);
216+
unset($alias_record['remote-user']);
217+
}
218+
}
219+
}
220+
205221
/**
206222
* Take action after a project has been downloaded.
207223
*/

examples/example.aliases.drushrc.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,9 @@
199199
* used on the remote machine in the 'remote-port' setting.
200200
* - 'remote-host': The fully-qualified domain name of the remote system
201201
* hosting the Drupal instance. **Important Note: The remote-host option
202-
* must be omitted for local sites, as this option controls whether or not
203-
* rsync parameters are for local or remote machines.
204-
* - '#check-local': Test to see if 'remote-host' is the local machine; if
205-
* it is, then 'remote-host' will be ignore. Useful when sharing aliases
206-
* among multiple machines.
202+
* must be omitted for local sites, as this option controls various
203+
* operations, such as whether or not rsync parameters are for local or
204+
* remote machines, and so on. @see hook_drush_sitealias_alter() in drush.api.php
207205
* - 'remote-user': The username to log in as when using ssh or rsync.
208206
* - 'os': The operating system of the remote server. Valid values
209207
* are 'Windows' and 'Linux'. Be sure to set this value for all remote

includes/backend.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ function _drush_backend_generate_command($site_record, $command, $args = array()
11321132
$ssh_options = $site_record['ssh-options'];
11331133
$os = drush_os($site_record);
11341134

1135-
if (array_key_exists('#check-local', $site_record) && drush_is_local_host($hostname)) {
1135+
if (drush_is_local_host($hostname)) {
11361136
$hostname = null;
11371137
}
11381138

includes/environment.inc

+8-21
Original file line numberDiff line numberDiff line change
@@ -619,30 +619,17 @@ function drush_read_drush_info() {
619619
* True if the host is local.
620620
*/
621621
function drush_is_local_host($host) {
622-
// In order for this to work right, you must use 'localhost' or '127.0.0.1'
623-
// or the machine returned by 'uname -n' for your 'remote-host' entry in
624-
// your site alias.
625-
if (($host == 'localhost') || ($host == '127.0.0.1')) {
622+
// Check to see if the provided host is "local".
623+
// @see hook_drush_sitealias_alter() in drush.api.php.
624+
if (
625+
($host == 'localhost') ||
626+
($host == '127.0.0.1') ||
627+
preg_match('/\.local$/', $host)
628+
) {
626629
return TRUE;
627630
}
628631

629-
// We'll start out by asking for uname -n via php_uname, since
630-
// that is most portable. On Linux, uname -n returns the contents of
631-
// /etc/hostname, which, according to `man hostname`, should never
632-
// contain the fqdn. We will therefore also try `hostname -f`, and
633-
// see if we can get the fqdn from that.
634-
$uname = php_uname('n');
635-
if ((strpos($uname,'.') === FALSE) && (!drush_is_windows())) {
636-
$hostname = exec('hostname -f');
637-
if (!empty($hostname)) {
638-
$uname = $hostname;
639-
}
640-
}
641-
// We will require an exact match if we have the fqdn for this
642-
// host; if we only have the short name, then we will consider it
643-
// to be a match if it matches the first part of the remote-host
644-
// item (up to the first dot).
645-
return (strpos($uname,'.') !== FALSE) ? ($host == $uname) : substr($host . '.',0,strlen($uname)+1) == $uname . '.';
632+
return FALSE;
646633
}
647634

648635
/**

includes/sitealias.inc

+9-5
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,15 @@ function drush_sitealias_is_bootstrapped_site($alias_record) {
10501050
* Returns TRUE if the alias refers to a remote site, FALSE if it does not, or NULL is unsure.
10511051
*/
10521052
function drush_sitealias_is_remote_site($alias) {
1053-
if (!is_string($alias) || !strlen($alias)) {
1054-
return NULL;
1053+
if (is_array($alias)) {
1054+
$site_record = $alias;
1055+
}
1056+
else {
1057+
if (!is_string($alias) || !strlen($alias)) {
1058+
return NULL;
1059+
}
1060+
$site_record = drush_sitealias_get_record($alias);
10551061
}
1056-
1057-
$site_record = drush_sitealias_get_record($alias);
10581062
if ($site_record) {
10591063
if (!empty($site_record['remote-host'])) {
10601064
return TRUE;
@@ -1774,7 +1778,7 @@ function drush_sitealias_set_alias_context($site_alias_settings, $prefix = '') {
17741778
// and 'remote-user' if 'remote-host' is actually the local machine.
17751779
// This prevents drush from using the remote dispatch mechanism (the command
17761780
// is just run directly on the local machine, bootstrapping to the specified alias)
1777-
elseif (array_key_exists('remote-host', $site_alias_settings) && array_key_exists('#check-local', $site_alias_settings) && drush_is_local_host($site_alias_settings['remote-host'])) {
1781+
elseif (array_key_exists('remote-host', $site_alias_settings) && drush_is_local_host($site_alias_settings['remote-host'])) {
17781782
$skip_list[] = 'remote-host';
17791783
$skip_list[] = 'remote-user';
17801784
}

0 commit comments

Comments
 (0)