-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-funcs.php
351 lines (322 loc) · 9.74 KB
/
package-funcs.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env php
<?php
class Packages
{
private string $pkgdir;
private string $dest;
private string $pkglimit = "";
private string $webroot = "/tmp/webroot";
private ?string $devdest = null;
private ?string $proddest = null;
private string $url = "https://example.com";
public function __construct(string $pkgdir, string $dest)
{
$this->pkgdir = $pkgdir;
$this->dest = $dest;
}
public function useWebroot(string $webroot): self
{
$this->webroot = $webroot;
return $this;
}
public function setBaseUrl(string $url): self
{
$this->url = $url;
return $this;
}
public function setDevDest(string $devdest): self
{
$this->devdest = $devdest;
return $this;
}
public function getDevDest(): string
{
return $this->devdest ?? $this->webroot . "/devpackages.json";
}
public function getCurrentDevPackages(): array
{
$file = $this->getDevDest();
if (!file_exists($file)) {
return [];
}
return json_decode(file_get_contents($file), true);
}
public function setProdDest(string $proddest): self
{
$this->proddest = $proddest;
return $this;
}
public function getProdDest(): string
{
return $this->proddest ?? $this->webroot . "/packages.json";
}
public function getCurrentProdPackages(): array
{
$file = $this->getProdDest();
if (!file_exists($file)) {
return [];
}
return json_decode(file_get_contents($file), true);
}
public function onlyPackage(string $pkg)
{
$this->pkglimit = $pkg;
}
public function getPkgArr()
{
$pkgs = glob($this->pkgdir . "/*/meta");
$pkgarr = [];
foreach ($pkgs as $meta) {
$pkgdir = dirname($meta);
$pkg = basename($pkgdir);
if ($this->pkglimit && $this->pkglimit !== $pkg) {
// Ignore because we've only been asked to do one.
continue;
}
$pkgarr[$pkg] = $this->getGitinfo($pkgdir);
}
return $pkgarr;
}
public function getGitinfo($pkgdir)
{
chdir($pkgdir);
$tags = ["master" => "master"];
$cmd = "git tag --list";
exec($cmd, $tagoutput, $res);
foreach ($tagoutput as $tag) {
$tags[$tag] = $tag;
}
$retarr = ["releases" => [], "latestdev" => "master", "latestprod" => "", "pkgdir" => $pkgdir];
$devreleases = [];
$releases = [];
foreach ($tags as $name) {
unset($output, $logoutput);
// print "Processing package $pkg branch $name\n";
$cmd = "git checkout --force $name 2>/dev/null";
exec($cmd, $output, $res);
if ($res !== 0) {
var_dump($cmd, $output, $res);
throw new \Exception("Something didn't work");
}
$tagarr = ["commit" => "unreleased", "utime" => 0, "descr" => "No description", "modified" => false];
// If it's 'master', or the tag name starts with 'dev', it's dev.
if ($name === "master" || strpos($name, "dev") === 0) {
$dev = true;
} else {
$dev = false;
}
$tagarr['dev'] = $dev;
$cmd = "git log -n1 --date='unix' . 2>/dev/null";
exec($cmd, $logoutput, $res);
if ($res !== 0) {
var_dump($cmd, $logoutput, $res);
throw new \Exception("Something didn't work");
}
foreach ($logoutput as $line) {
if (!$line) {
continue;
}
if (preg_match('/^commit (.+)$/', $line, $o)) {
$tagarr['commit'] = $o[1];
continue;
}
if (preg_match('/^Date:\s+(.+)$/', $line, $o)) {
$tagarr['utime'] = (int) $o[1];
continue;
}
if (preg_match('/^Author: (.+)$/', $line, $o)) {
$tagarr['author'] = $o[1];
continue;
}
$tagarr['descr'] = trim($line);
}
// If this is 'master', subtract 1 second from the utime, so if there is a
// named tag we prefer that.
$utime = $tagarr['utime'];
if ($name == 'master') {
$utime--;
}
if ($dev) {
$devreleases[$name] = $utime;
} else {
$releases[$name] = $utime;
}
$tagarr['date'] = gmdate("Y-m-d\TH:i:s\Z", $utime);
// If there are modified files in this package find the timestamp
// of the latest modified file, EXCLUDING the pkginfo.json file
// which is always going to be modified.
$cmd = "git status -s .";
exec($cmd, $soutput, $res);
foreach ($soutput as $line) {
$filearr = explode(' ', trim($line));
if (file_exists($filearr[1])) {
if ($filearr[1] == 'meta/pkginfo.json') {
continue;
}
$s = stat($filearr[1]);
if ($s['mtime'] > $tagarr['utime']) {
$tagarr['utime'] = $s['mtime'];
$tagarr['modified'] = true;
}
}
}
$retarr['releases'][$name] = $tagarr;
}
// Sort out packages by the value (utime), so we can pick the highest.
// Note that anything called 'master' is one second less than reality.
arsort($devreleases);
$retarr['latestdev'] = array_key_first($devreleases);
// If there's no releases yet, use dev
if (!$releases) {
$retarr['latestprod'] = $retarr['latestdev'];
} else {
arsort($releases);
$retarr['latestprod'] = array_key_first($releases);
}
return $retarr;
}
public function parsePkgArr(?array $pkgarr = null, bool $force = false, bool $showoutput = false)
{
if ($pkgarr === null) {
$pkgarr = $this->getPkgArr();
}
$retarr = [];
foreach ($pkgarr as $pkg => $data) {
$pkgdir = $data['pkgdir'];
$retarr[$pkg] = ["pkgdir" => $pkgdir, "tags" => [], "rebuilt" => []];
foreach ($data['releases'] as $rel => $d) {
$retarr[$pkg]['tags'][] = $rel;
$reldest = $this->dest . "/$pkg/" . str_replace('/', '_', $rel);
$filename = "$reldest/$pkg.squashfs";
$relmeta = "$filename.meta";
$shafile = "$filename.sha256";
$pkgarr[$pkg]['releases'][$rel]['reldest'] = $reldest;
$pkgarr[$pkg]['releases'][$rel]['squashfs'] = $filename;
$pkgarr[$pkg]['releases'][$rel]['meta'] = $relmeta;
$pkgarr[$pkg]['releases'][$rel]['sha256file'] = $shafile;
$pkgarr[$pkg]['releases'][$rel]['dirinfo'] = $this->genDirinfo($pkg, $rel, $d['commit']);
if (!is_dir($reldest)) {
mkdir($reldest, 0777, true);
chmod($reldest, 0777);
}
$rebuild = false;
if (file_exists($relmeta)) {
$meta = json_decode(file_get_contents($relmeta), true);
} else {
$meta = [];
$rebuild = true;
}
if (file_exists($filename)) {
if (function_exists('xattr_get')) {
$currenthash = xattr_get($filename, "sha256");
} else {
$currenthash = hash_file("sha256", $filename);
}
} else {
$currenthash = "0";
$rebuild = true;
}
$pkgarr[$pkg]['releases'][$rel]['sha256'] = $currenthash;
$d['sha256'] = $currenthash;
$metahash = $meta['sha256'] ?? 'nohash';
if ($force) {
if ($showoutput) print "Forcing rebuild of $filename\n";
$rebuild = true;
}
if ($currenthash !== $metahash) {
if ($showoutput) print "Current '$currenthash' and meta '$metahash' does not match\n";
$rebuild = true;
}
if ($meta != $d) {
if ($showoutput) {
print "Meta and d does not match\n";
print "Meta: " . json_encode($meta) . "\n";
print "d: " . json_encode($d) . "\n";
}
$rebuild = true;
}
if (!$rebuild) {
continue;
}
if ($showoutput) print "Changes detected in $pkg at $rel, rebuilding\n";
$newhash = $this->rebuildPkg($rel, $d, $pkgdir, $filename, $showoutput);
$d['sha256'] = $newhash;
$pkgarr[$pkg]['releases'][$rel]['sha256'] = $newhash;
$retarr[$pkg]['rebuilt'][] = $rel;
// Note the reldest/filename/meta etc above is not set inside
// $d, as it wasn't passed by ref, AND, it's not needed, as it
// will make the $meta == $d comparison much more complicated.
file_put_contents($relmeta, json_encode($d));
}
}
return ["pkgarr" => $pkgarr, "parsed" => $retarr];
}
public function publishPackage(string $pkgrel, array $pkgdata)
{
$destdir = $pkgdata['dirinfo']['webdest'];
$urlbase = $pkgdata['dirinfo']['urlbase'];
if (!is_dir($destdir)) {
mkdir($destdir, 0777, true);
chmod($destdir, 0777);
}
$retarr = $pkgdata;
unset($retarr['reldest'], $retarr['dirinfo']);
foreach (['squashfs', 'meta', 'sha256file'] as $k) {
$src = $pkgdata[$k];
$filename = basename($src);
$destfile = "$destdir/$filename";
copy($src, $destfile);
chmod($destfile, 0777);
$retarr[$k] = "$urlbase/$filename";
}
$retarr['releasename'] = $pkgrel;
return $retarr;
}
public function genDirinfo(string $module, string $branch, string $commit)
{
$url = $this->url;
$webroot = $this->webroot;
// Double hashing here because I want it hard to get a collision,
// whilst still keeping the string short. This is, honestly, probably
// less secure than just doing a single sha256, but better than md5!
$hash = hash('sha1', hash('sha256', "$module:$branch:$commit"));
$path = "$webroot/repo/$hash";
return ["webdest" => $path, "urlbase" => "$url/$hash"];
}
public function rebuildPkg($rel, $d, $srcdir, $outfile, bool $showoutput = false)
{
if ($showoutput) print "Creating squashfs from $srcdir on branch $rel\n";
$utime = $d['utime'];
// Delete the pkginfo file just in case
$pinfofile = "$srcdir/meta/pkginfo.json";
if (file_exists($pinfofile)) {
unlink($pinfofile);
}
chdir($srcdir);
$cmd = "git checkout --force $rel 2>/dev/null";
exec($cmd, $output, $res);
if ($res !== 0) {
var_dump($cmd, $output, $res);
throw new \Exception("Could not checkout branch $rel");
}
if (file_exists($outfile)) {
unlink($outfile);
}
// Create meta/pkginfo.json to put into the squashfs
$pkginfo = $d;
unset($pkginfo['sha256']);
$pkginfo['releasename'] = $rel;
file_put_contents($pinfofile, json_encode($pkginfo));
// Make sure this is repeatable - all timestamps in the squashfs are set to the
// latest commit utime.
$cmd = "mksquashfs $srcdir $outfile -all-root -mkfs-time $utime -all-time $utime -no-xattrs -e .git";
exec($cmd, $output, $ret);
chmod($outfile, 0777);
$hash = hash_file("sha256", $outfile);
if (function_exists('xattr_set')) {
xattr_set($outfile, "sha256", $hash);
}
file_put_contents("$outfile.sha256", $hash);
return $hash;
}
}