-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrush_msync.drush.inc
214 lines (189 loc) · 7.4 KB
/
drush_msync.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Implements hook_drush_command().
*/
function drush_msync_drush_command() {
$items = array();
// @todo Check if we must include that file.
$sql_sync_commands = sqlsync_drush_command();
$items['msync-sql'] = array(
'description' => "Does sql-sync for all sites of a multisite. The sites are matched according to their last component (separated by '.').",
) + $sql_sync_commands['sql-sync'];
$items['msync-sql']['options'] += _drush_msync_extra_options('sql-sync');
$core_commands = core_drush_command();
$items['msync-files'] = array(
'description' => "Does rsync for all sites of a multisite. The sites are matched according to their last component (separated by '.').",
) + $core_commands['core-rsync'];
$items['msync-sql']['options'] += _drush_msync_extra_options('rsync');
return $items;
}
function _drush_msync_extra_options($command) {
$options = [
'only-sites' => 'A comma-separeted list of sites to process.',
'exclude-sites' => 'A comma-separeted list of sites to exclude.',
];
if ($command === 'sql-sync') {
$options += [
// Same option name as in site-install.
'db-drop-existing-tables' => 'If the target DB should be emptied first. Defaults to 1=yes.',
];
}
return $options;
}
/**
* Implementation of drush_hook_COMMAND_validate().
*/
function drush_drush_msync_msync_sql_validate($source, $destination) {
return _drush_msync_validate($source, $destination);
}
/**
* Implementation of drush_hook_COMMAND_validate().
*/
function drush_drush_msync_msync_files_validate($source, $destination) {
return _drush_msync_validate($source, $destination);
}
/**
* @param $source
* @param $destination
* @return bool
*/
function _drush_msync_validate($source, $destination) {
// Rsync path handling, sql-sync alias just has no suffix.
list($source_alias, $source_suffix) = _drush_msync_split_path($source);
list($destination_alias, $destination_suffix) = _drush_msync_split_path($destination);
$source_record = drush_sitealias_get_record($source_alias);
$destination_record = drush_sitealias_get_record($destination_alias);
if (!isset($source_record['site-list'])) {
return drush_set_error('DRUSH_drush_sql_msync_ERROR', dt('Source record must be multiple.'));
}
if (!isset($destination_record['site-list'])) {
return drush_set_error('DRUSH_drush_sql_msync_ERROR', dt('Destination record must be multiple.'));
}
return TRUE;
}
/**
* Implementation of drush_hook_COMMAND().
*/
function drush_drush_msync_msync_sql($source, $destination) {
_drush_msync_process('sql-sync', $source, $destination);
}
/**
* Implementation of drush_hook_COMMAND().
*/
function drush_drush_msync_msync_files($source, $destination) {
_drush_msync_process('rsync', $source, $destination);
}
/**
* @param $drush_command
* @param $source_spec
* @param $destination_spec
*/
function _drush_msync_process($drush_command, $source_spec, $destination_spec) {
// Rsync path handling, sql-sync alias just has no suffix.
list($source_alias, $source_path_suffix) = _drush_msync_split_path($source_spec);
list($destination_alias, $destination_path_suffix) = _drush_msync_split_path($destination_spec);
list($sources, $destinations) = _drush_msync_prepare_site_list($source_alias, $destination_alias);
foreach ($sources as $site => $source) {
$destination = $destinations[$site];
$t_args = [
'@source' => $source,
'@destination' => $destination
];
drush_log(dt('Start: @source => @destination', $t_args), 'ok');
$context = array_diff_key(drush_get_context('cli'), _drush_msync_extra_options($drush_command));
if ($drush_command == 'sql-sync' && drush_get_option('db-drop-existing-tables', TRUE)) {
drush_invoke_process($destination, 'sql-drop', [], $context);
}
drush_invoke_process('@self', $drush_command, [
$source . $source_path_suffix,
$destination . $destination_path_suffix
], $context);
drush_log(dt('Done: @source => @destination', $t_args), 'success');
drush_log(dt('+----------------------------------------------+'), 'ok');
}
}
/**
* @param $source_alias
* @param $destination_alias
* @return array
*/
function _drush_msync_prepare_site_list($source_alias, $destination_alias) {
$source_record = drush_sitealias_get_record($source_alias);
$destination_record = drush_sitealias_get_record($destination_alias);
$sources_by_site = _drush_msync_aliases_by_site($source_record['site-list']);
$destinations_by_site = _drush_msync_aliases_by_site($destination_record['site-list']);
$sources = array_intersect_key($sources_by_site, $destinations_by_site);
$destinations = array_intersect_key($destinations_by_site, $sources_by_site);
$excess_sources = array_diff_key($sources_by_site, $destinations_by_site);
$excess_destinations = array_diff_key($destinations_by_site, $sources_by_site);
$restrict_sites = drush_get_option_list('only-sites');
if ($restrict_sites) {
$restrict_sites_as_keys = array_fill_keys($restrict_sites, 1);
$restrict_sites_to_display = array_keys(array_intersect_key($restrict_sites_as_keys, $sources));
$sources = array_intersect_key($sources, $restrict_sites_as_keys);
}
$exclude_sites = drush_get_option_list('exclude-sites');
if ($exclude_sites) {
$exclude_sites_as_keys = array_fill_keys($exclude_sites, 1);
$exclude_sites_to_display = array_keys(array_intersect_key($exclude_sites_as_keys, $sources));
$sources = array_diff_key($sources, $exclude_sites_as_keys);
}
if ($excess_sources) {
drush_log(dt('Excess sources found: @excess', ['@excess' => implode(', ', $excess_sources)]), 'warning');
}
if ($excess_destinations) {
drush_log(dt('Excess destinations found: @excess', ['@excess' => implode(', ', $excess_destinations)]), 'warning');
}
if (!empty($restrict_sites_to_display)) {
drush_log(dt('Restricted to sites: @restricted', ['@restricted' => implode(', ', $restrict_sites_to_display)]), 'warning');
}
if (!empty($exclude_sites_to_display)) {
drush_log(dt('Excluded sites: @excluded', ['@excluded' => implode(', ', $exclude_sites_to_display)]), 'warning');
}
if (!$sources) {
drush_log(dt('No sites found to process.'), 'warning');
}
else {
drush_log(dt('Syncing: @sites', ['@sites' => implode(', ', array_keys($sources))]), 'ok');
if (!drush_confirm('OK?')) {
$sources = $destinations = [];
}
}
return array($sources, $destinations);
}
/**
* @param $source
* @return array
* Splits 'alias:path' into ['alias',':path']
* and 'alias' into ['alias','']
*/
function _drush_msync_split_path($source) {
$source_parts = preg_split('/(?=:)/', $source, 2);
$source_parts += [1 => ''];
return $source_parts;
}
/**
* @param $site_list
* @return array
*/
function _drush_msync_aliases_by_site($site_list) {
$aliases_by_site = [];
foreach ($site_list as $site_alias_name) {
$site_name = _drush_msync_extract_alias_site($site_alias_name);
$aliases_by_site[$site_name] = $site_alias_name;
}
return $aliases_by_site;
}
/**
* @param $site_alias_name
* @return mixed
*/
function _drush_msync_extract_alias_site($site_alias_name) {
// Use simple naming scheme to match site.
// This covers naming schemes like name.site or name:site,
// and also drush's /drupal/path#site
// @todo Consider option to get the site name with (much slower) drush status.
$source_parts = array_reverse(preg_split('/[.:#]/', $site_alias_name));
$site_name = $source_parts[0];
return $site_name;
}