1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Task; |
9
|
|
|
|
10
|
|
|
use Deployer\Host\Host; |
11
|
|
|
use function Deployer\Support\array_flatten; |
12
|
|
|
|
13
|
|
|
class Task |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var callable |
22
|
|
|
*/ |
23
|
|
|
private $callback; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $description; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Should we run this task locally? |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $local = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Lists of hosts, roles there task should be executed. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $on = ['hosts' => [], 'roles' => []]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* List of task names to run before. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $before = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* List of task names to run after. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $after = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Make task internal and not visible in CLI. |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
private $hidden = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Mark task to run only once, of the first node from the pool |
67
|
|
|
* |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
private $once = false; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Mark if the task has run at least once |
74
|
|
|
* |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
private $hasRun = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Shallow task will not print execution message/finish messages. |
81
|
|
|
* Useful for success messages and info printing. |
82
|
|
|
* |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
private $shallow = false; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Task filepath location. |
89
|
|
|
* |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
private $filepath = ''; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name Tasks name |
96
|
|
|
* @param callable $callback Task code |
97
|
|
|
*/ |
98
|
17 |
|
public function __construct($name, callable $callback = null) |
99
|
|
|
{ |
100
|
17 |
|
$this->name = $name; |
101
|
17 |
|
$this->callback = $callback; |
102
|
17 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Context $context |
106
|
|
|
*/ |
107
|
3 |
|
public function run(Context $context) |
108
|
|
|
{ |
109
|
3 |
|
Context::push($context); |
110
|
|
|
|
111
|
|
|
// Call task |
112
|
3 |
|
call_user_func($this->callback); |
113
|
|
|
|
114
|
3 |
|
if ($this->once) { |
115
|
1 |
|
$this->hasRun = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Clear working_path |
119
|
3 |
|
if ($context->getConfig() !== null) { |
120
|
|
|
$context->getConfig()->set('working_path', false); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
Context::pop(); |
124
|
3 |
|
} |
125
|
|
|
|
126
|
4 |
|
public function getName() |
127
|
|
|
{ |
128
|
4 |
|
return $this->name; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __toString() |
132
|
|
|
{ |
133
|
|
|
return $this->getName(); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function getDescription() |
137
|
|
|
{ |
138
|
1 |
|
return $this->description; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $description |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
11 |
|
public function desc($description) |
146
|
|
|
{ |
147
|
11 |
|
$this->description = $description; |
148
|
11 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Mark this task local |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
1 |
|
public function local() |
157
|
|
|
{ |
158
|
1 |
|
$this->local = true; |
159
|
1 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
1 |
|
public function isLocal() |
166
|
|
|
{ |
167
|
1 |
|
return $this->local; |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
public function once() |
171
|
|
|
{ |
172
|
2 |
|
$this->once = true; |
173
|
2 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
public function isOnce() |
177
|
|
|
{ |
178
|
1 |
|
return $this->once; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param array $hosts |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
2 |
|
public function onHosts(...$hosts) |
186
|
|
|
{ |
187
|
2 |
|
$this->on['hosts'] = array_flatten($hosts); |
188
|
2 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $roles |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
2 |
|
public function onRoles(...$roles) |
196
|
|
|
{ |
197
|
2 |
|
$this->on['roles'] = array_flatten($roles); |
198
|
2 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Checks what task should be performed on one of hosts. |
203
|
|
|
* |
204
|
|
|
* @param Host[] $hosts |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
5 |
|
public function shouldBePerformed(...$hosts) |
208
|
|
|
{ |
209
|
|
|
// don't allow to run again it the task has been marked to run only once |
210
|
5 |
|
if ($this->once && $this->hasRun) { |
211
|
1 |
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
5 |
|
foreach ($hosts as $host) { |
215
|
2 |
|
$onHost = empty($this->on['hosts']) || in_array($host->alias(), $this->on['hosts'], true); |
|
|
|
|
216
|
|
|
|
217
|
2 |
|
$onRole = empty($this->on['roles']); |
218
|
2 |
|
foreach ((array) $host->get('roles', []) as $role) { |
219
|
2 |
|
if (in_array($role, $this->on['roles'], true)) { |
220
|
1 |
|
$onRole = true; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
2 |
|
if ($onHost && $onRole) { |
225
|
2 |
|
return true; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
4 |
|
return empty($hosts); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return boolean |
234
|
|
|
*/ |
235
|
1 |
|
public function isHidden() |
236
|
|
|
{ |
237
|
1 |
|
return $this->hidden; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Mark task as hidden |
242
|
|
|
* |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
1 |
|
public function hidden() |
246
|
|
|
{ |
247
|
1 |
|
$this->hidden = true; |
248
|
1 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $task |
253
|
|
|
* |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
1 |
|
public function addBefore(string $task) |
257
|
|
|
{ |
258
|
1 |
|
array_unshift($this->before, $task); |
259
|
1 |
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $task |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
1 |
|
public function addAfter(string $task) |
268
|
|
|
{ |
269
|
1 |
|
array_push($this->after, $task); |
270
|
1 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get before tasks names. |
275
|
|
|
* @return string[] |
276
|
|
|
*/ |
277
|
3 |
|
public function getBefore() |
278
|
|
|
{ |
279
|
3 |
|
return $this->before; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get after tasks names. |
284
|
|
|
* @return string[] |
285
|
|
|
*/ |
286
|
3 |
|
public function getAfter() |
287
|
|
|
{ |
288
|
3 |
|
return $this->after; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Sets task shallow. |
293
|
|
|
* |
294
|
|
|
* Shallow task will not print execution message/finish messages. |
295
|
|
|
* |
296
|
|
|
* @return $this |
297
|
|
|
*/ |
298
|
|
|
public function shallow() |
299
|
|
|
{ |
300
|
|
|
$this->shallow = true; |
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
public function isShallow() |
308
|
|
|
{ |
309
|
|
|
return $this->shallow; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @internal this is used by ParallelExecutor and prevent multiple run |
314
|
|
|
*/ |
315
|
|
|
public function setHasRun() |
316
|
|
|
{ |
317
|
|
|
if ($this->isOnce()) { |
318
|
|
|
$this->hasRun = true; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
|
public function getFilepath(): string |
326
|
|
|
{ |
327
|
|
|
return $this->filepath; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param string $filepath |
332
|
|
|
*/ |
333
|
10 |
|
public function setFilepath(string $filepath): void |
334
|
|
|
{ |
335
|
10 |
|
$this->filepath = $filepath; |
336
|
10 |
|
} |
337
|
|
|
} |
338
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.