forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutor.php
226 lines (206 loc) · 5.96 KB
/
Executor.php
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
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Acquia\Blt\Robo\Common;
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
use Acquia\Blt\Robo\Exceptions\BltException;
use GuzzleHttp\Client;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Robo\Collection\CollectionBuilder;
use Robo\Contract\VerbosityThresholdInterface;
use Robo\Robo;
use Robo\Contract\ConfigAwareInterface;
use Robo\Contract\IOAwareInterface;
use Symfony\Component\Process\Process;
/**
* A class for executing commands.
*
* This allows non-Robo-command classes to execute commands easily.
*/
class Executor implements ConfigAwareInterface, IOAwareInterface, LoggerAwareInterface {
use ConfigAwareTrait;
use IO;
use LoggerAwareTrait;
/**
* A copy of the Robo builder.
*
* @var \Acquia\Blt\Robo\BltTasks*/
protected $builder;
/**
* Executor constructor.
*
* @param \Robo\Collection\CollectionBuilder $builder
* This is a copy of the collection builder, required for calling various
* Robo tasks from non-command files.
*/
public function __construct(CollectionBuilder $builder) {
$this->builder = $builder;
}
/**
* Returns $this->builder.
*
* @return \Acquia\Blt\Robo\BltTasks
* The builder.
*/
public function getBuilder() {
return $this->builder;
}
/**
* Wrapper for taskExec().
*
* @param string $command
* The command to execute.
*
* @return \Robo\Task\Base\Exec
* The task. You must call run() on this to execute it!
*/
public function taskExec($command) {
return $this->builder->taskExec($command);
}
/**
* Executes a drush command.
*
* @param string $command
* The command to execute, without "drush" prefix.
*
* @return \Robo\Common\ProcessExecutor
* The unexecuted process.
*/
public function drush($command) {
// @todo Set to silent if verbosity is less than very verbose.
$bin = $this->getConfigValue('composer.bin');
/** @var \Robo\Common\ProcessExecutor $process_executor */
$drush_alias = $this->getConfigValue('drush.alias');
if (!empty($drush_alias)) {
$drush_alias = "@$drush_alias";
}
$process_executor = Robo::process(new Process("'$bin/drush' $drush_alias $command"));
return $process_executor->dir($this->getConfigValue('docroot'))
->interactive(FALSE)
->printOutput(TRUE)
->printMetadata(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERY_VERBOSE);
}
/**
* Executes a command.
*
* @param string $command
* The command.
*
* @return \Robo\Common\ProcessExecutor
* The unexecuted command.
*/
public function execute($command) {
/** @var \Robo\Common\ProcessExecutor $process_executor */
$process_executor = Robo::process(new Process($command));
return $process_executor->dir($this->getConfigValue('repo.root'))
->printOutput(FALSE)
->printMetadata(FALSE)
->interactive(FALSE);
}
/**
* Kills all system processes that are using a particular port.
*
* @param string $port
* The port number.
*/
public function killProcessByPort($port) {
$this->logger->info("Killing all processes on port '$port'...");
// This is allowed to fail.
// @todo Replace with standardized call to Symfony Process.
exec("command -v lsof && lsof -ti tcp:$port | xargs kill l 2>&1");
exec("pkill -f $port 2>&1");
}
/**
* Kills all system processes containing a particular string.
*
* @param string $name
* The name of the process.
*/
public function killProcessByName($name) {
$this->logger->info("Killing all processing containing string '$name'...");
// This is allowed to fail.
// @todo Replace with standardized call to Symfony Process.
exec("ps aux | grep -i $name | grep -v grep | awk '{print $2}' | xargs kill -9 2>&1");
// exec("ps aux | awk '/$name/ {print $2}' 2>&1 | xargs kill -9");.
}
/**
* Waits until a given URL responds with a non-50x response.
*
* This does have a maximum timeout, defined in wait().
*
* @param string $url
* The URL to wait for.
*/
public function waitForUrlAvailable($url) {
$this->wait([$this, 'checkUrl'], [$url], "Waiting for response from $url...");
}
/**
* Waits until a given callable returns TRUE.
*
* This does have a maximum timeout.
*
* @param callable $callable
* The method/function to wait for a TRUE response from.
* @param array $args
* Arguments to pass to $callable.
* @param string $message
* The message to display when this function is called.
*
* @return bool
* TRUE if callable returns TRUE.
*
* @throws \Exception
*/
public function wait(callable $callable, array $args, $message = '') {
$maxWait = 15 * 1000;
$checkEvery = 1 * 1000;
$start = microtime(TRUE) * 1000;
$end = $start + $maxWait;
if (!$message) {
$method_name = is_array($callable) ? $callable[1] : $callable;
$message = "Waiting for $method_name() to return true.";
}
// For some reason we can't reuse $start here.
while (microtime(TRUE) * 1000 < $end) {
$this->logger->info($message);
try {
if (call_user_func_array($callable, $args)) {
return TRUE;
}
}
catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
usleep($checkEvery * 1000);
}
throw new BltException("Timed out.");
}
/**
* Checks a URL for a non-50x response.
*
* @param string $url
* The URL to check.
*
* @return bool
* TRUE if URL responded with a non-50x response.
*/
public function checkUrl($url) {
try {
$client = new Client();
$res = $client->request('GET', $url, [
'connection_timeout' => 2,
'timeout' => 2,
'exceptions' => FALSE,
]);
if ($res->getStatusCode() && substr($res->getStatusCode(), 0, 1) != '5') {
return TRUE;
}
else {
return FALSE;
}
}
catch (\Exception $e) {
}
return FALSE;
}
}