7 * Run commands on remote server(s).
8 * @see example.aliases.yml
9 * @see http://drupal.org/node/670460
12 use Drush\Commands\core\StatusCommands;
14 use Drush\Log\LogLevel;
15 use Consolidation\SiteAlias\AliasRecord;
16 use Webmozart\PathUtil\Path;
19 * Option keys used for site selection.
21 function drush_sitealias_site_selection_keys() {
22 return ['remote-host', 'remote-user', 'ssh-options', '#name', 'os'];
26 * Get a site alias record given an alias name or site specification.
28 * If it is the name of a site alias, return the alias record from
29 * the site aliases array.
31 * If it is the name of a folder in the 'sites' folder, construct
32 * an alias record from values stored in settings.php.
34 * If it is a site specification, construct an alias record from the
35 * values in the specification.
37 * Site specifications come in several forms:
38 * - /path/to/drupal#sitename
39 * - user@server/path/to/drupal#sitename
40 * - user@server/path/to/drupal (sitename == server)
41 * - user@server#sitename (only if $option['r'] set in some drushrc file on server)
42 * - #sitename (only if $option['r'] already set, and 'sitename' is a folder in $option['r']/sites)
43 * - sitename (only if $option['r'] already set, and 'sitename' is a folder in $option['r']/sites)
45 * Note that in the case of the first four forms, it is also possible
46 * to add additional site variable to the specification using uri query
47 * syntax. For example:
49 * user@server/path/to/drupal?db-url=...#sitename
52 * An alias name or site specification
54 * An alias record, or empty if none found.
56 function drush_sitealias_get_record($alias, $alias_context = NULL) {
57 // If legacy code is still looking for an alias record this way, redirect the
58 // request to the alias manager.
59 $alias_record = Drush::aliasManager()->get($alias);
60 if (empty($alias_record)) {
63 $config_record = $alias_record->exportConfig();
64 $exported_config = $config_record->export();
65 return isset($exported_config['options']) ? $exported_config['options'] : [];
69 * Determines whether a given site alias is for a remote site.
71 * @param string $alias
72 * An alias name or site specification.
75 * Returns TRUE if the alias refers to a remote site, FALSE if it does not, or NULL is unsure.
77 function drush_sitealias_is_remote_site($alias) {
78 if (is_array($alias) && !empty($alias['remote-host'])) {
81 if (!is_string($alias) || !strlen($alias)) {
85 $site_record = drush_sitealias_get_record($alias);
87 if (!empty($site_record['remote-host'])) {
95 drush_set_error('Unrecognized site alias.');