/
daemon.php
266 lines (228 loc) · 7.53 KB
/
daemon.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
// Copyright (C) 2015 xaizek <xaizek@posteo.net>
//
// fragile is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, version 3.
//
// fragile is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
require_once __DIR__ . '/classes/Builds.php';
require_once __DIR__ . '/classes/Buildset.php';
require_once __DIR__ . '/classes/Buildsets.php';
require_once __DIR__ . '/classes/Utils.php';
require_once __DIR__ . '/config.php';
// TODO: maybe mark all "running" builds as failed
// TODO: maybe extract Builders and Builder classes
if (!putenv('FRAGILE_REPO=' . REPO_PATH)) {
die("Failed to set FRAGILE_REPO environment variable\n");
}
prepareRepository();
serve();
/**
* @brief Clones repository if it doesn't exist yet.
*/
function prepareRepository()
{
if (!createPath(REPO_PATH)) {
return;
}
system(__DIR__ . "/vcs/clone '" . REPO_URL . "'", $retval);
if ($retval != 0) {
Utils::delTree(REPO_PATH);
die("Failed to clone repository\n");
}
}
/**
* @brief Performs infinite loop of running builds.
*/
function serve()
{
while (true) {
$builds = Builds::getPendingBuilds();
while (empty($builds)) {
sleep(DAEMON_TIMEOUT);
$builds = Builds::getPendingBuilds();
}
runBuilds($builds);
}
}
/**
* @brief Executes all pending builds.
*
* @param builds List of builds to run.
*/
function runBuilds($builds)
{
$bysets = [];
foreach ($builds as $build) {
if (!array_key_exists($build->buildset, $bysets)) {
$bysets[$build->buildset] = [];
}
array_push($bysets[$build->buildset], $build);
}
$revision = '';
foreach ($bysets as $builds) {
$buildset = Buildset::get($build->buildset);
$prevBuildset = Buildsets::getLastCompletedOf($buildset->name);
if ($prevBuildset !== null) {
$prioritized = [];
foreach (Builds::getBuildsForAll([$prevBuildset]) as $build) {
if ($build->status !== 'OK') {
array_push($prioritized, $build->buildername);
}
}
usort($builds, function ($a, $b) use ($prioritized) {
$pa = in_array($a->buildername, $prioritized, TRUE) ? 1 : 0;
$pb = in_array($b->buildername, $prioritized, TRUE) ? 1 : 0;
if ($pb !== $pa) {
return $pb - $pa;
}
return Builds::builderCmp($a, $b);
});
} else {
usort($builds, "Builds::builderCmp");
}
if (!putenv('FRAGILE_REF=' . $buildset->name)) {
die("Failed to set FRAGILE_REF environment variable\n");
}
foreach ($builds as $build) {
// checkout revision while not doing anything if we already on it
if ($build->revision !== $revision) {
$revision = $build->revision;
system(__DIR__ . "/vcs/checkout '" . $revision . "'", $retval);
if ($retval != 0) {
$build->markAsFinished('ERROR',
"Failed to checkout revision\n",
$retval);
$revision = '';
continue;
}
}
runBuild($build);
}
}
}
/**
* @brief Executes a build managing its status and output information.
*
* @param build Build to perform.
*/
function runBuild($build)
{
// TODO: measure and record execution time of the build
$build->markAsStarted();
$rawOutput = '';
$buildPath = BUILDS_PATH . "/$build->buildername";
createPath($buildPath);
$handle = popen("cd $buildPath && "
. BUILDERS_PATH . '/' . $build->buildername . ' 2>&1', 'r');
while (!feof($handle)) {
$rawOutput .= fgets($handle);
// TODO: append output to database record every N lines (e.g. 100)
}
$exitcode = pclose($handle);
$output = makeReport($rawOutput);
$build->markAsFinished(($exitcode == 0) ? 'OK' : 'FAIL', $output,
$exitcode);
}
/**
* @brief Creates directory if it doesn't exist.
*
* @param path Path to create.
*
* @returns @c true if it was created, otherwise @c false is returned.
*/
function createPath($path)
{
// drop cached information about the path
clearstatcache(false, $path);
if (is_dir($path)) {
return false;
}
if (!mkdir($path, 0700, true)) {
die("Failed to create directory: $path\n");
}
return true;
}
/**
* @brief Formats output to produce build report.
*
* Result consists of two parts separated by double newline symbol. The first
* parts contains index of errors and warnings, the second one is formatted
* output.
*
* @param rawOutput Output from builder script as is.
*
* @returns Multiline build report.
*/
function makeReport($rawOutput)
{
$errors = [];
$warnings = [];
$input = preg_split('/\n/', $rawOutput);
$output = [];
$msgnum = 1;
foreach ($input as $line) {
$re = '/^(.*)(error|warning|Error|Warning|ERROR|WARNING|ERROR SUMMARY)(:\s+)(.*)$/';
preg_match($re, $line, $matches);
if (sizeof($matches) != 0 && $matches[4] != '0') {
$style = strcasecmp($matches[2], 'warning') == 0
? 'warning' : 'error';
$label = $matches[2] . ': ' . $matches[4];
} else {
$matches = [];
}
if (sizeof($matches) == 0) {
$re = '/^()(Segmentation fault)()()$/';
preg_match($re, $line, $matches);
if (sizeof($matches) != 0) {
$style = 'error';
$label = $matches[2];
}
}
if (sizeof($matches) == 0) {
$re = '/^()([^:]+:\d+)(:\s+)(recipe for target \'.*\' failed)$/';
preg_match($re, $line, $matches);
if (sizeof($matches) != 0) {
$style = 'error';
$label = $matches[4];
}
}
if (sizeof($matches) == 0) {
array_push($output, htmlentities($line));
continue;
}
$anchor = "<a name='m$msgnum'/>";
$link = "<a href='#m$msgnum'>" . htmlentities($label) . "</a>";
if ($style === 'warning') {
array_push($warnings, $link);
} else {
array_push($errors, $link);
}
$line = htmlentities($matches[1])
. "<span class='$style-title'>".htmlentities($matches[2])."</span>"
. htmlentities($matches[3])
. "<span class='$style-msg'>".htmlentities($matches[4])."</span>";
array_push($output, $anchor . $line);
++$msgnum;
}
$header = '';
if (sizeof($errors) != 0) {
$header .= "<span class='errors'>Errors:</span><ol><li>";
$header .= join("</li><li>", $errors);
$header .= "</li></ol>\n";
}
if (sizeof($warnings) != 0) {
$header .= "<span class='warnings'>Warnings:</span><ol><li>";
$header .= join("</li><li>", $warnings);
$header .= "</li></ol>\n";
}
return $header . "\n\n" . join("\n", $output);
}
?>