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
|
|
|
* @param string $name Tasks name |
89
|
|
|
* @param callable $callback Task code |
90
|
|
|
*/ |
91
|
17 |
|
public function __construct($name, callable $callback = null) |
92
|
|
|
{ |
93
|
17 |
|
$this->name = $name; |
94
|
17 |
|
$this->callback = $callback; |
95
|
17 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Context $context |
99
|
|
|
*/ |
100
|
3 |
|
public function run(Context $context) |
101
|
|
|
{ |
102
|
3 |
|
Context::push($context); |
103
|
|
|
|
104
|
|
|
// Call task |
105
|
3 |
|
call_user_func($this->callback); |
106
|
|
|
|
107
|
3 |
|
if ($this->once) { |
108
|
1 |
|
$this->hasRun = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Clear working_path |
112
|
3 |
|
if ($context->getConfig() !== null) { |
113
|
|
|
$context->getConfig()->set('working_path', false); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
Context::pop(); |
117
|
3 |
|
} |
118
|
|
|
|
119
|
4 |
|
public function getName() |
120
|
|
|
{ |
121
|
4 |
|
return $this->name; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function __toString() |
125
|
|
|
{ |
126
|
|
|
return $this->getName(); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function getDescription() |
130
|
|
|
{ |
131
|
1 |
|
return $this->description; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $description |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
11 |
|
public function desc($description) |
139
|
|
|
{ |
140
|
11 |
|
$this->description = $description; |
141
|
11 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Mark this task local |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
1 |
|
public function local() |
150
|
|
|
{ |
151
|
1 |
|
$this->local = true; |
152
|
1 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
1 |
|
public function isLocal() |
159
|
|
|
{ |
160
|
1 |
|
return $this->local; |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
public function once() |
164
|
|
|
{ |
165
|
2 |
|
$this->once = true; |
166
|
2 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
public function isOnce() |
170
|
|
|
{ |
171
|
1 |
|
return $this->once; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $hosts |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
2 |
|
public function onHosts(...$hosts) |
179
|
|
|
{ |
180
|
2 |
|
$this->on['hosts'] = array_flatten($hosts); |
181
|
2 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $roles |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
2 |
|
public function onRoles(...$roles) |
189
|
|
|
{ |
190
|
2 |
|
$this->on['roles'] = array_flatten($roles); |
191
|
2 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Checks what task should be performed on one of hosts. |
196
|
|
|
* |
197
|
|
|
* @param Host[] $hosts |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
5 |
|
public function shouldBePerformed(...$hosts) |
201
|
|
|
{ |
202
|
|
|
// don't allow to run again it the task has been marked to run only once |
203
|
5 |
|
if ($this->once && $this->hasRun) { |
204
|
1 |
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
5 |
|
foreach ($hosts as $host) { |
208
|
2 |
|
$onHost = empty($this->on['hosts']) || in_array($host->alias(), $this->on['hosts'], true); |
|
|
|
|
209
|
|
|
|
210
|
2 |
|
$onRole = empty($this->on['roles']); |
211
|
2 |
|
foreach ((array) $host->get('roles', []) as $role) { |
212
|
2 |
|
if (in_array($role, $this->on['roles'], true)) { |
213
|
1 |
|
$onRole = true; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
2 |
|
if ($onHost && $onRole) { |
218
|
2 |
|
return true; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
4 |
|
return empty($hosts); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return boolean |
227
|
|
|
*/ |
228
|
1 |
|
public function isHidden() |
229
|
|
|
{ |
230
|
1 |
|
return $this->hidden; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Mark task as hidden |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
1 |
|
public function hidden() |
239
|
|
|
{ |
240
|
1 |
|
$this->hidden = true; |
241
|
1 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param string $task |
246
|
|
|
* |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
1 |
|
public function addBefore(string $task) |
250
|
|
|
{ |
251
|
1 |
|
array_unshift($this->before, $task); |
252
|
1 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $task |
257
|
|
|
* |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
1 |
|
public function addAfter(string $task) |
261
|
|
|
{ |
262
|
1 |
|
array_push($this->after, $task); |
263
|
1 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get before tasks names. |
268
|
|
|
* @return string[] |
269
|
|
|
*/ |
270
|
3 |
|
public function getBefore() |
271
|
|
|
{ |
272
|
3 |
|
return $this->before; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get after tasks names. |
277
|
|
|
* @return string[] |
278
|
|
|
*/ |
279
|
3 |
|
public function getAfter() |
280
|
|
|
{ |
281
|
3 |
|
return $this->after; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Sets task shallow. |
286
|
|
|
* |
287
|
|
|
* Shallow task will not print execution message/finish messages. |
288
|
|
|
* |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
public function shallow() |
292
|
|
|
{ |
293
|
|
|
$this->shallow = true; |
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
public function isShallow() |
301
|
|
|
{ |
302
|
|
|
return $this->shallow; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @internal this is used by ParallelExecutor and prevent multiple run |
307
|
|
|
*/ |
308
|
|
|
public function setHasRun() |
309
|
|
|
{ |
310
|
|
|
if ($this->isOnce()) { |
311
|
|
|
$this->hasRun = true; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.