-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_sync.drush.inc
152 lines (130 loc) · 5.04 KB
/
file_sync.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
<?php
/**
* @file
* Provide a file-sync command to sync files directories between environments.
*/
/**
* Implements hook_drush_command().
*/
function file_sync_drush_command() {
$commands = array();
$commands['file-sync'] = array(
'description' => 'Sync files directories between sites',
'aliases' => array('fsync'),
'arguments' => array(
'source' => 'Source alias',
'destination' => 'Destination alias', // TODO make this optional and default to @self
),
'options' => array(
'only-public' => 'Only sync the public files directory',
'only-private' => 'Only sync the private files directory',
),
'required-arguments' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'examples' => array(
'drush fsync @source @destination' => 'Sync both the public and private files directories',
'drush fsync --only-public @source @destination' => 'Only sync the public files directory',
),
);
return $commands;
}
/**
* Implements hook_drush_help_alter().
*/
function file_sync_drush_help_alter(&$command) {
$commands = drush_get_commands();
// Append all of core-rsync's options to file-sync.
// This is needed to prevent drush_core_rsync() from sending the additional
// options for core-rsync through to rsync as well.
if ($command['command'] === 'file-sync' && isset($commands['core-rsync'])) {
$command['options'] += $commands['core-rsync']['options'];
}
}
/**
* File sync command callback.
*/
function drush_file_sync($source, $destination) {
$only_public = drush_get_option('only-public', FALSE);
$only_private = drush_get_option('only-private', FALSE);
if ($only_public && $only_private) {
drush_log(dt('Only one of --only-public or --only-private can be set'), 'error');
return false;
}
// drush_unset_option() is needed to prevent the options for file-sync getting
// passed through to core-rsync.
// core-rync loads the original command line options to pass to rsync, so some
// additional context-hacking is needed.
// see drush_core_rsync() and drush_get_original_cli_args_and_options().
$args = &drush_get_context('DRUSH_COMMAND_ARGS', array());
$original_args = $args;
$command = drush_get_command();
foreach (array_keys($command['options']) as $option) {
drush_unset_option($option, 'cli');
$arg_index = array_search('--' . $option, $args);
if ($arg_index !== FALSE) {
unset($args[$arg_index]);
}
}
$args = array_values($args);
$public_result = FALSE;
if (!$only_private) {
drush_log(dt('Syncing public files'), 'status');
$public_result = _drush_file_sync_path_alias($source, $destination, '%files');
}
$private_result = FALSE;
if (!$only_public) {
drush_log(dt('Syncing private files'), 'status');
$private_result = _drush_file_sync_path_alias($source, $destination, '%private', $only_private? 'error': 'warning');
}
// Restore the original arguments just to be safe.
$args = $original_args;
if (
(!$only_private && !$public_result)
||
($only_private && !$private_result)
) {
// If public transfer is attempted but fails, it's always an error.
// If private transfer is attempted but fails, it's an error if only
// transferring private files was attempted.
drush_log(dt('Sync could not complete successfully'), 'error');
}
else if (!$only_public && !$only_private && !$private_result) {
// A private file path may not be defined, so it's only a warning if the
// private transfer could not be completed while attempting to transfer all
// files.
drush_log(dt('Sync partially completed'), 'warning');
}
else {
drush_log(dt('Sync completed successfully'), 'success');
}
}
/**
* Helper function to sync a single path between two environments.
*
* @param $source
* @param $destination
* @param $path_alias
* @param $error_level
* If sync fails, what error level should be used.
* @return bool
*/
function _drush_file_sync_path_alias($source, $destination, $path_alias, $error_level = 'error') {
$additional_options = array();
// Check that the path alias can be resolved properly before starting the transfer.
$source_path = $source . ':'. $path_alias;
$source_alias_info = drush_sitealias_evaluate_path($source_path, $additional_options);
if (empty($source_alias_info['path-aliases'][$path_alias])) {
drush_log(dt('Could not evaluate source path !path.', array('!path' => $source_path)), $error_level);
return FALSE;
}
$destination_path = $destination . ':' . $path_alias;
$destination_alias_info = drush_sitealias_evaluate_path($destination_path, $additional_options);
if (empty($destination_alias_info['path-aliases'][$path_alias])) {
drush_log(dt('Could not evaluate destination path !path.', array('!path' => $destination_path)), $error_level);
return FALSE;
}
drush_invoke('rsync', array($source_path, $destination_path));
// drush_command() doesn't pass along the return value from rsync for
// drush_invoke() to make use of, so it's necessary to check the error log.
return drush_get_error() === DRUSH_SUCCESS;
}