|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage Task |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Task; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* RSync from/to the current node or all nodes if no current |
|
19
|
|
|
* |
|
20
|
|
|
* @category Netresearch |
|
21
|
|
|
* @package Netresearch\Kite |
|
22
|
|
|
* @subpackage Task |
|
23
|
|
|
* @author Christian Opitz <[email protected]> |
|
24
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
25
|
|
|
* @link http://www.netresearch.de |
|
26
|
|
|
*/ |
|
27
|
|
|
class RsyncTask extends \Netresearch\Kite\Task\ScpTask |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $defaultOptions = array( |
|
33
|
|
|
'compress' => true, |
|
34
|
|
|
'rsh' => 'ssh{node.sshOptions}', |
|
35
|
|
|
'recursive' => true, |
|
36
|
|
|
'times' => true, |
|
37
|
|
|
// 'perms', |
|
38
|
|
|
'links' => true |
|
39
|
|
|
/* 'delete', |
|
40
|
|
|
'delete-excluded' */ |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $defaultRules = array( |
|
47
|
|
|
'exclude' => array( |
|
48
|
|
|
'.*' |
|
49
|
|
|
), |
|
50
|
|
|
'include' => array( |
|
51
|
|
|
'.htaccess' |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $options; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Configure the options |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
protected function configureVariables() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
return array( |
|
68
|
|
|
'exclude' => array( |
|
69
|
|
|
'type' => 'array', |
|
70
|
|
|
'default' => array(), |
|
71
|
|
|
'label' => 'Array with files/dirs to explicitely exclude' |
|
72
|
|
|
), |
|
73
|
|
|
'include' => array( |
|
74
|
|
|
'type' => 'array', |
|
75
|
|
|
'default' => array(), |
|
76
|
|
|
'label' => 'Array with files/dirs to explicitely include' |
|
77
|
|
|
), |
|
78
|
|
|
'options' => array( |
|
79
|
|
|
'type' => 'array', |
|
80
|
|
|
'default' => array(), |
|
81
|
|
|
'label' => 'Array with options for rsync: Elements with numeric keys or bool true values will be --switches.' |
|
82
|
|
|
), |
|
83
|
|
|
'--' |
|
84
|
|
|
) + parent::configureVariables(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Run the task |
|
89
|
|
|
* |
|
90
|
|
|
* @return array|mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function run() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->console->getOutput()->isQuiet()) { |
|
95
|
|
|
$this->defaultOptions['quiet'] = true; |
|
96
|
|
|
} |
|
97
|
|
|
if ($this->console->getOutput()->isVeryVerbose()) { |
|
98
|
|
|
$this->defaultOptions['verbose'] = true; |
|
99
|
|
|
} |
|
100
|
|
|
$this->options = array_merge($this->defaultOptions, $this->get('options')); |
|
101
|
|
|
|
|
102
|
|
|
if (!$this->shouldExecute()) { |
|
103
|
|
|
$this->options['dry-run'] = true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->preview(); |
|
107
|
|
|
return $this->execute(); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the rsync command |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getScpCommand() |
|
116
|
|
|
{ |
|
117
|
|
|
$rsyncCommand = 'rsync' . $this->renderOptions($this->options); |
|
118
|
|
|
|
|
119
|
|
|
foreach (array('include', 'exclude') as $type) { |
|
120
|
|
|
$rules = $this->getMergedArrayOption($type, $this->defaultRules[$type]); |
|
121
|
|
|
foreach ($rules as $rule) { |
|
122
|
|
|
$rsyncCommand .= ' --' . $type . '=' . escapeshellarg($rule); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $rsyncCommand; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Merge an (optional) array option into defaults |
|
131
|
|
|
* |
|
132
|
|
|
* Given f.e. that the option has the following value: |
|
133
|
|
|
* array( |
|
134
|
|
|
* 'debug', |
|
135
|
|
|
* 'include' => '*', |
|
136
|
|
|
* 'quiet' => null, |
|
137
|
|
|
* ) |
|
138
|
|
|
* |
|
139
|
|
|
* and the defaults are: |
|
140
|
|
|
* array( |
|
141
|
|
|
* 'compress', |
|
142
|
|
|
* 'quiet' |
|
143
|
|
|
* ) |
|
144
|
|
|
* |
|
145
|
|
|
* the result would be: |
|
146
|
|
|
* array( |
|
147
|
|
|
* 'compress', |
|
148
|
|
|
* 'debug', |
|
149
|
|
|
* 'include' => '*' |
|
150
|
|
|
* ) |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $name The name |
|
153
|
|
|
* @param array $defaults The defaults |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getMergedArrayOption($name, array $defaults) |
|
158
|
|
|
{ |
|
159
|
|
|
$options = $defaults; |
|
160
|
|
|
foreach ($this->get($name) as $option => $value) { |
|
161
|
|
|
if (is_numeric($option)) { |
|
162
|
|
|
if (!in_array($value, $options)) { |
|
163
|
|
|
$options[] = $value; |
|
164
|
|
|
} |
|
165
|
|
|
} else { |
|
166
|
|
|
if (!$value) { |
|
167
|
|
|
if (array_key_exists($option, $options)) { |
|
168
|
|
|
unset($options[$option]); |
|
169
|
|
|
} else { |
|
170
|
|
|
$i = array_search($option, $options); |
|
171
|
|
|
if (is_numeric($i)) { |
|
172
|
|
|
unset($options[$i]); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} else { |
|
176
|
|
|
$options[$option] = $value; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
return $options; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
?> |
|
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.