Skip to content

Commit 5a68dce

Browse files
Inject options from core:rsync and sql:sync alias parameters. (#3075)
1 parent 8331bea commit 5a68dce

File tree

13 files changed

+398
-112
lines changed

13 files changed

+398
-112
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"psr/log": "~1.0",
2929
"psy/psysh": "~0.6",
3030
"league/container": "~2",
31-
"consolidation/config": "^1.0.3",
31+
"consolidation/config": "^1.0.5",
3232
"consolidation/robo": "^1.1.4",
3333
"symfony/config": "~2.2|^3",
3434
"chi-teck/drupal-code-generator": "^1.17.3",

examples/example.aliases.yml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,16 @@
160160
# if you set a 'remote-host' value, and your remote OS is Windows, if you
161161
# do not set the 'OS' value, it will default to 'Linux' and could cause
162162
# unintended consequences, particularly when running 'drush sql-sync'.
163-
# - 'ssh-options': If the target requires special options, such as a non-
163+
# - 'ssh': If the target requires special options, such as a non-
164164
# standard port, alternative identity file, or alternative
165-
# authentication method, ssh-options can contain a string of extra
166-
# options that are used with the ssh command, eg "-p 100"
165+
# authentication method, the `option` entry under the `ssh` item may
166+
# contain a string of extra options that are used with the ssh command,
167+
# e.g. "-p 100"
167168
# - 'paths': An array of aliases for common rsync targets.
168169
# Relative aliases are always taken from the Drupal root.
169170
# - 'files': Path to 'files' directory. This will be looked up if not
170171
# specified.
172+
# - 'drush-script': Path to the remot Drush command.
171173
# - 'command': These options will only be set if the alias
172174
# is used with the specified command. In the example below, the option
173175
# `--no-dump` will be selected whenever the @stage alias
@@ -177,6 +179,36 @@
177179
# - `drush sql-sync @live @stage`
178180
# NOTE: Setting boolean options broke with Symfony 3. This will be fixed
179181
# in a future release. See: https://github.com/drush-ops/drush/issues/2956
182+
# - 'alias-parameters': These options will only be set if the alias is
183+
# used as the specified parameter. `sql:sync` and `core:rsync` are the two
184+
# core commands that use this entry. These commands both have `source`
185+
# and `target` parameters.
186+
#
187+
# Complex example:
188+
#
189+
# @code
190+
# # File: remote.alias.yml
191+
# live:
192+
# host: server.domain.com
193+
# user: www-admin
194+
# root: /other/path/to/drupal
195+
# uri: http://example.com
196+
# ssh:
197+
# options: '-p 100'
198+
# paths:
199+
# drush-script: '/path/to/drush'
200+
# command:
201+
# site:
202+
# install:
203+
# options:
204+
# admin-password: 'secret-secret'
205+
# alias-parameters:
206+
# target:
207+
# core:
208+
# rsnyc:
209+
# options:
210+
# excplude-paths: sites/default/files/private
211+
# @endcode
180212
#
181213
# Altering aliases:
182214
#

isolation/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"php": ">=5.6.0",
1010
"ext-dom": "*",
1111
"psr/log": "~1.0",
12-
"consolidation/config": "dev-master",
12+
"consolidation/config": "^1.0.5",
1313
"grasmash/yaml-expander": "^1.1.1",
1414
"symfony/yaml": "~2.3|^3",
1515
"symfony/var-dumper": "~2.7|^3",

src/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public function refineUriSelection($cwd)
149149
return;
150150
}
151151
$selfAliasRecord = $this->aliasManager->getSelf();
152+
if (!$selfAliasRecord->hasRoot()) {
153+
return;
154+
}
152155
$uri = $selfAliasRecord->uri();
153156

154157
if (empty($uri)) {

src/Commands/core/RsyncCommands.php

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
1010
use Drush\SiteAlias\SiteAliasManagerAwareTrait;
1111
use Drush\Backend\BackendPathEvaluator;
12+
use Robo\Contract\ConfigAwareInterface;
13+
use Robo\Common\ConfigAwareTrait;
14+
use Drush\Config\ConfigLocator;
15+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1216

13-
class RsyncCommands extends DrushCommands implements SiteAliasManagerAwareInterface
17+
class RsyncCommands extends DrushCommands implements SiteAliasManagerAwareInterface, ConfigAwareInterface
1418
{
1519
use SiteAliasManagerAwareTrait;
20+
use ConfigAwareTrait;
1621

1722
/**
1823
* These are arguments after the aliases and paths have been evaluated.
@@ -21,7 +26,7 @@ class RsyncCommands extends DrushCommands implements SiteAliasManagerAwareInterf
2126
/** @var HostPath */
2227
public $sourceEvaluatedPath;
2328
/** @var HostPath */
24-
public $destinationEvaluatedPath;
29+
public $targetEvaluatedPath;
2530
/** @var BackendPathEvaluator */
2631
protected $pathEvaluator;
2732

@@ -37,7 +42,7 @@ public function __construct()
3742
*
3843
* @command core:rsync
3944
* @param $source A site alias and optional path. See rsync documentation and example.aliases.yml.
40-
* @param $destination A site alias and optional path. See rsync documentation and example.aliases.config.yml.',
45+
* @param $target A site alias and optional path. See rsync documentation and example.aliases.config.yml.',
4146
* @param $extra Additional parameters after the ssh statement.
4247
* @optionset_ssh
4348
* @option exclude-paths List of paths to exclude, seperated by : (Unix-based systems) or ; (Windows).
@@ -54,11 +59,11 @@ public function __construct()
5459
* @aliases rsync,core-rsync
5560
* @topics docs:aliases
5661
*/
57-
public function rsync($source, $destination, array $extra, $options = ['exclude-paths' => self::REQ, 'include-paths' => self::REQ, 'mode' => 'akz'])
62+
public function rsync($source, $target, array $extra, $options = ['exclude-paths' => self::REQ, 'include-paths' => self::REQ, 'mode' => 'akz'])
5863
{
5964
// Prompt for confirmation. This is destructive.
6065
if (!\Drush\Drush::simulate()) {
61-
$this->output()->writeln(dt("You will delete files in !target and replace with data from !source", array('!source' => $this->sourceEvaluatedPath->fullyQualifiedPathPreservingTrailingSlash(), '!target' => $this->destinationEvaluatedPath->fullyQualifiedPath())));
66+
$this->output()->writeln(dt("You will delete files in !target and replace with data from !source", array('!source' => $this->sourceEvaluatedPath->fullyQualifiedPathPreservingTrailingSlash(), '!target' => $this->targetEvaluatedPath->fullyQualifiedPath())));
6267
if (!$this->io()->confirm(dt('Do you want to continue?'))) {
6368
throw new UserAbortException();
6469
}
@@ -67,16 +72,16 @@ public function rsync($source, $destination, array $extra, $options = ['exclude-
6772
$rsync_options = $this->rsyncOptions($options);
6873
$parameters = array_merge([$rsync_options], $extra);
6974
$parameters[] = $this->sourceEvaluatedPath->fullyQualifiedPathPreservingTrailingSlash();
70-
$parameters[] = $this->destinationEvaluatedPath->fullyQualifiedPath();
75+
$parameters[] = $this->targetEvaluatedPath->fullyQualifiedPath();
7176

7277
$ssh_options = Drush::config()->get('ssh.options', '');
7378
$exec = "rsync -e 'ssh $ssh_options'". ' '. implode(' ', array_filter($parameters));
7479
$exec_result = drush_op_system($exec);
7580

7681
if ($exec_result == 0) {
77-
drush_backend_set_result($this->destinationEvaluatedPath->fullyQualifiedPath());
82+
drush_backend_set_result($this->targetEvaluatedPath->fullyQualifiedPath());
7883
} else {
79-
throw new \Exception(dt("Could not rsync from !source to !dest", array('!source' => $this->sourceEvaluatedPath->fullyQualifiedPathPreservingTrailingSlash(), '!dest' => $this->destinationEvaluatedPath->fullyQualifiedPath())));
84+
throw new \Exception(dt("Could not rsync from !source to !dest", array('!source' => $this->sourceEvaluatedPath->fullyQualifiedPathPreservingTrailingSlash(), '!dest' => $this->targetEvaluatedPath->fullyQualifiedPath())));
8085
}
8186
}
8287

@@ -90,7 +95,7 @@ public function rsyncOptions($options)
9095
$inc_ex_path = explode(PATH_SEPARATOR, @$options[$include_exclude . '-paths']);
9196
foreach ($inc_ex_path as $one_path_to_inc_ex) {
9297
if (!empty($one_path_to_inc_ex)) {
93-
$paths = ' --' . $include_exclude . '="' . $one_path_to_inc_ex . '"';
98+
$paths .= ' --' . $include_exclude . '="' . $one_path_to_inc_ex . '"';
9499
}
95100
}
96101
}
@@ -105,27 +110,54 @@ public function rsyncOptions($options)
105110
}
106111

107112
/**
108-
* Validate that passed aliases are valid.
113+
* Evaluate the path aliases in the source and destination
114+
* parameters. We do this in the pre-command-event so that
115+
* we can set up the configuration object to include options
116+
* from the source and target aliases, if any, so that these
117+
* values may participate in configuration injection.
109118
*
110-
* @hook validate core-rsync
111-
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
119+
* @hook command-event core:rsync
120+
* @param ConsoleCommandEvent $event
112121
* @throws \Exception
113122
* @return void
114123
*/
115-
public function validate(CommandData $commandData)
124+
public function preCommandEvent(ConsoleCommandEvent $event)
116125
{
117-
$destination = $commandData->input()->getArgument('destination');
118-
$source = $commandData->input()->getArgument('source');
126+
$input = $event->getInput();
127+
$this->sourceEvaluatedPath = $this->injectAliasPathParameterOptions($input, 'source');
128+
$this->targetEvaluatedPath = $this->injectAliasPathParameterOptions($input, 'target');
129+
}
119130

131+
protected function injectAliasPathParameterOptions($input, $parameterName)
132+
{
133+
// The Drush configuration object is a ConfigOverlay; fetch the alias
134+
// context, that already has the options et. al. from the
135+
// site-selection alias ('drush @site rsync ...'), @self.
136+
$aliasConfigContext = $this->getConfig()->getContext(ConfigLocator::ALIAS_CONTEXT);
120137
$manager = $this->siteAliasManager();
121-
$this->sourceEvaluatedPath = HostPath::create($manager, $source);
122-
$this->destinationEvaluatedPath = HostPath::create($manager, $destination);
123138

124-
$this->pathEvaluator->evaluate($this->sourceEvaluatedPath);
125-
$this->pathEvaluator->evaluate($this->destinationEvaluatedPath);
139+
$aliasName = $input->getArgument($parameterName);
140+
$evaluatedPath = HostPath::create($manager, $aliasName);
141+
$this->pathEvaluator->evaluate($evaluatedPath);
142+
143+
// Inject the source and target alias records into the alias config context.
144+
$evaluatedPath->getAliasRecord()->injectIntoConfig($aliasConfigContext, $parameterName);
126145

127-
if ($this->sourceEvaluatedPath->isRemote() && $this->destinationEvaluatedPath->isRemote()) {
128-
$msg = dt("Cannot specify two remote aliases. Instead, use one of the following alternate options:\n\n `drush {source} rsync @self {target}`\n `drush {source} rsync @self {fulltarget}\n\nUse the second form if the site alias definitions are not available at {source}.", array('source' => $source, 'target' => $destination, 'fulltarget' => $this->destinationEvaluatedPath->fullyQualifiedPath()));
146+
return $evaluatedPath;
147+
}
148+
149+
/**
150+
* Validate that passed aliases are valid.
151+
*
152+
* @hook validate core-rsync
153+
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
154+
* @throws \Exception
155+
* @return void
156+
*/
157+
public function validate(CommandData $commandData)
158+
{
159+
if ($this->sourceEvaluatedPath->isRemote() && $this->targetEvaluatedPath->isRemote()) {
160+
$msg = dt("Cannot specify two remote aliases. Instead, use one of the following alternate options:\n\n `drush {source} rsync @self {target}`\n `drush {source} rsync @self {fulltarget}\n\nUse the second form if the site alias definitions are not available at {source}.", array('source' => $source, 'target' => $target, 'fulltarget' => $this->targetEvaluatedPath->fullyQualifiedPath()));
129161
throw new \Exception($msg);
130162
}
131163
}

0 commit comments

Comments
 (0)