Repository navigation
Expand file tree
/
Copy pathworker.php
More file actions
126 lines (99 loc) · 4.05 KB
/
Copy pathworker.php
File metadata and controls
126 lines (99 loc) · 4.05 KB
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
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$configFilename = file_exists(__DIR__ . '/../config.json')
? __DIR__ . '/../config.json'
: __DIR__ . '/../config.json.dist';
$config = json_decode(file_get_contents($configFilename), true);
$config['default_user'] = isset($_SERVER['SUBSPLIT_DEFAULT_USER']) ? $_SERVER['SUBSPLIT_DEFAULT_USER'] : 'debian';
$socket_path = '/var/run/subsplit_worker.sock';
$socket_address = "unix://$socket_path";
$socket_user = isset($_SERVER['SUBSPLIT_SOCKET_USER']) ? $_SERVER['SUBSPLIT_SOCKET_USER'] : 'www-data';
@unlink($socket_path);
if (!$socket = stream_socket_server($socket_address, $errNo, $errMsg)) {
echo "Couldn't create stream_socket_server: [$errNo] $errMsg";
exit(1);
}
chown($socket_path, $socket_user);
chgrp($socket_path, $socket_user);
while (true) {
$client = stream_socket_accept($socket, -1);
if (!$client) {
continue;
}
while (true) {
if (stream_get_meta_data($client)['eof']) {
break;
}
if (!$payload = fgets($client)) {
continue;
}
processPayload($payload, $config);
}
fclose($client);
}
fclose($socket);
function processPayload($payload, $config) {
$data = json_decode($payload, true);
$name = null;
$project = null;
foreach ($config['projects'] as $testName => $testProject) {
if ($testProject['url'] === $data['repository']['ssh_url']) {
$name = $testName;
$project = $testProject;
break;
}
}
if (null === $name) {
echo sprintf('Skipping request for URL %s (not configured)', $data['repository']['ssh_url']) . "\n";
return;
}
$ref = $data['ref'];
$publishCommand = [
'git subsplit publish',
escapeshellarg(implode(' ', $project['splits'])),
];
if (preg_match('/refs\/tags\/(.+)$/', $ref, $matches)) {
if (isset($project['options']['no-tags']) && $project['options']['no-tags'] === true) {
echo sprintf("Skipping request for URL %s ('no-tags' option enabled)", $data['repository']['ssh_url']) . "\n";
return;
}
$publishCommand[] = escapeshellarg('--rebuild-tags');
$publishCommand[] = escapeshellarg('--no-heads');
$publishCommand[] = isset($project['tags'])
? escapeshellarg(sprintf('--tags=%s', implode(' ', $project['tags'])))
: escapeshellarg(sprintf('--tags=%s', $matches[1]));
} elseif (preg_match('/refs\/heads\/(.+)$/', $ref, $matches)) {
if (isset($project['options']['no-heads']) && $project['options']['no-heads'] === true) {
echo sprintf("Skipping request for URL %s ('no-heads' option enabled)", $data['repository']['ssh_url']) . "\n";
return;
}
$publishCommand[] = escapeshellarg('--no-tags');
$publishCommand[] = isset($project['heads'])
? escapeshellarg(sprintf('--heads=%s', implode(' ', $project['heads'])))
: escapeshellarg(sprintf('--heads=%s', $matches[1]));
} else {
echo sprintf('Skipping request for URL %s (unexpected reference detected: %s)', $data['repository']['ssh_url'], $ref) . "\n";
return;
}
$repositoryUrl = $project['url'];
echo sprintf('Processing subsplit for %s (%s)', $name, $ref) . "\n";
$workingDirectory = $config['working-directory'] . '/' . $name;
if (!file_exists($workingDirectory)) {
echo sprintf('Creating working directory for project %s (%s)', $name, $workingDirectory) . "\n";
mkdir($workingDirectory, 0750, true);
}
$become = sprintf('sudo -u %s ', $config['default_user']);
$command = implode(' && ', [
sprintf('cd %s', $workingDirectory),
sprintf('(%s git subsplit init %s || true )', $become, $repositoryUrl),
$become . 'git subsplit update',
$become . implode(' ', $publishCommand),
]);
passthru($command, $exitCode);
if (0 !== $exitCode) {
echo sprintf('Command %s had a problem, exit code %s', $command, $exitCode) . "\n";
return;
}
}